Thanks vbs. Great job, and very useful!
Posts made by Paduel Gerion

RE: Bokeh Integration  Interactive Webbrowser Plotting

RE: Heikin Ashi Delta indicator  Work but get warnings.
That I thought, Pythonized:
upw = dtr==false and dtr[1] and utr dnw = utr==false and dtr[1] and dtr
Meaning upw is true if current dtr is false and utr is true and the previous dtr is false. This would mark an upward trend change.
The same
upw = (not dtr) & dtr[1] & utr dnw = (not dtr) & dtr[1] & utr
In Metastock:
Assignation :=
Comparison = 
RE: Heikin Ashi Delta indicator  Work but get warnings.
Thank you.
I get the formula here, there are for several frameworks.
I have some problems with the code, I will try to solve when have more time.

RE: Heikin Ashi Delta indicator  Work but get warnings.
This is great!
I was trying to code the HACO indicator and the HACOLT, which use both price series data, and the opening and closing of Heikin Ashi candles. For which I had written a similar code, but this way you raise is simpler.
Thanks for including the haDelta in Backtrader.

RE: Heikin Ashi Delta indicator  Work but get warnings.
As I said, except for the warnings the indicator works well.
I'm glad to know that the indicator did not give you any warnings when using it.
I'll test it in another environment.Thanks for checking it.

Heikin Ashi Delta indicator  Work but get warnings.
Hi. With the addition of the filter for the Heikin Ashi candles, I have written code for the Heikin Ashi Delta, which was included in the original article by Dan Valcu in the Stocks & Commodities magazine of February 2014.
The indicator works fine, but I wanted to include a colored area using _fill_gt and _fill_lt, for a better visualization of it.
When I use it it colors correctly, but I receive these two warnings:
.../python2.7/sitepackages/backtrader/plot/plot.py:475: RuntimeWarning: invalid value encountered in greater kwargs['where'] = fop(y1, y2) .../python2.7/sitepackages/backtrader/plot/plot.py:475: RuntimeWarning: invalid value encountered in less kwargs['where'] = fop(y1, y2)
But if I remove _fill_gt and _fill_lt, from the code, the warnings disappear.
The haDelta indicator code:
#!/usr/bin/env python # * coding: utf8; pyindentoffset:4 * from __future__ import (absolute_import, division, print_function, unicode_literals) from backtrader import Indicator from backtrader.indicators import MovAv ''' Heikin Ashi Delta. Defined by Dan Valcu in his book "HeikinAshi: How to Trade Without Candlestick Patterns ". This indicator measures difference between Heikin Ashi close and open of Heikin Ashi candles, the body of the candle. To get signals add haDelta smoothed by 3 period moving average. For correct use, the data for the indicator must have been previously passed by the Heikin Ahsi filter. Formula:  haDelta = Heikin Ashi close  Heikin Ashi open  smoothed = movav(haDelta, period) ''' class haDelta(Indicator): alias = ('haD') lines = ('haDelta', 'smoothed') params = (('period', 3), ('movav', MovAv.SMA),) plotinfo = dict(subplot=True, ) plotlines = dict(haDelta=dict(color='red'), smoothed=dict(color='grey', _fill_gt=(0, 'green'), _fill_lt=(0, 'red'), ), ) def __init__(self): self.lines.haDelta = self.data.close  self.data.open self.lines.smoothed = self.p.movav(self.lines.haDelta, period=self.p.period) super(haDelta, self).__init__()
Chart of a test :
I know it is not very relevant, but by debugging the code and by the way learn something about plot in Backtrader, any idea of why the warnings?
And if you consider it appropriate, it could be incorporated into the Backtrader indicators.

RE: Hurst Exponent behavior in a Synthetic Trend Serie
These is a great solution. Thanks for taking into account my proposals and include them in this great framework.

