Problem with closing order when indicator touches the bar

 

Hi all,

I want to create an EA based on ichimoku strategy. My problem lies when I want to close the open order. The strategy is when the chikou span or lagging indicator touches the bar then the open order will be close.

My attempt is to count the bar using this code:

int FuncBars(int shift)
  {
   int count = 0;
   int counter = 0;
   for(int i = shift; i < Bars; i++)
     {
      counter ++;
      if(counter == shift)
         break;
     }
   return(counter);
  }

the logic behind this is that the code will count the bar from bar 0 to bar shift (in this case is 25 since bar is counted from 0).

then I use this code to close the order

if(chikouSpan() == FuncBars(25))
   {
    bool resOC = OrderClose(orderId,lots,Ask,0);
    if(!resOC)
      {
       Print("Error in Closing Order. Error code=",GetLastError());
       return;
      }
       else
        Print("Closing Order successfully for ticket: ", OrderTicket());
      }
    }

The code never close the order and the order is open until back testing is over. I hope someone can help me how to make this works.

Thank you.

 

Luandre Ezra: My attempt is to count the bar using this code:

The logic behind this is that the code will count the bar from bar 0 to bar shift (in this case is 25 since bar is counted from 0).

  1. Your function is equivalent to just shift (the value passed). Counting from zero through shift is shift.
  2. We have no idea what your chikouSpan() is.
         How To Ask Questions The Smart Way. 2004
              Be precise and informative about your problem

  3. If a bar had already touched the indicator, you would have already closed the order. Therefor all you need to do is test if the last bar is touching.
 

We have no idea what your chikouSpan() is.

Chikou span is the price line that is shifted 26 bars to the left.

If a bar had already touched the indicator, you would have already closed the order. Therefor all you need to do is test if the last bar is touching.

 I tried to put a print statement when the code but the result is the same, nothing happened. I don't know if it's because of the price line is a number and bars is not a number value that the code doesn't recognize the syntax. Basically that part of code never run.