Hello everyone,
I've been reading and playing around with Quantopian tools for a few weeks and now I'm trying to implement an algorith to learn the ropes.
Basically I am taking a simple MACD strategy and now I want to add a volume signal. I can use the day's volume with data[stock].volume but I would now like to try using a MA.
I know that mavg gives the price's moving average so I figured I would use talib's MA function. My problem is that I haven't been able to find the documentation that explains what parameters I need to pass. For example, to select SMA I can pass matype=0 apparently, then I pass timeperiod=5 to get the MA of the last 5 days, but it's not working for some reason.
After this I would like to try KAMA but again, I have no idea what the funciton signature is.
Any help is greatly appreciated.
import talib
import numpy as np
import pandas as pd
# Put any initialization logic here. The context object will be passed to
# the other methods in your algorithm.
def initialize(context):
set_symbol_lookup_date('2006-05-01')
context.stocks = symbols('JNJ', 'KO', 'MMM', 'DVY', 'VIG')
context.pct_per_stock = 1.0 / len(context.stocks)
# Create a variable to track the date change
context.date = None
# Will be called on every trade event for the securities you specify.
def handle_data(context, data):
todays_date = get_datetime().date()
# Do nothing unless the date has changed and its a new day.
if todays_date == context.date:
return
# Set the new date
context.date = todays_date
# Load historical data for the stocks
prices = history(40, '1d', 'price')
volumes = history(40, '1d', 'volume')
# Create the MACD signal and pass in the three parameters: fast period, slow period, and the signal.
# This is a series that is indexed by sids.
macd = prices.apply(MACD, fastperiod=12, slowperiod=26, signalperiod=9)
#NOT WORKING
volume_ma = volumes.apply(talib.MA, matype=0, timeperiod=5)
# Iterate over the list of stocks
for stock in context.stocks:
current_position = context.portfolio.positions[stock].amount
#log.info("VOLUME IS " + str(data[stock].volume)))
# Close position for the stock when the MACD signal is negative and we own shares.
if macd[stock] < 0 and current_position > 0 :
order_target(stock, 0)
# Enter the position for the stock when the MACD signal is positive and
# our portfolio shares are 0.
elif macd[stock] > 0 and current_position == 0:
order_target_percent(stock, context.pct_per_stock)
record(jnj=macd[symbol('JNJ')],
ko=macd[symbol('KO')],
dvy=macd[symbol('DVY')],
vig=macd[symbol('VIG')],
mmm=macd[symbol('MMM')])
# Define the MACD function
def MACD(prices, fastperiod=12, slowperiod=26, signalperiod=9):
'''
Function to return the difference between the most recent
MACD value and MACD signal. Positive values are long
position entry signals
optional args:
fastperiod = 12
slowperiod = 26
signalperiod = 9
Returns: macd - signal
'''
macd, signal, hist = talib.MACD(prices,
fastperiod=fastperiod,
slowperiod=slowperiod,
signalperiod=signalperiod)
return macd[-1] - signal[-1]