Back to Community
Looking for a Golden Cross/Death Cross Algo

Anyone have a golden cross/death cross algo they'd be willing to share?

2 responses

Harrison,

Depends on what you mean by 'Golden Cross'. I'm taking it to mean 150 and 50 moving averages -- you can change it to whatever you wish.

Happy hacking

Clone Algorithm
60
Loading...
Backtest from to with initial capital
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):
    context.ETFs = [sid(26807), # gld
                    sid(19920), # qqq
                    sid(23921)] # tlt
    context.QUICK = 50
    context.SLOW = 150
    context.fixed_frac = 1.0/len(context.ETFs)

def handle_data(context, data):
    closes = history(200, '1d', 'price')
    
    for etf in context.ETFs:
        quick = closes[etf].iloc[-context.QUICK:].mean()
        slow = closes[etf].iloc[-context.SLOW:].mean()
        if quick > slow:
            if not get_open_orders(etf):
                order_target_percent(etf, context.fixed_frac)
        else:
            if not get_open_orders(etf):
                order_target(etf, 0) 
                
    record(numberOfPositions=len(context.portfolio.positions))
There was a runtime error.

Harrison

I did this and I have found that the golden cross it usually it is the beginning of the Bull Market. I was checking the algo with leverage ETFs to park cash. Enjoy.

Clone Algorithm
57
Loading...
Backtest from to with initial capital
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
# To run an algorithm in Quantopian, you need two functions: initialize and 
# handle_data.
import pandas as pd 
import talib

def initialize(context):
    # This initialize function sets any data or variables that you'll use in
    # your algorithm.  For instance, you'll want to define the security (or 
    # securities) you want to backtest.  You'll also want to define any 
    # parameters or values you're going to use.

    # In our example, we're looking at Apple.  If you re-type this line 
    # yourself, you'll see the auto-complete that is available for the 
    # security ID.
    context.stock = sid(38533)
    #context.stock = sid(8554)


def handle_data(context, data):
    # This handle_data function is where the real work is done.  Our data is
    # minute-level tick data, and each minute is called a frame.  This function
    # runs on each frame of the data.
    
    # We will be working with Apple only
    stock = context.stock
    
    # And we will need Apple current and historic data
    stock_data = data[stock]
    
    # We will need average price data
    mavg_short = stock_data.mavg(50)
    mavg_long = stock_data.mavg(200)



    current_price = stock_data.price

    # In order to make market decisions we need to know how much cash we have
    cash = context.portfolio.cash
   

    # This is the meat of the algorithm, placed in this if statement.
    #if mavg_short > mavg_long and cash > current_price:
    if mavg_short > mavg_long and cash > current_price:
        
        # When buying we need to know how many shares we can buy
        number_of_shares = int(cash/current_price)
        # int() helps us get a integer value, we can't buy half a share you know
        
        # Finally, we place the buy order
        order(stock,  number_of_shares)
    elif mavg_short < mavg_long:
        # Before we sell we need to know how many shares we have
        number_of_shares = context.portfolio.positions[stock.sid].amount
        
        # Now we can sell
        order(stock, -number_of_shares) # When selling we need to pass a negative number of shares
There was a runtime error.