Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Nested SMA method algorithm

Can someone help me to write this nested SMA method algorithm?


import talib

def initialize(context):  
    context.stocks = [sid(8554),sid(33652)]

def handle_data(context, data):  
    high_price = history(bar_count=25, frequency='1d',field='high')  
    low_price = history(bar_count=25, frequency='1d',field='low')  
    close_price =history(bar_count=25, frequency='1d',field='close_price')  
    r = (close_price - low_price)/(high_price - low_price)  

   up = SMA(r*30,4,1)

   mid = SMA(up,3,1)

   ground = SMA(mid,2,1)

   resistance_line = 80

   longtrend = 9*up - 3*mid - 2*ground

   Sell if longtrend cross above resistance  
13 responses

Hi Qifeng,

If you can explain your idea in more detailed way I'll try to help.
What's enter and exit conditions of your algo? Is it long only, short only or both? What's the criteria of switching between securities? Is it pair trading or it's possible to trade more than 2 securities?
Please, provide as many details as you can.

Regards,
Ed

Hi, Ed

I want to build up a strategy that can trade more than 2 securities. This strategy contains two algorithm, the buy condition and the sell condition.

Enter condition:
MA(close_price, 5) cross above MA(close_price, 30) MA is a simple arithmetic mean function.

Exit condition:
resistance_line = 80
up = SMA(r*30,4,1)
mid = SMA(SMA(up,3,1)
ground = SMA(mid,2,1)
longtrend = 9*up - 3*mid - 2*ground

IF longtrend cross above resistance, THEN SELL.

Hi Qifeng,

Couple of questions:
What's SMA? It doesn't look like simple moving average to me.
Suppose your algo trades 2 or more securities. If buy signal is triggered for more than 2 securities what do you want to do? Buy both of them for 50% free cash each or buy only one?

Regards,
Ed

Hi, Ed

I am sorry for making you confused. The MA is represented for Simple Moving Average (SMA). The SMA in the Exit condition above is Smoothed Moving Average (SMMA).

If the buy signal is triggered, I want to buy 50% for each 2 securities.

Best,
Qifeng

Hi Qifeng,

SMMA has only one parameter - period as far as I can see here http://www2.wealth-lab.com/WL5Wiki/SMMA.ashx.
What are other two parameters?

Regards,
Ed

Hi, Ed

The other two parameters are already in the formula. For example,
up = SMA(r*30,4,1) SMA(data_series,N,M) So here r*30 is data_series, and N represents the data_series during N periods, here N=4. M represents the smoothing period, here M=1.

Thanks!

Best,
Qifeng

Hi Qifeng,

Unfortunately talib doesn't have SMMA, so we should write it ourself.
However, looking at various explanations on the Net I don't see data series period used anywhere. Only smoothing period is used. I already gave one example and here is another one: http://www.dailyfx.com/forex_forum/signal-strategy-fxcm-marketscope/237441-smoothed-moving-average-smma.html

It would help if you can explain how to calculate SMMA(dataseries, n, m). So far I could find only how to calculate SMMA(dataseries, m), where m is smoothing period.

Regards,
Ed

Hi, Ed

I copied the following formula for you. I think you can ignore the smoothed factor m, just use the N. The default value of m is 1.

Smoothed Moving Average (SMMA)
The first value of this smoothed moving average is calculated as the simple moving average (SMA):
SUM1 = SUM(CLOSE, N)
SMMA1 = SUM1/N

The second and succeeding moving averages are calculated according to this formula:
PREVSUM = SMMA(i-1) *N
SMMA(i) = (PREVSUM-SMMA(i-1)+CLOSE(i))/N

Where:
SUM1 — is the total sum of closing prices for N periods;
PREVSUM — is the smoothed sum of the previous bar;
SMMA1 — is the smoothed moving average of the first bar;
SMMA(i) — is the smoothed moving average of the current bar (except for the first one);
CLOSE(i) — is the current closing price;
N — is the smoothing period.

Best,
Qifeng

Hi Qifeng,

I started to work on your algo. Can you explain how you plan to allocate capital for your trades when trading multiple securities?
If algo trades 3 securities and enter conditions are met only for 1 security should we buy this security for all available cash, buy all 3 securities for 1/3rd of available cash each or buy one for 1/3rd of available cash?

Regards,
Ed

Hi, Ed

I think we can buy one for 1/3rd of available cash.

Thanks!

Best,
Qifeng

What's r in this formula: up = SMA(r*30,4,1)?
What does this mean: mid = SMA(SMA(up,3,1) ? Is it sma of sma SMA(SMA(up, 3, 1)) or just a mistake?

Sorry, I missed your r calculation in the first post. And I think SMA(SMA(up, 3, 1)) is what you called nested SMA. I hope I'll finish to program your algo soon.

Thanks so much for Ed Bartosh's help. Very appreciated! ! !