场景假设
在指标模式(历史K线,非实时),.最后一根k线使用开盘价替换收盘价来计算MACD(OnBarClose重新赋值close,bar内赋值open),
序列数据我已经验证过完全一样
这个逻辑下 开盘价和收盘价相等时,MACD应该一致.但是我写出来的不一样.麻烦帮看一下哪里有逻辑问题
//------------------------------------------------------------------------
// 简称: xxxx
// 名称: xxxx
// 类别: 策略应用
// 类型: 用户应用
// 输出: Void
//------------------------------------------------------------------------
Params
Vars
//此处添加变量
Series<Numeric> s_Close_O;//序列收盘价加最后一个是开盘价
Series<Numeric> s_Close_A;//序列收盘价加最后一个是模拟价(analog)
Defs
Events
//此处实现事件函数
//初始化事件函数,策略运行期间,首先运行且只有一次,应用在订阅数据等操作
OnInit()
{
//与数据源有关
Range[0:DataCount-1]
{
}
}
//Bar更新事件函数,参数indexs表示变化的数据源图层ID数组
OnBar(ArrayRef<Integer> indexs)
{
s_Close_O = Open;
s_Close_A = Close;
//Commentary(\"s_Close_A = \"+ Text(data[0].EMA(s_Close_A, 12)) + \" Close = \" + Text(data[0].EMA(Close, 12)));
Numeric DIFF_O = data[0].EMA(s_Close_O, 12) - data[0].EMA(s_Close_O, 26);
Numeric DEA_O = data[0].EMA(DIFF_O, 9);
Numeric MACD_O = DIFF_O - DEA_O;
Numeric DIFF = data[0].EMA(Close, 12) - data[0].EMA(Close, 26);
Numeric DEA = data[0].EMA(DIFF, 9);
Numeric MACD = DIFF - DEA;
Commentary(\"DIFF1 = \" + Text(data[0].EMA(s_Close_O, 12)));
Commentary(\"DIFF2 = \" + Text(data[0].EMA(close, 12)));
Numeric x;
Array<Numeric> arr_close;
Array<Numeric> arr_close_o;
for x = 100 DownTo 0
{
ArrayPushBack(arr_close,data[0].Close[x]);
ArrayPushBack(arr_close_o,data[0].s_Close_O[x]);
//save_log(log_path,\"序号 = \" + text(x) + \" close值:\" + text(data[i].Close[x]) + \" s_Close_O值:\" + text(data[i].s_Close_O[x]));
}
print(DateTimeToString(date + Time + Second) + \"close值:\" + TextArray(arr_close));
print(DateTimeToString(date + Time + Second) + \"close_o值:\" + TextArray(arr_close_o));
}
OnBarClose(ArrayRef<Integer> indexs)
{
Range[0:DataCount-1]
{
s_Close_O = Close;
s_Close_A = Close;
}
}
有意思的问题,按照编程的思维,ema函数是一个函数,传入相同的值返回相同的内容,
但是里面用序列,修改了历史值,导致了全部值的改变.
自定义一个ema解决
Numeric ema1(Numeric Price, Integer Length,Numeric last_price)
{
Numeric sFcactor;
sFcactor = 2 / ( Length + 1 );
if (CurrentBar == 0 )
{
XAvgValue1 = Price;
}
else
{
XAvgValue1 = XAvgValue1[1] + sFcactor * ( Price - XAvgValue1[1]);
XAvgValue2 = XAvgValue1[1] + sFcactor * ( last_price - XAvgValue1[1]);
FileAppend(log_path, \"K线时间 \" + DateTimeToString(date + Time + Second) + \" >>> \" + \"1 = \"+ text(XAvgValue1[1]) + \", 2 = \"+ text(sFcactor) + \", 3 = \"+ text(Price) + \", 4 = \"+ text(XAvgValue1[1]) + \", CurrentBar = \"+ text(CurrentBar), False );
}
Return XAvgValue2;
}