Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Calculating Gaps and HOD/LOD

Hello Quantopian Community,

I'm in the process of coding up my first strategy on the platform. I'm a seasoned Tradestation coder and just went through a Python course so I can learn to code here. Please excuse my newb Python code.

  1. I went through some examples and I'm trying to figure out how to compute a moving average of the gaps over the last 20 days. What I came up with is:

Gap = history(1, '1d', 'price')[stock] - history(0, '1d', 'open')[stock]

However the 'open' isn't supported and I'm not sure how I would use that to compute a moving average.

  1. I'm trying to compute the HOD/LOD in minute mode. To do this, I have two functions:

def HODLOD_Init (context, data):
HOD = data[context.stock].high
LOD = data[context.stock].low

def HODLOD (context, data):
if data[context.stock].high > HOD:
HOD = data[context.stock].high
elif data[context.stock].low > LOD:
LOD = data[context.stock].low

The scheduler runs the HODLOD_Init at 1 minute into the session. Then in:

def handle_data(context, data):
HODLOD()

I call the function every bar in the handle_data. Is this the correct way to do this?

2 responses

I can't be the first one with the need to compute gaps...

However the 'open' isn't supported

Open isn't supported but 'open_price' is, just use it.

You can get daily high with the same method (at least that's what I think, someone from quantopian could verify this).. so it's just history(1,'1d','low') gives you daily low (and 'high' gives you high)