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

Help for the blind. I just don't my error.

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

    Help for the blind. I just don't my error.

    I have been working on Ninja 7 for over a decade but am transitioning to Ninja 8.

    I have a set of conditional code but I don't see any error in my code. I am guessing something is different on Ninja 8.

    What I am trying to achieve: I added several time frames but only want to process these additional time frames after I hit a Session begin for BarsInProgress == 1. I am using the variable BarsinProgress1SessionStart to designate I hit a session begin. At first I used a bool but after having issues I tried an integer but currently it is a string.

    here is some code snippets:
    // Declare the variable
    private string BarsinProgress1SessionStart ="0";

    ----------------------------

    // set the variable default to "0" like a bool.
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    BarsinProgress1SessionStart = "0";​

    //Set the variable to "1" when IsFirstBarOfSession && BarsInProgress == 1). This code works fine.
    if (Bars.IsFirstBarOfSession && BarsInProgress == 1)//if New session - reset counters - Reset Arrays
    {
    Print("2.1 "+ Volume[0] + " , " + Bars.IsFirstBarOfSession + " , " + CurrentBar + " ," + BarsinProgress1SessionStart + " , " + Time[0] + " , " + BarsInProgress);
    BarsinProgress1SessionStart = "1";//Flag You have received an session begin​

    --------------------

    // Skip any ticks if the Session Start has not been hit
    if (BarsinProgress1SessionStart == "0");// If you have not received a session start or it is BarsInProgress == 0
    {
    if (DebugCount < 20)//Only print the first 20 ticks showing BarsInProgress and the value of BarsinProgress1SessionStart
    {
    DebugCount ++;
    Print("2.2a " + BarsInProgress + " , " + BarsinProgress1SessionStart);
    }
    return;//Exit - skip ticks until otherwise false
    }​


    // Her is a debug listing from the above code, you can clearly seethe 2.2a BarsinProgress1SessionStart is "1" but the "if" logic clearly says only do if: if (BarsinProgress1SessionStart == "0").
    The value is "1" but executes the code when it should not be.
    2.1 1 , True , 0 ,0 , 1/24/2023 2:20:00 AM , 1
    2.2a 1 , 1
    2.2a 1 , 1
    2.2a 2 , 1
    2.2a 1 , 1
    2.2a 1 , 1
    2.2a 1 , 1
    2.2a 1 , 1
    2.2a 2 , 1
    2.2a 5 , 1
    2.2a 2 , 1​

    Any help or suggestions for a blind guy? I have been a software developer for multiple decades so am not a newbie. I just don't see what I am doing wrong.

    Cheers
    Attached Files
    Last edited by ct; 01-26-2023, 03:31 AM.

    #2
    Have you tried moving the processing code to OnBarUpdate?

    ​I mean, after initializing your SessionStart variable to '0', the
    remainder of that code has no business being executed in
    OnStateChange.

    Isn't that code already in OnBarUpdate in your NT7 code?

    Think about it, at each bar close, you want to know if that bar
    was the start of the session, right? You want to repeat this
    check each time a bar closes. That screams locating that
    code in OnBarUpdate, not OnStateChange.

    Make sense?

    Comment


      #3
      As you move forward, you'll learn very little code needs to go
      into OnStateChange().

      These general rules may help:
      Your NT7 Initialize() code goes into State.SetDefaults.
      Your NT7 OnStartUp() code goes into State.DataLoaded.
      Your NT7 OnTermination() code goes into State.Terminated.
      Your NT7 OnBarUpdate() code stays in OnBarUpdate.

      Hang in there!
      Last edited by bltdavid; 01-26-2023, 10:47 PM. Reason: Added State.Terminated

      Comment


        #4
        Early in the NT8 beta period, someone wrote
        an NT7 to NT8 conversion utility.

        It was a C# program executed by LINQPad.

        I thought it worked pretty darn well.

        Let me find the link ...
        Last edited by bltdavid; 01-26-2023, 07:28 AM. Reason: Added link for LINQPad.

        Comment


          #5
          Found it.

          Study this thread.

          Comment


            #6
            Hello bltdavid,

            Thank you for providing the great advice!

            ct,

            bltdavid is correct. BarsInProgress, Bars.IsFirstBarOfSession, CurrentBar, Time[0], and Volume[0] are all series related and cannot be used in OnStateChange(). Move this code to OnBarUpdate().







            Below is a link to a forum post on porting scripts.


            As well as a link to a forum post with helpful resources on getting started with NinjaScript 8 scripts.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              To bltdavid​ I wish to say this blind moron says "thank you very much". You are an absolute star! I get it now. I feel like a rookie.
              Last edited by ct; 01-26-2023, 12:23 PM.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Shansen, 08-30-2019, 10:18 PM
              24 responses
              942 views
              0 likes
              Last Post spwizard  
              Started by Max238, Today, 01:28 AM
              0 responses
              9 views
              0 likes
              Last Post Max238
              by Max238
               
              Started by rocketman7, Today, 01:00 AM
              0 responses
              4 views
              0 likes
              Last Post rocketman7  
              Started by wzgy0920, 04-20-2024, 06:09 PM
              2 responses
              28 views
              0 likes
              Last Post wzgy0920  
              Started by wzgy0920, 02-22-2024, 01:11 AM
              5 responses
              33 views
              0 likes
              Last Post wzgy0920  
              Working...
              X