Close trade at the close of current bar.

nicholas herrera
17
nicholas herrera  

Hello I am looking to close a trade that i am in for the duration of 1 bar based on the prev 2 bars. if the parameters fit i enter a trade, but when i set my takeprofit to the close of the current bar it will look to execute a close order immediately as it takes the current close price and not the actual close price. How do i get around this? 


void OnTick()
  {
//---
  if(!CheckIfOpenOrdersByMagicNumber(MN))
  { 
   if (High[1] > High[2] && Close[1] < Close[2])
   {
      Alert("Going short. Reversal Bar.");
      
      double stopLossPrice = High[1];
      double takeProfitPrice = Close[0];
     
      int OrderID = OrderSend(NULL,OP_SELL,OptimalLotSize(maxRiskPerTrade,MathAbs(Bid - stopLossPrice)/GetPipValue()),Bid,20,stopLossPrice,takeProfitPrice,NULL,MN);
      
      if (OrderID < 0) Alert("order rejected. Order error: " + GetLastError());
   }
   else if (Low[1] < Low[2] && Close[1] > Close[2])
   {
      Alert("Going long. Reversal Bar.");
      
      double stopLossPrice = Low[1];
      double takeProfitPrice = Close[0];
      
      int OrderID = OrderSend(NULL,OP_BUY,OptimalLotSize(maxRiskPerTrade,MathAbs(Ask - stopLossPrice)/GetPipValue()),Ask,20,stopLossPrice,takeProfitPrice,NULL,MN);
      
      if (OrderID < 0) Alert("order rejected. Order error: " + GetLastError());
      
     
     
   }
  }
  }
Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Price Constants
Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Price Constants
  • www.mql5.com
Calculations of technical indicators require price values and/or values of volumes, on which calculations will be performed. There are 7 predefined identifiers from the ENUM_APPLIED_PRICE enumeration, used to specify the desired price base for calculations. If a technical indicator uses for calculations price data, type of which is set by...
Keith Watford
Moderator
26146
Keith Watford  
Please edit your post and
use the code button (Alt+S) when pasting code

I know that it is not obvious, but topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I will move your topic to the MQL4 and Metatrader 4 section.

nicholas herrera
17
nicholas herrera  
Keith Watford:
Please edit your post and
use the code button (Alt+S) when pasting code

I know that it is not obvious, but topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I will move your topic to the MQL4 and Metatrader 4 section.

thank you for the help Keith

William Roeder
23492
William Roeder  
nicholas herrera: a close order immediately as it takes the current close price and not the actual close price. How do i get around this? 

You don't.

  1. You can only close at the current market price.
  2. It is impossible to know the current candle close price, because it is impossible to know when a candle will close. A candle is closed when a tick arrives after the last possible second and a new candle is opened..
nicholas herrera
17
nicholas herrera  
William Roeder:

You don't.

  1. You can only close at the current market price.
  2. It is impossible to know the current candle close price, because it is impossible to know when a candle will close. A candle is closed when a tick arrives after the last possible second and a new candle is opened..
Ok so how would I be able to compare its datetime to until it changes?  Or would it be better to use a bar count and when that changes, exit the trade?
William Roeder
23492
William Roeder  
nicholas herrera: Ok so how would I be able to compare its datetime to until it changes?  Or would it be better to use a bar count and when that changes, exit the trade?

For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
          New candle - MQL4 programming forum #3 2014.04.04

I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
          Running EA once at the start of each bar - MQL4 programming forum 2011.05.06