Back to Community
New to Quantopian, Trying to add MACD signal to Moving average strategy

Hello everyone,

I am a college student that has done your traditional trading as a finance student. However, I recently have been trying to learn how to do trading systems.

I have created a simple moving average strategy, and seems to be working okay and working well. It's only works with one stock, however I will try to make it work with multiple in a later date. I plan on adding MACD to the strategy but note sure how to implement it correctly. Any help would be great.

Basically, I am trying to trigger a buy when moving average has been crossed and at the same time the MACD is over zero. And vise versa I would like to sell when it has crossed the moving average and MACD has gone below zero.

Once again, any help would be great!
Lastly, I realized that their is a few time metrics that are wrong with my code, I have already fixed them !

Clone Algorithm
7
Loading...
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
def initialize(context):  
    schedule_function(open_positions, date_rules.every_day(),  time_rules.market_open(minutes = 15))
    schedule_function(close_positions, date_rules.every_day(), time_rules.market_close(minutes = 11))
    schedule_function(record_variables, date_rules.every_day(), time_rules.market_close())
    
    context.amd = sid(351)  
    
    
def open_positions(context, data):
    
    MovingAvg1 = data.history(context.amd, 'price', 9,'1d').mean()  
    MovingAvg2 = data.history(context.amd, 'price', 40,'1d').mean()
      
    current_positions = context.portfolio.positions[context.amd].amount  
    if (MovingAvg1 > MovingAvg2) and current_positions == 0:  
    	order_target_percent(context.amd, .50)

def close_positions(context, data):
    
    MovingAvg1 = data.history(context.amd, 'price', 9,'1m').mean()  
    MovingAvg2 = data.history(context.amd, 'price', 40,'1m').mean()
     
    current_positions = context.portfolio.positions[context.amd].amount      
    if (MovingAvg1 < MovingAvg2) and current_positions != 0:  
	    order_target_percent(context.amd, 0)  
        
def record_variables(context, data):           
	record(Value=context.portfolio.portfolio_value)
There was a runtime error.
2 responses

@Juan

Here it is.Enjoy.

Clone Algorithm
11
Loading...
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# A Moving average strategy with MACD
import talib
# -------------------------------------------------------------------------
STOCK = symbol('AMD'); MA_F = 9;  MA_S = 40; Fast = 12; Slow = 26; Sig = 9;
# -------------------------------------------------------------------------
bars_macd = Fast + Slow + Sig;

def initialize(context):  
    schedule_function(open_positions, date_rules.every_day(),  time_rules.market_open(minutes = 15))
    schedule_function(close_positions, date_rules.every_day(), time_rules.market_close(minutes = 11))
    schedule_function(record_variables, date_rules.every_day(), time_rules.market_close())
    
def open_positions(context, data):
    
    mavg_f = data.history(STOCK, 'price', MA_F,'1d').mean()  
    mavg_s = data.history(STOCK, 'price', MA_S,'1d').mean()        
    macd, _, _ = talib.MACD(data.history(STOCK, 'price', bars_macd, '1d'), Fast, Slow, Sig)    
    stock_positions = context.portfolio.positions[STOCK].amount     
    if (mavg_f > mavg_s and macd[-1] > 0 and stock_positions <= 0):  
        order_target_percent(STOCK, 0.50);

def close_positions(context, data):
    
    mavg_f = data.history(STOCK, 'price', MA_F,'1d').mean()  
    mavg_s = data.history(STOCK, 'price', MA_S,'1d').mean() 
    macd, _, _ = talib.MACD(data.history(STOCK, 'price', bars_macd, '1d'), Fast, Slow, Sig)  
    stock_positions = context.portfolio.positions[STOCK].amount
    
    if (mavg_f < mavg_s and macd[-1] < 0  and stock_positions != 0):  
        order_target_percent(STOCK, 0.0);  
        
def record_variables(context, data):           
    record(Value = context.portfolio.portfolio_value)
There was a runtime error.

The same cuisine, the same tools with one more ingredient added.

Clone Algorithm
11
Loading...
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# A Moving average strategy with MACD
import talib
# -----------------------------------------------------------------------------------------------
STOCK = symbol('AMD'); BOND = symbol('TLT'); MA_F = 9;  MA_S = 40; Fast = 12; Slow = 26; Sig = 9;
# -----------------------------------------------------------------------------------------------
bars_macd = Fast + Slow + Sig;

def initialize(context):  
    schedule_function(open_positions, date_rules.every_day(),  time_rules.market_open(minutes = 15))
    schedule_function(close_positions, date_rules.every_day(), time_rules.market_close(minutes = 11))
    schedule_function(record_variables, date_rules.every_day(), time_rules.market_close())
    
def open_positions(context, data):
    
    mavg_f = data.history(STOCK, 'price', MA_F,'1d').mean()  
    mavg_s = data.history(STOCK, 'price', MA_S,'1d').mean()        
    macd, _, _ = talib.MACD(data.history(STOCK, 'price', bars_macd, '1d'), Fast, Slow, Sig)    
    stock_positions = context.portfolio.positions[STOCK].amount     
    if (mavg_f > mavg_s and macd[-1] > 0 and stock_positions <= 0):  
        order_target_percent(STOCK, 0.50);
        order_target_percent(BOND, 0.50);

def close_positions(context, data):
    
    mavg_f = data.history(STOCK, 'price', MA_F,'1d').mean()  
    mavg_s = data.history(STOCK, 'price', MA_S,'1d').mean() 
    macd, _, _ = talib.MACD(data.history(STOCK, 'price', bars_macd, '1d'), Fast, Slow, Sig)  
    stock_positions = context.portfolio.positions[STOCK].amount
    
    if (mavg_f < mavg_s and macd[-1] < 0  and stock_positions != 0):  
        order_target_percent(STOCK, 0.0);  
        order_target_percent(BOND, 1.0);
        
def record_variables(context, data):           
    record(Value = context.portfolio.portfolio_value)
There was a runtime error.