Add an option to close positions at specific time of day
Lindsey Starkweather
Not sure why this can't be done via TradingView... I send webhook signals at specific times of day (ex. send batch of orders at 9:30, send next batch at 9:32 to avoid rate limits); I start sending end of day orders 7 seconds before market closes or exactly at market close if an alert was not already sent.
<sorry for formatting below - this editor is not friendly for code snippets>
In PineScript...
Strategy Settings:
* calc_on_every_tick=true, process_orders_on_close=true
Time Constants:
* const int oneSecond = 1000
* const int tenSeconds = oneSecond x 10
Exit Logic:
* bool exit10SecondsToClose = timenow >= time_close - tenSeconds
* bool exitOnCandleClose = barstate.isconfirmed //used to send alert at close if there is no tick in last 10 seconds
Alert:
* varip bool alertSent = false
* if barstate.isnew
* alertSent := false
* If not alertSent and (sendOnCandleClose or send10SecondsToClose)
* alert("{JSON webhook garbledy-gook}", alert.freq_once_per_bar)
* alertSent := true
JSON:
Sounds like you may need to include these attributes in your webhook:
* action = exit (closes entire position without having to specify a quantity)
* cancel = true (cancels all standing orders before processing your exit order)
If you are trying to execute orders right at market close, include these attributes in your webhook to help ensure your order is filled.
* timeInForce = GTC
* extendedHours = true