How to code UNTIL?

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #113082 quote
    sulimaster
    Participant
    Average

    Hi

    I’m trying to code UNTIL using the below example. I used IF, WHILE and BREAK but an infinite loop was detected and stopped the backtest. Help please to correct my code.

    If my long trade closes early due to a stop loss, then I don’t want the system to enter into another long trade while x is still greater than y UNTIL  y > x i.e. exit long position condition is met which should reset a back to zero.

    I have tried to code a = 1 whilst x is still greater than y when my long trade closes early due to a stop loss.

    x = AroonUp[10]
    y = AroonDown[10]
    
    a = 0
    
    IF PositionPerf(1) < 0 AND TRADEPRICE(1) < TRADEPRICE(2) THEN
    WHILE (x > y)
    a = 1
    IF (y > x) THEN
    BREAK
    ENDIF
    WEND
    ENDIF
    
    // Conditions to enter long positions
    //
    IF a = 0 AND (x > y) AND (x > 70) THEN
    BUY 1 PERPOINT AT MARKET
    ENDIF
    
    // Conditions to exit long positions
    //
    IF (y > x) THEN
    SELL AT MARKET
    ENDIF

    Thanks

    Sachin

    #113084 quote
    robertogozzi
    Moderator
    Master

    It’s an infinite loop, in between WHILE ….WEND initial conditions  never change.

    Indeed there’s no need for an iteration, another IF…ENDIF will do.

    Replace lines 7-12 with:

    If x > y then
       a = 1
    ENDIF
    Nicolas and sulimaster thanked this post
    #113144 quote
    sulimaster
    Participant
    Average

    Thanks Roberto

    However, wouldn’t the suggested code change prevent the next long trade even after y becomes greater than x if there is no other trade executed after the stop loss exit trade?

    #113147 quote
    robertogozzi
    Moderator
    Master

    You’ll have to assign a different value to a when it’s time to, not always.

    Replace line 4 with:

    ONCE a = 0

    Then you’ll need to add:

    a = 0

    somewhere in the code to reenable trading when you want to, according to your conditions.

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

How to code UNTIL?


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
sulimaster @sulimaster Participant
Summary

This topic contains 3 replies,
has 2 voices, and was last updated by robertogozzi
6 years, 3 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 11/19/2019
Status: Active
Attachments: No files
Logo Logo
Loading...