RE: Hurst Exponent behavior in a Synthetic Trend Serie
Hello,
I have checked that the code for the Hurst Exponent is correct and consistent with the method used. However the results are not correct due to the parameters used for the calculation.
Because the nature of the method the result is sensitive to the size of the sample.
Thus I have done simulations using synthetic series (Browian Geometrical Movement, Mean Reversal and Trend), and checking that the values of the Hurst exponent are not consistent with samples of less than about 2000 observations, being stable from about 3000 And 5000 observations according to the sample.
Similarly, the value used to define the lags must be less than about 10 and the upper value of the range should be above 500.
Backtrader uses a range for lags between 2 and half of sample size (period), and I have verified that it is not a good system because even with large samples and large periods is unstable.
The simulations can be reproduced and tested in this Jupyter Notebook:
I also propose to modify the calculation of the Exponent of Hurst in Backtrader with the following code, and to advise in the documentation that is not reliable with period values inferior to 2,000 bars.
Code :
from __future__ import (absolute_import, division, print_function, unicode_literals) from . import PeriodN __all__ = ['HurstExponent', 'Hurst'] class HurstExponent(PeriodN): frompackages = ( ('numpy', ('asarray', 'log10', 'polyfit', 'sqrt', 'std', 'subtract')), ) alias = ('Hurst',) lines = ('hurst',) params = (('period', 2000), ('lag_start', 10), ('lag_end', 500),) def __init__(self): super(HurstExponent, self).__init__() # Prepare the lags array self.lags = asarray(range(self.p.lag_start, self.p.lag_end)) self.log10lags = log10(self.lags) def next(self): # Fetch the data ts = asarray(self.data.get(size=self.p.period)) # Calculate the array of the variances of the lagged differences tau = [sqrt(std(subtract(ts[lag:], ts[:lag]))) for lag in self.lags] # Use a linear fit to estimate the Hurst Exponent poly = polyfit(self.log10lags, log10(tau), 1) # Return the Hurst exponent from the polyfit output self.lines.hurst[0] = poly[0] * 2.0
In any case I find that this index is useful for an initial study of the series to determine what type of strategy is more propitious, but difficult to use in the strategy itself.

Hurst Exponent behavior in a Synthetic Trend Serie
Hi, Backtrader is fabulous! Mementum, congratulations on your work.
I tried to replicate the analysis done by Duk2 in this article El Exponente de Hurst y la memoria de los precios en bolsa, referring to the exponent of Hurst, but using Backtrader.
In her article she calculates a single value of the Hurst exponent for all the time serie, but I tried to use the Backtrader rolling indicator with three different periods (which generate different lags).
For this I created a syntetic trend serie. But the result surprised me because in the three periodsHurst exponent is less than 0.5, when it should be close to 1.
Here the code:
from __future__ import (absolute_import, division, print_function, unicode_literals) import datetime import backtrader as bt import pandas as pd from numpy import * class HurstST(bt.Strategy): params = ( ) def __init__(self): # Three Hurst Exponents with diferent periods hurst_1 = bt.ind.HurstExponent(period=40) hurst_2 = bt.ind.HurstExponent(period=200) hurst_3 = bt.ind.HurstExponent(period=1200) def next(self): pass if __name__ == '__main__': cerebro = bt.Cerebro() # Create a synthetic trend time serie in a pandas dataframe tr = pd.DataFrame(pd.date_range(start='200511', end='20170131', freq='D'), columns=['date']) tr['open'] = tr['high'] = tr['low'] = tr['close'] = pd.DataFrame(log(cumsum(random.randn(100000) + 1) + 1000)) tr.set_index(['date'], inplace=True) # Pass the syntectic dataframe to the backtrader datafeed and add it to the cerebro data = bt.feeds.PandasDirectData(dataname=tr, volume=1, openinterest=1 ) cerebro.adddata(data) cerebro.addstrategy(HurstST) cerebro.run(stdstats=False) cerebro.plot()
Here the chart :
Is this a mistake of mine or some problem with the indicator?
Thank you.