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

How to make purchases and sales at the close of the bar and update the stop in ticks

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

    #16
    Thanks ChelseaB, this is what the output shows me by placing it in my strategy.

    I send you the code for you to observe

    Comment


      #17

      I send you the code for you to observe
      Attached Files

      Comment


        #18
        Hello jmarcano,

        When enabling this script, what prints to the output window?

        Do any errors appear on the Log tab of the Control Center?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #19
          Good afternoon ChelseaB, no error appears, print the first tick of each bar, as I show you in the photo.

          Thank you.

          Comment


            #20
            Now when I activate the options for the execution of buy or sell the strategy, you do not take the conditions and do not execute neither buy nor sell, but it still shows the impression of the first bar tick without problem

            Comment


              #21
              Hello jmarcano,

              This is a first step.

              You have now gotten to the point that you can see that you are able to trigger actions only on the first tick of a new bar after a bar has closed. This should confirm that this works correctly.

              The next step is to add the values of the condition print.

              In the action block for 'if (IsFirstTickOfBar)' begin printing the values in the condition.

              For example the next condition:

              &&(Times[0][0].TimeOfDay >= Apertura.TimeOfDay)

              Print("\r\n" + Time[0].ToString() + " Times[0][0].TimeOfDay: " + Times[0][0].TimeOfDay.ToString() + " => Apertura.TimeOfDay: " + Apertura.TimeOfDay.ToString());

              What does this print to the output window?

              Please include the saved output as a text file as discussed in the video linked in the forum post that demonstrates how to use prints to understand behavior, linked from post #6 instead of a screenshot.

              I personally prefer string.Format() as demonstrated in the video linked that the forum post that demonstrates how to use prints to understand behavior.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #22
                Good afternoon Chelsea, sorry for the delay in sending the file with the print output.
                Attached Files

                Comment


                  #23
                  code

                  protected override void OnBarUpdate()
                  {

                  if (IsFirstTickOfBar
                  && (Times[0][0].TimeOfDay >= Apertura.TimeOfDay))
                  {
                  Print("\r\n" + Time[0].ToString() + " FIRST TICK OF BAR!!!!!!!!!!!!!!!!!");
                  Print("\r\n" + Time[0].ToString() + " Times[0][0].TimeOfDay: " + Times[0][0].TimeOfDay.ToString() + " => Apertura.TimeOfDay: " + Apertura.TimeOfDay.ToString());

                  }
                  else
                  {
                  Print(Time[0].ToString() + " not first tick of bar");
                  }


                  // Set 1
                  if (Position.MarketPosition == MarketPosition.Flat)
                  {
                  SetStopLoss(CalculationMode.Ticks, Stop);
                  }

                  // Set 2
                  if (IsFirstTickOfBar
                  &&(Times[0][0].TimeOfDay >= Apertura.TimeOfDay)
                  && (Times[0][0].TimeOfDay < Cierre.TimeOfDay)
                  && (Position.MarketPosition == MarketPosition.Flat)
                  && (VolumeUpDown1.UpVolume[0] > 1300)
                  && (CrossAbove(Close, Swing1.SwingHigh, 1))
                  && (Close[0] > EMA1[0])
                  && (Operaciones < TradeDay)
                  && (Long == true))
                  {
                  EnterLong(Convert.ToInt32(DefaultQuantity), "");
                  Operaciones = Operaciones + 1;
                  Print("\r\n" + Time[0].ToString() + " FIRST TICK OF BAR LONG!!!!!!!!!!!!!!!!!");
                  Print("\r\n" + Time[0].ToString() + " Times[0][0].TimeOfDay: " + Times[0][0].TimeOfDay.ToString() + " => Apertura.TimeOfDay: " + Apertura.TimeOfDay.ToString());

                  }

                  // Set 3
                  // If a long position is open, allow for stop loss modification to breakeven
                  else if ((Position.MarketPosition == MarketPosition.Long)
                  && (Close[0] >= Position.AveragePrice))
                  {
                  // Once the price is greater than entry price+50 ticks, set stop loss to breakeven
                  if (Close[0] >= Position.AveragePrice + BE * TickSize)
                  {
                  SetStopLoss(CalculationMode.Price, Position.AveragePrice + (6 * TickSize));
                  Print(Time[0].ToString() + " not first tick of bar Long");
                  }
                  }

                  Comment


                    #24
                    Hello jmarcano,

                    A good attempt.
                    It is important that we see the print (on the first tick of a bar) for every bar and not just when the condition is true.

                    Please replace:
                    Code:
                    if (IsFirstTickOfBar
                    && (Times[0][0].TimeOfDay >= Apertura.TimeOfDay))
                    {
                    Print("\r\n" + Time[0].ToString() + " FIRST TICK OF BAR!!!!!!!!!!!!!!!!!");
                    Print("\r\n" + Time[0].ToString() + " Times[0][0].TimeOfDay: " + Times[0][0].TimeOfDay.ToString() + " => Apertura.TimeOfDay: " + Apertura.TimeOfDay.ToString());
                    
                    }
                    else
                    {
                    Print(Time[0].ToString() + " not first tick of bar");
                    }
                    With:
                    Code:
                    if (IsFirstTickOfBar
                    && (Times[0][0].TimeOfDay >= Apertura.TimeOfDay))
                    {
                    Print("\r\n" + Time[0].ToString() + " Times[0][0].TimeOfDay: " + Times[0][0].TimeOfDay.ToString() + " => Apertura.TimeOfDay: " + Apertura.TimeOfDay.ToString());
                    }

                    The next line of the condition set is:
                    Code:
                    && (Times[0][0].TimeOfDay < Cierre.TimeOfDay)
                    Try creating a print for this, and adding this in the action block for 'if (IsFirstTickOfBar)'. The action block is between the curly braces that come after the if conditions where the actions are triggered.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #25
                      Good afternoon Chelsea, sending the file with the print output.

                      You do not send me any impressions because I think we are out of the hour of operation?

                      Attached Files

                      Comment


                        #26
                        Update Code

                        protected override void OnBarUpdate()
                        {

                        if (IsFirstTickOfBar
                        && (Times[0][0].TimeOfDay >= Apertura.TimeOfDay)
                        && (Times[0][0].TimeOfDay < Cierre.TimeOfDay))
                        {
                        Print("\r\n" + Time[0].ToString() + " Times[0][0].TimeOfDay: " + Times[0][0].TimeOfDay.ToString() + " => Apertura.TimeOfDay: " + Apertura.TimeOfDay.ToString());
                        }
                        // else
                        // {
                        // Print(Time[0].ToString() + " not first tick of bar");
                        // }

                        Comment


                          #27
                          Hello jmarcano,

                          Good job.

                          The next line of the condition set is:
                          Code:
                          && (Times[0][0].TimeOfDay < Cierre.TimeOfDay)
                          Try creating a print for this, and adding this in the action block for 'if (IsFirstTickOfBar)'. The action block is between the curly braces that come after the if conditions where the actions are triggered.

                          Include the output from the output window with your reply.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #28
                            Good afternoon Chelsea, sending the file with the print output.

                            Thank you very much for the support you are giving me.

                            Attached Files

                            Comment


                              #29
                              code

                              if (IsFirstTickOfBar
                              && (Times[0][0].TimeOfDay >= Apertura.TimeOfDay)
                              && (Times[0][0].TimeOfDay < Cierre.TimeOfDay))
                              {
                              Print("\r\n" + Time[0].ToString() + " Times[0][0].TimeOfDay: " + Times[0][0].TimeOfDay.ToString() + " => Apertura.TimeOfDay: " + Apertura.TimeOfDay.ToString());
                              Print("\r\n" + Time[0].ToString() + " Times[0][0].TimeOfDay: " + Times[0][0].TimeOfDay.ToString() + " < Cierre.TimeOfDay: " + Cierre.TimeOfDay.ToString());
                              }
                              // else
                              // {
                              // Print(Time[0].ToString() + " not first tick of bar");
                              // }

                              Comment


                                #30
                                Hello jmarcano,

                                You are doing great. That print is exactly what we need.

                                So far I see lots of bars that meet these requirements.

                                The rest of the condition is:
                                Code:
                                && (Position.MarketPosition == MarketPosition.Flat)
                                && (VolumeUpDown1.UpVolume[0] > 1300)
                                && (CrossAbove(Close, Swing1.SwingHigh, 1))
                                && (Close[0] > EMA1[0])
                                && (Operaciones < TradeDay)
                                && (Long == true))
                                Do you want to take this one condition at a time, or do you want to try adding prints for the rest of the conditions all at once?

                                Once we have prints for these conditions, and we have TraceOrders enabled, we will be able to see for any bar why this order was not submitted and filled.
                                Chelsea B.NinjaTrader Customer Service

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by timmbbo, Today, 08:59 AM
                                0 responses
                                1 view
                                0 likes
                                Last Post timmbbo
                                by timmbbo
                                 
                                Started by bmartz, 03-12-2024, 06:12 AM
                                5 responses
                                33 views
                                0 likes
                                Last Post NinjaTrader_Zachary  
                                Started by Aviram Y, Today, 05:29 AM
                                4 responses
                                14 views
                                0 likes
                                Last Post Aviram Y  
                                Started by algospoke, 04-17-2024, 06:40 PM
                                3 responses
                                28 views
                                0 likes
                                Last Post NinjaTrader_Jesse  
                                Started by gentlebenthebear, Today, 01:30 AM
                                1 response
                                8 views
                                0 likes
                                Last Post NinjaTrader_Jesse  
                                Working...
                                X