我以前自己写了一个,第一个是指标代码, //+------------------------------------------------------------------+ //| turtleChannel.mq4 | //| Copyright ?2008, xMath Corp. | //| http://www.xmath.net | //+------------------------------------------------------------------+ #property copyright "Copyright @2008, xMath Corp." #property link "http://www.xmath.net" #property indicator_chart_window #property indicator_buffers 4 #property indicator_color1 Red #property indicator_width1 1 #property indicator_color2 Green #property indicator_width2 1 #property indicator_color3 Red #property indicator_style3 2 #property indicator_color4 Green #property indicator_style4 2 //---- input parameters extern int UP=20; extern int DN=20; extern int nExitLong=10; extern int nExitShort=10; //---- buffers double UPBuffer[]; double DNBuffer[]; double exitLongBuffer[]; double exitShortBuffer[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- indicators SetIndexStyle(0,DRAW_LINE); SetIndexBuffer(0,UPBuffer); SetIndexStyle(1,DRAW_LINE); SetIndexBuffer(1,DNBuffer); SetIndexStyle(2,DRAW_LINE); SetIndexBuffer(2,exitLongBuffer); SetIndexStyle(3,DRAW_LINE); SetIndexBuffer(3,exitShortBuffer); //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { int counted_bars=IndicatorCounted(); int iH,iL; int i; double lastDN; double lastUP; //如果Bars数量不足UP数量,直接返回 if (UP>Bars) return(0); i=Bars-counted_bars-1; //Print("i=",i,"---",Open); //Print(i,"--",counted_bars,"--",Bars); while(i>=0) { iH=iHighest(NULL,0,MODE_HIGH,UP,i+1); UPBuffer=High[iH]; lastUP=UPBuffer; iL=iLowest(NULL,0,MODE_LOW,DN,i+1); DNBuffer=Low[iL]; lastDN=DNBuffer; exitShortBuffer=High[iHighest(NULL,0,MODE_HIGH,nExitShort,i+1)]; exitLongBuffer=Low[iLowest(NULL,0,MODE_LOW,nExitLong,i+1)]; //Print("iH=",iH,"-----","i=",i); i--; } /* if (Ask >=lastUP) Print("UP Breakout at :",Ask); if (Bid <= lastDN) Print("Down Breakout at :",Bid); if (Bid<=1.2700) { Alert(Bid); } */ //---- return(0); } //+------------------------------------------------------------------+
这个是EA代码,要求配合上面的指标代码使用: //+------------------------------------------------------------------+ //| xTurtle.mq4 | //| Copyright @2008, xMath Corp. | //| http://www.xMath.com | //+------------------------------------------------------------------+ #property copyright "Copyright @2008, xMath Corp." #property link "http://www.xMath.com" #define MAGICMA 20050610 //---- input parameters extern int UP=20; extern int DN=20; extern int nExitLong=10; extern int nExitShort=10; extern int nStopLoss=10; extern int nATR=14; extern int ATRfactor=2; extern double Lots=0.1; double breakUpPrice,breakDnPrice; double exitLong,exitShort; double ATR; double SL1,SL2,SL3,SL4; double EP1,EP2,EP3,EP4; int ticketEP1,ticketEP2,ticketEP3,ticketEP4; int i; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //---- check for history and trading if(Bars<100 || IsTradeAllowed()==false) return; //---- calculate open orders by current symbol, step 4 times CheckForOpenLong(); CheckForOpenShort(); CheckForClose(); //---- //---- return(0); } //+------------------------------------------------------------------+ //| Check for modify order stop loss price | //+------------------------------------------------------------------+ void ModifyStopLossPrice(int ticket,double newstoploss) { OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES); OrderModify(ticket,OrderOpenPrice(),newstoploss,0,0,Blue); } //+------------------------------------------------------------------+ //| Check for open long position | //+------------------------------------------------------------------+ void CheckForOpenLong() { breakUpPrice=iCustom(NULL,0,"turtleChannel",UP,DN,nExitLong,nExitShort,0,0); //if no position, first open long ,and then 2,3,4 long if(OrdersTotal()==0 && Close[0]>=breakUpPrice) { ATR=iATR(NULL,0,nATR,0); EP1=Ask; SL1=EP1-ATR*ATRfactor; ticketEP1=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,SL1,0,"open long 1 ",MAGICMA,0,Red); EP2=EP1+0.5*ATR; SL2=EP1-1.75*ATR; EP3=EP1+1.0*ATR; SL3=EP1-1.5*ATR; EP4=EP1+1.5*ATR; SL4=EP1-1.25*ATR; //return; } //long,second entry long if (OrderSelect(ticketEP1,SELECT_BY_TICKET,MODE_TRADES)==true && OrdersTotal()==1) { if(Close[0]>=EP2 && OrderType()==OP_BUY) { ticketEP2=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,SL2,0,"open long 2 ",MAGICMA,0,Red); //if ticketEP2 true, modify long stop loss price if (ticketEP2>0) { ModifyStopLossPrice(ticketEP1,SL2); } } //return; } //long,third entry LONG if (OrderSelect(ticketEP2,SELECT_BY_TICKET,MODE_TRADES)==true && OrdersTotal()==2) { if(Close[0]>=EP3 && OrderType()==OP_BUY) { ticketEP3=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,SL3,0,"open long 3 ",MAGICMA,0,Red); //if ticketEP3 true, modify long stop loss price if (ticketEP3>0) { ModifyStopLossPrice(ticketEP1,SL3); ModifyStopLossPrice(ticketEP2,SL3); } } //return; } //long,fourth entry LONG if (OrderSelect(ticketEP3,SELECT_BY_TICKET,MODE_TRADES)==true && OrdersTotal()==3) { if(Close[0]>=EP4 && OrderType()==OP_BUY) { ticketEP4=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,SL4,0,"open long 4 ",MAGICMA,0,Red); //if ticketEP4 true, modify long stop loss price if (ticketEP4>0) { ModifyStopLossPrice(ticketEP1,SL4); ModifyStopLossPrice(ticketEP2,SL4); ModifyStopLossPrice(ticketEP3,SL4); } } //return; } } //+------------------------------------------------------------------+ //| Check for open short position | //+------------------------------------------------------------------+ void CheckForOpenShort() { breakDnPrice=iCustom(NULL,0,"turtleChannel",UP,DN,nExitLong,nExitShort,1,0); //first short if(OrdersTotal()==0 && Close[0]<=breakDnPrice) { ATR=iATR(NULL,0,nATR,0); EP1=Bid; SL1=EP1+ATR*ATRfactor; ticketEP1=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,SL1,0,"open short 1 ",MAGICMA,0,Green); EP2=EP1-0.5*ATR; SL2=EP1+1.75*ATR; EP3=EP1-1.0*ATR; SL3=EP1+1.5*ATR; EP4=EP1-1.5*ATR; SL4=EP1+1.25*ATR; return; } //short,second entry short if (OrderSelect(ticketEP1,SELECT_BY_TICKET,MODE_TRADES)==true && OrdersTotal()==1 ) { if(Close[0]<=EP2 && OrderType()==OP_SELL) { ticketEP2=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,SL2,0,"open short 2 ",MAGICMA,0,Green); //if ticketEP2 true, modify short stop loss price if (ticketEP2>0) { ModifyStopLossPrice(ticketEP1,SL2); } } return; } //short,third entry short if (OrderSelect(ticketEP2,SELECT_BY_TICKET,MODE_TRADES)==true && OrdersTotal()==2) { if(Close[0]<=EP3 && OrderType()==OP_SELL) { ticketEP3=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,SL3,0,"open short 3 ",MAGICMA,0,Green); //if ticketEP3 true, modify long stop loss price if (ticketEP3>0) { ModifyStopLossPrice(ticketEP1,SL3); ModifyStopLossPrice(ticketEP2,SL3); } } return; } //short,fourth entry short if (OrderSelect(ticketEP3,SELECT_BY_TICKET,MODE_TRADES)==true && OrdersTotal()==3) { if(Close[0]<=EP4 && OrderType()==OP_SELL) { ticketEP4=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,SL4,0,"open short 4 ",MAGICMA,0,Green); //if ticketEP4 true, modify long stop loss price if (ticketEP4>0) { ModifyStopLossPrice(ticketEP1,SL4); ModifyStopLossPrice(ticketEP2,SL4); ModifyStopLossPrice(ticketEP3,SL4); } } return; } } //+------------------------------------------------------------------+ //| Check for Close position | //+------------------------------------------------------------------+ void CheckForClose() { exitLong =iCustom(NULL,0,"turtleChannel",UP,DN,nExitLong,nExitShort,2,0); exitShort=iCustom(NULL,0,"turtleChannel",UP,DN,nExitLong,nExitShort,3,0); //exit long pos if (Close[0]<=exitLong && OrderType()==OP_BUY) { if (OrdersTotal()>0) { for (i=OrdersTotal()-1;i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderType()==OP_BUY) { OrderClose(OrderTicket(),Lots,Bid,3,Red); //Sleep(5000); } } } } //exit short pos if (Close[0]>=exitShort && OrderType()==OP_SELL) { if (OrdersTotal()>0) { for (i=OrdersTotal()-1;i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderType()==OP_SELL) { OrderClose(OrderTicket(),Lots,Ask,3,Green); //Sleep(5000); } } } } }
求那位老师帮我改一下下面的代码,谢谢了!!! Open_Buy()和Open_Sell(),我想全部做反手,开始开了单后,后面全部是平空开多,平多开空,永不离场。 double Lots() 只要能抗300点左右,下最大的单子。 extern double Fuck1,Fuck2; int total; // 开单计算 int start() { total=OrdersTotal(); // 定单总数 if (total==2)return; // 已经打开定单 //------------------------------------------------------------------------- //------------------------------------------------------------------------- if Fuck1 Open_Buy(); if Fuck2 Open_Sell(); return; } //------------------------------------------------------------------------- int Open_Buy() // 函数打开 Buy { if (total==1) // 如果只有一个定单... { // ... 说明会打开其他定单 OrderSelect(0, SELECT_BY_POS); // 选择定单 if (OrderType()==0)return; // 如果是Buy定单, 退出 } OrderSend(Symbol(),0, Lots(), Ask, 0, 0, 0, "", 0, 0, Blue);// 打开 return; } int Open_Sell() // 函数打开Sell定单 { if (total==1) // 如果只有一个定单... { // ... 说明会打开其他定单 OrderSelect(0, SELECT_BY_POS); // 选择定单 if (OrderType()==1)return; // 如果是 Sell定单,退出 } OrderSend(Symbol(),1, Lots(), Bid, 0, 0, 0, "", 0, 0, Red);// 打开 return; } //------------------------------------------------------------------------- double Lots() // 标准手数计算 { Lot=NormalizeDouble(AccountEquity()*Prots/100/1000,1);// 计算标准手总数 double Min_Lot = MarketInfo(Symbol(), MODE_MINLOT); // 最小标准手值 if (Lot == 0 ) Lot = Min_Lot; // 剩余最小标准手数 return(Lot); } //-------------------------------------------------------------------------