【发布时间】:2016-05-18 13:11:59
【问题描述】:
我正在开发一个小型交易机器人作为练习。他日复一日地收到股票价格(表示为迭代)。
这是我的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_; // Either {-1; 0; 1} depending on the trend.
int numOfStocks_; // Number of stocks in our possession
int EMA_; // Exponential Moving Average
int lastEMA_; // Last EMA
public:
// functions
};
从我的最后两个属性可以看出,我希望将指数移动平均线作为趋势跟踪算法的一部分。
但我想我不太明白如何实现它;这是我的calcEMA 函数,它只计算EMA:
int Trade::calcEMA()
{
return ((this->currentStock_ - this->lastEMA_
* (2/(this->daysInTrading_ + 1)))
+ this->lastEMA_);
}
但是当我的股票值(在文件中传递)是这样的:
1000, 1100, 1200, 1300, 1400, 1500, 1400, 1300, 1200, 1100, 1000
为了确保我的 EMA 有意义,而且……它没有!
我的操作哪里出错了?
另外,如果这是我第一次调用calcEMA,我应该给lastEMA 什么值?
【问题讨论】:
-
2/(this->daysInTrading_ + 1)-- 这会截断,因为它是整数除法。这是你想做的吗?