In this programming for Finance with Python, Zipline, and Quantopian, we cover finishing up the development of our basic simple moving average crossover strategy, and then we back test it. This illustrates the power of Quantopian well, since your only job is to create the logic for the strategy itself, and then the back-testing, and all sorts of advanced analysis are done automatically for you. Very cool. sample code: http://pythonprogramming.net http://hkinsley.com https://twitter.com/sentdex http://sentdex.com http://seaofbtc.com
Visibility: 24759
Duration: 9m 54s
Rating: 176
"""
def initialize(context):
context.security = symbol('SPY')
def handle_data(context,data):
print(data)
MA_data_1 = data.history(context.security, 'price', 50, '1d')
MA1 = MA_data_1.mean()
MA_data_2 = data.history(context.security, 'price', 200, '1d')
MA2 = MA_data_2.mean()
current_price = data.current(context.security, "price")
current_positions = context.portfolio.positions[symbol('SPY')].amount
cash = context.portfolio.cash
if (MA1 > MA2) and current_positions == 0:
number_of_shares = int(cash/current_price)
order(context.security, +number_of_shares)
log.info("Buying shares")
elif (MA2 < MA1) and current_positions != 0:
order_target(context.security, 0)
log.info("Selling shares")
record(MA1 = MA1, MA2 = MA2, Price = current_price)
"""