#property copyright "请加QQ群:1128719745,直接下源码" #property link "https://www.mql5.com/" #property version "1.01" #property strict #include #include struct LastOrder{ datetime time; int profit; string sltp; int ticket; double slip; }; LastOrder last_buy, last_sell; extern string EA_Comment = "Kent"; // 订单注释 extern int MagicNumber = 11052016; //魔术号 extern int Slippage = 2; //滑点 extern double Lots = 0.1; //手数 extern double Auto_Risk = 0.0; //资金管理 extern string S_01 = "========= 开仓参数过滤设置 =========="; extern double Stop_Loss = 30; //止损点数 extern double Take_Profit = 35; //止盈点数 extern double Max_Spread = 40; //点差过滤:允许最大点差 extern double Max_Spread_mode2 = 0; //点差过滤:当前点差小于此值允许平仓 extern double Max_Spread_mode2_profit = 10; //点差过滤:平仓时需要的盈利点数 extern int Channel_Period = 13; //布林过滤:布林参数 extern int Entry_Break = 1; //布林过滤:布林加判间距 extern int Min_Volatility = 20; //布林过滤:布林轨最小间距 extern int DailyMA = 0; //均线过滤:参数 extern double DeltaMA = 0.5; //均线过滤:前后均线间距(大于此值不开仓) extern int time_break = 240; //亏损持仓过滤:距离最后一张亏损订单持仓时间 extern int loss = 30; //亏损持仓过滤:判定最后一张订单亏损利润 extern int MaxDailyRange = 0; //盈利过滤:当天盈利超过此值不开仓 extern string S_02 = "==========平仓参数过滤设置================"; extern bool NoCloseWhileOpenSignal = false; //有信号时不平仓 extern int Filter_1 = 1; //第一种过滤方式(时间过滤:1开启2关闭) extern int Exit_Minutes = 140; //时间过滤:持仓时间 extern int Time_Profit_Pips = 0; //时间过滤:利润 extern int Filter_2 = 1; //第二种过滤方式(布林指标过滤:1开启2关闭) extern int Exit_Distance = -13; //布林过滤:间距 extern int Exit_Profit_Pips = -12; //布林过滤:利润 extern bool ExitChannelTP = false; //布林过滤:修改止盈止损 extern int Filter_3 = 1; //第三种过滤方式(MA) extern int ma_period = 2; //均线过滤:周期 extern int Reverse_Profit = 20; //均线过滤:利润 extern int Filter_4 = 1; //第四种过滤方式(超时kdj) extern int TIME =180; //超时kdj过滤:时间 int MM_Depo = 0; ENUM_TIMEFRAMES TimeFrame = PERIOD_M15; ENUM_TIMEFRAMES TimeFrame_CCI = PERIOD_M15; ENUM_APPLIED_PRICE cci_Price_open = PRICE_CLOSE; ENUM_APPLIED_PRICE cci_Price_close = PRICE_CLOSE; bool LossPauseUseDirection = false; int LastHistoryTotal=0; double maxlot = 0.01; double lotstep = 0.01; double old_point = 0.0001; double minlot = 0.0; int stoplevel = 0; double lots = 0; int count_sell; int count_buy; int day_of_trade; string tr_hr = ""; string risk_info = ""; string set_name_info = ""; string warn_trading = ""; int slippage; datetime need_to_verify_channel; //-------------------------------------- extern string S_03 = "==========操作时间设置=============="; extern int monday = 1; //MONDAY 1允许交易 2禁止交易 extern string monday_open = "22:00"; extern string monday_close = "23:59"; extern int tuesday = 1; //TUESDAY 1允许交易 2禁止交易 extern string tuesday_open = "00:00"; extern string tuesday_close = "23:59"; extern int wednesday = 1; //WEDNESDAY 1允许交易 2禁止交易 extern string wednesday_open = "00:00"; extern string wednesday_close = "23:59"; extern int thursday = 1; //THURSDAY 1允许交易 2禁止交易 extern string thursday_open = "00:00"; extern string thursday_close = "23:59"; extern int friday = 1; //FRIDAY 1允许交易 2禁止交易 extern string friday_open = "00:00"; extern string friday_close = "23:59"; extern int sunday = 2; //SUNDAY 1允许交易 2禁止交易 extern string sunday_open = "08:30"; extern string sunday_close = "21:35"; double channel_upper; double channel_lower; double ma_shift_1; double ma_shift_2; double channel_width; double stoploss; double takeprofit; int ticksize; int flag_trade; datetime time1, time4; datetime lasttimewrite, lastprint, last_closetime = 0; bool _RealTrade = (!IsTesting() && !IsOptimization() && !IsVisualMode()); double openD1; bool setnamegood=false; ulong OnTickStart; double mintpb,mintps,tpb,tps; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { day_of_trade=-1; need_to_verify_channel=0; stoplevel = (int)MarketInfo(_Symbol, MODE_STOPLEVEL); minlot = MarketInfo(_Symbol, MODE_MINLOT); maxlot = MarketInfo(_Symbol, MODE_MAXLOT); lotstep = MarketInfo(_Symbol, MODE_LOTSTEP); ticksize = (int)MarketInfo(_Symbol,MODE_TICKSIZE); if (Digits <= 3) old_point = 0.01; else old_point = 0.0001; slippage = (int)NormalizeDouble(Slippage / Point * old_point,0); Take_Profit = MathMax(Take_Profit,NormalizeDouble(stoplevel * Point / old_point,1)); Stop_Loss = MathMax(Stop_Loss,NormalizeDouble(stoplevel * Point / old_point,1)); lots = fGetLots(); if(Auto_Risk > 0.0) { if (MM_Depo == 0) { risk_info = "\n AutoRisk - Activated" + "\n Risk = " + DoubleToStr(Auto_Risk, 2) + "%"; } else { risk_info = "\n AutoRisk - Activated" + "\n Risk = " + DoubleToStr(Lots, 2) + " Lot / " + IntegerToString(MM_Depo) + " "+AccountCurrency(); } } else risk_info = "\n\n AutoRisk - Not activated"; if(loss < 0) loss *= -1; Comment(""); set_name_info=(StringLen(EA_Comment)>28 ? StringSubstr(EA_Comment,0,28)+"..." : EA_Comment); string s=""; switch(_Period){ case PERIOD_M1: s = "M1"; break; case PERIOD_M5: s = "M5"; break; case PERIOD_M15: s = "M15"; break; case PERIOD_M30: s = "M30"; break; case PERIOD_H1: s = "H1"; break; case PERIOD_H4: s = "H4"; break; case PERIOD_D1: s = "D1"; break; case PERIOD_W1: s = "W1"; break; case PERIOD_MN1: s = "MN1"; break; } warn_trading="Set name: "; if(StringFind(EA_Comment, _Symbol)==-1) warn_trading+="[Symbol WARNING] "; else if(StringFind(EA_Comment, s)==-1) warn_trading+="[TF WARNING] "; else warn_trading+="[OK]"; return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { Comment(""); if(ObjectFind("info_panel") >= 0) // fRectLabelDelete(0,"info_panel"); if(ObjectFind("s_h_i_p") >= 0) // fRectLabelDelete(0,"s_h_i_p"); if(ObjectFind("show_i_p") >= 0) // fRectLabelDelete(0,"show_i_p"); if(ObjectFind("show_text") >= 0) fLabelDelete(0,"show_text"); fLabelDelete(0,"LV"); fLabelDelete(0,"BKGR"); fLabelDelete(0,"BKGR2"); fLabelDelete(0,"BKGR3"); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick(){ OnTickStart=GetMicrosecondCount(); if(day_of_trade != DayOfWeek()){ tr_hr = fInfoTradeHours(); fChoiceOfDay(); day_of_trade = DayOfWeek(); fCreateInfoPanel(); openD1 = iOpenX(NULL, _Period, iBarShift(NULL, _Period, StrToTime(TimeToStr(TimeCurrent(),TIME_DATE)))); } int ht=OrdersHistoryTotal(); if(LastHistoryTotal!=ht){ LastHistoryTotal=ht; GetLastProfit(ht); } bool _IsTime = IsTime(); if(!_IsTime && CountOrder() < 1) return; fCreateInfoPanel(); if(!fGetIndicatorData()) return; count_buy = 0; count_sell = 0; for(int pos = OrdersTotal() - 1; pos >= 0; pos--){ if(!OrderSelect(pos, SELECT_BY_POS, MODE_TRADES)){ string q = __FUNCTION__ + ": failed to allocate an order! " + fMyErDesc(); Print(Hour(),":",Minute()," | ",q); } else{ if(OrderType() <= OP_SELL && OrderSymbol() == _Symbol && OrderMagicNumber() == MagicNumber){ if(OrderType() == OP_BUY){ count_buy++; if(OrderStopLoss() == 0.0){ stoploss = NormalizeDouble(OrderOpenPrice() - Stop_Loss * old_point, Digits); takeprofit = NormalizeDouble(OrderOpenPrice() + Take_Profit * old_point, Digits); if(!ExitChannelTP) fModifyPosition(OrderTicket(), OrderOpenPrice(), stoploss, takeprofit, 0, clrGreen); else fModifyPosition(OrderTicket(), OrderOpenPrice(), stoploss, MathMax(tpb,mintpb), 0, clrGreen); continue; } if(1){ if(lastprint!=Time[0]) Print(Hour(),":",Minute()," | BUY | ",IntegerToString((TimeCurrent() - OrderOpenTime())/60)," 根数| ",DoubleToString((Bid - OrderOpenPrice())/old_point,1)," pips"); if(NoCloseWhileOpenSignal && Bid < channel_lower - Entry_Break * old_point) continue; if(Filter_1 == 1 && TimeCurrent() - OrderOpenTime() > 60 * Exit_Minutes && Bid - OrderOpenPrice() > Time_Profit_Pips * old_point){ fClosePosition(OrderTicket(), OrderLots(), NormalizeDouble(Bid, Digits), slippage, clrViolet, OrderType()); iSetLable("B1"); continue; } if(Filter_2 == 1 && Bid >= channel_upper + Exit_Distance * old_point && Bid - OrderOpenPrice() > Exit_Profit_Pips * old_point){ fClosePosition(OrderTicket(), OrderLots(), NormalizeDouble(Bid, Digits), slippage, clrViolet, OrderType()); iSetLable("B2"); continue; } if(Filter_3 == 1){ ma_shift_1 = iMA(NULL, PERIOD_M1, ma_period, 0, MODE_SMA, PRICE_CLOSE, 1); ma_shift_2 = iMA(NULL, PERIOD_M1, ma_period, 0, MODE_SMA, PRICE_CLOSE, 2); if(ma_shift_1 < ma_shift_2 && Bid - OrderOpenPrice() > Reverse_Profit * old_point){ fClosePosition(OrderTicket(), OrderLots(), NormalizeDouble(Bid, Digits), slippage, clrViolet, OrderType()); iSetLable("B3"); continue; } } if(Filter_4 == 1){ if(TimeCurrent() - OrderOpenTime() > 60 * TIME && iStochastic(NULL,PERIOD_M15,5,3,3,MODE_SMA,0,MODE_MAIN,2)>iStochastic(NULL,PERIOD_M15,5,3,3,MODE_SMA,0,MODE_MAIN,3) && iStochastic(NULL,PERIOD_M15,5,3,3,MODE_SMA,0,MODE_MAIN,2)>iStochastic(NULL,PERIOD_M15,5,3,3,MODE_SMA,0,MODE_MAIN,1)) { fClosePosition(OrderTicket(), OrderLots(), NormalizeDouble(Bid, Digits), slippage, clrViolet, OrderType()); iSetLable("B4"); continue; } } }else{ } count_buy++; continue; } if(OrderType() == OP_SELL){ count_sell++; if (OrderStopLoss() == 0.0){ stoploss = NormalizeDouble(OrderOpenPrice() + Stop_Loss * old_point, Digits); takeprofit = NormalizeDouble(OrderOpenPrice() - Take_Profit * old_point, Digits); if(!ExitChannelTP) fModifyPosition(OrderTicket(), OrderOpenPrice(), stoploss, takeprofit, 0, clrGreen); else fModifyPosition(OrderTicket(), OrderOpenPrice(), stoploss, MathMin(tps,mintps), 0, clrGreen); continue; } if(1){ if(lastprint!=Time[0]) Print(Hour(),":",Minute()," | SELL | ",IntegerToString((TimeCurrent() - OrderOpenTime())/60)," ? ",DoubleToString((OrderOpenPrice() - Ask)/old_point,1)," pips"); if(NoCloseWhileOpenSignal && Bid > channel_upper + Entry_Break * old_point) continue; if(Filter_1 == 1 && TimeCurrent() - OrderOpenTime() > 60 * Exit_Minutes && OrderOpenPrice() - Ask > Time_Profit_Pips * old_point){ fClosePosition(OrderTicket(), OrderLots(), NormalizeDouble(Ask, Digits), slippage, clrViolet, OrderType()); iSetLable("S1"); continue; } if(Filter_2 == 1 && Bid <= channel_lower - Exit_Distance * old_point && OrderOpenPrice() - Ask > Exit_Profit_Pips * old_point){ fClosePosition(OrderTicket(), OrderLots(), NormalizeDouble(Ask, Digits), slippage, clrViolet, OrderType()); iSetLable("S2"); continue; } if(Filter_3 == 1){ ma_shift_1 = iMA(NULL, PERIOD_M1, ma_period, 0, MODE_SMA, PRICE_CLOSE, 1); ma_shift_2 = iMA(NULL, PERIOD_M1, ma_period, 0, MODE_SMA, PRICE_CLOSE, 2); if(ma_shift_1 > ma_shift_2 && OrderOpenPrice() - Ask > Reverse_Profit * old_point){ fClosePosition(OrderTicket(), OrderLots(), NormalizeDouble(Ask, Digits), slippage, clrViolet, OrderType()); iSetLable("S3"); continue; } } if(Filter_4 == 1){ if(TimeCurrent() - OrderOpenTime() > 60 * TIME && iStochastic(NULL,PERIOD_M15,5,3,3,MODE_SMA,0,MODE_MAIN,2) channel_upper + Entry_Break * old_point){ OpenTradeConditions("SELL",OP_SELL,count_buy); } int Error = GetLastError(); if(Error != 0) Print(Hour(),":",Minute()," | ",__FUNCTION__," ",fMyErDesc(Error)); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //注释函数,方便观察用 void iSetLable(string LableDoc,color DocColor = clrRed) { string LableName = "平仓"+ IntegerToString(Time[0]); int LableX = 5; int LableY = 20; int DocSize = 10; string DocStyle = "Verdana"; ObjectCreate(LableName,OBJ_TEXT,0,Time[1],Low[1]); ObjectSetText(LableName,LableDoc,DocSize,DocStyle,DocColor); ObjectSet(LableName,OBJPROP_XDISTANCE,LableX); ObjectSet(LableName,OBJPROP_YDISTANCE,LableY); } bool OpenTradeConditions(string _OrderType, int OP_TYPE, int opposite_order) { if(DailyMA > 0){ ma_shift_1 = iMA(NULL, PERIOD_M15, DailyMA, 0, MODE_SMA, PRICE_CLOSE, 1); ma_shift_2 = iMA(NULL, PERIOD_M15, DailyMA, 0, MODE_SMA, PRICE_CLOSE, 2); if((OP_TYPE == OP_BUY && ma_shift_1 < ma_shift_2 && ma_shift_2 - ma_shift_1 >= DeltaMA*old_point) || (OP_TYPE == OP_SELL && ma_shift_1 > ma_shift_2 && ma_shift_1 - ma_shift_2 >= DeltaMA*old_point)){ return(false); } } if(MaxDailyRange > 0) { if(openD1==0) openD1 = iOpenX(NULL, _Period, iBarShift(NULL, _Period, StrToTime(TimeToStr(TimeCurrent(),TIME_DATE)))); if(openD1==0) return false; double mdr = OP_TYPE==OP_BUY ? openD1 - Bid : Bid - openD1; if(mdr > MaxDailyRange*old_point){ return(false); } } if (channel_width != 0 && channel_width < Min_Volatility*old_point) { return(false); } if (Max_Spread > 0 && (Ask - Bid) > Max_Spread*old_point && (Max_Spread_mode2==0 || OP_TYPE==OP_BUY || (Ask - Bid) > Stop_Loss/2*old_point)) { return(false); } datetime ordertime; if(time_break > 0 && lastloss(OP_TYPE,ordertime)) { return(false); } OpenTrade(_OrderType); return(true); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ double fGetLots(void){ double lot=Lots; if (Auto_Risk > 0.0) { if (MM_Depo == 0){ lot = (MathMax(minlot, MathMin(maxlot, MathFloor(AccountBalance()*Auto_Risk/100/MarketInfo(_Symbol,MODE_TICKVALUE)/(Stop_Loss*old_point/Point)/lotstep) * lotstep))); }else{ lot = NormalizeDouble(Lots * MathFloor(MathMin(AccountBalance(),AccountEquity())/MM_Depo), 2); lot = MathMin(MathMax(lot, minlot), maxlot); } } return lot; } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ bool fGetIndicatorData(){ if(need_to_verify_channel != Time[0]){ if(iBars(_Symbol, TimeFrame) < Channel_Period+1){ Print(Hour(),":",Minute()," | Error: k线根数过少!!!"); return false; } channel_upper = iBands(NULL,TimeFrame,Channel_Period,2,0,PRICE_CLOSE,MODE_UPPER,1); channel_lower = iBands(NULL,TimeFrame,Channel_Period,2,0,PRICE_CLOSE,MODE_LOWER,1); if(ExitChannelTP) fSetTPbyExitChannel(); channel_width = channel_upper - channel_lower; need_to_verify_channel = Time[0]; } return true; } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ void fCreateInfoPanel(void){ if(!IsVisualMode() && (IsTesting() || IsOptimization())) return; string info_panel; info_panel = "\n\n" + "\n GMT Time = " + TimeToStr(TimeGMT(),TIME_DATE|TIME_MINUTES) + tr_hr + "\n -----------------------------------------------" + "\n Take Profit = " + DoubleToStr(Take_Profit,1) + " pips" + "\n Stop Loss = " + DoubleToStr(Stop_Loss,1) + " pips" + "\n -----------------------------------------------" + "\n Max Spread = " + DoubleToStr(Max_Spread,1) + " pips" + "\n Spread = " + DoubleToStr((Ask - Bid) / old_point, 1) + " pips"; if(Ask - Bid > Max_Spread * old_point) info_panel = info_panel + " - HIGH "; else info_panel = info_panel + " - NORMAL"; info_panel = info_panel + risk_info + "\n Trading Lots = " + DoubleToStr(lots, 2) + "\n -----------------------------------------------" + "\n Total Profit = " + DoubleToStr(AccountProfit(), 2); Comment(info_panel); if (ObjectFind("LV") < 0) { ObjectCreate ("LV", OBJ_LABEL, 0, 0, 0); ObjectSetText("LV", "Kent EA", 9, "Tahoma Bold", White); ObjectSet ("LV", OBJPROP_CORNER, 0); ObjectSet ("LV", OBJPROP_BACK, FALSE); ObjectSet ("LV", OBJPROP_XDISTANCE, 35); ObjectSet ("LV", OBJPROP_YDISTANCE, 23); } if (ObjectFind("BKGR") < 0) { ObjectCreate ("BKGR", OBJ_LABEL, 0, 0, 0); ObjectSetText("BKGR", "g", 120, "Webdings", clrCoral); ObjectSet ("BKGR", OBJPROP_CORNER, 0); ObjectSet ("BKGR", OBJPROP_BACK, TRUE); ObjectSet ("BKGR", OBJPROP_XDISTANCE, 5); ObjectSet ("BKGR", OBJPROP_YDISTANCE, 15); } if (ObjectFind("BKGR2") < 0) { ObjectCreate ("BKGR2", OBJ_LABEL, 0, 0, 0); ObjectSetText("BKGR2", "g", 120, "Webdings", clrDarkGray); ObjectSet ("BKGR2", OBJPROP_BACK, TRUE); ObjectSet ("BKGR2", OBJPROP_XDISTANCE, 5); ObjectSet ("BKGR2", OBJPROP_YDISTANCE, 80); } if (ObjectFind("BKGR3") < 0) { ObjectCreate ("BKGR3", OBJ_LABEL, 0, 0, 0); ObjectSetText("BKGR3", "g", 120, "Webdings", clrDarkGray); ObjectSet ("BKGR3", OBJPROP_CORNER, 0); ObjectSet ("BKGR3", OBJPROP_BACK, TRUE); ObjectSet ("BKGR3", OBJPROP_XDISTANCE, 5); ObjectSet ("BKGR3", OBJPROP_YDISTANCE, 45); } } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ void fTrailingStopFunc(const double tr_start,const double tr_size,const double tr_step){ if(OrderType() == OP_BUY){ if(OrderStopLoss() < OrderOpenPrice() && Bid - OrderOpenPrice() >= tr_start){ fModifyPosition(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,clrGreen); return; } if(OrderStopLoss() >= OrderOpenPrice()){ double dif = Bid - OrderStopLoss() - tr_size; if(dif >= tr_step) fModifyPosition(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderStopLoss() + dif,Digits), OrderTakeProfit(),0,clrGreen); return; } } if(OrderType() == OP_SELL){ if(OrderStopLoss() > OrderOpenPrice() && OrderOpenPrice() - Ask >= tr_start){ fModifyPosition(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,clrTomato); return; } if(OrderStopLoss() <= OrderOpenPrice()){ double dif = OrderStopLoss() - Ask - tr_size; if(dif >= tr_step) fModifyPosition(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderStopLoss() - dif,Digits), OrderTakeProfit(),0,clrTomato); return; } } } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ void fClosePosition(int ticket,double lot,double price,int slip,color col, int type){ if(Max_Spread_mode2>0){ double OrderDistance = type == OP_BUY ? Bid - OrderOpenPrice() : OrderOpenPrice() - Ask; if ((Ask - Bid) > Max_Spread_mode2*old_point && type==OP_SELL && OrderDistance < Max_Spread_mode2_profit * old_point) { return; } } OrderCloseX(ticket, lot, type, slippage, col); return; } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ bool lastloss(int OP_TYPE, datetime &ordertime){ if((!LossPauseUseDirection || OP_TYPE == OP_BUY) && last_buy.profit*-1 >= loss && TimeCurrent()-last_buy.time < time_break*60){ ordertime=last_buy.time; return true; } if((!LossPauseUseDirection || OP_TYPE == OP_SELL) && last_sell.profit*-1 >= loss && TimeCurrent()-last_sell.time < time_break*60){ ordertime=last_sell.time; return true; } return false; } void GetLastProfit(int ht){ int changes=0; for(int iPos=ht-1; iPos >= 0; iPos--){ if(OrderSelect(iPos, SELECT_BY_POS, MODE_HISTORY)==false){ continue; } if(OrderSymbol() != _Symbol || OrderMagicNumber() != MagicNumber) continue; if(OrderType() == OP_BUY){ if(OrderCloseTime() > last_buy.time){ ZeroMemory(last_buy); last_buy.time = OrderCloseTime(); last_buy.profit = (int)NormalizeDouble((OrderClosePrice() - OrderOpenPrice()) / old_point,0); changes++; if(_RealTrade){ if(StringFind(OrderComment(), "[sl]", 0)!=-1){ last_buy.ticket = OrderTicket(); last_buy.sltp = "sl"; last_buy.slip = NormalizeDouble((OrderStopLoss() - OrderClosePrice())/old_point,1); } if(StringFind(OrderComment(), "[tp]", 0)!=-1){ last_buy.ticket = OrderTicket(); last_buy.sltp = "tp"; last_buy.slip = NormalizeDouble((OrderTakeProfit() - OrderClosePrice())/old_point,1); } } } }else{ if(OrderCloseTime() > last_sell.time){ ZeroMemory(last_sell); last_sell.time = OrderCloseTime(); last_sell.profit = (int)NormalizeDouble((OrderOpenPrice() - OrderClosePrice()) / old_point,0); changes++; if(_RealTrade){ if(StringFind(OrderComment(), "[sl]", 0)!=-1){ last_sell.ticket = OrderTicket(); last_sell.sltp = "sl"; last_sell.slip = NormalizeDouble((OrderClosePrice() - OrderStopLoss())/old_point,1); } if(StringFind(OrderComment(), "[tp]", 0)!=-1){ last_sell.ticket = OrderTicket(); last_sell.sltp = "tp"; last_sell.slip = NormalizeDouble((OrderClosePrice() - OrderTakeProfit())/old_point,1); } } } } } ResetLastError(); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ void fModifyPosition(int ticket,double price,double sl,double tp,datetime expir = 0,color col = clrNONE){ if(!OrderModify(ticket, price, sl, tp, expir, col)) Print(Hour(),":",Minute()," | ",__FUNCTION__," ",fMyErDesc()); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ int fGetCCISignal(int period,int typeprice,int toplevel,int lowerlevel,int sh){ double cci = iCCI(NULL,TimeFrame_CCI,period,typeprice,sh); if(cci > toplevel) return(OP_SELL); if(cci < lowerlevel) return(OP_BUY); return(-1); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ bool OpenTrade(string type){ double price = 0; double signalbid = Bid; int cmd = -1; color col_type = clrNONE; if(_RealTrade && !IsTradeAllowed()){ return false; } lots = fGetLots(); if(type == "BUY"){ cmd = OP_BUY; col_type = clrAqua; } if(type == "SELL"){ cmd = OP_SELL; col_type = clrRed; } double free_margin=AccountFreeMarginCheck(_Symbol,cmd,lots); if(free_margin<0){ return false; } string WorkTime=DoubleToString((GetMicrosecondCount()-OnTickStart)/1000.0,2); int err=0; for(int count = 0; count < 5; count++){ price = cmd==OP_BUY ? Ask : Bid; uint start=GetTickCount(); int ticket = OrderSend(_Symbol, cmd, lots, price, slippage, 0, 0, EA_Comment, MagicNumber, 0, col_type); if(ticket >= 0){ uint time=GetTickCount()-start; bool exit_loop = false; int retry = 10; int cnt1 = 0; while(!exit_loop && cnt1= time1 && TimeCurrent() <= time4) return(true); else return(false); } if(time1 > time4) { if(TimeCurrent() >= time1 || TimeCurrent() <= time4) return(true); else return(false); } return false; } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ ///////////////////////////////////////////////////////////////////////// void fChoiceOfDay(void) { switch(DayOfWeek()) { case 0: { if(sunday == 1) fSetTimeTrade(sunday_open,sunday_close); else{ time1 = StrToTime(StringConcatenate(TimeToStr(TimeCurrent(),TIME_DATE)," ","01:20")); time4 = StrToTime(StringConcatenate(TimeToStr(TimeCurrent(),TIME_DATE)," ","01:10")); } break; } case 1: { if(monday == 1) fSetTimeTrade(monday_open,monday_close); else{ time1 = StrToTime(StringConcatenate(TimeToStr(TimeCurrent(),TIME_DATE)," ","01:20")); time4 = StrToTime(StringConcatenate(TimeToStr(TimeCurrent(),TIME_DATE)," ","01:10")); } break; } case 2: { if(tuesday == 1) fSetTimeTrade(tuesday_open,tuesday_close); else{ time1 = StrToTime(StringConcatenate(TimeToStr(TimeCurrent(),TIME_DATE)," ","01:20")); time4 = StrToTime(StringConcatenate(TimeToStr(TimeCurrent(),TIME_DATE)," ","01:10")); } break; } case 3: { if(wednesday == 1) fSetTimeTrade(wednesday_open,wednesday_close); else{ time1 = StrToTime(StringConcatenate(TimeToStr(TimeCurrent(),TIME_DATE)," ","01:20")); time4 = StrToTime(StringConcatenate(TimeToStr(TimeCurrent(),TIME_DATE)," ","01:10")); } break; } case 4: { if(thursday == 1) fSetTimeTrade(thursday_open,thursday_close); else{ time1 = StrToTime(StringConcatenate(TimeToStr(TimeCurrent(),TIME_DATE)," ","01:20")); time4 = StrToTime(StringConcatenate(TimeToStr(TimeCurrent(),TIME_DATE)," ","01:10")); } break; } case 5: { if(friday == 1) fSetTimeTrade(friday_open,friday_close); else{ time1 = StrToTime(StringConcatenate(TimeToStr(TimeCurrent(),TIME_DATE)," ","01:20")); time4 = StrToTime(StringConcatenate(TimeToStr(TimeCurrent(),TIME_DATE)," ","01:10")); } break; } default:{ time1 = StrToTime(StringConcatenate(TimeToStr(TimeCurrent(),TIME_DATE)," ","01:20")); time4 = StrToTime(StringConcatenate(TimeToStr(TimeCurrent(),TIME_DATE)," ","01:10")); } } } //+------------------------------------------------------------------+ void fSetTimeTrade(string open,string close) { time1 = StrToTime(StringConcatenate(TimeToStr(TimeCurrent(),TIME_DATE)," ",open)); time4 = StrToTime(StringConcatenate(TimeToStr(TimeCurrent(),TIME_DATE)," ",close)); } bool fRectLabelDelete(const long chart_ID = 0, const string name = "RectLabel") { ResetLastError(); if(!ObjectDelete(chart_ID,name)) { Print(__FUNCTION__, ": failed to remove a rectangular mark! " + fMyErDesc()); return(false); } return(true); } bool fLabelDelete(const long chart_ID=0, const string name="Label") { ResetLastError(); if(!ObjectDelete(chart_ID,name)) { Print(__FUNCTION__, ": failed to remove the text label! Error code = ",GetLastError()); return(false); } return(true); } //+------------------------------------------------------------------+ int CountOrder(){ int orders=0; if(_RealTrade){ for(int i=OrdersTotal()-1;i>=0;i--){ if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==false){ ResetLastError(); continue; } if(OrderSymbol()!=_Symbol || OrderMagicNumber() != MagicNumber) continue; orders++; } }else orders=OrdersTotal(); return orders; } void OrderCloseX(int ticket1, double lot, int otype, int slp, color clr=CLR_NONE){ int err = 0; double price = 0; uint start=GetTickCount(); bool exit_loop = false; int retry = 5; int cnt1 = 0; string WorkTime=DoubleToString((GetMicrosecondCount()-OnTickStart)/1000.0,2); while(!exit_loop && cnt10 && tickvalue>0){ comission*=-1; if(comission<=0) return _Point; return NormalizeDouble(comission/lot/tickvalue*ticksize,_Digits); }else return 10*_Point; } void fSetTPbyExitChannel(){ stoplevel = (int)MarketInfo(_Symbol, MODE_STOPLEVEL); mintpb = NormalizeDouble(Ask+MathMax(stoplevel,1)*_Point,_Digits); mintps = NormalizeDouble(Bid-MathMax(stoplevel,1)*_Point,_Digits); tpb = NormalizeDouble(channel_upper+Exit_Distance*old_point,_Digits); tps = NormalizeDouble(channel_lower-(Exit_Distance*old_point)+MathMin(MathAbs(Max_Spread_mode2>0 ? Max_Spread_mode2*old_point : Max_Spread*old_point), (Ask-Bid)),_Digits); for(int i=OrdersTotal()-1;i>=0;i--){ if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==false){ Print(Hour(),":",Minute()," | ",__FUNCTION__," ",fMyErDesc()); continue; } if(OrderSymbol()!=_Symbol || OrderMagicNumber() != MagicNumber) continue; if(OrderType()==OP_BUY){ if(tpb > OrderOpenPrice()+Exit_Profit_Pips*old_point && !CompareDoubles(tpb,OrderTakeProfit()) && tpb >= mintpb) fModifyPosition(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), tpb, 0, clrWhite); }else{ if(tps < OrderOpenPrice()-Exit_Profit_Pips*old_point && !CompareDoubles(tps,OrderTakeProfit()) && tps <= mintps) fModifyPosition(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), tps, 0, clrWhite); } } } //+------------------------------------------------------------------+ string fMyErDesc(int err=-1){ int aErrNum; if(err == -1) aErrNum = GetLastError(); else aErrNum = err; return StringConcatenate("Error ? ",aErrNum," - ",ErrorDescription(aErrNum)); }