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

BarsInProgress 'if' 'else if' 'else'

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

    BarsInProgress 'if' 'else if' 'else'

    I am trying to verify my logic in the sense of what works and what doesnt, and many times the examples in the F1 arent what I need ... so ... as a test example and not real code, do each of the examples actually go long/short when the BIP and Closes conditions are true ... OR ... will only ONE of the sections become an entry even if BIP=1 and BIP=2 etc happen to close at the same moment which could easily happen on time bars ??

    Is the last conditional line correct with just a 'else' instead of another 'else if' ??

    For this example/question, there is no if (BarsInProgress != 0) return; && no if (BarsInProgress == 0) !!!

    protected override void OnBarUpdate()
    {
    if (BarsInProgress == 1 && Closes[1][1] < Closes[1][0])
    {
    EnterLong();
    Print("Went Long BIP=1 with Close above Close")
    }
    //
    else if (BarsInProgress == 2 && Closes[2][1] > Closes[2][0])
    {
    EnterShort();
    Print("Went Short BIP=2 with Close below Close")
    }
    //
    else if (BarsInProgress == 0 && Closes[0][1] < Closes[0][0])
    {
    EnterLong();
    Print("Went Long BIP=0 with Close above Close")
    }
    //
    else (BarsInProgress == 0 && Closes[0][2] > Closes[0][1])
    {
    EnterShort();
    Print("Went Short BIP=0 with Close below Close")
    }
    }
    Thanks,
    TJ

    #2


    I'm no code guru but I think the best practice is to only use if/else if ladders if each of the conditions are mutually exclusive.

    If you want your example to conform to that, perhaps add a != MarketPosition.Flat condition?

    If you want the logic to execute on each true statement, use if statements only.

    In a if/else ladder, the final else statement is used for a default action. In your case it looks like it should be just another if/else if statement. An example of an else only statement would be

    else
    {
    Print("Nothing happened on this bar");
    }

    Just a thought, and again I'm far from expert but if a reader can't see the logic in your code, it can probably be expressed better.

    Comment


      #3
      XMASJ,

      Thanks for the input. In hindsight I should just have posted the code I was trying to resolve, and with my 'gut feel' I had done what you thoughtfully suggested ... in my own way .. hopefully commented enough for reasonable understanding ...

      {
      if ( BarsInProgress == 1 && (iOrderL1 != null || iOrderL2 != null || iOrderL5 != null)
      && ( iOrderL1.OrderState != OrderState.Filled || iOrderL2.OrderState != OrderState.Filled || iOrderL5.OrderState != OrderState.Filled ) )
      { // trying to avoid BIP 1&2 from cancelling in progress orders
      Print("VitekV08L NO fills. BIP=1 bar= "+CurrentBar+" time="+Times[1][0]+" BIP1 try to resubmit order");
      DrawDiamond(CurrentBar.ToString() + "sl", true, 0, (Highs[1][0] + 3* TickSize), Color.Orange);
      return;
      }
      //
      if ( BarsInProgress == 2 && (iOrderL1 != null || iOrderL2 != null || iOrderL5 != null)
      && ( iOrderL1.OrderState != OrderState.Filled || iOrderL2.OrderState != OrderState.Filled || iOrderL5.OrderState != OrderState.Filled ) )
      { // trying to avoid BIP 1&2 from cancelling in progress orders
      Print("VitekV08L NO fills. BIP=2 bar= "+CurrentBar+" time="+Times[2][0]+" BIP2 trys to resubmit order");
      DrawDiamond(CurrentBar.ToString() + "sl", true, 0, (Highs[2][0] + 6* TickSize), Color.Black);
      return;
      }
      //
      if ( BarsInProgress == 0 && (iOrderL1 != null || iOrderL2 != null || iOrderL5 != null) && CurrentBar == MyEntryBar + 1
      && ( iOrderL1.OrderState != OrderState.Filled || iOrderL2.OrderState != OrderState.Filled || iOrderL5.OrderState != OrderState.Filled ) )
      { // avoid BIP 0 from cancelling in progress LIMIT orders for 2 bars
      Print("VitekV08L NO fills. BIP=0 bar= "+CurrentBar+" time="+Times[0][0]+" BIP0 trys to resubmit order on entryBar+1");
      DrawDiamond(CurrentBar.ToString() + "sl", true, 0, (Highs[0][0]), Color.Red);
      return;
      }
      //
      if ( BarsInProgress == 0 && (iOrderL1 != null || iOrderL2 != null || iOrderL5 != null) && CurrentBar == MyEntryBar + 2
      && ( iOrderL1.OrderState != OrderState.Filled || iOrderL2.OrderState != OrderState.Filled || iOrderL5.OrderState != OrderState.Filled ) )

      { // if open order after 2 bars, then cancel it and wait for another setup
      if(iOrderL1 != null)
      {CancelOrder(iOrderL1);
      Print("VitekV08L NO fills. BIP=0 bar= "+CurrentBar+" time="+Times[0][0]+" BIP0 cancel unfilled orders on entryBar+2");
      DrawDiamond(CurrentBar.ToString() + "sl", true, 0, (Highs[0][0]), Color.DarkGreen);
      }
      if(iOrderL2 != null)
      CancelOrder(iOrderL2);
      if(iOrderL5 != null)
      CancelOrder(iOrderL5);
      iOrderL1 = null; iOrderL2 = null; iOrderL5 = null;
      return;
      }

      Thanks again,
      Jon

      Comment


        #4
        No markers_ not as expected

        My error, on previous post I errored: within OnBarUpdate()
        there is no if (BarsInProgress != 0) return;
        THERE IS if (BarsInProgress == 0)

        What I am trying to do with the code is stop BIP=1 & BIP=2 from cancelling orders, and to only have BIP=0 maintain the EnterLongLimit() for two bars with the use of
        CurrentBar= MyEntryBar; during the ordering process.

        As per suggestion from Josh I modified his sample to include my situations


        The BIP=1 and BIP=2 are drawing the diamonds and printing in output, but the BIP=0 isnt drawing diamonds or printing output that it is doing anything during the bar formation ... the order isnt cancelled and stays active for many bars (until it crosses the order price and moves down, which is when it fills!)

        Image from todays price action shows the black and orange diamonds from the BIP=1/2 and that the trade gets filled almost 4 hours after the initial order is put in to MBTrading.

        Any assist as to what is wrong with the code is appreciated. The behavior is consistent as seen by another entry not cancelled.

        Jon
        Attached Files
        Last edited by Trader.Jon; 03-01-2011, 09:24 AM. Reason: added another screenshot

        Comment


          #5
          Jon,

          You should turn on TraceOrders = true and add prints inside OnOrderUpdate() to see the events of your orders. With those on you should see if your orders go to PendingCancel or Cancelled state.

          Without seeing where your entry orders are, you will also want to ensure you are actually calling CancelOrder() on the correct IOrder object.
          Josh P.NinjaTrader Customer Service

          Comment


            #6
            Josh,

            I have done prints (see code message #3 on this thread) but I am wondering where
            private int MyEntryBar = 0;
            should be located ?? I have it in the variables, I wonder if it should be in
            protected override void Initialize()
            or
            protected override void OnStartUp()
            ...
            as it has to be count 0 for start of each entry attempt.

            Thanks,
            Jon

            Comment


              #7
              Jon, initially in variables is fine for setting it up - to achieve the reset you should reset it's value as needed for example as the 'dealt with' entryOrder has been filled.
              BertrandNinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_Bertrand View Post
                Jon, initially in variables is fine for setting it up - to achieve the reset you should reset it's value as needed for example as the 'dealt with' entryOrder has been filled.
                Please, looking for suggestions:

                I removed the 'MyEntryBar' from the 'if' conditions to make tests simpler. I also made the EnterLongLimit easier to fill (?!) by use of 'getcurrentask' as the limit price.

                iOrderL1 = EnterLongLimit(0, false, iQuantity1, (GetCurrentAsk(0)), sENTRY1L);

                Frankly, I am amazed that it just doesnt fill on the next ask/offer that appears!!!

                Excerpts of one trade from print and trace log are attached. Print shows BIP=1 has (I believe) cancelled the order, account = TEST04 and strategy = V08Ja4.

                What can I add to the code seen in #3 message on this thread that will stop BIP=1 and BIP=2 from initiating cancellation of the order? The only option I see is the one that I am trying, that is, explicitly diverting away from processing a new order. Any new order has a check in it to verify that there is no active order, and cancelling any active order before sending in a new one. IF I qualify that process to only happen with BIP=0, then the strategy functions totally incorrectly as far as profiatble entries are concerned.

                This is the structure of the order processing:

                Code:
                 
                  if ( (Position.MarketPosition == MarketPosition.Flat) && (timeSeries[0] >= 1) )
                                    // Try to go LONG
                    {   if ( 
                    (  (sFT0Enter[0] == 1 && UseFT4Chop == 1) ||  UseFT4Chop == 0  )
                    && (  sMTFenterLong[0] == 1 || LongUseRule2 == 0 ) 
                    &&  (
                    (   (sMTFwatchChop[0]  == 1 && LongUseRule3 == 1 ) ||  LongUseRule3 == 0  )
                       || 
                    ( (sMTFwatchChop[0]  == 1 && LongUseRule3 == 2 ) ||  LongUseRule3 == 0  )  
                    )
                    && (
                     (UseFT4Chop == 1  &&  sFTwatch4Chop[0] == 1) ||  UseFT4Chop == 0  )
                    &&        WatchToGoLong == 1
                      )
                      //
                                       { // if there is open order then cancel it and try again
                    if(iOrderL1 != null)
                    {  CancelOrder(iOrderL1);
                     Print("V08Ja4 Cancelling unfilled order & TRY AGAIN L1 BIP=0 bar= "+CurrentBars[0]+" time= "+Times[0][0]+" LO= "+Lows[0][0]+" HI= "+Highs[0][0]);
                    }
                    //
                    if(iOrderL2 != null)
                    { CancelOrder(iOrderL2);
                    Print("V08Ja4 Cancelling unfilled order & TRY AGAIN L2 BIP=0 bar= "+CurrentBars[0]+" time= "+Times[0][0]+" LO= "+Lows[0][0]+" HI= "+Highs[0][0]);
                    }
                    //
                    if(iOrderL5 != null)
                    {   CancelOrder(iOrderL5);
                     Print("V08Ja4 Cancelling unfilled order & TRY AGAIN L5 BIP=0 bar= "+CurrentBars[0]+" time= "+Times[0][0]+" LO= "+Lows[0][0]+" HI= "+Highs[0][0]);
                        iOrderL1 = null; iOrderL2 = null; iOrderL5 = null;  }
                     // 
                      tVars.vLow = Close[0];
                      tVars.vHigh = Close[0];
                      tVars.cStopLossTicks = iStopLossTicks;
                      SetStopLoss(CalculationMode.Ticks, tVars.cStopLossTicks * vTickMult);     
                DrawTriangleUp(CurrentBar.ToString() + "sl", true, 0, Closes[0][0] - tVars.cStopLossTicks * TickSize * vTickMult, Color.DarkGreen);
                    //
                      if ((iOrderL1 == null || Historical) && iQuantity1 != 0)
                       iOrderL1 = EnterLongLimit(0, false, iQuantity1, (GetCurrentAsk(0)), sENTRY1L);
                      if ((iOrderL2 == null || Historical) && iQuantity2 != 0)
                       iOrderL2 = EnterLongLimit(0, false, iQuantity2, (GetCurrentAsk(0)), sENTRY2L);
                      if ((iOrderL5 == null || Historical) && iQuantity5 != 0)
                      { iOrderL5 = EnterLongLimit(0, false, iQuantity5, (GetCurrentAsk(0)), sENTRY5L);
                       Print("V08Ja4 TRY TO GO LONG target= "+(GetCurrentAsk(0))+" Trigger Bar= "+CurrentBars[0]+" time= "+Times[0][0]+" LO= "+Lows[0][0]+" HI= "+Highs[0][0]);
                       // MyEntryBar = CurrentBars[0];
                       // DrawText(Trigger bar is "+current bar#);
                      }
                      return;
                }
                Thanks for suggestions!
                Jon
                Attached Files

                Comment


                  #9
                  Jon, on which barstype are you running this script? I remember you had issues on a custom renko type in the past where the script works as expected on the default one we ship - have you tried across them?

                  In the end you need to first simplify the script to a starting point where things work as you expect and only then add in complexity, otherwise it will be very hard to track resulting issues down.
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    Bertrand,

                    Actually I think I found a workaround that will be usefull .. if you can just guide me to the correct input...

                    I use the following to be a condition of the 'try to Go LONG' in the previous code segment I posted.
                    There is a variable I have in the code
                    Code:
                     
                    [FONT=Arial][COLOR=#0000ff][FONT=Arial][COLOR=#0000ff]private[FONT=Arial] [/FONT][FONT=Arial][COLOR=#0000ff][FONT=Arial][COLOR=#0000ff]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Arial] iWatchToGoLong = [/FONT][FONT=Arial][COLOR=#800080][FONT=Arial][COLOR=#800080]1[/COLOR][/FONT][/COLOR][/FONT][FONT=Arial];
                    [Description([/FONT][FONT=Arial][COLOR=#800000][FONT=Arial][COLOR=#800000]" ALL LONG trades == ENABLE =1 else =0 "[/COLOR][/FONT][/COLOR][/FONT][FONT=Arial])]
                    [GridCategory([/FONT][FONT=Arial][COLOR=#800000][FONT=Arial][COLOR=#800000]"Trade Entry"[/COLOR][/FONT][/COLOR][/FONT][FONT=Arial])]
                    [Gui.Design.DisplayName([/FONT][FONT=Arial][COLOR=#800000][FONT=Arial][COLOR=#800000]"1.b ALL LONG trades.. ENABLE =1"[/COLOR][/FONT][/COLOR][/FONT][FONT=Arial])]
                    [/FONT][FONT=Arial][COLOR=#0000ff][FONT=Arial][COLOR=#0000ff]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Arial] [/FONT][FONT=Arial][COLOR=#0000ff][FONT=Arial][COLOR=#0000ff]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Arial] WatchToGoLong
                    { [/FONT][FONT=Arial][COLOR=#0000ff][FONT=Arial][COLOR=#0000ff]get[/COLOR][/FONT][/COLOR][/FONT][FONT=Arial] { [/FONT][FONT=Arial][COLOR=#0000ff][FONT=Arial][COLOR=#0000ff]return[/COLOR][/FONT][/COLOR][/FONT][FONT=Arial] iWatchToGoLong; } [/FONT][FONT=Arial][COLOR=#0000ff][FONT=Arial][COLOR=#0000ff]set[/COLOR][/FONT][/COLOR][/FONT][FONT=Arial] { iWatchToGoLong = value; } }[/FONT]
                    [/COLOR][/FONT][/COLOR][/FONT]
                    to switch-off taking trades: is there an specific way I can change it to '0' after I put in the long order, and then I am thinking it would turn itself back to '1' when trades are done (ready for the next trade)?

                    Thanks,
                    Jon

                    ps: I looked at reset in the help index but it only has to do with dataseries



                    Comment


                      #11
                      Jon,

                      When you go long, just go WatchToGoLong = 0. When your trade is flat again set WatchToGoLong = 1.

                      Code:
                      WatchToGoLong = 0;
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #12
                        Josh,

                        Thanks for the tip ... I do think it is working now the way I want!

                        Jon

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by helpwanted, Today, 03:06 AM
                        0 responses
                        3 views
                        0 likes
                        Last Post helpwanted  
                        Started by Brevo, Today, 01:45 AM
                        0 responses
                        7 views
                        0 likes
                        Last Post Brevo
                        by Brevo
                         
                        Started by aussugardefender, Today, 01:07 AM
                        0 responses
                        5 views
                        0 likes
                        Last Post aussugardefender  
                        Started by pvincent, 06-23-2022, 12:53 PM
                        14 responses
                        242 views
                        0 likes
                        Last Post Nyman
                        by Nyman
                         
                        Started by TraderG23, 12-08-2023, 07:56 AM
                        9 responses
                        384 views
                        1 like
                        Last Post Gavini
                        by Gavini
                         
                        Working...
                        X