Close order error

 

Hi, I'm new in this industrial. I have one error regrading the close order.
My EA is using indicator crossover to open an order, and my condition to close an order is when iClose is greater then indicator it will automatic close and order after the specific bar of candlestick  closed.
But I having the error is the specific candlestick haven't close above (which still floating) the indicator it close automatically.

The picture attached below, the last candlestick didn't close above the the red line and it automatically close the order. my condition is the order will close after the candlestick close above the line and form a green-line instead red-line.
Or I need to place a RefreshRate(); in the code?

double close_price_Main_Sub = iClose(NULL,0,shift);
double Main_Trend = iCustom(NULL,0,"super-trend",Main_Nbr_Periods,Main_Multiplier,applied_price,shift);
double Sub_Trend = iCustom(NULL,0,"super-trend",Sub_Nbr_Periods,Sub_Multiplier,applied_price,shift);


if(close_price_Main_Sub > Main_Trend && close_price_Main_Sub < Sub_Trend)
      {
      close_all_long();
      } 
else if(close_price_Main_Sub < Main_Trend && close_price_Main_Sub > Sub_Trend)
      {
      close_all_short();
      }
 
Woods Lim:

Hi, I'm new in this industrial. I have one error regrading the close order.
My EA is using indicator crossover to open an order, and my condition to close an order is when iClose is greater then indicator it will automatic close and order after the specific bar of candlestick  closed.
But I having the error is the specific candlestick haven't close above (which still floating) the indicator it close automatically.

The picture attached below, the last candlestick didn't close above the the red line and it automatically close the order. my condition is the order will close after the candlestick close above the line and form a green-line instead red-line.
Or I need to place a RefreshRate(); in the code?

I think the problem comes from the fact that if shift is 0 in earlier condition or loop it will check Close[0] which is the current price while candle not closed. So I think you should check with that to start from candle [1] - Close[1] iCustom(....,1) which will not change while candle is still not closed. 

 
Woods Lim:

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.

 
Nikolay Georgiev:

I think the problem comes from the fact that if shift is 0 in earlier condition or loop it will check Close[0] which is the current price while candle not closed. So I think you should check with that to start from candle [1] - Close[1] iCustom(....,1) which will not change while candle is still not closed. 

 

Yes, I have checked the Moving Average EA in the MetaTrader and I manage to solve the problem. Much appreciate your reply.
 
Keith Watford:

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.

Sorry for any inconvenience because I when I registered it pop out the MQL5 community only. Where should I post could you kindly advice me on this?

 
Woods Lim:

Sorry for any inconvenience because I when I registered it pop out the MQL5 community only. Where should I post could you kindly advice me on this?

Click on Forum at the top of the page then click on the section that you want to read/post.

 
  1. you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum?
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. double close_price_Main_Sub = iClose(NULL,0,shift);
    double Main_Trend = iCustom(NULL,0,…,shift);
    double Sub_Trend = iCustom(NULL,0,…

    Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum 2020.07.25
 
William Roeder:
  1. you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum?
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum 2020.07.25

Many thx on your reply and explanation and I manage to solve the problem.

Reason: