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 get current seconds

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

    How to get current seconds

    Hi team,

    I am trying to create an entry filter in an OnEachTick strategy to enter only if current second is below 50.

    For this i am calling a private int CurrentBarSec using "marketDataUpdate.Time.Second" in OnMarketData and applying the filter condition in OnBarUpdate (CurrentBarSec < 50). However, it's not working when i am doing a backtest...

    Any idea why this is happening?
    Is there any other way to get this condition?

    Thank you in advance!
    Best,

    #2
    Hello linkcou,

    Thank you for your post.

    OnMarketData() is a real time data stream, so retrieving marketDataUpdate.Time.Second would not typically work in historical.

    To have OnMarketData() called historically, you can use Tick Replay.



    Please let us know if you have any further questions.
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      Hi Gaby,

      I was indeed using tick replay with 1 tick data. For this reason i wasn't sure why it is not working. Sharing below the piece of code for the backtest:

      Load tick data for the tick replay + called variable "CurrentBarSec":
      Click image for larger version

Name:	image.png
Views:	48
Size:	18.7 KB
ID:	1291711

      OnBarUpdate piece of code where i introduce the condition of CurrentBarSec < 50 to enter long:
      Click image for larger version

Name:	image.png
Views:	59
Size:	28.2 KB
ID:	1291712

      When doing the backtest i check the Tick Replay box.

      DO you see anything wrong?

      Thank you in advance!​

      Comment


        #4
        Hello linkcou,

        TickReplay events only occur on the "Last" market data type. Try adding a print for the Time when marketDataUpdate.MarketDataType == MarketDataType.Last. Take a look at the code snippet from the help guide page I linked in my previous post for an example.

        Also make sure the option to allow for Tick Replay is located in Tools > Options > Market Data > "Show Tick Replay".

        Please let me know if I can assist further.
        Gaby V.NinjaTrader Customer Service

        Comment


          #5
          Hi Gaby, thanks for the help.
          I added the piece of code as mentioned. See screenshot below.
          Click image for larger version

Name:	image.png
Views:	56
Size:	11.9 KB
ID:	1291858

          However, it still takes some trades that i don't want.

          For context, i just want to take trades if the current second is less than 50 and during 9am-4pm. If these conditions are met, I put 5 limit orders at different levels. If not, i want to cancel them. See below the piece of code that i have:

          if (Position.MarketPosition == MarketPosition.Flat
          )
          {

          if (IsFirstTickOfBar)
          {
          CancelOrder(entryOrder1);
          CancelOrder(entryOrder2);
          CancelOrder(entryOrder3);
          CancelOrder(entryOrder4);
          CancelOrder(entryOrder5);
          }

          if ((ToTime(Time[0]) >= 090500 && ToTime(Time[0]) <= 160500)
          && (CurrentBarSec < LimitSecL)
          )
          {
          if (entryOrder1 == null
          && Nbars_MMDL1E != 0
          )
          {
          EnterLongLimit(1, true, Qty, LLL1 - EntryFactorL, "MMDOL1");
          }

          if (entryOrder2 == null
          && Nbars_MMDL2E != 0
          )
          {
          EnterLongLimit(1, true, Qty, LLL1 - EntryAggressiveL1, "MMDOL2");
          }

          if (entryOrder3 == null
          && Nbars_MMDL3E != 0
          )
          {
          EnterLongLimit(1, true, Qty, LLL1 - EntryAggressiveL2, "MMDOL3");
          }

          if (entryOrder4 == null
          && Nbars_MMDL4E != 0
          )
          {
          EnterLongLimit(1, true, Qty, LLL1 - EntryAggressiveL3, "MMDOL4");
          }

          if (entryOrder5 == null
          && Nbars_MMDL5E != 0
          )
          {
          EnterLongLimit(1, true, Qty, LLL1 - EntryAggressiveL4, "MMDOL5");
          }
          }
          else
          {
          CancelOrder(entryOrder1);
          CancelOrder(entryOrder2);
          CancelOrder(entryOrder3);
          CancelOrder(entryOrder4);
          CancelOrder(entryOrder5);
          }
          }

          Do you see any problem with it¿
          Let me know if i can provide some additional context to be useful.

          Thank you in advance!
          Best,​

          Comment


            #6
            Hello,

            To understand why the script is behaving as it is, such as placing orders or not placing orders (or drawing objects or other actions) when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

            In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar.

            This will print to the output window. Backtest the script and when the output from the output window appears save this by right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output text file to your reply.

            Output from prints will appear in the NinjaScript Output window.
            NT8: New -> NinjaScript Output

            The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very helpful to include labels and operators in the print to understand what is being compared in the condition sets.

            Below I am providing a link to videos that demonstrate adding prints to a script to get further information about the behavior of the script.
            NT8 —


            It is also helpful to set TraceOrders to true in State.Configure as well as print the order object in OnOrderUpdate().
            TraceOrders will output to the NinjaScript Output window a message when orders are being submitted, ignored, cancelled, or rejected.
            Printing the order object in OnOrderUpdate() will allow you to track the progression of the order from submitted, to working, to filled, cancelled, or rejected.

            These tools will let you know what happens to the order.

            TraceOrders - https://ninjatrader.com/support/help...raceorders.htm
            OnOrderUpdate() - https://ninjatrader.com/support/help...rderupdate.htm

            I'm also including a link to a forum post with further suggestions on debugging a script.


            Save the output from the output window to a text file. Let me know if you need assistance creating a print or enabling TraceOrders.

            I am happy to assist with analyzing the output from prints and TraceOrders.​
            Gaby V.NinjaTrader Customer Service

            Comment


              #7
              Hi Gaby, thanks for your response.
              I actually found the source of error being the marketposition = flat that doesnt allow to cancel some of the orders. Tried again and now it works perfectly!
              Thanks for your support.
              Best,

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by llanqui, Today, 03:53 AM
              0 responses
              2 views
              0 likes
              Last Post llanqui
              by llanqui
               
              Started by burtoninlondon, Today, 12:38 AM
              0 responses
              10 views
              0 likes
              Last Post burtoninlondon  
              Started by AaronKoRn, Yesterday, 09:49 PM
              0 responses
              14 views
              0 likes
              Last Post AaronKoRn  
              Started by carnitron, Yesterday, 08:42 PM
              0 responses
              11 views
              0 likes
              Last Post carnitron  
              Started by strategist007, Yesterday, 07:51 PM
              0 responses
              14 views
              0 likes
              Last Post strategist007  
              Working...
              X