For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

ZigZag indicator for live trading.



  • Hi, everyone.

    This is a MQL style Zigzag indicator written for live trading. It has 2 params: first is retrace - retracement in percent, second one is minbars - how many bars indicator will skip after last extremum before searching opposite extremum. The logic of indicator is quite complex, but it works. )

    TODO: Handle the outer bar. It is a bar witch values being higher and lower at the same time than last extremum value.

    If you find any bug, please, let me know.

    ##+------------------------------------------------------------------+
    ## Copyright (C) 2019 Nikolai Khramkov
    ##
    ## This program is free software: you can redistribute it and/or modify
    ## it under the terms of the GNU General Public License as published by
    ## the Free Software Foundation, either version 3 of the License, or
    ## (at your option) any later version.
    ##
    ## This program is distributed in the hope that it will be useful,
    ## but WITHOUT ANY WARRANTY; without even the implied warranty of
    ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    ## GNU General Public License for more details.
    ## You should have received a copy of the GNU General Public License
    ## along with this program.  If not, see <http://www.gnu.org/licenses/>.
    ##+------------------------------------------------------------------+
    
    
    import backtrader as bt
    
    class ZigZag(bt.ind.PeriodN):
        '''
          ZigZag indicator.
        '''
        lines = (
            'trend',
            'last_high',
            'last_low',
            'zigzag',
        )
    
        plotinfo = dict(
            subplot=False,
            plotlinelabels=True, plotlinevalues=True, plotvaluetags=True,
        )
    
        plotlines = dict(
            trend=dict(_plotskip=True),
            last_high=dict(color='green', ls='-', _plotskip=True),
            last_low=dict(color='black', ls='-', _plotskip=True),
            zigzag=dict(_name='zigzag', color='blue', ls='-', _skipnan=True),
        )
    
        params = (
            ('period', 2),
            ('retrace', 0.05), # in percent
            ('minbars', 2), # number of bars to skip after the trend change
        )
    
        def __init__(self):
            super(ZigZag, self).__init__()
    
            assert self.p.retrace > 0, 'Retracement should be above zero.'
            assert self.p.minbars >= 0, 'Minimal bars should be >= zero.'
    
            self.ret = self.data.close * self.p.retrace / 100
            self.minbars = self.p.minbars
            self.count_bars = 0
            self.last_pivot_t = 0
            self.last_pivot_ago = 0
    
        def prenext(self):
            self.l.trend[0] = 0
            self.l.last_high[0] = self.data.high[0]
            self.l.last_low[0] = self.data.low[0]
            self.l.zigzag[0] = (self.data.high[0] + self.data.low[0]) / 2
    
        def next(self):
            curr_idx = len(self.data)
            self.ret = self.data.close[0] * self.p.retrace / 100
            self.last_pivot_ago = curr_idx - self.last_pivot_t
            self.l.trend[0] = self.l.trend[-1]
            self.l.last_high[0] = self.l.last_high[-1]
            self.l.last_low[0] = self.l.last_low[-1]
            self.l.zigzag[0] = float('nan')
    
            # Search for trend
            if self.l.trend[-1] == 0:
                if self.l.last_low[0] < self.data.low[0] and self.l.last_high[0] < self.data.high[0]:
                    self.l.trend[0] = 1
                    self.l.last_high[0] = self.data.high[0]
                    self.last_pivot_t = curr_idx
    
                elif self.l.last_low[0] > self.data.low[0] and self.l.last_high[0] > self.data.high[0]:
                    self.l.trend[0] = -1
                    self.l.last_low[0] = self.data.low[0]
                    self.last_pivot_t = curr_idx
    
            # Up trend
            elif self.l.trend[-1] == 1:
                if self.data.high[0] > self.l.last_high[-1]:
                    self.l.last_high[0] = self.data.high[0]
                    self.count_bars = self.minbars
                    self.last_pivot_t = curr_idx
    
                elif self.count_bars <= 0 and self.l.last_high[0] - self.data.low[0] > self.ret and self.data.high[0] < self.l.last_high[0]:
                    self.l.trend[0] = -1
                    self.count_bars = self.minbars
                    self.l.last_low[0] = self.data.low[0]
                    self.l.zigzag[-self.last_pivot_ago] = self.l.last_high[0]
                    self.last_pivot_t = curr_idx
    
                elif self.count_bars < self.minbars and self.data.close[0] < self.l.last_low[0]:
                    self.l.trend[0] = -1
                    self.count_bars = self.minbars
                    self.l.last_low[0] = self.data.low[0]
                    self.l.zigzag[-self.last_pivot_ago] = self.l.last_high[0]
                    self.last_pivot_t = curr_idx
    
            # Down trend
            elif self.l.trend[-1] == -1:
                if self.data.low[0] < self.l.last_low[-1]:
                    self.l.last_low[0] = self.data.low[0]
                    self.count_bars = self.minbars
                    self.last_pivot_t = curr_idx
    
                elif self.count_bars <= 0 and self.data.high[0] - self.l.last_low[0] > self.ret and self.data.low[0] > self.l.last_low[0]:
                    self.l.trend[0] = 1
                    self.count_bars = self.minbars
                    self.l.last_high[0] = self.data.high[0]
                    self.l.zigzag[-self.last_pivot_ago] = self.l.last_low[0]
                    self.last_pivot_t = curr_idx
    
                elif self.count_bars < self.minbars and self.data.close[0] > self.l.last_high[-1]:
                    self.l.trend[0] = 1
                    self.count_bars = self.minbars
                    self.l.last_high[0] = self.data.high[0]
                    self.l.zigzag[-self.last_pivot_ago] = self.l.last_low[0]
                    self.last_pivot_t = curr_idx
    
            # Decrease minbars counter
            self.count_bars -= 1


  • Hi Nikolai, I am also looking for a good example of the zigzag indicator.

    I believe other implementations of the indicator, is not live in the sense that I think it has no way to calculate the last value.
    example: previous prices is 1 then 5, 4, 3, then current 2. It knows 5 is a peak, but it has no idea that 3 is a valley in a live scenario where u take in new prices line by line.

    I kinda am having a mindblock at this stage. Can I ask if yours is able to back calculate someway or how do you use it in strategy class such that I can know ?



  • Hi J T.
    When you use the indicator for live trading, you need to keep two contexts in mind: now and the past. Zigzag knows that there was a peak only after the event happened in the past. You need to update the data of other indicators and make decisions based on these two contexts. Tell me what the idea is. How do you want to use zigzag?



  • Hi Niko,
    I was just trying your indicator to understand it better.

    It seems to be changing the peak or valley at exactly the same time the data comes in. So this does not happen at t+1. But at t.

    At t: When there is a zigzag value of say new low, it overwrites the last low value u stored. At t+1, there could even be a newer low. So the zigzag value at t is overwritten again. So it is not a turning point.

    I am trying to find turning points, and I would have thought this should happen at t+1. So how you use this, do u compare like the below?

    pseudo code
    if t+1.low < t+1.last_low (t+1.last_low is actually t.last_low anyway)
    turning_point = True
    else:
    # turning_point = False # t+1.low could be same or lower than t+1.last_low.

    I am assuming that when there is a zigzag value, all the other retrace, minbar conditions have been met.

    sorry I hope I explained myself well. And many thanks for sharing this indicator.



  • Hi J T,

    I found a video for you https://www.youtube.com/watch?time_continue=60&v=ambgNP0Qd9Y
    Take look at how ZigZag works. Trend changes in the past.

    So at (t = now) you can have information that -5 candles ago was a peak or a valley. But the trend changes at (t=now).

    You can change _plotskip to False to see how indicator works.

    I hope i explained. Ask more if it’s not clear.





  • I understand more now, appreciate you taking time.

    I am not too clear of the trend line you have in the indicator. Not sure if it says anything, seem to be able to flip from 1 to -1 even before next turning point.
    How do you use this trend line?

    If you dont mind also, what values do you normally use for period, retrace and minbars?



  • Also from your zigzag line, last_high line and last_low line, there is no way to know whether it was the last turn was a peak or valley correct? I need additional code to do keep track of this?



  • Hi. Once again from the beginning. )
    When a new candle comes in real time, the indicator processes the incoming data. If the conditions laid down in the indicator are met, the trend changes in real time. But the peak or valley is in the past. We have the information how many candles back there was a peak or a valley.

    Example:

    if self.zigzag.trend[-1] == -1 and self.zigzag.trend[0] == 1:
    
        # Zigzag indicator can draw trend line in past
        # When it happens we should redraw lines
        self.keep_long_lines(self.zigzag.last_pivot_ago)
    
    elif self.zigzag.trend[-1] == 1 and self.zigzag.trend[0] == -1:
    
        # Zigzag indicator can draw trend line in past time
        # When it happens we should redraw lines
        self.keep_long_lines(self.zigzag.last_pivot_ago)
    

    In this example, I update the strategy when a peak or valley has formed. self.zigzag.last_pivot_ago - number of candles ago where a peak or valley was. You should think in two contexts now and past when you work with ZigZag in live trading. There should be dynamically organized interaction between these contexts in the strategy.



  • @Nikolai said in ZigZag indicator for live trading.:

    self.keep_long_lines(self.zigzag.last_pivot_ago)

    In second case.

    elif self.zigzag.trend[-1] == 1 and self.zigzag.trend[0] == -1:
    
        self.keep_short_lines(self.zigzag.last_pivot_ago)


  • @J-T said in ZigZag indicator for live trading.:

    If you dont mind also, what values do you normally use for period, retrace and minbars?

    It depends on an instrument, timeframe, goals. I have the strategy with 2 Zigzags with different params. You should optimize these params for your goals.



  • Hi Niko, Thanks once again.
    I see you use the trend instead of the zz value. Can I ask why?

    This is what I did and I wonder how different it would be to yours.

    def conditions_check(curr_px, zz, peak, valley): 
    ''' 
    1. check and set a global var p_or_v if a zigzag value comes in. becasue I have
    no idea it was a peak or valley. 
    2. next data that comes in goes into the else segment
    Sorry I am exposing how elementary I am at this... 
    '''
        action = 'not sure yet'
        if not math.isnan(zz):  # zz detected
            if zz == peak:
                globe.p_or_v = 'p'  # this is a global variable
            else:
                globe.p_or_v = 'v'
            return 'zz detected'
    
        else:  # regular rows no zz value
            if globe.p_or_v == 'p':  # if it is a p or v, it means the last data is a zigzag point
                    #more logic of other tests and actions to take
            elif globe.p_or_v == 'v':
                    #more logic of other tests and actions to take
    
            globe.p_or_v = 'p_or_v'
            return # whatever action or results.
    


  • Actually when you live trade does the zz value actually appear? Meaning, would you ever get a zigzag value like the below?

    2020-04-07 22:31:00: close:44.59, zigzag: 44.45, trend: -1.0, last_high: 46.36, last_low: 44.45



  • Because i use Backtrader framework infrastructure. Data type



  • Data type - line

    @J-T said in ZigZag indicator for live trading.:

    2020-04-07 22:31:00: close:44.59, zigzag: 44.45, trend: -1.0, last_high: 46.36, last_low: 44.45

    Yes.

    You should dive in to the theoretical part if this indicator. There are many implementations. Each of them has its pros and cons.

    https://www.mql5.com/en/articles/1468
    https://www.mql5.com/en/articles/1531





  • Thanks alot. I will do some reading into these articles. Also will be diving into connection with IB.

    Pls stay safe in the meantime. And really appreciate your help.



  • Hi Nikolai

    I just set up for fx eur usd pair live trading to take in data. All the values of zigzag is nan except the first one. You mentioned above that you get zigzag values at the turning point, am i doing something wrong? Also added zigzag[-1]. So in backtest, the zigzag values will change from nan to a certain price, but when trading live it does not is it?

    ***** DATA NOTIFY: DELAYED
    Datetime, Open, High, Low, Close, ZZ, ZZ-1,Trend, L_High, L_Low, 
    2020-04-13 07:00:00,1.0935,1.0938,1.0934,1.0934,1.0936,1.0936,0.0000,1.0938,1.0934
    2020-04-13 07:01:00,1.0934,1.0935,1.0934,1.0934,nan,1.0936,0.0000,1.0938,1.0934
    2020-04-13 07:02:00,1.0934,1.0935,1.0934,1.0934,nan,nan,0.0000,1.0938,1.0934
    2020-04-13 07:03:00,1.0934,1.0935,1.0934,1.0934,nan,nan,0.0000,1.0938,1.0934
    2020-04-13 07:04:00,1.0934,1.0935,1.0934,1.0934,nan,nan,0.0000,1.0938,1.0934
    2020-04-13 07:05:00,1.0934,1.0936,1.0934,1.0935,nan,nan,0.0000,1.0938,1.0934
    2020-04-13 07:06:00,1.0935,1.0936,1.0935,1.0935,nan,nan,0.0000,1.0938,1.0934
    2020-04-13 07:07:00,1.0935,1.0936,1.0935,1.0936,nan,nan,0.0000,1.0938,1.0934
    2020-04-13 07:08:00,1.0936,1.0936,1.0935,1.0936,nan,nan,0.0000,1.0938,1.0934
    2020-04-13 07:09:00,1.0936,1.0936,1.0935,1.0936,nan,nan,0.0000,1.0938,1.0934
    2020-04-13 07:10:00,1.0936,1.0936,1.0935,1.0935,nan,nan,0.0000,1.0938,1.0934
    2020-04-13 07:11:00,1.0935,1.0935,1.0934,1.0935,nan,nan,0.0000,1.0938,1.0934
    2020-04-13 07:12:00,1.0935,1.0936,1.0935,1.0935,nan,nan,0.0000,1.0938,1.0934
    2020-04-13 07:13:00,1.0935,1.0936,1.0935,1.0936,nan,nan,0.0000,1.0938,1.0934
    2020-04-13 07:14:00,1.0936,1.0936,1.0935,1.0936,nan,nan,0.0000,1.0938,1.0934
    2020-04-13 07:15:00,1.0936,1.0937,1.0935,1.0936,nan,nan,0.0000,1.0938,1.0934
    2020-04-13 07:16:00,1.0936,1.0937,1.0936,1.0936,nan,nan,0.0000,1.0938,1.0934
    2020-04-13 07:17:00,1.0936,1.0936,1.0934,1.0934,nan,nan,0.0000,1.0938,1.0934
    2020-04-13 07:18:00,1.0934,1.0934,1.0934,1.0934,nan,nan,0.0000,1.0938,1.0934
    2020-04-13 07:19:00,1.0934,1.0934,1.0933,1.0934,nan,nan,-1.0000,1.0938,1.0933
    2020-04-13 07:20:00,1.0934,1.0935,1.0933,1.0934,nan,nan,-1.0000,1.0938,1.0933
    2020-04-13 07:21:00,1.0934,1.0936,1.0934,1.0935,nan,nan,-1.0000,1.0938,1.0933
    2020-04-13 07:22:00,1.0935,1.0936,1.0935,1.0936,nan,nan,-1.0000,1.0938,1.0933
    2020-04-13 07:23:00,1.0936,1.0936,1.0935,1.0935,nan,nan,-1.0000,1.0938,1.0933
    2020-04-13 07:24:00,1.0935,1.0936,1.0935,1.0935,nan,nan,-1.0000,1.0938,1.0933
    2020-04-13 07:25:00,1.0935,1.0935,1.0934,1.0935,nan,nan,-1.0000,1.0938,1.0933
    2020-04-13 07:26:00,1.0935,1.0935,1.0934,1.0934,nan,nan,-1.0000,1.0938,1.0933
    2020-04-13 07:27:00,1.0934,1.0935,1.0934,1.0934,nan,nan,-1.0000,1.0938,1.0933
    2020-04-13 07:28:00,1.0934,1.0935,1.0934,1.0935,nan,nan,-1.0000,1.0938,1.0933
    2020-04-13 07:29:00,1.0935,1.0936,1.0935,1.0935,nan,nan,-1.0000,1.0938,1.0933
    2020-04-13 07:30:00,1.0935,1.0935,1.0934,1.0935,nan,nan,-1.0000,1.0938,1.0933
    2020-04-13 07:31:00,1.0935,1.0935,1.0935,1.0935,nan,nan,-1.0000,1.0938,1.0933
    2020-04-13 07:32:00,1.0935,1.0935,1.0935,1.0935,nan,nan,-1.0000,1.0938,1.0933
    2020-04-13 07:33:00,1.0935,1.0935,1.0935,1.0935,nan,nan,-1.0000,1.0938,1.0933
    2020-04-13 07:34:00,1.0935,1.0936,1.0934,1.0935,nan,nan,-1.0000,1.0938,1.0933
    2020-04-13 07:35:00,1.0935,1.0935,1.0934,1.0935,nan,nan,-1.0000,1.0938,1.0933
    2020-04-13 07:36:00,1.0935,1.0937,1.0934,1.0936,nan,nan,-1.0000,1.0938,1.0933
    2020-04-13 07:37:00,1.0936,1.0936,1.0935,1.0936,nan,nan,-1.0000,1.0938,1.0933
    ...
    2020-04-13 12:04:00,1.0935,1.0935,1.0935,1.0935,nan,nan,1.0000,1.0935,1.0925
    2020-04-13 12:05:00,1.0935,1.0936,1.0935,1.0936,nan,nan,1.0000,1.0936,1.0925
    2020-04-13 12:06:00,1.0936,1.0936,1.0936,1.0936,nan,nan,1.0000,1.0936,1.0925
    2020-04-13 12:07:00,1.0936,1.0937,1.0936,1.0936,nan,nan,1.0000,1.0937,1.0925
    2020-04-13 12:08:00,1.0936,1.0936,1.0936,1.0936,nan,nan,1.0000,1.0937,1.0925
    2020-04-13 12:09:00,1.0936,1.0936,1.0936,1.0936,nan,nan,1.0000,1.0937,1.0925
    2020-04-13 12:10:00,1.0936,1.0937,1.0936,1.0936,nan,nan,1.0000,1.0937,1.0925
    2020-04-13 12:11:00,1.0936,1.0937,1.0936,1.0936,nan,nan,1.0000,1.0937,1.0925
    2020-04-13 12:12:00,1.0936,1.0938,1.0936,1.0937,nan,nan,1.0000,1.0938,1.0925
    2020-04-13 12:13:00,1.0937,1.0937,1.0936,1.0936,nan,nan,1.0000,1.0938,1.0925
    2020-04-13 12:14:00,1.0936,1.0936,1.0936,1.0936,nan,nan,1.0000,1.0938,1.0925
    2020-04-13 12:15:00,1.0936,1.0936,1.0936,1.0936,nan,nan,1.0000,1.0938,1.0925
    2020-04-13 12:16:00,1.0936,1.0936,1.0936,1.0936,nan,nan,1.0000,1.0938,1.0925
    2020-04-13 12:17:00,1.0936,1.0936,1.0936,1.0936,nan,nan,1.0000,1.0938,1.0925
    2020-04-13 12:18:00,1.0936,1.0936,1.0936,1.0936,nan,nan,1.0000,1.0938,1.0925
    2020-04-13 12:19:00,1.0936,1.0936,1.0936,1.0936,nan,nan,1.0000,1.0938,1.0925
    2020-04-13 12:20:00,1.0936,1.0939,1.0936,1.0938,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:21:00,1.0938,1.0938,1.0938,1.0938,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:22:00,1.0938,1.0938,1.0938,1.0938,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:23:00,1.0938,1.0938,1.0937,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:24:00,1.0937,1.0937,1.0937,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:25:00,1.0937,1.0938,1.0937,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:26:00,1.0937,1.0938,1.0937,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:27:00,1.0937,1.0937,1.0937,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:28:00,1.0937,1.0937,1.0936,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:29:00,1.0937,1.0937,1.0936,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:30:00,1.0937,1.0937,1.0935,1.0935,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:31:00,1.0935,1.0938,1.0935,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:32:00,1.0937,1.0938,1.0937,1.0938,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:33:00,1.0938,1.0939,1.0938,1.0939,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:34:00,1.0939,1.0939,1.0938,1.0938,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:35:00,1.0938,1.0938,1.0937,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:36:00,1.0937,1.0937,1.0936,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:37:00,1.0936,1.0937,1.0936,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:38:00,1.0936,1.0937,1.0936,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:39:00,1.0937,1.0937,1.0937,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:40:00,1.0937,1.0937,1.0937,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:41:00,1.0937,1.0937,1.0937,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:42:00,1.0937,1.0937,1.0937,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:43:00,1.0937,1.0938,1.0937,1.0938,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:44:00,1.0938,1.0938,1.0937,1.0938,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:45:00,1.0938,1.0938,1.0937,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:46:00,1.0937,1.0937,1.0936,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:47:00,1.0936,1.0936,1.0935,1.0935,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:48:00,1.0935,1.0936,1.0935,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:49:00,1.0936,1.0938,1.0936,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:50:00,1.0937,1.0938,1.0936,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:51:00,1.0936,1.0938,1.0936,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:52:00,1.0937,1.0937,1.0936,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:53:00,1.0936,1.0936,1.0936,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:54:00,1.0936,1.0936,1.0935,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:55:00,1.0936,1.0936,1.0936,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:56:00,1.0936,1.0937,1.0936,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:57:00,1.0937,1.0937,1.0936,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:58:00,1.0936,1.0938,1.0935,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 12:59:00,1.0937,1.0937,1.0937,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:00:00,1.0937,1.0937,1.0935,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:01:00,1.0936,1.0936,1.0935,1.0935,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:02:00,1.0935,1.0935,1.0934,1.0935,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:03:00,1.0935,1.0935,1.0934,1.0934,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:04:00,1.0934,1.0935,1.0933,1.0935,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:05:00,1.0935,1.0936,1.0935,1.0935,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:06:00,1.0935,1.0935,1.0935,1.0935,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:07:00,1.0935,1.0935,1.0935,1.0935,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:08:00,1.0935,1.0936,1.0935,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:09:00,1.0936,1.0937,1.0936,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:10:00,1.0936,1.0936,1.0936,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:11:00,1.0936,1.0937,1.0936,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:12:00,1.0936,1.0936,1.0936,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:13:00,1.0936,1.0937,1.0936,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:14:00,1.0937,1.0937,1.0936,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:15:00,1.0936,1.0937,1.0936,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:16:00,1.0936,1.0937,1.0936,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:17:00,1.0937,1.0937,1.0936,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:18:00,1.0937,1.0937,1.0937,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:19:00,1.0937,1.0937,1.0936,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:20:00,1.0936,1.0936,1.0936,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:21:00,1.0936,1.0936,1.0935,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:22:00,1.0936,1.0936,1.0935,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:23:00,1.0936,1.0936,1.0935,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:24:00,1.0936,1.0936,1.0935,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:25:00,1.0936,1.0936,1.0936,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:26:00,1.0936,1.0936,1.0936,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:27:00,1.0936,1.0936,1.0936,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:28:00,1.0936,1.0936,1.0936,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:29:00,1.0936,1.0937,1.0936,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:30:00,1.0937,1.0937,1.0937,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:31:00,1.0937,1.0937,1.0936,1.0936,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:32:00,1.0936,1.0937,1.0936,1.0937,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:33:00,1.0937,1.0938,1.0937,1.0938,nan,nan,1.0000,1.0939,1.0925
    2020-04-13 13:34:00,1.0938,1.0940,1.0938,1.0940,nan,nan,1.0000,1.0940,1.0925
    2020-04-13 13:35:00,1.0940,1.0940,1.0938,1.0938,nan,nan,1.0000,1.0940,1.0925
    2020-04-13 13:36:00,1.0938,1.0939,1.0938,1.0939,nan,nan,1.0000,1.0940,1.0925
    2020-04-13 13:37:00,1.0939,1.0939,1.0938,1.0939,nan,nan,1.0000,1.0940,1.0925
    2020-04-13 13:38:00,1.0939,1.0939,1.0938,1.0939,nan,nan,1.0000,1.0940,1.0925
    2020-04-13 13:39:00,1.0939,1.0940,1.0939,1.0940,nan,nan,1.0000,1.0940,1.0925
    2020-04-13 13:40:00,1.0940,1.0940,1.0940,1.0940,nan,nan,1.0000,1.0940,1.0925
    2020-04-13 13:41:00,1.0940,1.0940,1.0939,1.0939,nan,nan,1.0000,1.0940,1.0925
    2020-04-13 13:42:00,1.0939,1.0939,1.0939,1.0939,nan,nan,1.0000,1.0940,1.0925
    2020-04-13 13:43:00,1.0939,1.0940,1.0939,1.0939,nan,nan,1.0000,1.0940,1.0925
    2020-04-13 13:44:00,1.0939,1.0939,1.0939,1.0939,nan,nan,1.0000,1.0940,1.0925
    2020-04-13 13:45:00,1.0939,1.0939,1.0939,1.0939,nan,nan,1.0000,1.0940,1.0925
    2020-04-13 13:46:00,1.0939,1.0939,1.0939,1.0939,nan,nan,1.0000,1.0940,1.0925
    2020-04-13 13:47:00,1.0939,1.0939,1.0938,1.0939,nan,nan,1.0000,1.0940,1.0925
    2020-04-13 13:48:00,1.0939,1.0939,1.0938,1.0939,nan,nan,1.0000,1.0940,1.0925
    2020-04-13 13:49:00,1.0939,1.0939,1.0939,1.0939,nan,nan,1.0000,1.0940,1.0925
    2020-04-13 13:50:00,1.0939,1.0939,1.0938,1.0939,nan,nan,1.0000,1.0940,1.0925
    2020-04-13 13:51:00,1.0939,1.0939,1.0939,1.0939,nan,nan,1.0000,1.0940,1.0925
    2020-04-13 13:52:00,1.0939,1.0939,1.0936,1.0936,nan,nan,1.0000,1.0940,1.0925
    2020-04-13 13:53:00,1.0936,1.0938,1.0936,1.0937,nan,nan,1.0000,1.0940,1.0925
    2020-04-13 13:54:00,1.0937,1.0937,1.0936,1.0936,nan,nan,1.0000,1.0940,1.0925
    2020-04-13 13:55:00,1.0936,1.0937,1.0936,1.0936,nan,nan,1.0000,1.0940,1.0925
    2020-04-13 13:56:00,1.0936,1.0936,1.0936,1.0936,nan,nan,1.0000,1.0940,1.0925
    2020-04-13 13:57:00,1.0936,1.0936,1.0935,1.0935,nan,nan,1.0000,1.0940,1.0925
    2020-04-13 13:58:00,1.0935,1.0936,1.0934,1.0934,nan,nan,-1.0000,1.0940,1.0934
    2020-04-13 13:59:00,1.0934,1.0934,1.0934,1.0934,nan,nan,-1.0000,1.0940,1.0934
    2020-04-13 14:00:00,1.0934,1.0935,1.0934,1.0935,nan,nan,-1.0000,1.0940,1.0934
    2020-04-13 14:01:00,1.0935,1.0937,1.0934,1.0934,nan,nan,-1.0000,1.0940,1.0934
    2020-04-13 14:02:00,1.0934,1.0935,1.0934,1.0935,nan,nan,-1.0000,1.0940,1.0934
    2020-04-13 14:03:00,1.0935,1.0935,1.0934,1.0934,nan,nan,-1.0000,1.0940,1.0934
    2020-04-13 14:04:00,1.0934,1.0936,1.0934,1.0936,nan,nan,-1.0000,1.0940,1.0934
    2020-04-13 14:05:00,1.0936,1.0937,1.0936,1.0937,nan,nan,-1.0000,1.0940,1.0934
    2020-04-13 14:06:00,1.0937,1.0938,1.0936,1.0938,nan,nan,-1.0000,1.0940,1.0934
    2020-04-13 14:07:00,1.0938,1.0939,1.0938,1.0939,nan,nan,-1.0000,1.0940,1.0934
    2020-04-13 14:08:00,1.0939,1.0939,1.0938,1.0939,nan,nan,-1.0000,1.0940,1.0934
    2020-04-13 14:09:00,1.0939,1.0939,1.0938,1.0939,nan,nan,-1.0000,1.0940,1.0934
    2020-04-13 14:10:00,1.0939,1.0939,1.0938,1.0938,nan,nan,-1.0000,1.0940,1.0934
    2020-04-13 14:11:00,1.0938,1.0939,1.0938,1.0938,nan,nan,-1.0000,1.0940,1.0934
    2020-04-13 14:12:00,1.0938,1.0939,1.0938,1.0938,nan,nan,-1.0000,1.0940,1.0934
    2020-04-13 14:13:00,1.0938,1.0940,1.0938,1.0939,nan,nan,-1.0000,1.0940,1.0934
    2020-04-13 14:14:00,1.0939,1.0940,1.0939,1.0940,nan,nan,1.0000,1.0940,1.0934
    2020-04-13 14:15:00,1.0940,1.0941,1.0940,1.0940,nan,nan,1.0000,1.0941,1.0934
    2020-04-13 14:16:00,1.0940,1.0940,1.0938,1.0939,nan,nan,1.0000,1.0941,1.0934
    2020-04-13 14:17:00,1.0939,1.0940,1.0939,1.0940,nan,nan,1.0000,1.0941,1.0934
    2020-04-13 14:18:00,1.0940,1.0941,1.0940,1.0940,nan,nan,1.0000,1.0941,1.0934
    2020-04-13 14:19:00,1.0940,1.0942,1.0940,1.0942,nan,nan,1.0000,1.0942,1.0934
    2020-04-13 14:20:00,1.0942,1.0942,1.0941,1.0942,nan,nan,1.0000,1.0942,1.0934
    2020-04-13 14:21:00,1.0942,1.0942,1.0942,1.0942,nan,nan,1.0000,1.0942,1.0934
    2020-04-13 14:22:00,1.0942,1.0946,1.0942,1.0946,nan,nan,1.0000,1.0946,1.0934
    2020-04-13 14:23:00,1.0946,1.0947,1.0945,1.0946,nan,nan,1.0000,1.0947,1.0934
    2020-04-13 14:24:00,1.0946,1.0949,1.0945,1.0949,nan,nan,1.0000,1.0949,1.0934
    2020-04-13 14:25:00,1.0949,1.0950,1.0947,1.0947,nan,nan,1.0000,1.0950,1.0934
    2020-04-13 14:26:00,1.0947,1.0948,1.0946,1.0947,nan,nan,1.0000,1.0950,1.0934
    2020-04-13 14:27:00,1.0947,1.0950,1.0947,1.0950,nan,nan,1.0000,1.0950,1.0934
    2020-04-13 14:28:00,1.0950,1.0951,1.0949,1.0951,nan,nan,1.0000,1.0951,1.0934
    2020-04-13 14:29:00,1.0951,1.0951,1.0948,1.0949,nan,nan,1.0000,1.0951,1.0934
    2020-04-13 14:30:00,1.0949,1.0950,1.0947,1.0950,nan,nan,1.0000,1.0951,1.0934
    2020-04-13 14:31:00,1.0950,1.0950,1.0949,1.0950,nan,nan,1.0000,1.0951,1.0934
    2020-04-13 14:32:00,1.0950,1.0950,1.0949,1.0949,nan,nan,1.0000,1.0951,1.0934
    2020-04-13 14:33:00,1.0949,1.0949,1.0947,1.0947,nan,nan,1.0000,1.0951,1.0934
    2020-04-13 14:34:00,1.0947,1.0947,1.0945,1.0946,nan,nan,1.0000,1.0951,1.0934
    2020-04-13 14:35:00,1.0946,1.0947,1.0944,1.0947,nan,nan,1.0000,1.0951,1.0934
    2020-04-13 14:36:00,1.0947,1.0948,1.0946,1.0947,nan,nan,1.0000,1.0951,1.0934
    2020-04-13 14:37:00,1.0947,1.0951,1.0947,1.0951,nan,nan,1.0000,1.0951,1.0934
    2020-04-13 14:38:00,1.0951,1.0958,1.0950,1.0957,nan,nan,1.0000,1.0958,1.0934
    2020-04-13 14:39:00,1.0957,1.0960,1.0955,1.0960,nan,nan,1.0000,1.0960,1.0934
    2020-04-13 14:40:00,1.0960,1.0962,1.0957,1.0958,nan,nan,1.0000,1.0962,1.0934
    2020-04-13 14:41:00,1.0958,1.0961,1.0958,1.0961,nan,nan,1.0000,1.0962,1.0934
    2020-04-13 14:42:00,1.0961,1.0963,1.0961,1.0963,nan,nan,1.0000,1.0963,1.0934
    ***** DATA NOTIFY: LIVE
    2020-04-13 14:43:00,1.0963,1.0964,1.0961,1.0962,nan,nan,1.0000,1.0964,1.0934
    
    

    Thanks!



  • Hi Niko,
    Is it possible to help me take a look at multi time frames?

    When lines drawn does not connect peak to peak but has a horizontal portion.

    class BaseStrategy(bt.Strategy):  # Strategy is a lines object
        params = dict(order_percentage=0.25, ticker='ORA', zzperiod=2, retrace1=1.75, retrace2=2.5, minbars=2)
        parent_order = None  # default value for a potential order
        stoploss = None
    
        def __init__(self):
            self.order = None  # To keep track of pending orders, init with None
            self.buyprice = None
            self.buycomm = None
            self.bar_executed = None
            self.size = None
            self.lendata1 = 0
            # self.broker.set_coc(True)
            self.zz = ZigZag(self.data, period=self.p.zzperiod, retrace=self.p.retrace1, minbars=self.p.minbars)
            self.zz1 = ZigZag(self.data1, period=self.p.zzperiod, retrace=self.p.retrace2, minbars=self.p.minbars)
    
        def nextstart(self):
            self.lendata1 = len(self.data1)
            super(BaseStrategy, self).nextstart()
    
        def next(self):
    
            txt = list()
            txt.append('Data0')
            txt.append('{}'.format(len(self.data0)))
            txt.append('{}'.format(self.data.datetime.datetime(0)))
            txt.append('{}'.format(self.data.open[0]))
            txt.append('{}'.format(self.data.high[0]))
            txt.append('{}'.format(self.data.low[0]))
            txt.append('{}'.format(self.data.close[0]))
            txt.append('{}'.format(self.zz.l.trend[0]))
            print(', '.join(txt))
    
            if len(self.datas) > 1 and len(self.data1):
                txt = list()
                txt.append('Data1')
                txt.append('{}'.format(len(self.data1)))
                txt.append('{}'.format(self.data1.datetime.datetime(0)))
                txt.append('{}'.format(self.data1.open[0]))
                txt.append('{}'.format(self.data1.high[0]))
                txt.append('{}'.format(self.data1.low[0]))
                txt.append('{}'.format(self.data1.close[0]))
                txt.append('{}'.format(self.zz1.l.trend[0]))
                print(', '.join(txt))
    
            if len(self.data1) > self.lendata1:
                print(len(self.data1))
                self.lendata1 += 1
                print(self.lendata1)
                print('new higher tf bar')
    
    def main():
        cerebro = bt.Cerebro()  
        cerebro.addstrategy(BaseStrategy)
    
        df = pd.read_csv('datas\\EURUSD2019.csv', sep=',', header=0, index_col=0, parse_dates=True)
        
        # Create a Data Feed and add to cerebro
        data = bt.feeds.PandasData(dataname=df, timeframe=bt.TimeFrame.Minutes, compression=1)
        cerebro.adddata(data)
    
        cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=480, name='data0')
        strat = cerebro.run(maxcpus=3)
    
        cerebro.plot(style='candlestick', fmt_x_ticks='%d-%b %H:%M', fmt_x_data='%d-%b %H:%M', volume=False)
    
    if __name__ == '__main__':
        main()
    
    


  • Hi.
    You have a very big value of retrace param, try:
    retrace1=0.25
    retrace2=0.125


Log in to reply
 

});