quantipoian

Here is the code:

 

 

import talib as ta
# Put any initialization logic here. The context object will be passed to
# the other methods in your algorithm.
def initialize(context):
context.stock = symbol('AAPL')

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

#Get the high points of the stock market
highs = history(100, '1d', 'high')
#Get the low points of the stock market
lows = history(100, '1d', 'low')
#Get the overall price
close = history(100, '1d', 'price')

#Calculate tyhe ADX
adx = ta.ADX(highs[context.stock], lows[context.stock], close[context.stock], timeperiod=14)
#Determines if the current low - prior low is less than the current high - prior high
mDI = ta.MINUS_DI(highs[context.stock], lows[context.stock], close[context.stock], timeperiod=14)
#Determines if the current high - prior high is less than the current low - prior low
pDI = ta.PLUS_DI(highs[context.stock], lows[context.stock], close[context.stock], timeperiod=14)
#Simple trading logic
if(adx[-1] > 20) and (pDI[-1] > mDI[-1]):
order(context.stock, 50)
elif(adx[-1] > 25) and (pDI[-1] < mDI[-1]):
order(context.stock, -50)
#Record the data
record(ADX=adx[-1], minusDI = mDI[-1], plusDI=pDI[-1])

EXPLAINED:
The key thing to see here is the toatle returns. That tells you overall how well you did. Then, you look at Volatity, Alpha, and Beta. The lower these are, the better you did.
 

Advertisements