Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Help with creating a method

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Help with creating a method

    Hello,
    I have a bit of code that I'm having to rewrite many times so I thought I'd create a method to save time. Unfortunately the desired outcome of this bit of code takes several bars to develop and with the OnBarUpdate() the method gets called on every bar. Is there a way to allow a method to keep working though consecutive bars?

    public void CrossingThreshold_Down(BoolSeries cond_1, IDataSeries indi_2)
    {
    bool abc_Down_Peak_1 = false;
    bool abc_Down_Valley_1 = false;
    bool abc_Down_Peak_2 = false;
    int abc_Down_Peak_1_Value_Bar = 0;
    double abc_Down_Peak_1_Value = 0;
    bool abc_Down_CanCrossThreshold = false;
    int abc_Down_Valley_1_Value_Bar = 0;
    double abc_Down_Valley_1_Value = 0;
    double abc_Down_Valley_Threshold = 0;
    int abc_Down_Peak_2_Value_Bar = 0;
    double abc_Down_Peak_2_Value = 0;

    if(cond_1[0] && Finding_Peaks(indi_2[2], indi_2[1], indi_2[0])){
    DrawDiamond("Stoch Peak 1 " + CurrentBar, true, 1, High[0] + .3, Color.Gold);
    abc_Down_Peak_1 = true;
    abc_Down_Peak_1_Value_Bar = HighestBar(indi_2, 10);
    abc_Down_Peak_1_Value = indi_2[abc_Down_Peak_1_Value_Bar];
    abc_Down_CanCrossThreshold = true;
    }
    if(abc_Down_CanCrossThreshold && CrossBelow(indi_2,abc_Down_Valley_Threshold, 1)){
    abc_Down_CanCrossThreshold = false;
    DrawDiamond("Cross Threshold Down " + CurrentBar, true, 0, Low[0] - .2, Color.BurlyWood);
    }
    if(abc_Down_Peak_1 && Finding_Valleys(indi_2[2], indi_2[1], indi_2[0])){
    DrawDiamond("Stoch Valley 1 " + CurrentBar, true, 0, Low[0] - .3, Color.AntiqueWhite);
    abc_Down_Peak_1 = false;
    abc_Down_Valley_1 = true;
    abc_Down_Valley_1_Value_Bar = LowestBar(indi_2, 2);
    abc_Down_Valley_1_Value = indi_2[abc_Down_Valley_1_Value_Bar];
    abc_Down_Valley_Threshold = abc_Down_Valley_1_Value;
    DrawText("Threshold Level " + CurrentBar, " " + abc_Down_Valley_Threshold, 0, Low[0] - .25, Color.Black);
    }
    if(abc_Down_Valley_1 && Finding_Peaks(indi_2[2], indi_2[1], indi_2[0])){
    DrawDiamond("Stoch Peak 2 " + CurrentBar, true, 0, High[0] + .3, Color.Black);
    abc_Down_Valley_1 = false;
    abc_Down_Peak_2 = true;
    abc_Down_Peak_2_Value_Bar = HighestBar(indi_2, 2);
    abc_Down_Peak_2_Value = indi_2[abc_Down_Peak_2_Value_Bar];
    }

    }

    I thought that I could use a series of loops to check for the past conditions on every bar but I wonder about the performance of the program at that point.
    P.S. What happened to the nice code block feature in the text window? (Choosing the ## to insert code into).
    Thanks.
    Last edited by CaptainAmericaXX; 10-24-2018, 07:32 PM.

    #2
    Your question seems to be a bit off. You describe the quintessential way in which all stream editors work, on all bars consecutively, then ask a question: "Is there a way to allow a method to keep working though consecutive bars?"

    All code in OnBarUpdate() works on consecutive bars. What am I confused about?

    Comment


      #3
      Hello CaptainAmericaXX,

      Thanks for your post.

      You can control processing your method when you have the required number of bars loaded. For example, in OnBarUpdate():

      if (CurrentBar > 20) CrossingThreshold_Down(BoolSeries cond_1, IDataSeries indi_2); // call the method after 20 bars loaded

      Alternately you can perform a similar check in your method and just return if not enough bars loaded.

      Paul H.NinjaTrader Customer Service

      Comment


        #4
        koganam and PaulH thank you for responding. I wasn't very clear with my question. I'll be as clear as I know how.
        This bit of code usually takes 3 to 11 bars to get the final result. First, what I call a "peak" is found on the indicator. A few bars later a "valley" will form and I save that value. A few bars later a final "peak" will form. Once the 2nd peak forms I look for the indicator to cross below the valley value. This works just fine by just using if statements in OnBarUpdate(), but when I tried to turn this into a method nothing works past the first if statement in the method because the OnBarUpdate() is called on every new bar. The other 3 steps are never true. So looking at the chart I only get gold diamonds at each indicator peak.
        What I'm wanting to do probably can't be turned into a method. I need to write that same bit of code for several different indicators and I'm trying to cut down on the amount of typing its taking.

        Comment


          #5
          Hello CaptainAmericaXX ,

          Thanks for your reply.

          It is a bit hard to follow what you are doing but if you are calling this on every OBU then clearly the issue is you are declaring and resetting your method variables every time. In your code where this was working did you declare and reset the variables more than once or reset after some condition?

          If you were only declaring them once and resetting them once then perhaps you need to remove them from the method and create them at the class level.

          Also, on your question, "P.S. What happened to the nice code block feature in the text window? (Choosing the ## to insert code into)." In the panel header, on the right-hand side, click the Icon with the underlined A to expose a formatting header which includes the code#
          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by CaptainAmericaXX View Post
            Hello,
            I have a bit of code that I'm having to rewrite many times so I thought I'd create a method to save time. Unfortunately the desired outcome of this bit of code takes several bars to develop and with the OnBarUpdate() the method gets called on every bar. Is there a way to allow a method to keep working though consecutive bars?

            public void CrossingThreshold_Down(BoolSeries cond_1, IDataSeries indi_2)
            {
            bool abc_Down_Peak_1 = false;
            bool abc_Down_Valley_1 = false;
            bool abc_Down_Peak_2 = false;
            int abc_Down_Peak_1_Value_Bar = 0;
            double abc_Down_Peak_1_Value = 0;
            bool abc_Down_CanCrossThreshold = false;
            int abc_Down_Valley_1_Value_Bar = 0;
            double abc_Down_Valley_1_Value = 0;
            double abc_Down_Valley_Threshold = 0;
            int abc_Down_Peak_2_Value_Bar = 0;
            double abc_Down_Peak_2_Value = 0;

            if(cond_1[0] && Finding_Peaks(indi_2[2], indi_2[1], indi_2[0])){
            DrawDiamond("Stoch Peak 1 " + CurrentBar, true, 1, High[0] + .3, Color.Gold);
            abc_Down_Peak_1 = true;
            abc_Down_Peak_1_Value_Bar = HighestBar(indi_2, 10);
            abc_Down_Peak_1_Value = indi_2[abc_Down_Peak_1_Value_Bar];
            abc_Down_CanCrossThreshold = true;
            }
            if(abc_Down_CanCrossThreshold && CrossBelow(indi_2,abc_Down_Valley_Threshold, 1)){
            abc_Down_CanCrossThreshold = false;
            DrawDiamond("Cross Threshold Down " + CurrentBar, true, 0, Low[0] - .2, Color.BurlyWood);
            }
            if(abc_Down_Peak_1 && Finding_Valleys(indi_2[2], indi_2[1], indi_2[0])){
            DrawDiamond("Stoch Valley 1 " + CurrentBar, true, 0, Low[0] - .3, Color.AntiqueWhite);
            abc_Down_Peak_1 = false;
            abc_Down_Valley_1 = true;
            abc_Down_Valley_1_Value_Bar = LowestBar(indi_2, 2);
            abc_Down_Valley_1_Value = indi_2[abc_Down_Valley_1_Value_Bar];
            abc_Down_Valley_Threshold = abc_Down_Valley_1_Value;
            DrawText("Threshold Level " + CurrentBar, " " + abc_Down_Valley_Threshold, 0, Low[0] - .25, Color.Black);
            }
            if(abc_Down_Valley_1 && Finding_Peaks(indi_2[2], indi_2[1], indi_2[0])){
            DrawDiamond("Stoch Peak 2 " + CurrentBar, true, 0, High[0] + .3, Color.Black);
            abc_Down_Valley_1 = false;
            abc_Down_Peak_2 = true;
            abc_Down_Peak_2_Value_Bar = HighestBar(indi_2, 2);
            abc_Down_Peak_2_Value = indi_2[abc_Down_Peak_2_Value_Bar];
            }

            }

            I thought that I could use a series of loops to check for the past conditions on every bar but I wonder about the performance of the program at that point.
            P.S. What happened to the nice code block feature in the text window? (Choosing the ## to insert code into).
            Thanks.
            You have declared 11 variables inside the method. Each time the method is called, they will be reset, because they will be redeclared.

            Declare those variables outside the method, in class scope (because they are required to maintain their value between calls), and then after you have completed whatever operation you want to do with them, you probably want to reset them, so that you can start over when the necessary conditions occur afresh.

            Comment


              #7
              Hi CaptainAmericaXX
              when the fast SMIAMB (red line) crosses below a previous valley, while it is lower than the slow SMIAMB (yellow line) AND while this happens on the overbought region, I find this signal as very reliable. (trying to exemplify on chart attached - and - will see if it is going to work also these hours).
              Unfortunately I never been able to get even close to building an indicator detecting this signal. As far as I can understand you are trying to do something similar. Please would explain a little bit more in detail how you are trying to do that? Thank you.

              Click image for larger version

Name:	image_51273.png
Views:	514
Size:	35.2 KB
ID:	1037836
              Click image for larger version

Name:	image_51270.png
Views:	536
Size:	108.7 KB
ID:	1037833

              Click image for larger version

Name:	image_51271.png
Views:	514
Size:	160.0 KB
ID:	1037834
              added NQ chart at signal - cross below

              Click image for larger version

Name:	NQ 12-18 (27 Minute) 2018_10_31 (17_05_43).png
Views:	534
Size:	94.1 KB
ID:	1037885



              Last edited by guidoisot; 10-31-2018, 10:18 AM.

              Comment


                #8
                guidoisot,
                I'm sorry this message is so late in coming. I don't believe I've been to support for many months. I'm not a very good programmer, but I hope I can help you.
                I have to say first that I am using NT7. From what your pictures showed we have the same idea. What you need to mark a "valley" with your indicator. I usually do this by:
                Code:
                public bool Finding_Valleys_Indi(IDataSeries indi)
                                        {
                                            double two_bars_ago    = indi[2];
                                            double prev_bar    = indi[1];
                                            double current_bar        = indi[0];
                                            if( prev_bar < current_bar && (two_bars_ago > prev_bar || prev_bar == two_bars_ago))
                                            {
                                                return true;
                                            }
                                            else if(prev_bar == two_bars_ago && current_bar > two_bars_ago)
                                            {
                                                return true;    
                                            }
                                            else
                                            {return false;}    
                                        }
                Then you must get the value of the valley. I do this by:
                Code:
                public double Valley_Value(IDataSeries indi)
                                    {
                                        double indi_Valley_Value    = 0;
                                        if(Finding_Valleys_Indi(indi)){
                                            indi_Valley_Value     = indi[1];
                                            return indi_Valley_Value;
                                        }else
                                            return -1;
                
                                    }
                Once you have the indicator value at the valley you can check if the current value of your indicator has crossed that value.
                I hope that helps.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by wzgy0920, 04-20-2024, 06:09 PM
                2 responses
                27 views
                0 likes
                Last Post wzgy0920  
                Started by wzgy0920, 02-22-2024, 01:11 AM
                5 responses
                32 views
                0 likes
                Last Post wzgy0920  
                Started by wzgy0920, 04-23-2024, 09:53 PM
                2 responses
                49 views
                0 likes
                Last Post wzgy0920  
                Started by Kensonprib, 04-28-2021, 10:11 AM
                5 responses
                193 views
                0 likes
                Last Post Hasadafa  
                Started by GussJ, 03-04-2020, 03:11 PM
                11 responses
                3,235 views
                0 likes
                Last Post xiinteractive  
                Working...
                X