Hi! I was wondering if anyone could help me whith this code.
I have tested the code below on a timeperiod where
‘Entertrade’ and ‘Cantrade = 1’ are met at the beginning of the timeperiod,
resulting in one trade (and I have set DEFPARAM CumulateOrders = True).
// Trading condition with trade check
IF not shortonmarket and EnterTrade AND CanTrade = 1 THEN
SELLSHORT 1 CONTRACT AT MARKET
AveragingCount = 0
ENDIF
Yet when I try this (starting with the same code, and adding my averaging down logic)
no trades happen during the same period.
// Trading condition with trade check
IF not shortonmarket and EnterTrade AND CanTrade = 1 THEN
SELLSHORT 1 CONTRACT AT MARKET
AveragingCount = 0
ENDIF
// Averaging Down Logic
IF shortonmarket AND AveragingCount = 0 AND positionprice <= (tradeprice * 0.99867) THEN
SELLSHORT 1 CONTRACT AT MARKET
AveragingCount = 1
ENDIF
// Take Profit Logic for Accumulated Position
IF positionprice >= (tradeprice * 1.00088) THEN
EXITSHORT AT MARKET
ENDIF
What could be wrong? I’m using IG as booker, if that has any impact.
Best regards
I guess above is not your full code as Topic title mentions Longs and posted code does not mention Longs.
Anyway try putting below after Defparam statements at top of your code
Once AveragingCount = 0
Thank you for your quick response. I realize now that my title is confusing. I meant that the beginning of the code works, while the full code does not.
I will try adding ‘once’ to the code.
Best regards,
Adding ‘once’ made no difference. And also, I don’t see why I should add it. If the strategy enters a trade, notes that zero averaging downs has happened, and then averages down when the conditions for averaging down are met, and then exits the trade when the conditions for exiting the trade are met, it should be able to start over again when the strategy’s conditions for entering a trade are met anew, and start counting instances of averaging down again. Or am I missing something?
Because
positionprice <= (tradeprice * 0.99867)
wll NEVER be true, because positionprice and tradeprice are EQUAL.
They will only be different AFTER at least an additional position opens, which will never happen, though.
This snippet will work:
DEFPARAM CumulateOrders = True
Entertrade = 1
Cantrade = 1
// Trading condition with trade check
IF not shortonmarket and EnterTrade AND CanTrade THEN
SELLSHORT 1 CONTRACT AT MARKET
AveragingCount = 0
ENDIF
// Averaging Down Logic
IF shortonmarket AND AveragingCount = 0 AND close <= (PositionPrice * 0.99867) THEN
SELLSHORT 1 CONTRACT AT MARKET
AveragingCount = 1
ENDIF
// Take Profit Logic for Accumulated Position
IF OnMarket AND (close >= (positionprice * 1.00088)) THEN
EXITSHORT AT MARKET
ENDIF
graphonprice PositionPrice coloured("Blue")
graphonprice TradePrice coloured("Red")
Thank you for your sollution. Yes, that works. However, I want the strategy to act as soon as the level is triggered, not wait until the bar is closed.