I need some advice. I have been searching the web for a long time without any lock.
Short story: I have been waking up at 00:00 UTC every single day for the past 5 years to execute my trades and a month ago I came up with an idea I have a problem writing code on. So I need your help.
My goal is to use the 4-hour chart and what I want to achieve is to backtest on ONLY the close of the first 4-hour candle, this way I can backtest and create a trading strategy where I can execute in the morning instead of waking up middle of the night.
So let's play with 4 hours in this example and UTC
So 4-hour candle opens at 00:00 UTC, and the first candle closes at 04:00 UTC
I would like my strategy to calculate the backtesting on BUY/CLOSE TRADE based on the closing price of candle 1 that close at 04:00 UTC or the OPEN price of the second candle that opens at 04:00 UTC ( if you get my thinking ? )
Here is my code:
// TIMESTAMP input - to choose strategy testing timeframe
i_startTime = input(defval = timestamp("20 Mar 2020 00:00 +0000"), title = "Start Time", type = input.time)
i_endTime = input(defval = timestamp("01 Jan 2023 00:00 +0000"), title = "End Time", type = input.time)
i_length = input(defval = 20, title = "Length", type = input.integer)
inDateRange = time >= i_startTime and time <= i_endTime
inCondition = not na(close[i_length])
hh = highest(high, i_length)
ll = lowest(low, i_length)
// compute the indicators
smaInput = input(title="SMA", type=input.integer, defval=1)
indicator1 = sma(close,smaInput)
// plot the indicators
plot(indicator1, title="Indicator1", color=color.red, linewidth=2)
// Trading Logic
EnterLong = crossover(close,indicator1)
ExitLong = crossunder(close,indicator1)
// Execution Logic - Placing Orders
if (inCondition and inDateRange)
strategy.entry("LONG", strategy.long, when=EnterLong)
strategy.close("LONG", when=ExitLong)
// Background color backtesting period
bgcolor(inDateRange ? color.green : na, 90)```