Getting previous candlestick

 

i am coding a expert advisor that get the candlestick a sell trade was taken on and then use that to get the candlestick before that sell candlestick and then place a buy trade and use it as stop loss but the ea open many orders which is not my problem the issue is that it uses the sell candlestick as stop loss for one order and then candlestick before the sell candlestick as stop loss for another order, this is the code

 // Check for closed sell trades with a loss
   for (int i = OrdersHistoryTotal() - 1; i >= 0; i--)
     {
      if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) && OrderType() == OP_SELL && OrderProfit() < 0)
        {
         // Get the time of the sell trade candlestick
         datetime sellTradeTime = OrderOpenTime();
         int sellCandleIndex = iBarShift(Symbol(), Period(), sellTradeTime);
         
         // Get the time of the previous candlestick before the sell trade
         datetime previousCandleTime = iTime(Symbol(), Period(), sellCandleIndex);

         // Calculate the index of the previous candlestick
         int previousCandleIndex = iBarShift(Symbol(), Period(), previousCandleTime - 1);

         // Get the low price of the previous candlestick
         double previousCandleLow = iLow(Symbol(), Period(), previousCandleIndex);
         
what can be the issue
 
  1.    for (int i = OrdersHistoryTotal() - 1; i >= 0; i--)
         {
          if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) && OrderType() == OP_SELL && OrderProfit() < 0)

    What happens if you find an order from a different symbol? Or with a different Magic Number?

  2. Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum (2012)
              Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020)

  3.          datetime previousCandleTime = iTime(Symbol(), Period(), sellCandleIndex);

    That's not the previous candle's time, that's the order open candle time.

  4.          int previousCandleIndex = iBarShift(Symbol(), Period(), previousCandleTime - 1);

    You know the open order candle index, the previous is one more. No need for a search.

  5. GODismyFather: the issue is that it uses the sell candlestick as stop loss for one order and then candlestick before the sell candlestick as stop loss for another order,
    That is what your code is looking for, the previous candle and its low.
 

thank you for your reply William Roeder, i don't care if i find an order from a different symbol because it is only one symbol and timeframe the ea will trade

3. yes i use it to get it candlestick so that i can work with it to get the candlestick before it and that is why i use this expression:

int previousCandleIndex = iBarShift(Symbol(), Period(), previousCandleTime - 1);

but as i said above, it result to the issue i said, for your number 4. how do i do it, please help

 
GODismyFather #: for your number 4. how do i do it, please help

When in doubt, think!

William Roeder #:
  1. You know the open order candle index, the previous is one more. No need for a search.

         int sellCandleIndex = iBarShift(Symbol(), Period(), sellTradeTime);
         // Calculate the index of the previous candlestick
         int previousCandleIndex = sellCandleIndex + 1;
 
William Roeder  i tried your code still the same issue
 
GODismyFather #: William Roeder  i tried your code still the same thing

Of course, it's still the same thing. I provided no answer for your problem, because it is doing exactly what you said you wanted. #1.5

 
ok please how do i get it to achieve as i explained please help. i have tried different approaches still the same issues i complained about
Reason: