Code to exit position at next bar

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #236014 quote
    lgovender
    Participant
    New

    Hi

    Please see code below. Basically i am trying to indicate that the position must be closed at the next bar open if the stop and profit targets are not met.

    the code i have in there returns zero trades on the backtest.

    IF c2 or c3 then
    SELL AT MARKET
    ELSE
    SELL AT MARKET NextBarOpen
    ENDIF

    #236016 quote
    robertogozzi
    Moderator
    Master

    NextBarOpen is an obsolete keyword that is ignored, but still prsent to grant older code works, but it’s the default rule as ALL strategies are executed at the closing of each bar and ALL trades are opened and closed after that, that is when the next bar opens.

    Your code CAN’T start any trade because you have only used SELL, which is the instruction to close a Long trade.

    Here are the 4 keywords involved in opening and closing trades:

    • BUY opens a Long positions
    • SELL closes a Long position
    • SELLSHORT opens a Short position
    • EXITSHORT closes a Short position.
    lgovender and Iván González thanked this post
    #236051 quote
    lgovender
    Participant
    New

    there’s the full code

    on the backtest over 20 days, it is only taking 8 trades, i want it to take a trade every candle, which is 1 hour

    // Definition of code parameters
    DEFPARAM CumulateOrders = False // Cumulating positions deactivated

    // Prevents the system from placing new orders on specified days of the week
    daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0

    // Conditions to enter long positions
    c1 = (Close[1] < Close[2])

    IF c1 AND not daysForbiddenEntry THEN
    BUY 1 CONTRACT AT MARKET
    ENDIF

    //Conditions to exit long positions
    c2 = ((TRADEPRICE/Close[1])-1) > 0.006
    c3 = ((TRADEPRICE/Close[1])-1) > -0.004

    IF c2 or c3 THEN
    SELL AT MARKET

    ENDIF

    // Conditions to enter short positions
    c4 = (Close[1] > Close[2])

    IF c4 AND not daysForbiddenEntry THEN
    SELLSHORT 1 CONTRACT AT MARKET
    ENDIF

    // Conditions to exit short positions
    c5 = ((TRADEPRICE/Close[1])-1) > 0.006
    c6 = ((TRADEPRICE/Close[1])-1) > -0.004

    IF c5 or c6 THEN
    EXITSHORT AT MARKET

    ENDIF

    #236052 quote
    Iván González
    Moderator
    Master

    Hello,
    Looking over your code I see that you have set conditions for buying and short selling that I think you should review.

    c2 = ((TRADEPRICE/Close[1])-1) > 0.006
    c3 = ((TRADEPRICE/Close[1])-1) > -0.004

    As you can see, condition c2 or c3 is redundant. It would suffice to put c3 as c2 will always be true.

    c2 = ((TRADEPRICE/Close[1])-1) > 0.006
    c3 = ((TRADEPRICE/Close[1])-1) < -0.004
    

    The same is true for shorts.

    lgovender thanked this post
    #236072 quote
    lgovender
    Participant
    New

    thank you, made an error with the sign.

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

Code to exit position at next bar


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
lgovender @lgovender Participant
Summary

This topic contains 4 replies,
has 3 voices, and was last updated by lgovender
1 year, 6 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 08/01/2024
Status: Active
Attachments: No files
Logo Logo
Loading...