Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Calling Historical Prices

Hi, I would like to do the following in the code. Can someone show me how please?

Rules:
Trading only once per day at 3:50pm

StockDelta( 3:50pm Today) = 1.0 * R(1) + 2.0 * R(2) + 3.0 * R(3) + 4.0 * R(4) + 5.0 * R(5)

R(1) = Price( 3:50pm Today) / Price ( T-1 Close) - 100%
R2) = Price( T-1 Close) / Price ( T-2 Close) - 100%
R(3) = Price( T-2 Close) / Price ( T-3 Close) - 100%
R(4) = Price( T-3 Close) / Price ( T-4 Close) - 100%
R(5) = Price( T-4 Close) / Price ( T-5 Close) - 100%

import numpy as np  
import math  
from pytz import timezone  

set_commission(commission.PerShare(cost=0.0))  
set_slippage(slippage.FixedSlippage(spread=0.0))

def initialize(context):  
    context.stock = sid(24)  
def handle_data(context, data):

    #set timezone to EST  
    exchange_time = get_datetime().astimezone(timezone('US/Eastern'))  
    #Rebalance at 3:50PM  
    if exchange_time.hour == 15 and exchange_time.minute == 50:  
        price_history = history(bar_count=6, frequency='1d', field='price')  
        return1 = price_history.ix[0] / price_history.ix[-1] - 1.0  
        return2 = price_history.ix[-1] / price_history.ix[-2] - 1.0  
        return3 = price_history.ix[-2] / price_history.ix[-3] - 1.0  
        return4 = price_history.ix[-4] / price_history.ix[-5] - 1.0  
        return5 = price_history.ix[-5] / price_history.ix[-6] - 1.0  
        StockDelta = 1.0 * return1 + 2.0 * return2 + 3.0 * return3 + 4.0 * return4 + 5.0 * return5  
        StockDelta_limit = max( -1.0, min( 1.0, StockDelta) )  
        record( tgt_pct = 100 * StockDelta_limit)  
        order_target_percent( context.stock, StockDelta )  

1 response

Hi Brian,

Take a look at this attached code, it follows your criteria. One note is that I updated your indexes. For example in order to do:
R(1) = Price( 3:50pm Today) / Price ( T-1 Close)

You need this code, which takes the ratio of today's price history divided by yesterday's price history.

return1 = price_history.ix[-1] / price_history.ix[-2]  

In your example you used:

return1 = price_history.ix[0] / price_history.ix[-1] ` Which will actually take the ratio of the price 5 days ago (the first element in the array) divided by today's price (the last element in the array).

Cheers,
Alisa

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.