20160321-道指美油价差

思想:根据道指和美油的价差,在一定范围内波动,在价差高点做空,在价差低点做多,来获利。

问题分析:虽然实战中总是获利,但是没有止损,成为此EA的致命弱点。每次到设定最高点的时候做空,甚至会继续亏损100多点,价差才会慢慢缩小,直到盈利。逐步加大做空的点位,减小做多的点位,这样下来,开仓的条件就越来越苛刻,以致很难开仓。

代码:

extern double min_open=13094;//开道多开油空 US30_ASK - USOIL_BID,13186时最多亏了140,要调整到13054
extern double min_close=13237;//平道多平油空 US30_BID - USOIL_ASK
extern double max_open=13460;//开道空开油多 US30_BID - USOIL_ASK,最大差价13500,调整到13460
extern double max_close=13225;//平道空平油多 US30_ASK - USOIL_BID
double min_open_old;
double max_open_old;
bool optimize_allowed=1;
int US30_BUY=2001, US30_SELL=2002, USOIL_BUY=1001, USOIL_SELL=1002;
int cnt, total;
int start()
{
int main_switch=1;
int jiacha_ASK_BID = MarketInfo("US30",MODE_ASK)-MarketInfo("US_OIL",MODE_BID)*100;
int jiacha_BID_ASK = MarketInfo("US30",MODE_BID)-MarketInfo("US_OIL",MODE_ASK)*100;

//printf("jiacha_ASK_BID:"+jiacha_ASK_BID);
//printf("jiacha_BID_ASK:"+jiacha_BID_ASK);
//printf("start: "+OrdersTotal());
//printf(OrdersTotal());

if (IsTradeAllowed("US_OIL",TimeCurrent())==0 || IsTradeAllowed("US30",TimeCurrent())==0) main_switch = 0;//可开仓或者平仓的条件
if (AccountBalance() < 200) main_switch = 0; //账户余额限制条件
//设定程序运行总开关部分结束,下边为程序主要运行部分,开仓和平仓

if(main_switch == 1)
{
if(jiacha_ASK_BID < min_open && OrdersTotal()==0) { OrderSend("US30",OP_BUY,1,Ask,3,0,0,"US30_BUY_ORDER",US30_BUY,0,Red); OrderSend("US_OIL",OP_SELL,1,Bid,3,0,0,"USOIL_SELL_ORDER",USOIL_SELL,0,Red); } // --- else if(jiacha_BID_ASK > min_close && OrdersTotal()!=0) bool close_test = CloseMax();//平多

//---
else if(jiacha_BID_ASK > max_open && OrdersTotal()==0)
{
OrderSend("US30",OP_SELL,1,Bid,3,0,0,"US30_SELL_ORDER",US30_SELL,0,Red);
OrderSend("US_OIL",OP_BUY,1,Ask,3,0,0,"USOIL_BUY_ORDER",USOIL_BUY,0,Red);
}
//---
else if(jiacha_ASK_BID < max_close && OrdersTotal()!=0) close_test = CloseMin();//平空

// 程序运行部分结束,下边为参数自动优化部分

if(jiacha_ASK_BID<(min_open-40)) { if(optimize_allowed==1) { min_open_old=min_open; } min_open=jiacha_ASK_BID; optimize_allowed=0; printf("set new min_open: "+min_open); printf("set optimize_allowed: 0"); } // if(jiacha_BID_ASK>(max_open+40))
{
if(optimize_allowed==1)
{
max_open_old=max_open;
}
max_open=jiacha_BID_ASK;
optimize_allowed=0;
printf("set new max_open: "+max_open);
printf("set optimize_allowed: 0");
}//

if(optimize_allowed==0 && jiacha_BID_ASK >(min_open_old+10) &&OrdersTotal()!=0)//平多止损
{
close_test = CloseMax();
if (close_test == 1)
{
optimize_allowed=1;
printf("set optimize_allowed: 1");
}
}//

if(optimize_allowed==0 && jiacha_ASK_BID < (max_open_old-10) && OrdersTotal()!=0)//平空止损 { close_test = CloseMin(); if (close_test == 1) { optimize_allowed=1; printf("set optimize_allowed: 1"); } }// } return(0); } bool CloseMax()//平多 { total=OrdersTotal(); for(cnt=total-1;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber()== US30_BUY)
{
bool close1 = OrderClose(OrderTicket(),OrderLots(),Bid,3,CLR_NONE);
}
if(OrderMagicNumber()== USOIL_SELL)
{
bool close2 = OrderClose(OrderTicket(),OrderLots(),Ask,3,CLR_NONE);
}
}
if (close1==1 && close2==1) return true;
else return false;
}

bool CloseMin()//平空
{
total=OrdersTotal();
for(cnt=total-1;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber()== US30_SELL)
{
bool close1 = OrderClose(OrderTicket(),OrderLots(),Ask,3,CLR_NONE);
}
if(OrderMagicNumber()== USOIL_BUY)
{
bool close2 = OrderClose(OrderTicket(),OrderLots(),Bid,3,CLR_NONE);
}
}
if (close1==1 && close2==1) return true;
else return false;
}

打赏