Thanks GumRai,
should i have to change this
if(StopLoss!=0)bsl = Ask-(StopLoss*pips);
to
if(StopLoss!=0)bsl = Ask-(PriceLevel*pips);
entry=Ask - PriceLevel * Point; if(StopLoss!=0)bsl = entry-(StopLoss*pips); if(TakeProfit!=0)btp = entry+(TakeProfit*pips);.
Hi GumRai,
Thank you for your patience, I modify the code as you told , But some times it is hit the take profit without closing the trade. as it is showing in the attached photo.Also some time it is not showing stop loss or take profit?
I think the problem with the (Stop Loss , Take profit ) Setting, But i'm not able to find the solution. Please help.
//+------------------------------------------------------------------+ //| My Moving Average EA.mq4 | //| Copyright 2015, AL-Tubaiti Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2015, AL-Tubaiti Software Corp." #property link "https://www.mql5.com" extern int PriceLevel = 10; extern int TakeProfit = 50; extern int StopLoss = 20; extern double LotSize = 1.0; extern bool UseMoveToBreakEven=false; extern int WhenToMoveToBe = 100; extern int PipsToLockIn = 5; extern bool UseTrailingStop = true; extern int WhenToTrail = 15; extern int TrailAmount =15; extern bool UseCandleTrail = false; extern int PadAmount =10; extern int CandleBack =5; extern int REDMA = 5; extern int YELLOWMA = 21; extern int BLUEMA = 50; extern int MagicNumber = 1234; double pips; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- double TickSize = MarketInfo(Symbol(),MODE_TICKSIZE); if(TickSize == 0.00001 || TickSize == 0.001) pips = TickSize*10; else pips = TickSize; Comment("\nTestv4.mq4"); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- if(OpenOrdersThisPair(Symbol())>=1) { if(UseMoveToBreakEven)MoveToBreakeven(); if(UseTrailingStop)AdjustTrail(); } if(IsNewCandle())CheckForMaTrade(); //--- return(0); } //+------------------------------------------------------------------+ void MoveToBreakeven() { for(int b = OrdersTotal()-1; b >= 0; b--); { if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) if(OrderMagicNumber()== MagicNumber) if(OrderSymbol()==Symbol()) if(OrderType()==OP_BUY) if(Bid-OrderOpenPrice() > WhenToMoveToBe*pips) if(OrderOpenPrice()>OrderStopLoss()) OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+(PipsToLockIn*pips),OrderTakeProfit(),0,clrNONE); } for(int s = OrdersTotal()-1;s>=0; s--) { if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES)) if(OrderMagicNumber()==MagicNumber) if(OrderSymbol()==Symbol()) if(OrderType()==OP_SELL) if(OrderOpenPrice()-Ask>WhenToMoveToBe*pips) if(OrderOpenPrice()<OrderStopLoss()) OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-(PipsToLockIn*pips),OrderTakeProfit(),0,clrNONE); } } //+------------------------------------------------------------------+ //| trailing stop function | //+------------------------------------------------------------------+ void AdjustTrail() { int buyStopCandle = iLowest (NULL,0,1,CandleBack,0); int SellStopCandle = iHighest(NULL,0,2,CandleBack,0); // buy order section for(int b=OrdersTotal() -1; b >= 0; b--) { if(OrderSelect(b, SELECT_BY_POS, MODE_TRADES)) if(OrderMagicNumber()==MagicNumber) if(OrderSymbol() ==Symbol()) if(OrderType()==OP_BUY) if(UseCandleTrail) { if(IsNewCandle()) if(OrderStopLoss()<Low[buyStopCandle]-PadAmount*pips) OrderModify(OrderTicket(),OrderOpenPrice(), Low[buyStopCandle]-PadAmount*pips,OrderTakeProfit(),0,clrNONE); } else if(Bid-OrderOpenPrice()> WhenToTrail*pips) if(OrderStopLoss()<Bid-pips*TrailAmount) OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(pips*TrailAmount),OrderTakeProfit(),0,clrNONE); } // sell order section for(int s=OrdersTotal()-1; s>=0; s--) { if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES)) if(OrderMagicNumber() == MagicNumber) if(OrderSymbol() == Symbol()) if(OrderType() ==OP_SELL) if(UseCandleTrail) { if(IsNewCandle()) if(OrderStopLoss()>High[SellStopCandle]+PadAmount*pips) OrderModify(OrderTicket(),OrderOpenPrice(),High[SellStopCandle]+PadAmount*pips,OrderTakeProfit(),0,clrNONE); } else if(OrderOpenPrice() -Ask>WhenToTrail*pips) if(OrderStopLoss()>Ask+TrailAmount*pips || OrderStopLoss() ==0) OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(TrailAmount*pips),OrderTakeProfit(),0,clrNONE); } } bool IsNewCandle() { static int BarsOnChart=0; if(Bars ==BarsOnChart) return(false); BarsOnChart = Bars; return(true); } //+------------------------------------------------------------------+ void CheckForMaTrade() { //RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR double M30_Red_0,M30_Yellow_0, M30_Red_1,M30_Yellow_1; M30_Red_0 = iMA(Symbol(),PERIOD_M30,5, 0,MODE_SMA,PRICE_CLOSE,0); M30_Yellow_0 = iMA(Symbol(),PERIOD_M30,21,0,MODE_SMA,PRICE_CLOSE,0); M30_Red_1 = iMA(Symbol(),PERIOD_M30,5, 0,MODE_SMA,PRICE_CLOSE,1); M30_Yellow_1 = iMA(Symbol(),PERIOD_M30,21,0,MODE_SMA,PRICE_CLOSE,1); double CrossUp = M30_Red_1 < M30_Yellow_1 && M30_Red_0 > M30_Yellow_0; double CrossDn = M30_Red_1 > M30_Yellow_1 && M30_Red_0 < M30_Yellow_0; //RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR if( CrossUp) OrderEntry(0); if( CrossDn) OrderEntry(1); //RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR } void OrderEntry(int direction) { if (direction ==0) { double bsl=0; double btp=0; //if(StopLoss!=0)bsl = Ask-(StopLoss*pips); // if(TakeProfit!=0)btp = Ask+(TakeProfit*pips); double entry = Ask - PriceLevel * pips; if(StopLoss!=0)bsl = entry-(StopLoss*pips); if(TakeProfit!=0)btp = entry+(TakeProfit*pips); if(OpenOrdersThisPair(Symbol())==0)int buyticket = OrderSend(Symbol(),OP_BUYLIMIT,LotSize,entry,3,0,0, NULL,MagicNumber,TimeCurrent()+5400,clrDarkOrange); if(buyticket > 0) OrderModify(buyticket,OrderOpenPrice(),bsl,btp,0,clrGreen); } if (direction ==1) { double ssl=0; double stp=0; //if(StopLoss !=0)ssl = Bid+(StopLoss*pips); // if(TakeProfit !=0)stp = Bid-(TakeProfit*pips); double Sentry = Bid + PriceLevel * pips; if(StopLoss!=0)ssl = Sentry + (StopLoss*pips); if(TakeProfit!=0)stp = Sentry - (TakeProfit*pips); if(OpenOrdersThisPair(Symbol())==0)int sellticket = OrderSend(Symbol(),OP_SELLLIMIT,LotSize,Sentry,3,0,0, NULL,MagicNumber,TimeCurrent()+5400,clrYellow); if(sellticket > 0) OrderModify(sellticket,OrderOpenPrice(),ssl,stp,0,clrGreen); } } int OpenOrdersThisPair(string pair) { int total=0; for(int i=OrdersTotal()-1; i >= 0; i--) { OrderSelect(i,SELECT_BY_POS,MODE_TRADES); if(OrderSymbol() == pair) total++; } return(total); }
Hi,
I think i found the problem, I changed this line
OrderModify(sellticket,OrderOpenPrice(),ssl,stp,0,clrGreen);
to
OrderModify(sellticket,Sentry,ssl,stp,0,clrGreen)
and it seem to work.
Please confirm if this is right?
As you didn't select the order before using OrderOpenPrice(), then it could cause an error.
I don;t believe that that explains the image that you showed earlier though.
It would appear that, because of the spread, the Ask did not go low enough to trigger the order. So it remained a limit order.
A limit order has to be triggered before it can hit TP
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
I mide some change to this EA from
to
and it work fine expet (Stop loss and tak profit) it is not working?