Hi. I would have liked to have a code that would prevent it from selling between 9:45 PM and 9:15 AM.
Hi,
The trick is that your window crosses midnight (21:45 → 09:15 next day), so you can’t use AND — you need OR. If you write time >= 214500 AND time <= 091500 it will always return false.
Solution using the native opentime variable (returns the bar’s opening time as an integer in HHMMSS format):
// Blocked window: from 21:45 to 09:15 next day
noSellZone = opentime >= 214500 OR opentime < 091500
canSell = NOT noSellZone
Then guard your sell orders with canSell. Two typical cases:
To prevent the system from opening shorts:
IF yourShortEntryCondition AND canSell THEN
SELLSHORT 1 CONTRACT AT MARKET
ENDIF
To prevent the system from closing longs during that window:
IF yourExitCondition AND canSell THEN
SELL AT MARKET
ENDIF
Thanks for the code. But I can’t get it to work so that it won’t sell my position that I have between 9:45 PM and 9:15 AM. It sells as usual. When I put the code in my buy script to not place a new order between 9:45 PM and 9:15 AM, it works.
// Blocked window: from 21:45 to 09:15 next day
noSellZone = opentime >= 214500 OR opentime < 091500
canSell = NOT noSellZone
LongConditions = canSell and defL1 and c156712 and datr215l and datarl60 and datarl30lb
IF LongConditions and TradeON ThEN
BUY positionsize CONTRACT AT MARKET
ENDIF
iF datarl60s and defL1 and c156712 and datr215s and noSellZone THEN
SELL AT MARKET
ENDIF
I got it working now. I mixed up your codes. Thank you very much