//+------------------------------------------------------------------+ //| Exp_WPRSIsignal.mq4 | //| Copyright 2020, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "2005-2020, MetaQuotes Software Corp." #property link "http://www.mql4.com" //--- Inputs input double Lots = 0.1; // Lot input double MaximumRisk = 0.02; // MaximumRisk input double DecreaseFactor = 3; // DecreaseFactor input double TakeProfit = 90000; // Take Profit input double TrailingStop = 36000; // Фиксированный размер трала input double TrailingStep = 18000; // Шаг трала input string short_name = "WPRSIsignal"; // Name Indicators input bool InpOnlyOne = false; // Close opposite input bool ObjRevers = false; // Revers //------- bool AllPositions = false; // Управлять всеми позициями bool ProfitTrailing = true; // Тралить только профит bool UseSound = true; // Использовать звуковой сигнал string NameFileSound = "expert.wav"; // Наименование звукового файла //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CheckForClose(void) { double MacdCurrent,SignalCurrent; int cnt,total; //--- if(Bars<100) { Print("bars less than 100"); return; } if(TakeProfit<10) { Print("TakeProfit less than 10"); return; } //--- to simplify the coding and speed up access data are put into internal variables MacdCurrent=iCustom(NULL,0,short_name,MODE_MAIN,1); SignalCurrent=iCustom(NULL,0,short_name,MODE_SIGNAL,1); total=OrdersTotal(); if(total<1) { //--- no opened orders identified if(AccountFreeMargin()<(1000*LotsOptimized())) { Print("We have no money. Free Margin = ",AccountFreeMargin()); return; } } //--- it is important to enter the market correctly, but it is more important to exit it correctly... for(cnt=0; cntSignalCurrent)) { //--- close order and exit if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet)) Print("OrderClose error ",GetLastError()); return; } } } else // go to short position { if(!InpOnlyOne) { //--- should it be closed? if((ObjRevers && MacdCurrent>SignalCurrent) || (!ObjRevers && MacdCurrent0) return(buys); else return(-sells); } //+------------------------------------------------------------------+ //| Calculate optimal lot size | //+------------------------------------------------------------------+ double LotsOptimized() { double lot=Lots; int orders=HistoryTotal(); // history orders total int losses=0; // number of losses orders without a break //--- select lot size lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1); //--- calcuulate number of losses orders without a break if(DecreaseFactor>0) { for(int i=orders-1; i>=0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; } if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue; //--- if(OrderProfit()>0) break; if(OrderProfit()<0) losses++; } if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1); } //--- return lot size if(lot<0.1) lot=0.1; return(lot); } //+------------------------------------------------------------------+ //| Check for open order conditions | //+------------------------------------------------------------------+ void CheckForOpen() { double MacdCurrent,SignalCurrent; int res; //--- go trading only for first tiks of new bar if(Volume[0]>1) return; //--- get Moving Average MacdCurrent=iCustom(NULL,0,short_name,MODE_MAIN,1); SignalCurrent=iCustom(NULL,0,short_name,MODE_SIGNAL,1); //--- sell conditions if((ObjRevers && MacdCurrentSignalCurrent)) { res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,Bid-TakeProfit*Point,"",16384,0,Red); return; } //--- buy conditions if((ObjRevers && MacdCurrent>SignalCurrent) || (!ObjRevers && MacdCurrentTrailingStop*pp) { if(OrderStopLoss()TrailingStop*pp) { if(OrderStopLoss()>pAsk+(TrailingStop+TrailingStep-1)*pp || OrderStopLoss()==0) { ModifyStopLoss(pAsk+TrailingStop*pp); return; } } } } //+------------------------------------------------------------------+ //| Перенос уровня StopLoss | //| Параметры: | //| ldStopLoss - уровень StopLoss | //+------------------------------------------------------------------+ void ModifyStopLoss(double ldStopLoss) { bool fm; fm=OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE); if(fm && UseSound) PlaySound(NameFileSound); } //+------------------------------------------------------------------+