为什么这个EA复盘测试的时候,速度很慢
本人刚刚接触EA没有多久,高手请指点EA复盘测试的时候,速度很慢------不知道是什么原因造成的#property copyright ""#property link "" string 名称 = "转头对冲"; extern double 第一次手数 = 0.01; double 手数 ; extern double 启动值 = 20;extern double 保护值 = 3;extern int Magic = 201108225;extern int 挂单距离 = 5;extern int 开单距离 = 30;extern double 锁单倍数 = 1;extern double 下次倍数 = 2; extern bool 强平 = false;extern bool 挂单 = true;extern bool 追单 = false;//货币信息修改 double 小数位 = 0.01; int 取值位 = 2;externstring 说明 = "0=sma, 1=ema, 2=smma, 3=lwma";extern int FasterMode = 1; //0=sma, 1=ema, 2=smma, 3=lwmaextern int FasterMA = 21;extern int SlowerMode = 1; //0=sma, 1=ema, 2=smma, 3=lwmaextern int SlowerMA = 55;extern int VerMA = 60; color 多单颜色 = DodgerBlue; //买,宝蓝色表示 color 空单颜色 = Red; //卖,红色表示 color 修改颜色 = Silver; //修改,银色表示 color 平仓颜色 = Gold; //平仓,金色表示 color 文字颜色 = Magenta;//内部参数string Comment8= "";string sComment = ""; //屏幕显示变量double BuyLots = 0; //手数统计double SellLots = 0;int BuyCnt = 0; //已成交订单统计int SellCnt = 0;int BuyLimitCnt = 0; //多单挂单统计int SellLimitCnt= 0;int BuyStopCnt= 0; //顺势挂单统计int SellStopCnt = 0;double BuyProfit = 0; //订单利润统计double SellProfit= 0;int RepeatN = 3; //重复执行次数int Slippage = 3; //允许的滑点double MaxLose = 0,MaxLose1=0,aa=0,Profit;double 止损值 = 0;int ticket,MaxLose2=0,Slipage=3; bool buy,sell;int init() { int Decimal = 1; if (Digits == 5 || Digits == 3) Decimal = 10;Slipage = Slipage * Decimal;开单距离= 开单距离 * Decimal;if (Digits == 3 ){小数位 = 0.01;取值位 = 2;}手数=第一次手数; return (0); }int deinit(){ObjectsDeleteAll( EMPTY, OBJ_LABEL) ; return(0);}int start(){buy=false;sell=false; for (int li_28 = 1; li_28 < Bars; li_28++) { double vaS=iMA(NULL, 0, FasterMA, 0, FasterMode, PRICE_CLOSE,li_28);double vaS1=iMA(NULL, 0, FasterMA, 0, FasterMode, PRICE_CLOSE, li_28+1);double vaB=iMA(NULL, 0, SlowerMA, 0, SlowerMode, PRICE_CLOSE, li_28);double vaB1=iMA(NULL, 0, SlowerMA, 0, SlowerMode, PRICE_CLOSE, li_28+1); if(vaS1>vaB1&&vaS<vaB) {sell=true; Comment8="SELL"+"第"+li_28+"柱的信号"; break;}if(vaS1<vaB1&&vaS>vaB) {buy=true;Comment8="BUY"+"第"+li_28+"柱的信号";break;} } RecountOrders(); if(buy==true){ deleteOrder(OP_SELLLIMIT);deleteOrder(OP_SELLSTOP);if(BuyLots<SellLots) { int ticket=OrderSend(Symbol(),OP_BUY,MathAbs(BuyLots-SellLots)*锁单倍数,Ask,3,0,0,"转头对冲多",Magic,0,Green);GlobalVariableSet( Symbol(), FindLastLots(OP_SELL)*下次倍数 ); 手数=GlobalVariableGet(Symbol()); OpenOrder(OP_BUY,手数,Ask,Magic); PlaySound("alert.wav"); } if(SellLots<=0) 手数=第一次手数; else手数=GlobalVariableGet(Symbol()); chkOrder(OP_BUY); }RecountOrders();if(sell==true){deleteOrder(OP_BUYLIMIT);deleteOrder(OP_BUYSTOP); if(BuyLots>SellLots){ticket=OrderSend(Symbol(),OP_SELL,MathAbs(BuyLots-SellLots)*锁单倍数,Bid,3,0,0,"转头对冲空",Magic,0,Green); GlobalVariableSet( Symbol(), FindLastLots(OP_BUY)*下次倍数); 手数=GlobalVariableGet(Symbol()); OpenOrder(OP_SELL,手数,Bid,Magic); PlaySound("alert.wav"); }if(BuyLots<=0) 手数=第一次手数; else 手数=GlobalVariableGet(Symbol());chkOrder(OP_SELL); } RecountOrders();TrailPositions(); commentTitle(); return(0);} void commentTitle(){ TotProfit(); CreateLabel("转头对冲", Comment8 , 1, 5,10, 1, 文字颜色, "Arial"); CreateLabel("buyCnt", "多单总数:" + BuyCnt, 1, 5,10, 2, 文字颜色, "Arial"); CreateLabel("buyLots", "多单手数:" + DoubleToStr(BuyLots,2) , 1, 5,10, 3, 文字颜色, "Arial"); CreateLabel("buyProfit", "多单利润:" + DoubleToStr(BuyProfit,2), 1, 5,10, 4, 文字颜色, "Arial"); CreateLabel("line3", "=======================", 1, 5,10, 5, 文字颜色, "Arial"); CreateLabel("SellCnt", "空单总数:" + SellCnt, 1, 5,10, 6, 文字颜色, "Arial"); CreateLabel("SellLots", "空单手数:" + DoubleToStr(SellLots,2) , 1, 5,10, 7, 文字颜色, "Arial"); CreateLabel("SellProfit", "空单利润:" + DoubleToStr(SellProfit,2), 1, 5,10, 8, 文字颜色, "Arial"); CreateLabel("line4", "=======================", 1, 5,10, 9, 文字颜色, "Arial"); CreateLabel("profit", "总体利润:" + DoubleToStr(Profit,2), 1, 5,10, 10, 文字颜色, "Arial"); if(Profit< 0 && MaxLose > Profit){ MaxLose = Profit; } CreateLabel("maxLose", "最大浮亏:" + DoubleToStr(MaxLose,2), 1, 5,10, 11, 文字颜色, "Arial"); if(Profit> 0&& MaxLose2 < Profit) MaxLose2 = Profit; CreateLabel("maxLose1", "最大盈利:" + DoubleToStr(MaxLose2,2), 1, 5,10, 12, 文字颜色, "Arial"); if((MaxLose1-保护值)<0)aa=0; elseaa=(MaxLose1-保护值); CreateLabel("myProfit", "预期利润:" + DoubleToStr(aa,2), 1, 5,10, 13, 文字颜色, "Arial");} //统计定单数量void RecountOrders(){ BuyLots = 0; BuyCnt = 0; BuyLimitCnt = 0; BuyStopCnt = 0; BuyProfit = 0; SellLots = 0; SellCnt = 0; SellLimitCnt = 0; SellStopCnt = 0; SellProfit = 0; for (int cnt = OrdersTotal() - 1; cnt >= 0; cnt--) { if (!OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)) continue; if (OrderSymbol() != Symbol()) continue; if (OrderMagicNumber() != Magic) continue; if (OrderType() == OP_BUY&&Magic==OrderMagicNumber()&&OrderSymbol() == Symbol()) { BuyCnt++; BuyLots = BuyLots + OrderLots(); BuyProfit = BuyProfit + OrderProfit()+OrderCommission( )+OrderSwap( ) ; } if (OrderType() == OP_BUYLIMIT&&Magic==OrderMagicNumber()&&OrderSymbol() == Symbol()) BuyLimitCnt++; if (OrderType() == OP_BUYSTOP&&Magic==OrderMagicNumber()&&OrderSymbol() == Symbol()) BuyStopCnt++; if(OrderType() == OP_SELL&&Magic==OrderMagicNumber()&&OrderSymbol() == Symbol()) { SellCnt++; SellLots = SellLots + OrderLots(); SellProfit = SellProfit +OrderProfit()+OrderCommission( )+OrderSwap( ) ; } if(OrderType() == OP_SELLLIMIT&&Magic==OrderMagicNumber()&&OrderSymbol() == Symbol()) SellLimitCnt++; if(OrderType() == OP_SELLSTOP&&Magic==OrderMagicNumber()&&OrderSymbol() == Symbol()) SellStopCnt++; }} //获取手数double getLots(int flag){ return(手数);} int chkOrder(int flag){ double opStopPrice = 0; double opLimitPrice = 0; double nowPrice = 0;bool B=MathAbs(iMA(NULL, 0, SlowerMA, 0, SlowerMode, PRICE_CLOSE, 1)-(Bid+Ask)/2)<=开单距离*Point;RecountOrders(); if(BuyCnt == 0 && flag == 0&&B==1) { RefreshRates(); nowPrice = Ask; OpenOrder(OP_BUY,手数,nowPrice,Magic); return; } if(SellCnt == 0 && flag == 1&&B==1) { RefreshRates(); nowPrice = Bid; OpenOrder(OP_SELL,手数,nowPrice,Magic); return; } if(flag == 0&&BuyCnt>=1 ) { nowPrice = getLastOrderPrice(OP_BUY); if(追单 == true && BuyCnt >= 1) opStopPrice = NormalizeDouble(nowPrice + (挂单距离 * BuyCnt * 小数位),取值位); else opStopPrice = NormalizeDouble(nowPrice + (挂单距离 * 小数位),取值位); if(挂单 == true && BuyCnt >= 1) opLimitPrice =NormalizeDouble(nowPrice - (挂单距离 * BuyCnt * 小数位),取值位); else opLimitPrice =NormalizeDouble(nowPrice - (挂单距离 * 小数位),取值位); if((findOrderLots(OP_BUYSTOP,NormalizeDouble(getLots(flag),2)) == false || findOrderPrice(OP_BUYSTOP,opStopPrice) == false)&& MathAbs(findOrderPrices(OP_BUYSTOP) - MarketInfo(Symbol(),MODE_BID)) > 15 * 小数位) { deleteOrder(OP_BUYSTOP); OpenOrder(OP_BUYSTOP,getLots(flag),NormalizeDouble(opStopPrice,取值位),Magic); } if((findOrderLots(OP_BUYLIMIT,NormalizeDouble(getLots(flag),2)) == false || findOrderPrice(OP_BUYLIMIT,opLimitPrice) == false)&& MathAbs(findOrderPrices(OP_BUYLIMIT) - MarketInfo(Symbol(),MODE_BID)) > 15 * 小数位) { deleteOrder(OP_BUYLIMIT); OpenOrder(OP_BUYLIMIT,getLots(flag),NormalizeDouble(opLimitPrice,取值位),Magic); } } if(flag == 1&&SellCnt>=1) { nowPrice = getLastOrderPrice(OP_SELL); if(挂单 == true && SellCnt >= 1) opLimitPrice = NormalizeDouble(nowPrice + (挂单距离 * SellCnt * 小数位),取值位); else opLimitPrice = NormalizeDouble(nowPrice + (挂单距离 * 小数位),取值位); if(追单 == true && SellCnt >= 1) opStopPrice = NormalizeDouble(nowPrice - (挂单距离 * SellCnt * 小数位),取值位); else opStopPrice = NormalizeDouble(nowPrice - (挂单距离 * 小数位),取值位); if((findOrderLots(OP_SELLLIMIT,NormalizeDouble(getLots(flag),2)) == false || findOrderPrice(OP_SELLLIMIT,opLimitPrice) == false) && MathAbs(findOrderPrices(OP_SELLLIMIT) - MarketInfo(Symbol(),MODE_ASK)) > 15 * 小数位) { deleteOrder(OP_SELLLIMIT); OpenOrder(OP_SELLLIMIT,getLots(flag),NormalizeDouble(opLimitPrice,取值位),Magic); } if((findOrderLots(OP_SELLSTOP,NormalizeDouble(getLots(flag),2)) == false || findOrderPrice(OP_SELLSTOP,opStopPrice) == false) && MathAbs(findOrderPrices(OP_SELLSTOP) - MarketInfo(Symbol(),MODE_ASK)) > 15 * 小数位) { deleteOrder(OP_SELLSTOP); OpenOrder(OP_SELLSTOP,getLots(flag),NormalizeDouble(opStopPrice,取值位),Magic); } }} bool findOrderLots(int type,double Lots){ int cnt = OrdersTotal(); for (int i=0; i < cnt; i++) { if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue; if (OrderSymbol() != Symbol()) continue; if (OrderMagicNumber() != Magic) continue; if (OrderType() == type&&Magic==OrderMagicNumber()) { if(OrderLots() == Lots) return(true); } } return(false);}void deleteOrder(int type){ int cnt = OrdersTotal(); for (int i=0; i < cnt; i++) { if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue; if (OrderSymbol() != Symbol()) continue; if (OrderMagicNumber() != Magic) continue; if (OrderType() == type&&OrderSymbol() == Symbol()&&OrderMagicNumber() == Magic) { OrderDelete(OrderTicket(),Yellow); break; } }} //寻找是否已有这个价格的定单bool findOrderPrice(int orderType,double orderPrice){ int cnt = OrdersTotal(); for(int i = 0; i < cnt; i++) { if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue; if (OrderSymbol() != Symbol()) continue; if (OrderMagicNumber() != Magic) continue; if (OrderType() != orderType) continue; if (NormalizeDouble(OrderOpenPrice(),取值位) == NormalizeDouble(orderPrice,取值位)) if (OrderSymbol() == Symbol()&&OrderMagicNumber() == Magic) return(true); } return(false);}double findOrderPrices(int orderType){ int cnt = OrdersTotal(); for(int i = 0; i < cnt; i++) { if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue; if (OrderSymbol() != Symbol()) continue; if (OrderMagicNumber() != Magic) continue; if (OrderType() != orderType) continue; if (OrderSymbol() == Symbol()&&OrderMagicNumber() == Magic)return(NormalizeDouble(OrderOpenPrice(),取值位)); } return(0);}//获取最后成交定单的价格double getLastOrderPrice(int type){ //datetime openTime; for(int cl = 0; cl <= 1000; cl++) ticket = 0; int cnt = OrdersTotal(); for (int i=0; i < cnt; i++) { if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue; if (OrderSymbol() != Symbol()) continue; if (OrderMagicNumber() != Magic) continue; if (type != OrderType()) continue; if (OrderSymbol() == Symbol()&&OrderMagicNumber() ==Magic) ticket = OrderTicket(); } ArraySort(ticket,WHOLE_ARRAY,0,MODE_DESCEND); if(OrderSelect(ticket,SELECT_BY_TICKET) == true) { if (OrderSymbol() == Symbol()&&OrderMagicNumber() == Magic)return(NormalizeDouble(OrderOpenPrice(),取值位)); }} //开单函数,需要传入买卖标示,手数,识别码int OpenOrder(int buyFlag,double Lots,double price,int magicNum){ int ticket; //交易返回码 for (int i=0; i<RepeatN; i++) { ticket = Buy(Symbol(),buyFlag, Lots, price, 0, 0, magicNum,名称); if (ticket > 0) break; } return;}//多单函数int Buy(string symbol,int OpFlag, double lot, double price, double sl, double tp, int magic, string comment) { int dig = MarketInfo(symbol, MODE_DIGITS); price = NormalizeDouble(price, dig); sl = NormalizeDouble(sl, dig); tp = NormalizeDouble(tp, dig); string _lot = DoubleToStr(lot, 2); string _price = DoubleToStr(price, dig); string _sl = DoubleToStr(sl, dig); string _tp = DoubleToStr(tp, dig); string flag = "Buy"; colorclFlag = 多单颜色; switch(OpFlag) { case 0: flag = "Buy"; clFlag = 多单颜色; comment=WindowExpertName( )+"开多"; break; case 1: flag = "Sell"; clFlag = 空单颜色; comment=WindowExpertName( )+"开空"; break; case 2: flag = "BuyLimit"; clFlag = MediumBlue; comment=WindowExpertName( )+"BuyLimit"; break; case 3: flag = "SellLimit"; clFlag = OrangeRed; comment=WindowExpertName( )+"SellLimit"; break; case 4: flag = "BuyStop"; clFlag = DarkBlue; comment=WindowExpertName( )+"BuyStop"; break; case 5: flag = "SellStop"; clFlag = Brown; comment=WindowExpertName( )+"SellStop"; break; } int res = OrderSend(symbol, OpFlag, lot, price, Slippage, 0, 0, comment, magic, 0, clFlag); if (res >= 0) { return (res); } int code = GetLastError(); switch(code) { case 130: if(OpFlag == OP_BUYLIMIT || OpFlag == OP_BUYSTOP) res = OrderSend(symbol, OP_BUY, lot, Ask, Slippage, 0, 0, comment, magic, 0, clFlag); if(OpFlag == OP_SELLLIMIT || OpFlag == OP_SELLSTOP) res = OrderSend(symbol, OP_SELL, lot, Bid, Slippage, 0, 0, comment, magic, 0, clFlag); if(res >= 0) return(res); case 4107: if(OpFlag == OP_BUYLIMIT || OpFlag == OP_BUYSTOP) res = OrderSend(symbol, OP_BUY, lot, Ask, Slippage, 0, 0, comment, magic, 0, clFlag); if(OpFlag == OP_SELLLIMIT || OpFlag == OP_SELLSTOP) res = OrderSend(symbol, OP_SELL, lot, Bid, Slippage, 0, 0, comment, magic, 0, clFlag); return(res); } return (-1);} //平仓函数void CloseAll() { int l_cmd_20; for (int l_pos_16 = OrdersTotal() - 1; l_pos_16 >= 0; l_pos_16--) { OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES); if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magic) continue; if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) { l_cmd_20 = OrderType(); switch (l_cmd_20) { case OP_BUY: OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 10, Red); break; case OP_SELL: OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 10, Red); break; case OP_BUYSTOP: OrderDelete(OrderTicket(),Yellow); break; case OP_BUYLIMIT: OrderDelete(OrderTicket(),Yellow); break; case OP_SELLSTOP: OrderDelete(OrderTicket(),Yellow); break; case OP_SELLLIMIT: OrderDelete(OrderTicket(),Yellow); break; } } } return (0);} void CreateLabel(string a_name_0, string a_text_8, int a_fontsize_16, int a_corner_20, int ai_24, double ad_28, color a_color_36 = -1, string a_fontname_40 = "Arial Bold") { int l_x_48; a_fontsize_16 += 9; int l_y_52 = 22 + 14 * ad_28; if (a_corner_20 == 0) l_x_48 = 100 + ai_24 * 9 / 9 * 1.0; else { if (a_corner_20 == 1) l_x_48 = 10 + ai_24 * 1.0; else { if (a_corner_20 == 2) l_x_48 = 100 + ai_24 * 9 / 9 * 1.0; else { if (a_corner_20 == 3) { l_x_48 = ai_24 * 1.0; l_y_52 = ad_28; } else { if (a_corner_20 == 5) { l_x_48 = ai_24 * 1.0; l_y_52 = 14 * ad_28; a_corner_20 = 1; } } } } } if (a_color_36 == CLR_NONE) a_color_36 = DeepSkyBlue; ObjectCreate(a_name_0, OBJ_LABEL, 0, 0, 0); ObjectSetText(a_name_0, a_text_8, a_fontsize_16, a_fontname_40, a_color_36); ObjectSet(a_name_0, OBJPROP_CORNER, a_corner_20); ObjectSet(a_name_0, OBJPROP_XDISTANCE, l_x_48); ObjectSet(a_name_0, OBJPROP_YDISTANCE, l_y_52);} void TrailPositions(){ TotProfit();if(Profit > 启动值&& MaxLose1 < Profit) MaxLose1 = Profit; if(保护值 < MaxLose1-Profit && 保护值 > 0&&Profit > 0) { 强平 = true; } else 强平 = false; if(强平 == true) { CloseAll(); 强平 = false; MaxLose = 0; MaxLose1=0; MaxLose2 =0; 手数=第一次手数; aa=0; GlobalVariableDel(Symbol()) ; }}void TotProfit(){Profit=0;for (int cnt = OrdersTotal() - 1; cnt >= 0; cnt--) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if (OrderSymbol() != Symbol()) continue; if (OrderMagicNumber() != Magic) continue; if((OrderType() == OP_SELL||OrderType() == OP_BUY)&&Magic==OrderMagicNumber()&&OrderSymbol() == Symbol()) { Profit = Profit +OrderProfit()+OrderCommission( )+OrderSwap( ) ; } } }double FindLastLots(int oType) { double ticketorderopenprice; int cnt = 0, oldticketnumber, ticketnumber = 0; switch (oType) { case 0: for (cnt = OrdersTotal() - 1; cnt >= 0; cnt--) { if (OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) == false) break; if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magic || OrderType() != OP_BUY||COMM()!=0) continue; if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == OP_BUY) { oldticketnumber = OrderTicket(); if (oldticketnumber > ticketnumber) { ticketorderopenprice = OrderLots(); ticketnumber = oldticketnumber; } } } break; case 1: for (cnt = OrdersTotal() - 1; cnt >= 0; cnt--) { if (OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) == false) break; if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magic || OrderType() != OP_SELL||COMM()!=0) continue; if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == OP_SELL) { oldticketnumber = OrderTicket(); if (oldticketnumber > ticketnumber) { ticketorderopenprice = OrderLots(); ticketnumber = oldticketnumber; } } } break; } return (ticketorderopenprice);}bool COMM(){if("转头对冲多"==OrderComment( )||"转头对冲空"==OrderComment( )||"2点加仓快买0.1手"==OrderComment( )||"2点加仓快卖0.1手"==OrderComment( )||"2点加仓快卖0.05手"==OrderComment( )||"2点加仓快买0.05手"==OrderComment( )||"5点加仓快买0.1手"==OrderComment( )||"5点加仓快卖0.1手"==OrderComment( )||"5点加仓快卖0.05手"==OrderComment( )||"5点加仓快买0.05手"==OrderComment( )) return (1); elsereturn (0);}
sun001002003 发表于 2015-5-12 17:19
The same questions,the same answers:
http://bbs.520fx.com/forum.php?mod=viewthread&tid=68217&page=1# ...
好的,谢谢
天雨虽宽,难润无根之苗。佛法无边,难渡无缘之人 The same questions,the same answers:
http://bbs.520fx.com/forum.php?mod=viewthread&tid=68217&page=1#pid1230767
http://bbs.520fx.com/forum.php?mod=viewthread&tid=68104&page=1#pid1228452
还不错的嘛! 还不错的嘛! 谢谢分享,学编程,学交易,实现程序化交易。 路过回复下
页:
[1]