from zipline.api import order, symbol

# Put any initialization logic here.  The context object will be passed to
# the other methods in your algorithm.
def initialize(context):
    context.stocks = symbols('aapl')

# Will be called on every trade event for the securities you specify. 
def handle_data(context, data):
    # Implement your algorithm logic here.

    # data[sid(X)] holds the trade event data for that security.
    # context.portfolio holds the current portfolio state.

    # Place orders with the order(SID, amount) method.

    # TODO: implement your own logic here.
    order(symbol('aapl'), 50)
    print(context.portfolio.portfolio_value)


@@ -26,566 +26,848 @@
 rder
-, symbol%0A%0A# Put any initialization logic here.  The context object will be passed to%0A# the other methods in your algorithm.%0Adef initialize(context):%0A    context.stocks = symbols('aapl')%0A%0A# Will be called on every trade event for the securities you specify. %0Adef handle_data(context, data):%0A    # Implement your algorithm logic here.%0A%0A    # data%5Bsid(X)%5D holds the trade event data for that security.%0A    # context.portfolio holds the current portfolio state.%0A%0A    # Place orders with the order(SID, amount) method.%0A%0A    # TODO: implement your own logic he
+_target, record, symbol, history, add_history%0A%0A%0Adef initialize(context):%0A    # Register 2 histories that track daily prices,%0A    # one with a 100 window and one with a 300 day window%0A    add_history(100, '1d', 'price')%0A    add_history(300, '1d', 'price')%0A%0A    context.i = 0%0A%0A%0Adef handle_data(context, data):%0A    # Skip first 300 days to get full windows%0A    context.i += 1%0A    if context.i %3C 300:%0A        return%0A%0A    # Compute averages%0A    # history() has to be called with the same params%0A    # from above and returns a pandas dataframe.%0A    short_mavg = history(100, '1d', 'price').mean()%0A    long_mavg = history(300, '1d', 'price').mean()%0A%0A    sym = symbol('AAPL')%0A%0A    # Trading logic%0A    if short_mavg%5Bsym%5D %3E long_mavg%5Bsym%5D:%0A        # order_target orders as many shares as needed to%0A        # achieve the desired number of sha
 re
+s
 .%0A
+    
     
@@ -875,71 +875,242 @@
 rder
-(symbol('aapl')
+_target(sym, 100)%0A    elif short_mavg%5Bsym%5D %3C long_mavg%5Bsym%5D:%0A        order_target(sym
 , 
-5
 0)%0A
+%0A
     
-print(context.portfolio.portfolio_value)%0A%0A
+# Save values for later inspection%0A    record(AAPL=data%5Bsym%5D.price,%0A           short_mavg=short_mavg%5Bsym%5D,%0A           long_mavg=long_mavg%5Bsym%5D)

update buyapple.py