在C++中使用指数移动平均线对 P&Q 规则进行编码

Coding the P&Q rule using Exponential Moving Average in C++

本文关键字:规则 编码 C++ 指数 移动      更新时间:2023-10-16

我正在开发一个使用C++作为练习的小型交易机器人。他将首先收到基本信息,如我们的资本和日常股票价值(以迭代表示)。

这是我的Trade类:

class   Trade
{
private:
  int                   capital_;
  int                   days_; // Total number of days of available stock prices                                       
  int                   daysInTrading_; // Increments as days go by.                                                   
  std::list<int>        stockPrices_; // Contains stock prices day after day.                                          
  int                   currentStock_; // Current stock we are dealing with.                                           
  int                   lastStock_; // Last stock dealt with                                                           
  int                   trend_;                                          
  int                   numOfStocks_; // Amount of stock options in our possession
  float                 EMA_; // Exponential Moving Average                                                            
  float                 lastEMA_; // Last EMA                                                                          
public:
// functions                              
};

正如你从最后两个属性中可以看出的那样,我使用的是指数移动平均原理和趋势跟踪算法。

我在这篇论文里读到了这件事http://www.cis.umac.mo/~fstasp/paper/jetwi2011.pdf(主要在第3页),并希望实现他们与我们共享的伪代码;它是这样的:

Repeat  
Compute EMA(T)  
If no position opened 
   If EMA(T) >= P
      If trend is going up 
        Open a long position 
      Else if trend is going down  
        Open a short position 
Else if any position is opened 
   If EMA(¬T) >= Q
     Close position 
   If end of market  
     Close all opened position 
Until market close 

到目前为止,我是这样做的:

void    Trade::scanTrend()
{                 
  if (this->numOfStocks_ == 0) // If no position is opened
    {
      this->EMA_ = this->calcEMA(); // Calculates the EMA
      if (this->EMA_ >= this->currentStock_) //  If EMA(T) >= P
        {
          if (this->EMA_ > this->lastEMA_) // If trend is going up
            // Trend is going up, open a long position
          else if (this->EMA_ < this->lastEMA_) // If trend is going down
            // Trend is going down, open a short position                                                                                  
        }
    }
  else // Else if any position is opened 
    {
      this->EMA_ = this->calcEMA();
      // How may I represent closing positions?
    }
  this->lastEMA_ = this->EMA_;
}

我的问题来自于不理解"打开"answers"关闭"一个职位的行为。这和买卖股票有关系吗?到目前为止,我所拥有的似乎与伪代码相符吗?

在这种情况下,p和Q是两个日期之间的阈值或EMA变化。

p将是第一次观测和当前观测之间的变化。如果P是正的,那么你可以假设P<0.

Q也是EMA的一个变化,但用于知道何时平仓,它是第一次观察的最高(最低)EMA值与当前上升(下降)趋势的EMA值之间的差值。