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

Finding local maxima on price chart

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

    Finding local maxima on price chart

    Solved

    Hey,

    I'm new to Ninja Trader scripting. It took me a while to figure out the way things work. If I understand correctly, the code below will draw a the Highs.

    Code:
    protected override void OnStateChange()
            {
                if (State == State.SetDefaults){
    
                    IsOverlay                    = true;
                    AddPlot(Brushes.DarkCyan, "High");
    
                }
            
            }
    
            protected override void OnBarUpdate()
            {
                Value[0]  = High[0];
            }

    I would like to determine the local maxima on the price chart. In order to do that I want to do the following:
    - loop through the bars
    - if the 4 bars at its left and right are lower, set it as a maximum
    - return the array of local maxima

    Thanks in advance,

    Ivan
    Last edited by alphabetacharlie; 05-21-2017, 08:50 AM. Reason: Possible solution

    #2
    Hello Ivan,

    I'm not quite sure i'm able to understand your meaning when you say "if the 4 bars at its left and right are lower".

    Are on each new bar, are you wanting to re-run through the entire history of the bars?

    On each new bar are you wanting to check the bar 4 bars ago to see if the 4 bars previous to it (from 9 bars ago to 5 bars ago) are less than the bar 4 bars ago and that the 4 bars after (from 3 bars ago to 0 bars ago (the currently closed bar)) are also less than the bar 4 bars ago?

    For this you could use HighestBar(). If the HighestBar with a period of 9 is 4, then the bar 4 bars ago is higher than the preceeding 4 bars and following 4 bars.


    On each new bar, if the condition is not true, are you wanting a blank for the plot on that bar? If the condition is not true, do you want to use the previous value for the plot on that bar?

    As an example:
    Code:
    if (CurrentBar < 9)
    return;
    
    if (HighestBar(High, 9) == 4)
    {
    Value[0] = High[4];
    }
    else if (Value.IsValidDataPoint(1))
    {
    Value[0] = Value[1];
    }
    With this code, if the bar 4 bars ago is the highest bar in the last 9 bars, the plot is set to the high of the bar 4 bars ago. If it is not, it is set to the previous value of that plot as long as the previous bar has a value set.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you

      Thank you very much for your help. I think I understand that.

      However I have stepped back a little from ninja trader to understand what I was going after. Basically, I would like to code an indicator that identifies patterns (ratio patterns). My problem is not in coding it, I know a bit of C# and have already coded the software with python. What I don't know is how to program it on ninja trader.

      Let me explain. At first I was surprised at how NT works: the bar ago system confuses me... Because in the system I've made with python it's pretty simple, I've got an array of all prices (o h l c), I run my algorithm on it. It selects the points if they make up a pattern. Then when all is done. I draw all those patterns on the chart.

      Can I work on an array of all prices that are displayed on the chart instead of having to go through onbarupdate() ?

      Thank you Sir, you're really helping me,

      - Ivan

      Comment


        #4
        Hello Ivan,

        Python is a high level language that does not require methods (but can).

        C# is object oriented and your code must be in a method that gets triggered at some time.

        OnBarUpdate() is a method that triggers for each bar on the chart (and for any added series). In general, this is where you would want your logic to go.
        Each time it triggers, there are properties are pointed to that bar.


        CurrentBar tells you the bar number being processed.


        Close[int barsAgo] will hold the close price for the current bar and for every bar previous to this. (The indexes for close are updated on each new bar. 0 will always represent the most recent bar as this would be 0 bars ago.)


        You could create your own method and roll through whatever data exists at the time your method is triggered. You would also need to trigger your method. When would you want this to run?

        As a heads up, the collections are always changing. If you plan on calling bar data from outside of OnBarUpdate, the collections need to be synced just before you call them.

        This can be done with TriggerCustomEvent().


        Or with Bars.Get() methods such as Bars.GetClose().
        Last edited by NinjaTrader_ChelseaB; 04-18-2017, 07:08 AM.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Value[0] and triangles

          Hi there,

          Thank you again for your reply.

          I think I get it now, I should be able to achieve the same redult with C#.
          It just needs a little work.

          I have another question: I know I can store prices in Value and then add them as a plot on the chart. If I get to define triangles during onBarUpdate (), how will I manage to draw them on the chart?

          - Ivan

          Comment


            #6
            Hello Ivan,

            Below is a link to the help guide on AddPlot().


            If desired use the PlotStyle.TriangleUp as the plot style.

            Each time AddPlot() is called, this adds an element to the Values collection. (If there is only one, this can called with Value)



            See here about getting started learning NinjaScript (and c#).
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              triangles

              Ok, it's drawTriangle() I need to be using in order to draw a triangle shape, (not an icon). I read the wiki page. I'll try it out and update you on it.

              Many thanks,

              Ivan

              Comment


                #8
                Hello Ivan,

                You can always draw object, however, I thought you were trying to set the Value of a Plot and have this display as triangles?

                If you want to draw a triangle without a plot you can do this with Draw.Triangle(), Draw.TriangleUp() or Draw.TriangleDown().



                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  It has been a long time since I last been here. I have made some progress thanks to you.

                  Currently I'm able to display all local maxima and minima on the chart.

                  I came upon a little issue. I'm using Series<> objects. Is there a way to store arrays or lists inside those types of objects?

                  Currently I am using Series<> like this:
                  Code:
                  Series<double> maxs;
                  
                  // maxs are; called like so
                  
                  maxs[barAgo];
                  However I would like to store two or more prices in there so that I can do:
                  Code:
                  maxs[barAgo][0] // to access first price of maxs[barAgo]
                  maxs[barAgo][1] // to access second. etc
                  That's why I'm searching to store arrays or lists inside the Series<> object.

                  Comment


                    #10
                    Hi alphabetacharlie,

                    Yes, you can store a list in a series.

                    For example:
                    Code:
                    private Series<List<string>> stringListSeries;
                    Code:
                    stringListSeries = new Series<List<string>>(this);
                    Code:
                    stringListSeries[0] = new List<string>()
                    { "a"+CurrentBar, "b"+CurrentBar, "c"+CurrentBar};
                    
                    Print(stringListSeries[0][0]);
                    Print(stringListSeries[0][1]);
                    Print(stringListSeries[0][2]);
                    Chelsea B.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by frankthearm, Today, 09:08 AM
                    5 responses
                    14 views
                    0 likes
                    Last Post NinjaTrader_Clayton  
                    Started by jeronymite, 04-12-2024, 04:26 PM
                    3 responses
                    43 views
                    0 likes
                    Last Post jeronymite  
                    Started by yertle, Today, 08:38 AM
                    5 responses
                    16 views
                    0 likes
                    Last Post NinjaTrader_BrandonH  
                    Started by adeelshahzad, Today, 03:54 AM
                    3 responses
                    19 views
                    0 likes
                    Last Post NinjaTrader_BrandonH  
                    Started by bill2023, Yesterday, 08:51 AM
                    6 responses
                    27 views
                    0 likes
                    Last Post NinjaTrader_Erick  
                    Working...
                    X