均线策略EA源码:
可做1分钟和5分钟,主要以波动大的品种为主,如GBPUSD,USDJPY等,一天信号不少,但如果设置止损,止损太小容易被扫,止损大太,就容易一单大亏损就把原来做的盈利给赔进去,有高手可以参详一下不?
extern double 仓位资金=100;
extern int 止损=150;
extern int 止盈1=350;
extern int 止盈2=500;
extern int 均线1=20;
extern int 均线2=60;
extern int 点差=20;
enum leixing{
SMA=0,
EMA=1,
SMMA=2,
LWMA=3,
};
input leixing 均线1类型=1;
input leixing 均线2类型=1;
extern string 开始交易时间="00:10";
extern string 结束交易时间="23:00";
extern int magic=9899;
string 止盈时间="20:00";
int time=200;
int OnInit()
{
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
}
void OnTick()
{
double shoushu;
shoushu=(int)(AccountBalance()/仓位资金)+1;
shoushu=shoushu*0.01;
double Bands1_U;
double Bands1_L;
double Bands2_U;
double Bands2_L;
double ma1_1,ma2_1,ma1_2,ma2_2;
ma1_1=iMA(NULL,0,均线1,0,(int)均线1类型,PRICE_CLOSE,1);
ma2_1=iMA(NULL,0,均线2,0,(int)均线2类型,PRICE_CLOSE,1);
ma1_2=iMA(NULL,0,均线1,0,(int)均线1类型,PRICE_CLOSE,2);
ma2_2=iMA(NULL,0,均线2,0,(int)均线2类型,PRICE_CLOSE,2);
Bands1_U = iBands(Symbol(), PERIOD_M30,20,2, 0, PRICE_CLOSE, MODE_UPPER, 1);
Bands1_L = iBands(Symbol(), PERIOD_M30, 20,2, 0, PRICE_CLOSE, MODE_LOWER, 1);
Bands2_U = iBands(Symbol(), PERIOD_M15,20,2, 0, PRICE_CLOSE, MODE_UPPER, 2);
Bands2_L = iBands(Symbol(), PERIOD_M15, 20,2, 0, PRICE_CLOSE, MODE_LOWER, 2);
bool buy,sell;
if(ma1_1>ma2_1 && ma1_2ma2_2)
{
sell=true;
}
bool mabuy,masell;
if(ma1_1>ma2_1 && Close[1]-ma1_1iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0)
&& iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0)>0)
{
macdorder = 1;
}
else if(iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0)[i]StrToTime(止盈时间)) {
if(止盈2==0 )
{
stk_b=0;
stk_s=0;
}
else
{
stk_b=Ask+止盈2*Point;
stk_s=Bid-止盈2*Point;
}
}
bool shijian;
if( StrToTime(结束交易时间)>StrToTime(开始交易时间) && TimeLocal()>StrToTime(开始交易时间) && TimeLocal()StrToTime(开始交易时间) || TimeLocal() 0.0) gd_400 = gd_384;
if (gd_392 > 0.0) gd_408 = gd_392;
res5= OrderSend(Symbol(),OP_BUY,shoushu,Ask,30, stp_b, stk_b, "1",magic,0,Red);
if(res5>0)
{
open_time=Time[0];
}
if (res50)
{
open_time=Time[0];
}
if (res5 0 )
{
for(i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if( OrderType()==OP_SELL && OrderSymbol()==Symbol() )
if (OrderMagicNumber()==magic )
{
order_switch=false;
}
}
}
return(order_switch);
}
bool cheakorders_buy()
{int i; //OrderClose
bool order_switch=true;
if (OrdersTotal( ) > 0 )
{
for(i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if( OrderType()==OP_BUY && OrderSymbol()==Symbol() )
if (OrderMagicNumber()==magic )
{
order_switch=false;
}
}
}
return(order_switch);
}
void mstoploss_1(int stoploss)
{
bool modifys = false;
for (int i=0; i stoploss*MarketInfo(OrderSymbol(), MODE_POINT) ) //开仓价格 当前止损和当前价格比较判断是否要修改跟踪止赢设置
{
if ( (OrderStopLoss() (stoploss*MarketInfo(OrderSymbol(), MODE_POINT))) //开仓价格 当前止损和当前价格比较判断是否要修改跟踪止赢设置
{
if ((OrderStopLoss()) > (Ask + stoploss*MarketInfo(OrderSymbol(), MODE_POINT)) || (OrderStopLoss()==0))
{
modifys = OrderModify(OrderTicket(),
OrderOpenPrice(),
Ask + stoploss*MarketInfo(OrderSymbol(),MODE_POINT),
OrderTakeProfit(),
0,
clrNONE);
}
}
}
}
}
void closebuy()
{
int res=1;
int i;
if (OrdersTotal() > 0 )
{
for(i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && OrderMagicNumber()==magic )
{
res=OrderClose(OrderTicket(),OrderLots(),Bid,30,White);
if (res 0 )
{
for(i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderType()==OP_SELL&& OrderSymbol()==Symbol() && OrderMagicNumber()==magic )
{
res=OrderClose(OrderTicket(),OrderLots(),Ask,30,White);
if (res 0 )
{
for(i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if( OrderType()==OP_SELL || OrderType()==OP_BUY )
if (OrderMagicNumber()==magic&& OrderSymbol()==Symbol())
{
order_switch=false;
}
}
}
return(order_switch);
}
|