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

basic for loop

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

    #31
    Hello PaulMohn,

    Thanks for your reply.

    So your chart bars are 1 minute and you have one added data series which is a 1 tick bid series of the chart instrument.

    When the 1 minute bars call OnBarUpdate this would be BarsInProgress == 0. The current bar data would be access by Opens[0][0], Highs[0][0], Lows[0][0], and Closes[0][0]. The previous bar would be accessed by Opens[0][1], Highs[0][1], Lows[0][1], and Closes[0][1].

    To help with some of the concepts that may not be clear I have created a short example. I applied this to a futures chart of just 1 day of data to keep the amount of tick information down.

    It will add a 1 tick bid series.

    I created a pretend entry and exit by using the bar numbers from the chart. From charts bars 21 - 30 was the duration of the "trade" and during that period I pulled each tick and checked for the max value. On the 30th bar, I print the value found and draw a vertical line on the chart to show where it occurred.

    I hope this helps.


    ExampleMaxValue.zip




    Paul H.NinjaTrader Customer Service

    Comment


      #32
      Yes that helps a lot thank you. That's a great example. Taking some time to grasp the new concepts with the CurrentBars special checks, and the narrow bool use and special 21 - 30 testing interval demonstration idea.
      It seems with your example I wouldn't need to save the bar numbers for the MAX() function use and could use instead the myMaxBid2 variable directly (not sure will test tomorrow) but the example is great to show how to do save the bar number too with the Draw.VerticalLine example.
      I've been thinking about it and it seems I could even get the EntryBar single first Bar number/index by adding for example a EntryBar = CurrentBars[1]; action inside the bool/doitonce2 if statement scope and retrieve it that way (not sure will test tomorrow).
      I'll test tomorrow asap. Many thanks again, you've been very helpful. Very much appreciate the great illustration.

      Comment


        #33
        Hello Paul your code is working very well as intended. Thank you very much for the great insights and effective help!

        I got the isolated EntryBar (A0, A1, A2) in the prints (won't need it for this project but it's good to know how to do it thanks!)

        I also checked the whole execution with the prints below to understand exactly how it was processed

        A0 (EntryBar before bool doitonce2 if statement executes)
        A1 (EntryBar while/just after bool doitonce2 if statement executes)
        A2 (EntryBar after Max Bid code)

        B0 (myBid2 after doitonce2 and before Closes[1][0]; assignment executes)
        B1 (myBid2 after Closes[1][0]; assignment and before Max Bid code executes)
        B2 (myBid2 while/just after Max Bid code executes)
        B3 (myBid2 after and out Max Bid code)

        C0 (myMaxBid2 after doitonce2 and before Closes[1][0]; assignment executes)
        C1 (myMaxBid2 after Closes[1][0]; assignment and before Max Bid code executes)
        C2 (myMaxBid2 while/just after Max Bid code executes)
        C3 (myMaxBid2 after and out Max Bid code)

        D0 (peakBar after and out Max Bid code)

        Code:
        [B]A0 14/01/2022 12:23:00 0[/B]
        [B]A1 14/01/2022 12:23:00 9[/B]
        B0 14/01/2022 12:23:00 0
        C0 14/01/2022 12:23:00 0
        B1 14/01/2022 12:23:00 83.2
        C1 14/01/2022 12:23:00 0
        B2 14/01/2022 12:23:00 83.2
        C2 14/01/2022 12:23:00 83.2
        A2 14/01/2022 12:23:00 9
        B3 14/01/2022 12:23:00 83.2
        C3 14/01/2022 12:23:00 83.2
        D0 14/01/2022 12:23:00 9
        [B]A0 14/01/2022 12:23:00 9[/B]
        B0 14/01/2022 12:23:00 83.2
        C0 14/01/2022 12:23:00 83.2
        B1 14/01/2022 12:23:00 83.2
        C1 14/01/2022 12:23:00 83.2
        [B]A2 14/01/2022 12:23:00 9[/B]
        B3 14/01/2022 12:23:00 83.2
        C3 14/01/2022 12:23:00 83.2
        D0 14/01/2022 12:23:00 9
        [B]A0 14/01/2022 12:23:00 9[/B]
        B0 14/01/2022 12:23:00 83.2
        C0 14/01/2022 12:23:00 83.2
        B1 14/01/2022 12:23:00 83.21
        C1 14/01/2022 12:23:00 83.2
        B2 14/01/2022 12:23:00 83.21
        C2 14/01/2022 12:23:00 83.21
        [B]A2 14/01/2022 12:23:00 9[/B]
        B3 14/01/2022 12:23:00 83.21
        C3 14/01/2022 12:23:00 83.21
        D0 14/01/2022 12:23:00 20
        [B]A0 14/01/2022 12:23:00 9[/B]
        B0 14/01/2022 12:23:00 83.21
        C0 14/01/2022 12:23:00 83.21
        B1 14/01/2022 12:23:00 83.21
        C1 14/01/2022 12:23:00 83.21
        [B]A2 14/01/2022 12:23:00 9[/B]
        B3 14/01/2022 12:23:00 83.21
        C3 14/01/2022 12:23:00 83.21
        D0 14/01/2022 12:23:00 20
        [B]A0 14/01/2022 12:23:00 9[/B]
        B0 14/01/2022 12:23:00 83.21
        C0 14/01/2022 12:23:00 83.21
        B1 14/01/2022 12:23:00 83.21
        C1 14/01/2022 12:23:00 83.21
        [B]A2 14/01/2022 12:23:00 9[/B]
        B3 14/01/2022 12:23:00 83.21
        C3 14/01/2022 12:23:00 83.21
        D0 14/01/2022 12:23:00 20
        [B]A0 14/01/2022 12:23:00 9[/B]
        B0 14/01/2022 12:23:00 83.21
        C0 14/01/2022 12:23:00 83.21
        B1 14/01/2022 12:23:00 83.22
        C1 14/01/2022 12:23:00 83.21
        B2 14/01/2022 12:23:00 83.22
        C2 14/01/2022 12:23:00 83.22
        [B]A2 14/01/2022 12:23:00 9[/B]
        B3 14/01/2022 12:23:00 83.22
        C3 14/01/2022 12:23:00 83.22
        D0 14/01/2022 12:23:00 25
        [B]A0 14/01/2022 12:23:00 9[/B]
        B0 14/01/2022 12:23:00 83.22
        C0 14/01/2022 12:23:00 83.22
        B1 14/01/2022 12:23:00 83.2
        C1 14/01/2022 12:23:00 83.22
        [B]A2 14/01/2022 12:23:00 9[/B]
        B3 14/01/2022 12:23:00 83.2
        C3 14/01/2022 12:23:00 83.22
        D0 14/01/2022 12:23:00 25
        [B]A0 14/01/2022 12:23:00 9[/B]
        B0 14/01/2022 12:23:00 83.2
        C0 14/01/2022 12:23:00 83.22
        B1 14/01/2022 12:23:00 83.2
        C1 14/01/2022 12:23:00 83.22
        [B]A2 14/01/2022 12:23:00 9[/B]
        B3 14/01/2022 12:23:00 83.2
        C3 14/01/2022 12:23:00 83.22
        D0 14/01/2022 12:23:00 25
        [B]A0 14/01/2022 12:23:00 9[/B]
        B0 14/01/2022 12:23:00 83.2
        C0 14/01/2022 12:23:00 83.22
        B1 14/01/2022 12:23:00 83.19
        C1 14/01/2022 12:23:00 83.22
        [B]A2 14/01/2022 12:23:00 9[/B]
        B3 14/01/2022 12:23:00 83.19
        C3 14/01/2022 12:23:00 83.22
        D0 14/01/2022 12:23:00 25
        [B]A0 14/01/2022 12:23:00 9[/B]
        B0 14/01/2022 12:23:00 83.19
        C0 14/01/2022 12:23:00 83.22
        B1 14/01/2022 12:23:00 83.21
        C1 14/01/2022 12:23:00 83.22
        [B]A2 14/01/2022 12:23:00 9[/B]
        B3 14/01/2022 12:23:00 83.21
        C3 14/01/2022 12:23:00 83.22
        D0 14/01/2022 12:23:00 25
        [B]A0 14/01/2022 12:23:00 9[/B]
        B0 14/01/2022 12:23:00 83.21
        C0 14/01/2022 12:23:00 83.22
        B1 14/01/2022 12:23:00 83.21
        C1 14/01/2022 12:23:00 83.22
        [B]A2 14/01/2022 12:23:00 9[/B]
        B3 14/01/2022 12:23:00 83.21
        C3 14/01/2022 12:23:00 83.22
        D0 14/01/2022 12:23:00 25
        A0 14/01/2022 12:23:00 9
        B0 14/01/2022 12:23:00 83.21
        C0 14/01/2022 12:23:00 83.22
        B1 14/01/2022 12:23:00 83.2
        C1 14/01/2022 12:23:00 83.22
        [B]A2 14/01/2022 12:23:00 9[/B]
        B3 14/01/2022 12:23:00 83.2
        C3 14/01/2022 12:23:00 83.22
        D0 14/01/2022 12:23:00 25
        [B]A0 14/01/2022 12:23:00 9[/B]
        B0 14/01/2022 12:23:00 83.2
        C0 14/01/2022 12:23:00 83.22
        B1 14/01/2022 12:23:00 83.2
        C1 14/01/2022 12:23:00 83.22
        [B]A2 14/01/2022 12:23:00 9[/B]
        B3 14/01/2022 12:23:00 83.2
        C3 14/01/2022 12:23:00 83.22
        D0 14/01/2022 12:23:00 25

        PHP Code:
        namespace NinjaTrader.NinjaScript.Strategies
        {
             public class 
        BidAskIncDecr Strategy
             
        {
                  private 
        int peakBar;
                  private 
        double myMaxBid2;
                  private 
        double myBid2;
                  private 
        bool doitonce2;

                  private 
        double maxBid;

                  private 
        int EntryBar;

                  ...

                  protected 
        override void OnStateChange()
                  {
                       if (
        State == State.SetDefaults)
                       {
                            ...

                            
        IsUnmanaged true;

                            
        doitonce2 true;

                       }
                       else if (
        State == State.Configure)
                       {
                            
        // Add a secondary data series to sync our Secondary Series<double>
                            
        AddDataSeries(nullBarsPeriodType.Tick1MarketDataType.Bid);
                       }
                       else if (
        State == State.DataLoaded)
                       {
                            
        bidValsfrmEntry = new Series<double>(thisMaximumBarsLookBack.Infinite);
                       }
                       else if (
        State == State.Realtime)
                       {
                       if (
        initialExitOrderLong != null)
                            
        initialExitOrderLong GetRealtimeOrder(initialExitOrderLong);
                       }
                  }

                  protected 
        override void OnBarUpdate()
                  {

                     
        //Print(BarsInProgress);


                    
        if (CurrentBars[0] < || CurrentBars[1] < 1)
                        return;

                    if(
        BarsInProgress == || BarsInProgress == 2)
                        return;

                       if ( 
        PositionAccount.MarketPosition == MarketPosition.Long )
                       {
                           Print(
        "A0" " " Time[0] + " " EntryBar);

                           if (
        doitonce2)
                           {
                                 
        EntryBar CurrentBars[1];
                                 
        myMaxBid2 0// only need to set this to 0 once
                                 
        doitonce2 false;
                                 Print(
        "A1" " " Time[0] + " " EntryBar);
                           }

                           Print(
        "B0" " " Time[0] + " " myBid2);
                           Print(
        "C0" " " Time[0] + " " myMaxBid2);

                           
        myBid2 Closes[1][0];

                           Print(
        "B1" " " Time[0] + " " myBid2);
                           Print(
        "C1" " " Time[0] + " " myMaxBid2);

                           if (
        myBid2 myMaxBid2)
                           {
                                 
        myMaxBid2 myBid2;
                                 
        peakBar CurrentBars[1]; // save the bar number
                                 
        Print("B2" " " Time[0] + " " myBid2);
                                 Print(
        "C2" " " Time[0] + " " myMaxBid2);
                           }


                           Print(
        "A2" " " Time[0] + " " EntryBar);
                           Print(
        "B3" " " Time[0] + " " myBid2);
                           Print(
        "C3" " " Time[0] + " " myMaxBid2);
                           Print(
        "D0" " " Time[0] + " " peakBar);
                           ... 

        Simplified further (no prints)
        PHP Code:
        namespace NinjaTrader.NinjaScript.Strategies
        {
             public class 
        BidAskIncDecr Strategy
             
        {
                  private 
        int peakBar;
                  private 
        double myMaxBid2;
                  private 
        double myBid2;
                  private 
        bool doitonce2;

                  private 
        double maxBid;

                  private 
        int EntryBar;

                  ...

                  protected 
        override void OnStateChange()
                  {
                       if (
        State == State.SetDefaults)
                       {
                            ...

                            
        IsUnmanaged true;

                            
        doitonce2 true;

                       }
                       else if (
        State == State.Configure)
                       {
                            
        // Add a secondary data series to sync our Secondary Series<double>
                            
        AddDataSeries(nullBarsPeriodType.Tick1MarketDataType.Bid);
                       }
                       else if (
        State == State.DataLoaded)
                       {
                            
        bidValsfrmEntry = new Series<double>(thisMaximumBarsLookBack.Infinite);
                       }
                       else if (
        State == State.Realtime)
                       {
                       if (
        initialExitOrderLong != null)
                            
        initialExitOrderLong GetRealtimeOrder(initialExitOrderLong);
                       }
                  }

                  protected 
        override void OnBarUpdate()
                  {


                    if (
        CurrentBars[0] < || CurrentBars[1] < 1)
                        return;

                    if(
        BarsInProgress == || BarsInProgress == 2)
                        return;

                       if ( 
        PositionAccount.MarketPosition == MarketPosition.Long )
                       {

                           if (
        doitonce2)
                           {
                                 
        EntryBar CurrentBars[1];
                                 
        myMaxBid2 0// only need to set this to 0 once
                                 
        doitonce2 false;
                           }

                           
        myBid2 Closes[1][0];

                           if (
        myBid2 myMaxBid2)
                           {
                                 
        myMaxBid2 myBid2;
                                 
        peakBar CurrentBars[1]; // save the bar number
                           


        I'll test next with branching the BarsInProgress == 1 and BarsInProgress == 0 as in your example.
        Many thanks again for the great solution and support and help!
        Last edited by PaulMohn; 06-23-2022, 11:01 AM.

        Comment


          #34
          New derived use for later reference
          #regions related to
          DIRECTION LOCK (LONG & SHORT StopLoss Order Rejection Prevention Move Lock)

          20220624 Update A MultiOrders Newer version of this Indicator is available here This previous single Orders version is updated below to reflect some of the newer version features. Newer Version Features Included below: LONG and SHORT StopLoss OCO Move Up/Down Orders Rejection Prevention LONG and SHORT StopLoss OCO Move Up/Down Direction Lock (LONG & SHORT [&#8230;]

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by sastrades, 05-10-2024, 09:59 AM
          3 responses
          54 views
          0 likes
          Last Post rc5781
          by rc5781
           
          Started by guyonabuffalo, Yesterday, 10:01 PM
          2 responses
          18 views
          0 likes
          Last Post guyonabuffalo  
          Started by thumper57, Yesterday, 04:30 PM
          5 responses
          15 views
          0 likes
          Last Post thumper57  
          Started by reynoldsn, 05-10-2024, 07:04 PM
          5 responses
          27 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Started by llanqui, Today, 11:10 AM
          1 response
          16 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Working...
          X