Back to Community
Need help creating a "Buy" signal when divergence is a certain value (MACD)

I need help creating an algorithm that signals a "buy" when the divergence is some number (say -.2) and a "sell" or perhaps a "short" when the divergence is equal to +.2.
Fast period = 5 days
Slow period = 10-20 days
Signal line = 9-15 days
The algorithm attached is what I was working on before regarding AMD, it is just a simple MACD that buys when the MACD and signal line converge, this does not have the divergence magnitude in it (I do not know how to do this).
Any help would be great!

Clone Algorithm
4
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
import talib
import numpy as np
import pandas as pd

#------------------------------------------------------
# The initialize function sets any data or variables 
# that you'll use in your algorithm. It's only called 
# once at the beginning of your algorithm.
#------------------------------------------------------  

def initialize(context):
    
    # Trading Guards
    set_long_only()
    
    # Assets traded
    context.AMD = sid(5061)

    
#------------------------------------------------------
# At the beginning of each day reset 'traded' status
#------------------------------------------------------ 

def before_trading_start(context):
    context.traded_AMD_today = False

#------------------------------------------------------
# Functions within this section will run every minute
#------------------------------------------------------

def handle_data(context, data):
    macd_AMD(context, data)

    

#------------------------------------------------------
# Stock specific functions
#------------------------------------------------------
def macd_AMD(context, data):

    # Define current QTY, PRICE, and TRADED TODAY
    qty_AMD = context.portfolio.positions[context.AMD].amount
    price_AMD = data.current(context.AMD,'price')
    traded_AMD = context.traded_AMD_today
    
    # Obtain price history in MINUTE interval
    #     Trim the 1-MINUTE interval data for 30-MINUTE intervals (Keep every 30 data points)
    #     Final data emulates Google Finance 1-Month window, 30-minute interval view
    price_history = data.history(
        context.AMD,
        fields='price',
        bar_count=3000,
        frequency='1m')
    price_history = price_history[::30]
    
    # Use TA-Lib to calculate MACD data using calibrated settings
    macd_raw, signal, macd_hist = talib.MACD(price_history, 
                                             fastperiod=30, 
                                             slowperiod=40, 
                                             signalperiod=45)
    
    # Trading logic based on MACD signal
    #     BUY stock if MACD is positive
    #     SELL stock if MACD is negative
    if macd_hist[-1] > 0 and data.can_trade(context.AMD) and qty_AMD == 0 and traded_AMD == 0:
        order_target_percent(context.AMD, 1)
        context.traded_AMD_today = True
    elif macd_hist[-1] < 0 and data.can_trade(context.AMD) and qty_AMD > 0 and traded_AMD == 0:
        order_target_percent(context.AMD, 0)
        context.traded_AMD_today = True
   
    # Record interesting variables
    record(macd_raw=macd_raw[-1],signal=signal[-1])
There was a runtime error.