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
JSParticipant
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
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 😅
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
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.
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
Seems like the first signal is beeing ignored in any case:
JSParticipant
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
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 🥲
JSParticipant
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
Yes we had that code before, but then upcoming signals are being ignored:
JSParticipant
Senior
Strange, in my graph the first signal is executed…
Can you maybe try it with MBTCUSD and 1 day timeframe?
JSParticipant
Senior
On MBTCUSD TimeFrame 1 day…
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.