Short code works, long code not
Forums › ProRealTime English forum › General trading discussions › Short code works, long code not
- This topic has 5 replies, 3 voices, and was last updated 10 months ago by upupandaway.
-
-
01/18/2024 at 1:56 PM #226507
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
ENDIFYet 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
ENDIFWhat could be wrong? I’m using IG as booker, if that has any impact.
Best regards
01/18/2024 at 2:15 PM #226509I 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
1Once AveragingCount = 01 user thanked author for this post.
01/18/2024 at 3:49 PM #226520Thank 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,
01/18/2024 at 4:03 PM #226521Adding ‘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?
01/18/2024 at 4:36 PM #226529Because
1positionprice <= (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:
12345678910111213141516171819202122DEFPARAM CumulateOrders = TrueEntertrade = 1Cantrade = 1// Trading condition with trade checkIF not shortonmarket and EnterTrade AND CanTrade THENSELLSHORT 1 CONTRACT AT MARKETAveragingCount = 0ENDIF// Averaging Down LogicIF shortonmarket AND AveragingCount = 0 AND close <= (PositionPrice * 0.99867) THENSELLSHORT 1 CONTRACT AT MARKETAveragingCount = 1ENDIF// Take Profit Logic for Accumulated PositionIF OnMarket AND (close >= (positionprice * 1.00088)) THENEXITSHORT AT MARKETENDIFgraphonprice PositionPrice coloured("Blue")graphonprice TradePrice coloured("Red")2 users thanked author for this post.
01/19/2024 at 12:02 PM #226578Thank 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.
1 user thanked author for this post.
-
AuthorPosts
Find exclusive trading pro-tools on