One-time buy Signal at System Start

Viewing 15 posts - 1 through 15 (of 46 total)
  • Author
    Posts
  • #239684 quote
    stalba
    Participant
    New

    I’m setting up a simple trading system and need a way to trigger a one-time entry (either long or short) at the start if there’s no existing position. This is particularly important after rolling contracts and restarting the system, so it can re-enter immediately if required. I’ve tried using ONCE and various flags, but the system keeps triggering unintended signals. Looking for a simple, reliable code solution to execute an entry just once at the start without affecting ongoing trade signals. Any suggestions?

    // Set code parameters
    DEFPARAM CumulateOrders = False // Cumulating positions disabled
    
    ONCE checkLongPosition = NOT LONGONMARKET
    
    // Buy only once on system start
    IF checkLongPosition THEN
    BUY 1 SHARE AT MARKET
    ENDIF
    
    superTrendValue = SuperTrend[5.4,36]
    
    // Conditions for entering long positions
    c1 = (close CROSSES OVER superTrendValue)
    
    IF NOT LONGONMARKET AND c1 THEN
    BUY 1 SHARES AT MARKET
    ENDIF
    
    // Conditions for exiting long positions
    c2 = (close CROSSES UNDER superTrendValue)
    
    IF LONGONMARKET AND c2 THEN
    SELL AT MARKET
    ENDIF
    
    // Conditions for entering short positions
    c3 = (close CROSSES UNDER superTrendValue)
    
    IF NOT SHORTONMARKET AND c3 THEN
    SELLSHORT 1 SHARES AT MARKET
    ENDIF
    
    // Conditions for exiting short positions
    c4 = (close CROSSES OVER superTrendValue)
    
    IF SHORTONMARKET AND c4 THEN
    EXITSHORT AT MARKET
    ENDIF
    #239751 quote
    JS
    Participant
    Senior

    Not a bad idea from ChatGPT… 🙂

    DEFPARAM CumulateOrders = False  // Prevents multiple entries
    
    ONCE EntryFlag = 0  // Initialization for one-time entry
    
    // Check if there is no position at the start and EntryFlag is 0
    IF NOT OnMarket AND EntryFlag = 0 THEN
        // Define your entry condition here
        IF /* Your Entry Condition */ THEN
            BUY 1 CONTRACT AT MARKET  // Long Entry
            EntryFlag = 1  // Set the flag to avoid re-triggering
        ELSEIF /* Alternative Short Condition */ THEN
            SELLSHORT 1 CONTRACT AT MARKET  // Short Entry
            EntryFlag = 1  // Set the flag to avoid re-triggering
        ENDIF
    ENDIF
    
    stalba, robertogozzi and Iván González thanked this post
    #239753 quote
    stalba
    Participant
    New
    Thank you! ChatGPT couldn’t solve this, that’s why I am here haha Unfortunately I’m not getting any entries with this code. I’ve tried it both nested and un-nested, as well as with and without additional conditions, but nothing seems to happen:
    // Set code parameters
    DEFPARAM CumulateOrders = False // Prevents multiple entries
    
    // Initialize flag for one-time entry
    ONCE EntryFlag = 0
    
    // Check for initial entry at the start if there's no position and EntryFlag is 0
    IF NOT ONMARKET AND EntryFlag = 0 THEN
    BUY 1 SHARES AT MARKET // Long entry
    EntryFlag = 1  // Set the flag to prevent re-triggering the initial entry
    ENDIF
    Sorry… newby here 😅
    #239754 quote
    stalba
    Participant
    New
    This was another try, with the full code, but nothing happens:
    // Set code parameters
    DEFPARAM CumulateOrders = False // Prevents multiple entries
    
    ONCE EntryFlag = 0  // Initialization for one-time entry
    superTrendValue = SuperTrend[5.4,36]
    
    // Check if there is no LONG position at the start and EntryFlag is 0
    IF NOT LONGONMARKET AND EntryFlag = 0 THEN
    BUY 1 CONTRACT AT MARKET
    IF NOT LONGONMARKET AND close CROSSES OVER superTrendValue THEN
    BUY 1 CONTRACT AT MARKET // Long Entry
    EntryFlag = 1  // Set the flag to avoid re-triggering
    ELSIF NOT SHORTONMARKET AND close CROSSES UNDER superTrendValue THEN
    SELLSHORT 1 CONTRACT AT MARKET // Short Entry
    EntryFlag = 1  // Set the flag to avoid re-triggering
    ENDIF
    ENDIF
    #239755 quote
    robertogozzi
    Moderator
    Master
    Add this line at the very beginning:
    DEFPARAM PreLoadBars = 0
    it will open only ONE trade and will never close, as there is no indication when it must be exited.
    Iván González and stalba thanked this post
    #239765 quote
    stalba
    Participant
    New
    Thanks, the one-time entry works now! 🙏🏼 With the following code, the other conditions also seem to work, except the first 2 Supertrend crossings directly after the one-time entry are missing (see attachment) – any idea why? The rest of the signals work fine!
    // Set code parameters
    DEFPARAM PreLoadBars = 0
    DEFPARAM CumulateOrders = False // Prevents multiple entries
    
    ONCE EntryFlag = 0  // Initialization for one-time entry
    superTrendValue = SuperTrend[5.4,36]
    
    // Check if there is no position at the start and EntryFlag is 0
    IF NOT ONMARKET AND EntryFlag = 0 THEN
    SELLSHORT 1 CONTRACT AT MARKET
    ENDIF
    
    // Long Conditions
    IF NOT LONGONMARKET AND close CROSSES OVER superTrendValue THEN
    BUY 1 CONTRACT AT MARKET // Long Entry
    EntryFlag = 1  // Set the flag to avoid re-triggering
    ELSIF LONGONMARKET AND close CROSSES UNDER superTrendValue THEN
    SELL 1 CONTRACT AT MARKET // Long Exit
    ENDIF
    
    // Short Conditions
    IF NOT SHORTONMARKET AND close CROSSES UNDER superTrendValue THEN
    SELLSHORT 1 CONTRACT AT MARKET // Short Entry
    EntryFlag = 1  // Set the flag to avoid re-triggering
    ELSIF SHORTONMARKET AND close CROSSES OVER superTrendValue THEN
    EXITSHORT 1 CONTRACT AT MARKET // Short Exit
    ENDIF
    missing-entries.png missing-entries.png
    #239767 quote
    stalba
    Participant
    New
    Seems like the first signal is beeing ignored in any case:
    missing-entries-1.png missing-entries-1.png
    #239772 quote
    JS
    Participant
    Senior
    Hi, Try this one:  
    // Set code parameters
    DEFPARAM PreLoadBars = 0
    DEFPARAM CumulateOrders = False // Prevents multiple entries
    
    ONCE EntryFlag = 0  // Initialization for one-time entry
    superTrendValue = SuperTrend[5.4,36]
    
    // Check if there is no position at the start and EntryFlag is 0
    IF NOT ONMARKET AND EntryFlag = 0 THEN
    SELLSHORT 1 CONTRACT AT MARKET
    EntryFlag=1
    ENDIF
    
    // Long Conditions
    IF NOT LONGONMARKET AND EntryFlag=1 and close CROSSES OVER superTrendValue THEN
    BUY 1 CONTRACT AT MARKET // Long Entry
    ELSIF LONGONMARKET AND EntryFlag=1 and close CROSSES UNDER superTrendValue THEN
    SELL 1 CONTRACT AT MARKET // Long Exit
    ENDIF
    
    // Short Conditions
    IF NOT SHORTONMARKET AND EntryFlag=1 and close CROSSES UNDER superTrendValue THEN
    SELLSHORT 1 CONTRACT AT MARKET // Short Entry
    ELSIF SHORTONMARKET AND EntryFlag=1 and  close CROSSES OVER superTrendValue THEN
    EXITSHORT 1 CONTRACT AT MARKET // Short Exit
    ENDIF
    
    stalba thanked this post
    Scherm­afbeelding-2024-10-30-om-12.25.22.png Scherm­afbeelding-2024-10-30-om-12.25.22.png
    #239775 quote
    stalba
    Participant
    New
    When i use your code 1:1 the entry on the backtest starting date is missing – it enters a short position on the next Supertrend short signal (Screenshot attached) – the problem is that I want to do auto trading with Futures, and when I have an open position and need to roll over to a new contract, I have to close that position but might want to continue with the position / auto trading within the new contract (as the trend is still ongoing), and don’t want to wait until the next signal. Also, opening a position manually won’t work with auto trading systems: PRT Conditions of execution of automatic trading systems “When you begin an automatic trading system, any pre-existing positions placed manually are closed first and any pre-existing orders placed manually are canceled. The first position of the system may be opened at the earliest at the open of the next bar after the system is started.” How do you handle this, as this is sub-optimal for Futures auto trading? It’s kinda frustrating 🥲
    entry-missing.png entry-missing.png
    #239777 quote
    JS
    Participant
    Senior
    With short position on start/backtest date:    
    // Set code parameters
    DEFPARAM PreLoadBars = 0
    DEFPARAM CumulateOrders = False // Prevents multiple entries
    
    ONCE EntryFlag = 0  // Initialization for one-time entry
    superTrendValue = SuperTrend[5.4,36]
    
    // Check if there is no position at the start and EntryFlag is 0
    IF NOT ONMARKET AND EntryFlag = 0 THEN
    SELLSHORT 1 CONTRACT AT MARKET
    ENDIF
    
    // Long Conditions
    IF NOT LONGONMARKET and close CROSSES OVER superTrendValue THEN
    BUY 1 CONTRACT AT MARKET // Long Entry
    EntryFlag = 1  // Set the flag to avoid re-triggering
    ELSIF LONGONMARKET and close CROSSES UNDER superTrendValue THEN
    SELL AT MARKET // Long Exit
    EntryFlag = 1
    ENDIF
    
    // Short Conditions
    IF NOT SHORTONMARKET AND close CROSSES UNDER superTrendValue THEN
    SELLSHORT 1 CONTRACT AT MARKET // Short Entry
    EntryFlag = 1  // Set the flag to avoid re-triggering
    ELSIF SHORTONMARKET and  close CROSSES OVER superTrendValue THEN
    EXITSHORT AT MARKET // Short Exit
    EntryFlag = 1
    ENDIF
    Scherm­afbeelding-2024-10-30-om-13.01.52.png Scherm­afbeelding-2024-10-30-om-13.01.52.png
    #239779 quote
    stalba
    Participant
    New
    Yes we had that code before, but then upcoming signals are being ignored:
    signal-ignored-01.png signal-ignored-01.png signal-ignored-02.png signal-ignored-02.png
    #239782 quote
    JS
    Participant
    Senior
    Strange, in my graph the first signal is executed…
    Scherm­afbeelding-2024-10-30-om-13.26.45.png Scherm­afbeelding-2024-10-30-om-13.26.45.png
    #239785 quote
    stalba
    Participant
    New
    Can you maybe try it with MBTCUSD and 1 day timeframe?
    #239788 quote
    JS
    Participant
    Senior
    On MBTCUSD TimeFrame 1 day…
    stalba thanked this post
    Scherm­afbeelding-2024-10-30-om-13.45.28.png Scherm­afbeelding-2024-10-30-om-13.45.28.png
    #239793 quote
    stalba
    Participant
    New
    It depends on the selected units, when i do a backtest (e.g. 3 or 5 years), 1day timeframe, 10k units there is an inconsistency within the entries. With 100 units (as in your test) it seems to work.
Viewing 15 posts - 1 through 15 (of 46 total)
  • You must be logged in to reply to this topic.

One-time buy Signal at System Start


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
stalba @stalba Participant
Summary

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

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 10/29/2024
Status: Active
Attachments: 20 files
Logo Logo
Loading...