Back to Community
How To: Store Current Day's Open Price

Quick question I'm hoping someone can help me with: How would I go about storing the current day's open price if using one-minute bars? I only need to retain this price for the day, and then each day, this variable's value would be updated to be the current day's open price. I'm not an avid programmer so if someone could walk me through this I would very much appreciate it! Thanks!

5 responses

Hello William,

I don't have time to work up an example now, but I suggest reading the help docs on the 'history' API. There's a native way to do it. Specifically, note:

Returned Data
The returned data for daily history, for each day, is:

open_price: the open of the first minute bar of the given day.
high: the maximum of the minute highs for the given day.
low: the minimum of the minute bar lows for the given day.
close_price: the close of the last minute bar in the specified period. This value is not forward-filled. If there is no price in the bar, the returned price is NaN.
price: the close of the last minute bar as of the specified period. This value is forward-filled. If there is no price in the bar, the returned price is the previous price, until there is new trade data available.
volume: the sum of all minute volumes. For the current day, the sum of volume thus far.

Grant

Thanks for the reply Grant; unfortunately I'm still lost. If anyone could dive in a bit deeper it would be greatly appreciated.

Thanks!

William,

Here's something you can play around with. It illustrates that you always get the open price for the day using:

O = history(1,'1d','open_price')  

There's a bit of a learning curve to using the 'history' API, but it is well worth the effort.

Grant

Clone Algorithm
33
Loading...
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
def initialize(context):
    
    context.spy = sid(8554) # SPY  
    schedule_function(func=bar_data,date_rule=date_rules.every_day(),time_rule=time_rules.market_open())
    schedule_function(func=bar_data,date_rule=date_rules.every_day(),time_rule=time_rules.market_open(minutes=60))

def handle_data(context, data):
    
    pass

def bar_data(context,data):
    
    O = history(1,'1d','open_price')
    H = history(1,'1d','high')
    L = history(1,'1d','low')
    C = history(1,'1d','close_price')
    V = history(1,'1d','volume')
    
    print get_datetime()
    print 'O = ' + str(float(O.values))
    print 'H = ' + str(float(H.values))
    print 'L = ' + str(float(L.values))
    print 'C = ' + str(float(C.values))
    print 'V = ' + str(float(V.values))
There was a runtime error.

in daily mode you dont need that scheduling right, you only work with close prices but can get the high & low prices

Hi William,

Schedule_function is powerful. In your initialize function, write something like the following:

schedule_function(handle_data_daily, date_rules.every_day(), time_rules.market_open(minutes=1))  

You would then create a function called "handle_data_daily" with whatever code you want to run at 9:31:00 AM EST every day (because the schedule_function above is telling your program to run "handle_data_daily" at market_open + 1). You MUST keep "handle_data" separate as your default function for minutely action (you cannot use schedule_function on handle_data, as far as I know)

Here's an example to get you started:

def initialize(context):  
    schedule_function(handle_data_daily, date_rules.every_day(), time_rules.market_open(minutes=1))

def handle_data_daily(context, data):  
    global my_open  
    open_price = history(bar_count=10, frequency='1d', field='open_price')  
    for stock in data:  
        my_open = open_price[stock][-1] #[-1] pulls latest open value

def handle_data(context, data):  
    global my_open  
    #do something with "my_open"  

Global declarations aren't exactly Pythonic but they allow you to use the same variable in multiple functions. I've yet to find a better way to do this but there might be.

Hope that helps!