0
votes

I am trying to create an overnight gap trading backtester. The idea is to have the algo enter long at 3:59 pm before market close and exit long at 9:00 am after market open when the long condition is met. In this case, the long condition is simply an overnight gap of greater than 10% over the past two previous opens. I figured the easiest way to do this would be to define the session hours and create a variable for marketOpen and marketClose at the desired entry and exit times. The lines of code doing this are not giving me errors so I believe they are correct. However, when I use these variables in the following lines of code, which serve to calculate daily gap percentages, I get an argument error saying I cannot call 'operator -' with arguments. I have tried everything I could think of, from adding input functions to the gapSize 1 & 2 lines, adding quotation marks, parentheses, etc. I am confident if I can get the algo to accept these variables the strategy will work. Any help and/or advice is greatly appreciated.

'''
//@version=4
strategy(title="Gap Strategy", overlay=true)

t = time(resolution=timeframe.period, session="0930-1600")
marketOpen = input(defval="0930", title="marketOpen", type=input.time)
marketClose = input(defval="1559", title="marketClose", type=input.time)

gapSize1 = ((marketOpen[2] - marketClose[3]) / marketClose[3]) * 100
gapSize2 = ((marketOpen[1] - marketClose[2]) / marketClose[2]) * 100
longCondition = (gapSize1 > 10) and (gapSize2 > 10)

if (longCondition)
    strategy.order(id="buy", long=true, when=marketClose[1])
    strategy.order(id="sell", long=false, when=marketOpen)

'''
1
If you provide a full scenario for the study, I will try to help you. - AnyDozer

1 Answers

0
votes

There are three errors in your script. First, you are trying to apply the history operator [] to input variables marketOpen and marketClose. The history operator [] is only applicable for series types. Second, you have defined the variables marketOpen and marketClose of type input.time, and you are trying to calculate the price gap from their values. Third, the condition longCondition for entering a long position will be checked earlier than the time you specified 3:59pm.