Short code works, long code not

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #226507 quote
    upupandaway
    Participant
    New

    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

    #226509 quote
    GraHal
    Participant
    Master

    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
    upupandaway thanked this post
    #226520 quote
    upupandaway
    Participant
    New

    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,

    #226521 quote
    upupandaway
    Participant
    New

    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?

    #226529 quote
    robertogozzi
    Moderator
    Master

    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")
    GraHal and upupandaway thanked this post
    #226578 quote
    upupandaway
    Participant
    New

    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.

    robertogozzi thanked this post
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.

Short code works, long code not


General Trading: Market Analysis & Manual Trading

New Reply
Author
Summary

This topic contains 5 replies,
has 3 voices, and was last updated by upupandaway
2 years ago.

Topic Details
Forum: General Trading: Market Analysis & Manual Trading
Language: English
Started: 01/18/2024
Status: Active
Attachments: No files
Logo Logo
Loading...