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

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

    Good afternoon I am making a strategy where given the conditions makes a purchase or sale at the close of a bar, and the execution of the breakeven updates it in ticks, when the execution I do it in Calculate.OnEachTick, I make the purchase or sale in every tick and not the candle close as it should be.

    I have been checking the IsFirstTickOfBar process But it does not take the instruction and it still executes everything in ticks.

    #2
    Hello jmarcano,

    Welcome to the NinjaTrader forums!

    I can say that a strategy that requires IsFirstTickOfBar to be true in the condition, will only run the condition when the bool property is true, on the first tick of a bar.

    Below is a link to a short video that demonstrations. Notice that the bar type is a 20 second chart, Calculate is 'On each tick', but the prints only appear every 20 seconds and not for every tick.
    https://drive.google.com/file/d/1Ry_...w?usp=drivesdk

    Attached is the test script to test on your end and observe for yourself.

    Note, that if you are changing default values for new instances in State.SetDefaults, you will need to remove the instance of the script and add a new instance, as reloading will not run OnStateChange() with State.SetDefaults.

    Last, below is a link to a forum post with helpful information about getting started with NinjaScript.
    Attached Files
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi chelseaB thanks for your support, i will verify my strategy with you recommendation and leet you know.

      thanks

      Comment


        #4

        Good morning ChelseaB, make the changes to my code with your recommendations, applied to my logic, but no purchase or sale is executed at the time of the first tick, and the impression is not marked in real time.


        When I reload the strategy, it tells me that I make an entry but it does not actually execute it



        Comment


          #5


          This is part of my logic



          {

          if (CurrentBar < BarsRequiredToTrade)
          return;


          // 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!!!!!!!!!!!!!!!!!");
          }

          // 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


            #6
            Hello jmarcano,

            This is a new inquiry correct?

            You are able to print outside of any conditions and see the prints appearing on each bar close as I have demonstrated in my video and test script I have provided, is this correct?


            With this new inquiry about orders being filled, enable TraceOrders (in State.Configure so a new instance does not have to be added).

            In order to better understand how the code is working, it will be necessary to use Print and enable TraceOrders to see how the conditions are evaluating and if orders are being submitted, ignored, or cancelled.

            Below is a link to a forum post that demonstrates using prints to understand behavior.
            https://ninjatrader.com/support/foru...121#post791121

            Enable TraceOrders, print the time of the bar and all values used in the conditions that submit entry orders.

            Let me know if you need any assistance creating a print or enabling TraceOrders.

            Save the output from the output window to a text file and provide this with your next post.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7

              Thanks for the answer, if the printing works correctly, but not in real time, as for the strategy when I put the IsFirstTickOfBar function, the buy or sell parameters are not executed.

              If I remove the IsFirstTickOfBar function, everything works fine.

              Could you help me with this option by creating a print or enabling TraceOrders, to see if I can get the execution of it.

              Thanks.

              Comment


                #8
                Hello jmarcano,

                Take things one step at a time.

                You have stated that the script I have provided you is not printing the message 'FIRST TICK OF BAR!!!!!!!!!!!!!!!!!' only when a bar closes in real-time, as I have demonstrated in the video testing that script.

                Is this correct?

                If so, I will need to schedule a call to confirm, that you are testing the script I have provided you and that you are getting different behavior than what is shown in the video.

                If you are getting different behavior than what is shown in the video testing the script I have provided you, please send an email to platformsupport [at] ninjatrader [dot] with a link to this thread. We will schedule a call so that I may observe this test script is behaving differently on your computer.


                After this is confirmed, the next step will be to print the values in the conditions and enable TraceOrders as directed in Post #6. We will need the output from the prints.

                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9

                  I think I may have the error is that I make the comparison in the Zero candle and it should be the previous candle that would be the one, but I place it in the 1 candle and nothing is executed.

                  Comment


                    #10
                    Hello jmarcano,

                    This would not cause the print added in the script I have provided you to print on every tick instead of just when a bar closes as designed.

                    Are you certain this is still an issue?

                    Are you certain you have tested the script I have provided you and the behavior is different than what is shown in the video?

                    Have you sent an email to platformsupport [at] ninjatrader [dot] com so that I may schedule a call and confirm that the script I have provided you is not behaving as shown in the video?
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11

                      Good afternoon, thanks for the answer, disable the option of executing the strategy in a certain time and leave it open for the whole day and placing the instruction as you sent it, no signal is executed.

                      Comment


                        #12
                        BreakOutV02.txt

                        Comment


                          #13
                          Hello jmarcano,

                          The script I have provided you in post #2. is named IsFirstTickOfBarTest_NT8.

                          You are not testing the script I have provided you, which allows us to confirm the behavior before continuing.
                          This will affect issues and understanding of the prints added later when investigating orders.

                          What prints to the output window, when testing the script I have provided you?

                          I'd like to inquire if we may be having a communication issue.
                          Just as a tip, Google translate can be used to translate information into non-english languages with fairly high accuracy.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Excuse me ChelseB, I did not understand that you were requesting me, if your example sent works well for me, it does not work when I apply it to my strategy.

                            I send you a capture of your example on my computer.

                            Thank you and sorry for the misunderstanding

                            Comment


                              #15
                              Hello jmarcano,

                              The test script does appear to be working as intended and printing the 'FIRST TICK OF BAR!!!!!!!!!!!!!!!!!' text only when the bar closes, and not for every tick.

                              This is to teach you how IsFirstTickOfBar works.

                              Add the code from this in OnBarUpdate to the very top of OnBarUpdate in your script, outside of any conditions.

                              Then compile and run the code again. You should see the exact out that was seen in the test script I have provided you.

                              From there, we will change the text to the values used in the conditions below in your script. (Don't do this yet. Just copying the code that shows when the primary bar is closing from the test script I have provided you).

                              Then please re-run the script and provide the output to confirm the prints are appearing. After this we can move forward to analyzing the logic in your script.
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by yertle, Today, 08:38 AM
                              3 responses
                              9 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by NinjaTrader_ChelseaB, 01-08-2017, 06:59 PM
                              80 responses
                              19,667 views
                              5 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by adeelshahzad, Today, 03:54 AM
                              2 responses
                              16 views
                              0 likes
                              Last Post adeelshahzad  
                              Started by dappa, Today, 09:18 AM
                              1 response
                              6 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by bill2023, Yesterday, 08:51 AM
                              5 responses
                              25 views
                              0 likes
                              Last Post bltdavid  
                              Working...
                              X