Back to Posts
Listen to Thread

Hello All,

This is my untested implementation of the EMA - it looks much like the one Yahoo produces on their chart for the same period.

Hello Dan et al at Quantopian,

But with TA-Lib (the indicator library, see: http://ta-lib.org/ ) and ta-lib (the Python wrapper, see: https://github.com/mrjbq7/ta-lib ) I could get the EMA in one line of code. Is there a time-line for implementing the import of the 'talib' package?

Regards,

Peter

Clone Algorithm
34
Loading...
Backtest from to with initial capital ( data)
Cumulative performance:
Algorithm Benchmark
Custom data:
Week
Month
All
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Information Ratio
--
Benchmark Returns
--
Volatility
--
Max Drawdown
--
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
Information Ratio 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
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

Hi Peter,

Thanks for posting the EMA.

And yes, TAlib should really be available. There's some work on this which actually looks very promising (https://github.com/quantopian/zipline/pull/100). I will try to pull this in soon to make it available to a larger audience.

Thomas

Hello Thomas,

Thanks. I was playing today with talib and zipline but I'm sure integrating talib and Quantopian requires some preparation.

Regards,

Peter

It does. But the most of the work of figuring out the best way is already done. We just need to finalize it. After that, it's just a bunch of copy&paste to wrap the whole TAlib library.

There are many good things in the works to address certain short-comings. It's a matter of resources for the most part.

Hello Peter & Thomas,

Looks like pandas has a built-in exponential moving average:

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.stats.moments.ewma.html

http://pandas.pydata.org/pandas-docs/dev/computation.html

Should be possible to use it in Quantopian, right?

Grant

Hello Grant,

Thanks. This is my attempt but I don't think it's quite right. Any ideas?


import pandas

Stock1 = 0

def initialize(context):  
    global Stock1  
    context.Stock1 = sid(24)  
    Stock1 = int(str(context.Stock1)[9:-1])

@batch_transform(window_length=20)  
def get_EMA(datapanel):  
    prices=datapanel['price']  
    EMA = pandas.stats.moments.ewma(prices[0:1], span=20)  
    return EMA 

def handle_data(context, data):  
    EMA = get_EMA(data)  
    if EMA is None:  
        return  
    record(Price=data[Stock1].price, EMA=EMA[Stock1][0], MA=data[Stock1].mavg(20))

Regards,

Peter

Any better?

import pandas

Stock1 = 0

def initialize(context):  
    global Stock1  
    context.Stock1 = sid(24)  
    Stock1 = int(str(context.Stock1)[9:-1])

@batch_transform(window_length=40)  
def get_EMA(datapanel):  
    global Stock1  
    prices=datapanel['price']  
    EMA = pandas.stats.moments.ewma(prices, span=20)  
    return EMA[Stock1][39]

def handle_data(context, data):  
    EMA = get_EMA(data)  
    if EMA is None:  
        return  
    record(Price=data[Stock1].price, EMA=EMA, MA=data[Stock1].mavg(20))  

Hi Peter,

I don't have time right now to try it, but did the code above not run? Or are you just wondering if it is computing the moving average correctly?

Grant

Hello Grant,

They both run but I'm sure the first one is wrong. I'm not sure about the second one but it looks better. I'm looking for corrections and/or improvements.

Regards,

Peter

Hi Peter,

I ran your second version above, and attached the backtest. Nothing obviously wrong that I can see. In doing some googling, I found mention of using scipy.signal.lfilter instead of an EWMA. For a variety of reasons, applying a more fundamental signal processing technique might be the better approach here.

Grant

Clone Algorithm
8
Loading...
Backtest from to with initial capital ( data)
Cumulative performance:
Algorithm Benchmark
Custom data:
Week
Month
All
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Information Ratio
--
Benchmark Returns
--
Volatility
--
Max Drawdown
--
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
Information Ratio 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
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

Hello Grant,

Thanks. I think I got a bit confused with the window length - it seems to works with the more obvious:

@batch_transform(window_length=20)  
def get_EMA(datapanel):  
    global Stock1  
    prices=datapanel['price']  
    EMA = pandas.stats.moments.ewma(prices, span=20)  
    return EMA[Stock1][19]  

I'm interested in signal processing techniques like John Ehlers' in 'Cybernetic Analysis for Stock and Futures' and 'Rocket Science for Traders - Digital Signal Processing Applications'. I wasn't aware of scipy.signal.lfilter

Regards,

Peter

Hi Peter,

I'm new to the site and a fan of Ehlers work. I was wondering how to reference past bars in Python, specifically if we have any examples on Quantopian that show the code in action.

Thanks.

Hello CA,

Have you made any progress or do you have a specific problem I could try and help with? The problem I have with Ehlers is that I don't understanf the EasyLangauge examples in his book. I had a play here but it was a long time ago: https://www.quantopian.com/posts/john-ehlers-fischer-transform

P.

Hi Peter,

Thanks for the Ehlers example. I come from a ThinkScript(TOS) background and have some experience with Ehlers work.

The code I am looking to translate into Python is used in Ehlers Fisher Cyber Cycle and used to smooth the price:

Price = (H+L) / 2
Smooth = (Price + 2*Price[1] + 2*Price[2] + Price[3]) / 6

Where [1], [2], [3] references the price level 1, 2, and 3 bars ago.

I am still working on my algorithm currently, but I was wondering how to most efficiently implement the Smoothed Price using Python.

Thanks.

Hello CA,

I'm lost already, I'm afraid. Are we looking for:

Mid = (H+L) / 2  
Smooth = (Mid + 2*Mid[1] + 2*Mid[2] + Mid[3]) / 6  

P.

Clone Algorithm
1
Loading...
Backtest from to with initial capital ( data)
Cumulative performance:
Algorithm Benchmark
Custom data:
Week
Month
All
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Information Ratio
--
Benchmark Returns
--
Volatility
--
Max Drawdown
--
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
Information Ratio 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
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

Hi Peter,

Possibly...let me clarify something with your code. In your code does context.window[1] denote the (H+L) / 2 of 1 bar ago? I'm not familiar with the Python context. In EasyLanguage, the interpreter automatically recognizes close[1] as the closing price 1 bar ago, high[1] as the high price 1 bar ago and so on. I am assuming here that context.window[1] means that we are referencing the stock price 1 bar ago?

In Ehlers point of view, assigning higher value to the price level 1 & 2 bars ago is a version of smoothing.

Hello CA,

I used a queue which grows from no items to four and then stays at four as it is FIFO. The queue is indexed as [0] through [3] where [3] is the current day's price, [2] is yesterday etc. There will be several better ways of doing this. I revised it to use a queue for 'high' and a queue for 'low'. Now it does look like a smoothed price which I can't explan!

P.

Clone Algorithm
1
Loading...
Backtest from to with initial capital ( data)
Cumulative performance:
Algorithm Benchmark
Custom data:
Week
Month
All
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Information Ratio
--
Benchmark Returns
--
Volatility
--
Max Drawdown
--
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
Information Ratio 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
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

Here's a version using batch transform.

P.

Clone Algorithm
14
Loading...
Backtest from to with initial capital ( data)
Cumulative performance:
Algorithm Benchmark
Custom data:
Week
Month
All
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Information Ratio
--
Benchmark Returns
--
Volatility
--
Max Drawdown
--
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
Information Ratio 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
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

Hi Peter,

Your smoothing code looks good!

You can see how many of the peaks that would otherwise obscure a trend become eliminated in the Smoothed_Price data. I believe the point in using the Smoothed_Price in algorithms is an attempt to filter out the "noisey" price movements and begin with data that more closely resembles the "true" price movement of the security. Something akin to a signal/noise ratio could also be used in conjunction with this code to formulate a strategy.

I would think that any algorithm trading in a choppy price environment would excel using the Smoothed_Price rather than any other price form for calculations. I will test that theory today.

CA

Hi guys,

For what it's worth, scipy has a bunch of built-in filter functionality (see http://docs.scipy.org/doc/scipy/reference/signal.html). I've had in mind to tinker with it in the context of Quantopian at some point. Perhaps of interest to you.

Grant

Grant,

Thank you so much for the reference. You guys are filling up my free-time research slot with newly discovered ideas, sites, and libraries rather quickly!

I appreciate it!

CA

Hi everyone,

So, if we are to use the Smoothed_Price data rather than the regular Price in determining the MA's as in this example:

fastMA = data[context.stock].mavg(50)
slowMA = data[context.stock].mavg(200)

How would we properly structure the syntax? Basically, using a Smoothed_Price crossover to initiate position changes would be my goal.

Thanks!

CA

Hello CA,

This is a first atempt with a single SID that just counts possible setups i.e. simple crosses. It produces very few setups but the answer to that is to trade it with the NASDAQ 100 or some similar universe. Did I mention it's untested?

P.

Clone Algorithm
14
Loading...
Backtest from to with initial capital ( data)
Cumulative performance:
Algorithm Benchmark
Custom data:
Week
Month
All
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Information Ratio
--
Benchmark Returns
--
Volatility
--
Max Drawdown
--
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
Information Ratio 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
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.
Log in to reply to this thread.
Not a member? Sign up!