Trailing at the iLowest iOpen of the previous 45 bars

 

I am trying to trail with a Stop loss at the lowest open of the last 45 1 minute candles.

This is what my code looks like:

if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) {
        printf("TSL: "+New_TSL);
        printf("45 bars TSL: "+iOpen(par_temp,PERIOD_M1,iLowest(Symbol(),0,MODE_OPEN,45,0)));
        New_TSL=MathMax(New_TSL,iOpen(par_temp,PERIOD_M1,iLowest(Symbol(),0,MODE_OPEN,45,0)));
}

My 2 printfs return the following:

2021.12.11 19:44:35.427 2021.06.01 02:23:45   TSL: 0.77388

2021.12.11 19:44:35.974 2021.06.01 02:23:45   45 bars TSL: 0.77437

2021.12.11 19:44:35.974 2021.06.01 02:23:45   position modified [#2 buy 0.3 AUDUSD 0.77439 sl: 0.77437 tp: 0.77623]

The iOpen does not return the correct value, as shown on my printf "45 bars TSL"

It then modifies the Stoploss to highest of the two values, which is what I intended IF the calculation had gone correctly.


I believe I made a mistake on this potion of code but I can't see the error. Can someone point me in the right direction?

iOpen(par_temp,PERIOD_M1,iLowest(Symbol(),0,MODE_OPEN,45,0))



Basic Principles - Trading Operations - MetaTrader 5 Help
Basic Principles - Trading Operations - MetaTrader 5 Help
  • www.metatrader5.com
Before you proceed to study the trade functions of the platform, you must have a clear understanding of the basic terms: order, deal and position...
 
… iOpen(par_temp, PERIOD_M1,iLowest(Symbol(),0,MODE_OPEN,45,0))…

You are mixing apples and oranges. Code fails on any non-M1 chart.

Code fails on any symbol except the current chart.

 
William Roeder #:

You are mixing apples and oranges. Code fails on any non-M1 chart.

Code fails on any symbol except the current chart.

Will check those two posts. Thanks for the pointer

EDIT: Yeap, I spotted the symbol issue - replaced Symbol() with the referenced variable par_temp
 
2021.12.11 21:21:32.876 2021.06.01 02:23:45   TSL: 0.77388
2021.12.11 21:21:33.386 2021.06.01 02:23:45   45 bars TSL: 0.77406
2021.12.11 21:21:33.386 2021.06.01 02:23:45   position modified [#2 buy 0.3 AUDUSD 0.77439 sl: 0.77406 tp: 0.77623]


The links you gave were quite helpful. Managed to fix it.

Thanks @William Roeder