- 显示:
- 6640
- 等级:
- 已发布:
- 2020.04.01 17:00
- 已更新:
- 2020.04.27 04:49
-
需要基于此代码的EA交易或指标吗?请在自由职业者服务中订购 进入自由职业者服务
加载时,输入要比较的货币并选择算法,如图所示:
副图显示了K线和最新价格。 (wti * dxy)的效果是这样的:
金油比(xauusd / wti)的效果是这样的:
在4H中还是可以看出有一定的趋势性:
新增对数除法:
你还可以查看交叉货币,例如GBPJPY(GBPUSD * USDJPY)。中间的图表是指标的计算得出的K线。下面的图表是实际GBPJPY的价格走势以进行比较:
通过对比可以看出,K线的高点和低点之间存在着细微的差异,因为该指标只是简单的对高价和低价进行同类的比较计算,但是开盘价和收盘价没有影响。
代码部分:
首先定义一个枚举方法:
enum method { add = 1, // "+" subtract, // "-" multiply, // "*" divide, // "/" logdivide, // "log(/)" }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double apply(double x,double y,method f) { double ret = EMPTY_VALUE; switch(f) { case 1: ret = x + y; break; case 2: ret = x - y; break; case 3: ret = x * y; break; case 4: if(y != 0) ret = x / y; break; case 5: if(x > 0 && y > 0) ret = log((double)x / (double)y); break; } return ret; }
然后在初始化期间简单地处理输入的货币:
StringTrimRight(iname); StringTrimLeft(iname); StringToUpper(iname); bool is_custom; if(!SymbolExist(iname,is_custom)) { Alert(iname + ":没有找到该交易品种!"); return(INIT_FAILED); } else if(is_custom) Alert(iname + ":自定义交易品种!"); CopyRates(iname,PERIOD_CURRENT,0,(int)SeriesInfoInteger(iname,PERIOD_CURRENT,SERIES_BARS_COUNT),rates); ChartSetInteger(0,CHART_FOREGROUND,ChartWindowFind(),false); ChartSetInteger(0,CHART_SHOW_PRICE_SCALE,ChartWindowFind(),true); string pstr = EnumToString(_Period); string mstr = EnumToString(InpMethod); if(!StringToUpper(mstr)) mstr = method_tostr(InpMethod); myname = iname + method_tostr(InpMethod) + _Symbol; if(InpMethod > 2) { string str[4]; str[0] = StringSubstr(iname,0,3); str[1] = StringSubstr(iname,3,-1); str[2] = StringSubstr(_Symbol,0,3); str[3] = StringSubstr(_Symbol,3,-1); //ArrayPrint(str); if(InpMethod == multiply) { if(str[1] == str[2]) myname = str[0] + str[3]; if(str[0] == str[3]) myname = str[2] + str[1]; } if(InpMethod == divide) { if(str[1] == str[3]) myname = str[0] + str[2]; if(str[0] == str[2]) myname = str[3] + str[1]; } } string comment = myname + "," + StringSubstr(pstr,StringFind(pstr,"_") + 1);
然后在OnCalculate的时候,获取目标 Symbol 的 Rates数据:
int to_copy; if(prev_calculated > rates_total || prev_calculated <= 0) to_copy = rates_total; else { to_copy = rates_total - prev_calculated; to_copy++; } int bars = iBars(iname,PERIOD_CURRENT); if(limit < to_copy) to_copy = limit; if(bars < to_copy) //(int)MathMin(bars,MathMax(showbars,to_copy) to_copy = bars; if(1 > to_copy) to_copy = 1; int copied = CopyRates(iname,PERIOD_CURRENT,0,to_copy,rates); if(copied != to_copy) { copied = CopyRates(iname,PERIOD_CURRENT,0,showbars,rates); if(prev_calculated < 1) printf("For the symbol %s, managed to receive only %d bars of %d requested ones",iname,copied,to_copy); if(copied < 1) return 0; }
最后计算:
int pos = 0; for(int i = 0; i < copied; i++) { while(pos < copied && pos <= rates_total - prev_calculated && !_StopFlag) { if(rates[i].time > time[pos]) break; KBuffer1[pos] = apply(rates[i].open,open[pos],InpMethod); KBuffer2[pos] = apply(rates[i].high,high[pos],InpMethod); KBuffer3[pos] = apply(rates[i].low,low[pos],InpMethod); KBuffer4[pos] = apply(rates[i].close,close[pos],InpMethod); KColors[pos] = (KBuffer1[pos] > KBuffer4[pos]) ? 1 : 0; pos ++; } }
需要注意的是:
1.除法和减法是由输入的目标货币在前,当前图表的货币在后,如果反了,你调换一下输入和图表就行。
2.由于有时目标货币在本地没有足够的数据,需要等待一段时间下载,如果没有显示的话你可以切换下周期试试。

这个脚本是计算等价等量加仓步骤的,比如在当前原油价格低迷情况下,又在反复震荡,投资者又想抄底,可以通过这个脚本来计算需要间隔多少来实现逐步等量加仓。

MT5版本三线RSI指标,根据通达信公式改编。

于主图显示,类似于期货日内均线,可以选择更多的结算周期

在做EA账户风控时,有时候需要知道账户各个时间段的资金数据,本EA可以基于每个净值变动来记录,并画出图表K线或保存至Excel。