Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    How to Create Binary Switch Signal

    Indicators/Strategies/Analyzers
    3
    4
    47
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • G
      GladE last edited by

      Hello,

      I would like ask support to define a custom indicator that I could not manage to work it properly. I want to create a binary signal based on AverageDirectionalMovementIndex where the value becomes "1" when it crosses down a upper threshold and stay still as 1 unless two other criteria met (Either it may cross down DirectionalMovementIndext strength line or cross up upperthreshold). In such cases, I am expecting the signal turn 0. In my code, I manage to receive "1" signal when ADX cross down uppertreshold but it does not stay 1 unless two other conditions are met. Any suggestions?

      Thanks in advance

      class ADXSellSignal(bt.Indicator):

      lines = ('adxsignal',)
      params = (
          ('Trend_Strength', 20),
          ('ADX_Up_Tresh', 40),
          ('adx_period',7)
          )
      
      def __init__(self):
          
          self.adx=bt.indicators.AverageDirectionalMovementIndex(period=self.params.adx_period)
          self.adxcross=bt.indicators.CrossOver(self.adx,self.params.ADX_Up_Tresh)
          self.adxreset=bt.indicators.CrossDown(self.adx,self.params.Trend_Strength)
      
      def next(self):
          
          self.trigger=0
          
          if self.adxcross[0]<0:
              self.trigger=1
          
          elif self.adxreset[0]>0:
              self.trigger=-1
          
          elif self.adxcross[0]>0:
              self.trigger=-1
          
          self.lines.adxsignal[0]=+ self.trigger[0]
      lekatariba run-out 2 Replies Last reply Reply Quote 0
      • lekatariba
        lekatariba @GladE last edited by

        @glade ![alt text]DAX30H1.png
        NIKK225H1.jpg

        //|                                                   ADXcrosses.mq4 |
        //|                      Copyright © 2004, MetaQuotes Software Corp. |
        //|                                        http://www.metaquotes.net |
        //+------------------------------------------------------------------+
        #property copyright "Copyright © 2004, MetaQuotes Software Corp."
        #property link "http://www.metaquotes.net"
        
        #property indicator_chart_window
        #property indicator_buffers 2
        #property indicator_color1 Blue
        #property indicator_color2 Red
        //---- input parameters
        extern int ADXcrossesPeriod = 14;
        //---- buffers
        double ExtMapBuffer1[];
        double ExtMapBuffer2[];
        //----
        double b4plusdi, b4minusdi, nowplusdi, nowminusdi;
        int    nShift;   
        //+------------------------------------------------------------------+
        //| Custom indicator initialization function                         |
        //+------------------------------------------------------------------+
        int init()
          {
        //---- indicators
            SetIndexStyle(0, DRAW_ARROW, 0, 1);
            SetIndexArrow(0, 233);
            SetIndexBuffer(0, ExtMapBuffer1);
        //----
            SetIndexStyle(1, DRAW_ARROW, 0, 1);
            SetIndexArrow(1, 234);
            SetIndexBuffer(1, ExtMapBuffer2);
        //---- name for DataWindow and indicator subwindow label
            IndicatorShortName("ADXcrosses(" + ADXcrossesPeriod + ")");
            SetIndexLabel(0, "ADXcrUp");
            SetIndexLabel(1, "ADXcrDn"); 
        //----
            switch(Period())
              {
                case     1: nShift = 1;   break;    
                case     5: nShift = 3;   break; 
                case    15: nShift = 5;   break; 
                case    30: nShift = 10;  break; 
                case    60: nShift = 15;  break; 
                case   240: nShift = 20;  break; 
                case  1440: nShift = 80;  break; 
                case 10080: nShift = 100; break; 
                case 43200: nShift = 200; break;               
              }
        //----
            return(0);
          }
        //+------------------------------------------------------------------+
        //| Custor indicator deinitialization function                       |
        //+------------------------------------------------------------------+
        int deinit()
          {
        //----
            return(0);
          }
        //+------------------------------------------------------------------+
        //| Custom indicator iteration function                              |
        //+------------------------------------------------------------------+
        int start()
          {
            int limit;
            int counted_bars = IndicatorCounted();
        //---- check for possible errors
            if(counted_bars < 0) 
                return(-1);
        //---- last counted bar will be recounted
            if(counted_bars > 0) 
                counted_bars--;
            limit = Bars - counted_bars;
        //----
            for(int i = 0; i < limit; i++)
              {
                b4plusdi = iADX(NULL, 0, ADXcrossesPeriod, PRICE_CLOSE, MODE_PLUSDI, i - 1);
                nowplusdi = iADX(NULL, 0, ADXcrossesPeriod, PRICE_CLOSE, MODE_PLUSDI, i);
                b4minusdi = iADX(NULL, 0, ADXcrossesPeriod, PRICE_CLOSE, MODE_MINUSDI, i - 1);
                nowminusdi = iADX(NULL, 0, ADXcrossesPeriod, PRICE_CLOSE, MODE_MINUSDI, i);  
                //----
                if(b4plusdi > b4minusdi && nowplusdi < nowminusdi)
                    ExtMapBuffer1[i] = Low[i] - nShift*Point;
                //----
                if(b4plusdi < b4minusdi && nowplusdi > nowminusdi)
                    ExtMapBuffer2[i] = High[i] + nShift*Point;
              }
        //----
            return(0);
          }
        //+------------------------------------------------------------------+
        
        
        1 Reply Last reply Reply Quote 0
        • run-out
          run-out @GladE last edited by

          @glade To start you set up a line with 0 for above and 1 for below the ADX_Up_Thresh. You could try something like this in your init:

          self.adxcross=self.adx < self.params.ADX_Up_Tresh
          

          Then you create a line for the two other conditions.

          self.cancel_adx = bt.Or(self.adx < self.params.Trend_Strength, self.adx > self.params.ADX_Up_Thresh)
          self.adxsignal = bt.If(self.cancel_adx,  0, self.adx)
          

          I clearly haven't tested this so hopefully this will work, or at least get you on the right track. Good luck.

          run-out 1 Reply Last reply Reply Quote 1
          • run-out
            run-out @run-out last edited by

            @run-out said in How to Create Binary Switch Signal:

            self.adxsignal = bt.If(self.cancel_adx, 0, self.adx)

            I think I made a mistake ane the last item self.adx should be self.adxcross.

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
            $(document).ready(function () { app.coldLoad(); }); }