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

Variable transfer/access between OnBarUpdate and OnExecution

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

    Variable transfer/access between OnBarUpdate and OnExecution

    Hi guys,

    I have a strategy that works as follows (without all the necessary parenthesis stuff):

    public class Strategy123
    {
    int StoppedOut = 0;

    protected override void OnBarUpdate()
    {
    if StoppedOut == 1
    {Say "You are stopped out" or do nothing}
    else if StoppedOut == 0
    --> set up a stop loss STPLMT order "SL123"
    }

    protected override void OnExecution(Iexecution execution)
    {
    if SL123.Token == execution.Order.Token
    {StoppedOut = 1; DrawText(I am stopped out);}
    }

    }

    What happens is: The Stop Loss order SL123 gets set up correctly within the OnBarUpdate() method and eventually triggers and executes correctly. The system then correctly recognizes via OnExecution that the SL123 order has been executed and amends the value of the StoppedOut variable to 1. However, once the next bar updates and OnBarUpdate() should now see the StoppedOut variable as 1, it simply does not do so.
    Instead, for example if I tell it to DrawText using the now-should-be-1-StoppedOut variable, it simply does not draw any text; it seems to have "lost" the variable.

    Any info about how I can tell the system to "memorize" the change in the StoppedOut variable, so that the next OnBarUpdate() can make use of it?

    Thank you all for taking the time to read.

    Best regards,
    noincomenojobnoassets

    #2
    Print the value of your variable on each step of the way. Remember these things can be running on separate threads so the next OnBarUpdate() simply triggered too early.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Hi Josh,

      Thanks for answering.

      When I code a DrawText saying "Stoppedout" + StoppedOut into OnExecution, it correcty draws the text "Stoppedout1" into the chart. Therefore, within the OnExecution method it seems to know the "new" value of 1 once the position gets stopped out.

      Or does printing the current value of the variable help anything in terms of letting the OnBarUpdate method know the current value?

      Coming to your idea of the OnBarUpdate triggering too early:
      Even when I get stopped out "early", with still a few minutes to go until the next OnBarUpdate triggers, the strategy seems to "lose" the variable. I don't get any sign that OnBarUpdate has an idea about the variable StoppedOut being set to 1 now, not even that StoppedOut exists anymore. Similar DrawText attempts as set out above don't work...

      Any more ideas? Help is, as always, greatly appreciated.

      Thanks,
      n.i.n.j.n.a.

      Comment


        #4
        Not sure I follow what you mean exactly, but once you store a value into a variable, it is accessible throughout any method regardless of if that is OnExecution or OnBarUpdate.

        You should be creating these as private variables in the variable region instead of declaring them inside the method. If you declared them inside the method then they won't be accessible in other methods. If you declare them in the variables region of the code then they will be accessible.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Hi Josh,

          I'm creating the variable outside the methods - this is not the problem.

          Printing the variable's value actually STOPS after it has been set to 1 by the OnExecution method - after correctly printing "The variable is now 1" from the OnExecution method, all the following Print statements within the next OnBarUpdate simply do not trigger anymore.

          The strategy seems to dislike the variable being given a different value in the OnExecution method - when I have it changed within the OnBarUpdate method, it correctly refers to the new value afterwards. When I have it changed in the OnExecution method, the system somehow loses it. It appears the strategy stops - not even a Print("Hello") command is executed in the OnBarUpdate method once the variable's value has been set to 1 in the OnExecution method.

          Any suggestions? =/

          Comment


            #6
            It doesn't matter where you set variables, it is all the same. You will need to debug why your strategy stops working. Please check your Control Center logs for errors.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              It says

              Error on calling the 'OnExecution' method for strategy '<strategyname>': Object reference not set to an instance of an object.

              Comment


                #8
                Have you checked for null before trying to access IOrders and such? http://www.ninjatrader-support2.com/...ead.php?t=4226
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  For the Stop Loss order, I have a CancelOrder command in the OnBarUpdate method that checks for null before cancelling the order. (The order is then set up and transmitted again further on in the OnBarUpdate code).

                  Hm, as to the remaining code, I can't exactly tell, since I can't access it from here now - will check and let you know tomorrow. Thanks so far!

                  Comment


                    #10
                    That did it!

                    I wasn't checking for null inside the OnExecution method - it started with
                    if SL123.Token == execution.Order.Token...

                    After I included an if SL123 != null check before the Token check, it all started running properly and smoothly all of a sudden!

                    Thank you very mich for your help, Josh - you damn sure know your stuff! Thumbs up!

                    Will let you know in case not everything works out as expected.

                    Thanks,

                    n.i.n.j.n.a.

                    *** SOLVED ***

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Board game geek, Today, 01:34 AM
                    0 responses
                    3 views
                    0 likes
                    Last Post Board game geek  
                    Started by morrnel, 05-12-2024, 06:07 PM
                    3 responses
                    36 views
                    0 likes
                    Last Post wzgy0920  
                    Started by FishTrade, Yesterday, 11:11 PM
                    0 responses
                    7 views
                    0 likes
                    Last Post FishTrade  
                    Started by Austiner87, Yesterday, 03:42 PM
                    1 response
                    21 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by cshox, Yesterday, 11:11 AM
                    2 responses
                    16 views
                    0 likes
                    Last Post cshox
                    by cshox
                     
                    Working...
                    X