Back to Community
Combination of Supertrend with EMA Crossover

Hi All,

Is there a way to device an algo which kind of combines the Supertrend(7,2) signal buy/sell with subsequently EMA(5,21,55) crossover?

Is there a way to code such an algo.

Suggestions will be highly appreciated

7 responses

Hi Varun,

It is possible to write a moving average crossover strategy using Quantopian, but it will be hard to make it conform to our contest rules. The Quantopian contest encourages people to write algorithms that assign a number to each asset in a trading universe, every day. We call the algorithms that produce these numbers "alpha factors" and the more predictive the alpha factor is (as in, assets with a high alpha factor value tend to go up more, and assets with a low alpha factor value go down more), the more profitable your trading strategy will be.

I highly recommend working through the getting started tutorial to learn how to create alpha factors, and write trading strategies around those alpha factors. One of our lectures, titled: "Long-Short Equity" does a good job describing the kind of algorithm that Quantopian encourages people to write.

Should you decide to continue writing algorithms that use moving average crossovers, I recommend looking at this lesson in the Pipeline tutorial that shows how to build custom factors. You could create a custom factor that returns whether or not a given asset has experienced a moving average crossover within the past 5 days, and then use that to raise (or lower) the alpha factor score being generated by the other factors in your pipeline.


The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Hi Varun,

Here is a customfactor I've been using for Supertrend:

def columnwise_anynan(array2d):  
    return np.isnan(array2d).any(axis=0)

def Final_UpperBand(BUB, C):  
    FUB = np.zeros(len(BUB))  
    FUB[0] = BUB[0]  
    for i in range(1, len(BUB)):  
        if (BUB[i] < FUB[i-1]) or (C[i-1] > FUB[i-1]):  
            FUB[i] = BUB[i]  
            FUB[i] = FUB[i-1]  
    return FUB  
def Final_LowerBand(BLB, C):  
    FLB = np.zeros(len(BLB))  
    FLB[0] = BLB[0]  
    for i in range(1, len(BLB)):  
        if (BLB[i] > FLB[i-1]) or (C[i-1] < FLB[i-1]):  
            FLB[i] = BLB[i]  
            FLB[i] = FLB[i-1]  
    return FLB  
def SPR_T(FUB, FLB, C, period):  
    ST = np.zeros(len(FUB))  
    ST[0] = FUB[0]  
    for i in range(1, len(FLB)):  
        if (ST[i-1] == FUB[i-1]) and (C[i] < FUB[i]):  
            ST[i] = FUB[i]  
        elif (ST[i-1] == FUB[i-1]) and (C[i] > FUB[i]):  
            ST[i] = FLB[i]  
        elif (ST[i-1] == FLB[i-1]) and (C[i] > FLB[i]):  
            ST[i] = FLB[i]  
        elif (ST[i-1] == FLB[i-1]) and (C[i] < FLB[i]):  
            ST[i] = FUB[i]  
    return ST  
class Super_Trend(CustomFactor):  
    inputs = [USEquityPricing.high, USEquityPricing.low, USEquityPricing.close]  
    def compute(self, today, assets, out, high, low, close):  
        multiplier = 3.  
        period = 10  
        anynan = columnwise_anynan(close)  
        for col_ix, have_nans in enumerate(anynan):  
            if have_nans:  
                out[col_ix] = np.nan  
            N = self.window_length  
            H = high[:,col_ix]  
            L = low[:,col_ix]  
            C = close[:,col_ix]

            MP = (H+L)/2  
            ATR = talib.ATR(H,L,C,timeperiod=period)  
            ATR = np.nan_to_num(ATR)  
            BASIC_UPPERBAND = MP + multiplier * ATR  
            BASIC_LOWERBAND = MP - multiplier * ATR  
            FINAL_UPPERBAND = Final_UpperBand(BASIC_UPPERBAND, C)  
            FINAL_LOWERBAND = Final_LowerBand(BASIC_LOWERBAND, C)  

            out[col_ix] = SUPER_TREND[-1]  

Hi Mathieu,

Thanks a lot for the supertrend algo!

Hi Vladimir,

Thanks a lot for the algo !!!

Does the Supertrend have a combination with the EMA crossovers?

Could you please elaborate on the same?

Does the Supertrend have a combination with the EMA crossovers?
The algo takes bullish position when stock price is above Supertrend and EMA(21) > EMA(55)

 if ((P > SAR) and (EMA2 > EMA3)):  

It takes bearish position when stock price is belowe Supertrend and EMA(5) < EMA(21)

elif ((P < SAR) and (EMA1 < EMA2)):  

Copy my code, paste it to IDE and Run Full Backtest from 06/01/2007 to 01/04/2019 on $100000 initial capital.

Good Luck.

Thanks, Vladimir once again!

Much Appreciated!!

I don't see Vladimir's algorithm, could you please share with me? I am trying to do the same but could not find.