Order not stopped on TP, please help!

 

Hi!


Sometimes on some indicators when price hits TP it doesnt close order, it just goes until next SL... i think that something in my code is wrong, but i cannot figure out what...can you help me?



my EA:

//+------------------------------------------------------------------+
//|                                                         test.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#include <FinalCustomFunctions.mqh>

input double riskPerTrade = 0.02;
int magicNB = 55555;
int openOrderID;

//trade with trend
extern int Nbr_Periods = 4;
extern double Multiplier = 2.0;

//ATR

input int InpAtrPeriod=14;







//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---


   

   //ATR
   double ATR = iCustom(_Symbol, _Period, "ATR", InpAtrPeriod, 0,0);
   int TrailingStop = ATR *2;
   
   
   
   
   
     
  
    
   double t_trend_up = iCustom(_Symbol, _Period, "Entry//tradewithtrend~", Nbr_Periods, Multiplier, 0,1);
   double t_trend_down = iCustom(_Symbol, _Period, "Entry//tradewithtrend~", Nbr_Periods, Multiplier, 1,1);

   
   
   
   if(!CheckIfOpenOrdersByMagicNB(magicNB)){
   

    if(t_trend_up != EMPTY_VALUE  ){
    
         Print( "Sending buy order");

         double stopLossPrice = Bid - ATR * 1.5;

         double takeProfitPrice = Bid + ATR;

         //Print("Entry Price = " + Ask);

         //Print("Stop Loss Price = " + stopLossPrice);

         //Print("Take Profit Price = " + takeProfitPrice);

         

         double lotSize = OptimalLotSize(riskPerTrade,Bid,stopLossPrice);
         openOrderID = OrderSend(NULL,OP_BUYLIMIT,lotSize,Bid,10,stopLossPrice,takeProfitPrice,NULL,magicNB);
         if(openOrderID < 0) Alert("Buy order rejected. Order error: " + GetLastError());
    
    
    
    }else if( t_trend_down != EMPTY_VALUE){
         Print(" Sending short order");
         double stopLossPrice = Ask + ATR * 1.5;
         double takeProfitPrice = Ask - ATR;

         //Print("Entry Price = " + Bid);

         //Print("Stop Loss Price = " + stopLossPrice);

         //Print("Take Profit Price = " + takeProfitPrice);

          

          double lotSize = OptimalLotSize(riskPerTrade,Ask,stopLossPrice);
          openOrderID = OrderSend(NULL,OP_SELLLIMIT,lotSize,Ask,10,stopLossPrice,takeProfitPrice,NULL,magicNB);
          if(openOrderID < 0) Alert(" Sell order rejected. Order error: " + GetLastError());
    
    }
  }
  
 }
//+------------------------------------------------------------------+
 
Anyone?
 
Hi, maybe there is a problem with broker not code. Check other broker and see what happen. Regards Greg
 
Josip Marić:
Anyone?
Grzegorz Pawlak:
Hi, maybe there is a problem with broker not code. Check other broker and see what happen. Regards Greg
oh, really i never thought that... thank you very much!
 
Josip Marić:

Hi!


Sometimes on some indicators when price hits TP it doesnt close order, it just goes until next SL... i think that something in my code is wrong, but i cannot figure out what...can you help me?



my EA:

I'm going to make a guess that you only see this problem on sell orders. Here's what might be happening.

Buy orders will open at the Ask price and close at the Bid price. Sell orders open at Bid and close at Ask. When your EA places the order you will see the TP and SL lines on screen so you know exactly where they are. Rest assured that if the price hits one of those lines the broker will close the order.

But, if you haven't modified your chart at all you will only see one price line on screen, the Bid price. So you might be seeing the bid price on a sell come down and touch your TP but the Ask price will still be a little above so the TP isn't triggered.

Also, you may not be watching the chart live but seeing in history somewhere that the low was at or below your TP price. In history the OHLC prices are all for Bid.

 
Turn on the Ask line so you see how big the spread is.
 
Arthur Joseph Mcalister:

I'm going to make a guess that you only see this problem on sell orders. Here's what might be happening.

Buy orders will open at the Ask price and close at the Bid price. Sell orders open at Bid and close at Ask. When your EA places the order you will see the TP and SL lines on screen so you know exactly where they are. Rest assured that if the price hits one of those lines the broker will close the order.

But, if you haven't modified your chart at all you will only see one price line on screen, the Bid price. So you might be seeing the bid price on a sell come down and touch your TP but the Ask price will still be a little above so the TP isn't triggered.

Also, you may not be watching the chart live but seeing in history somewhere that the low was at or below your TP price. In history the OHLC prices are all for Bid.

i see both lines on the chart, and this happens on buy and sell orders, and it happens only if the bar where i entered spikes above TP.... it just passes trough Tp and continues on...
 
William Roeder:
Turn on the Ask line so you see how big the spread is.
I have fixed spread... its 20
Reason: