Back to Community
How do I trade due to opening?

Could someone please tell me how to trade by opening of the 8554. For example, if it were to close at 180.30, and open at 180.54 I would like to make a long right after opening. Or, vice-versa, If it were to close at 180.23, and open at 176.23 I would like to make a short at opening. Please code this for me as I am lost as to how to do this!
Thankyou

3 responses

Something like this?

Clone Algorithm
35
Loading...
Backtest from to with initial capital
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
import pytz 

def initialize(context):
    context.stock = sid(8554)
    context.date = None

def handle_data(context, data):
    exchange_time = get_datetime().astimezone(pytz.timezone('US/Eastern'))
    if exchange_time.date() != context.date: #is this a new day?
        context.date = exchange_time.date()
        closes = history(bar_count=2, frequency='1d', field='price')
        opens = history(bar_count=1, frequency='1d', field='open_price')
        s = context.stock
        log.info('{dt}: Shares on record in {s}= {amt}'.format(dt=exchange_time,
                                             s=s.symbol,
                                             amt=context.portfolio.positions[s].amount))
        if opens[s][0] > closes[s][0]:
            order_target_percent(s, 1)
        elif closes[s][0] > opens[s][0]:
            order_target_percent(s, -1)
There was a runtime error.

Close to that yes, but longing with ANY increase... not just by 1%. The thought being the opening of the market is a huge determining factor in the market movement for that day.

I really should write more readable code...

OK, here's the same code, more clearly written with comments to help explain:

import pytz 

def initialize(context):  
    context.stock = sid(8554)  
    context.date = None

def handle_data(context, data):  
    exchange_time = get_datetime().astimezone(pytz.timezone('US/Eastern'))  
    if exchange_time.date() != context.date: #is this a new day?  
        context.date = exchange_time.date()  
        # Get closing price for yesterday and closing of today's 1st minute (so 2 bars)  
        closes = history(bar_count=2, frequency='1d', field='price')  
        # Get opening price for today's 1st minute (i.e. today's open, only 1 bar requested)  
        opens = history(bar_count=1, frequency='1d', field='open_price')  
        s = context.stock  
        log.info('{dt}: Shares on record in {s}= {amt}'.format(dt=exchange_time,  
                                             s=s.symbol,  
                                             amt=context.portfolio.positions[s].amount))  
        yesterdays_close = closes[s][0]  
        todays_open = opens[s][0]  
        if todays_open > yesterdays_close:  
            order_target_percent(s, 1) # as in target 100% of your portfolio long in SPY (+1)  
        elif todays_open < yesterdays_close:  
            order_target_percent(s, -1) # as in target 100% of your portfolio short in SPY (-1)