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

Add Profit Target and Stop Loss

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

    #16
    Hello ronhb107,

    OnStateChange with State.DataLoaded runs before OnStateChange with State.Historical and before the first time OnBarUpdate() runs.

    The account variable is null.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #17
      That's fine. But OnBarUpdate() never gets fired.

      Now, if I comment out the Terminated and DataLoaded, then OnBarUpdate() does fire.

      There seems to be something blocking the firing of OnBarUpdate(), but I can't seem to get a handle on it.

      What am I missing here?

      Comment


        #18
        Hello ronhb107,

        The issue is that the account variable is null when you try and use it in OnStateChange when the state is State.DataLoaded or State.Terminated.

        You cannot use a null variable. You must set the account before the code is called in OnStateChange when the State is State.DataLoaded.

        Assigning the account in OnBarUpdate will not work, because this is not run until after the code causing error has be run in OnStateChange.

        You need to assign the account variable to an Account object before it used.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #19
          Add Profit Target and Stop Loss

          Chelsea, I think we're going in circles here.

          Unless the attached code I am sending you is not being updated when exported, I can assure you that there is a check for a null value for account in both Terminated and DataLoaded.

          To assure we are looking at the same code, why don't you copy those two areas and place it in the Reply. Let's confirm that we are on the same page.
          Attached Files

          Comment


            #20
            Add Profit Target and Stop Loss

            Chelsea, here's the latest code. It explicitly sets a new instance of all values in question.

            This has helped in getting OnBarUpdate() to work (at least once) and I am getting the current account name. However, changing the Account on Chart Trader does not get picked up.

            Any thoughts?

            I'm making some progress, but it's slow.
            Attached Files

            Comment


              #21
              Add Profit Target and Stop Loss

              Making further progress after removing the second account = xxx in OnBarUpdate().

              Now the code recognizes changes in the Account in Chart Trader.

              Moving forward to the next step: Orders.

              Comment


                #22
                Hello ronhb107,

                In the most recent post's file I am showing:
                Code:
                else if (State == State.Terminated)
                {
                	Print("\nState.Terminated");
                				
                	if (account != null)
                	{
                		account.AccountItemUpdate -= OnAccountItemUpdate;
                		account.ExecutionUpdate -= OnAccountExecutionUpdate;
                		account.OrderUpdate -= OnOrderUpdate;
                	}
                //				doOnce = false;
                }
                This should correctly prevent an error when the termination state is reached.

                The previous post's file, however, showed:
                Code:
                else if (State == State.Terminated)
                {
                	MainAccount.AccountItemUpdate -= OnAccountItemUpdate;
                	MainAccount.ExecutionUpdate -= OnAccountExecutionUpdate;
                //				MainAccount.OrderUpdate -= OnOrderUpdate;
                	doOnce = false;
                }
                This would cause an error.

                Also from the most recent post's file:
                Code:
                if (account != null)
                {				
                	lock (Account.All)
                		account = Account.All.FirstOrDefault(a => a.Name == account.Name);
                		//MainAccount.AccountStatusUpdate += OnAccountStatusUpdate;
                				
                
                	account.AccountItemUpdate += OnAccountItemUpdate;
                	account.ExecutionUpdate += OnAccountExecutionUpdate;
                	account.OrderUpdate += OnOrderUpdate;
                }
                This will only create a new account if an account object is already assigned to the account variable and account is not null.
                Since its not created first, it will always be null.
                This code will never run.

                You should assign the account outside of the check for null.


                If you want to know when an AtmStrategySelector has changed, add an event handler to the AccountSelector.SelectionChanged event.
                Last edited by NinjaTrader_ChelseaB; 01-17-2018, 03:19 PM.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #23
                  Hi Chelsea:

                  I have eliminated all the Object errors.

                  However, a couple of questions.

                  First, if I don't select an ATM strategy, the program does not recognize any Entry placements, executions (fills), or cancellations (all of which are recognized if an ATM strategy is selected). Why?

                  And second, when the program executes a change in the stop-loss quantity and then tries to add a Target (for the now reversed position), the added Target does not show up on the chart (nor does it fill).

                  Btw, none of the Event handlers are firing, and occasionally the system acts odd by moving or filling unseen orders (I suspect the interaction of the Target order or failure to remove orders from the Orders collection).

                  See attached program update.

                  Ron

                  Thanks.
                  Attached Files
                  Last edited by ronhb107; 01-20-2018, 09:06 AM.

                  Comment


                    #24
                    Hello Ron,

                    In OnStateChange when State is State.DataLoaded, the account variable is never set to anything. This does not hold an account.

                    Where you have:
                    if (account != null)

                    The account variable is null. That whole section is skipped and no method handlers are ever assigned to any accounts.

                    Add a print to the action block of this condition. Does the print appear?

                    Lines 95 and 96 are never hit.
                    Code:
                    lock (Account.All)
                    	account = Account.All.FirstOrDefault(a => a.Name == account.Name);
                    This never runs.

                    You are requiring the account variable to have an account before an Account object is assigned to the account variable.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #25
                      Add Profit Target and Stop Loss

                      Chelsea:

                      The attached works, although I had to bypass using the Event handlers and use the Orders.Clear() after all orders had been processed and the position closed.

                      I have tried numerous ways to use the DataLoaded method for the Event handlers to no avail. Perhaps it should be loaded elsewhere (your thoughts). (Btw, I left the original code for DataLoaded unchanged after exhausting other approaches.)

                      See the attached.
                      Attached Files
                      Last edited by ronhb107; 01-22-2018, 09:19 PM.

                      Comment


                        #26
                        Hello Ron,

                        You have not corrected the logic in OnStateChange when State is State.DataLoaded.

                        Create your account before checking it is null.

                        You need to check for a variable to not be null when you are going to use the variable. You don't have to check for null when you are assigning a variable.

                        Change:
                        Code:
                        if (account != null)
                        {				
                        	lock (Account.All)
                        		account = Account.All.FirstOrDefault(a => a.Name == account.Name);
                        		//MainAccount.AccountStatusUpdate += OnAccountStatusUpdate;
                        
                        	account.AccountItemUpdate += OnAccountItemUpdate;
                        	account.ExecutionUpdate += OnAccountExecutionUpdate;
                        	account.OrderUpdate += OnOrderUpdate;
                        					
                        	Print("State.DataLoaded account set");
                        }
                        To:
                        Code:
                        lock (Account.All)
                        	account = Account.All.FirstOrDefault(a => a.Name == account.Name);
                        if (account != null)
                        {			
                        		//MainAccount.AccountStatusUpdate += OnAccountStatusUpdate;
                        	account.AccountItemUpdate += OnAccountItemUpdate;
                        	account.ExecutionUpdate += OnAccountExecutionUpdate;
                        	account.OrderUpdate += OnOrderUpdate;
                        					
                        	Print("State.DataLoaded account set");
                        }
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #27
                          Add Profit Target and Stop Loss

                          Chelsea:

                          This current code appears to work properly.

                          I've moved the Event handler initialization out of DataLoaded and into its own method Subscribe().

                          Next step is to move the code within OnBarUpdate() to its own method and have the Event handler OnOrderUpdate() handle the triggering event (rather than require 'By Tick').

                          Let me know if there are any additional issues when you review the code.

                          Thanks
                          Attached Files

                          Comment


                            #28
                            Hello Rob,

                            The Subscribe() method checks for null, but still the account is only assigned in OnBarUpdate on line 145..

                            I am advising you assign the account variable to an Account object in OnStateChange() when State is State.DataLoaded outside of a check for null.

                            And that you remove the assigning of the Account object out of the OnBarUpdate method which updates anytime a bar closes.

                            (Notice that you are assigning the account object without a check for null in OnBarUpdate()).

                            The assignment doesn't need a check for null. Anytime you use a variable, this is when it would need a check for null.

                            However, if you script is behaving as you would like, are you having an issue you would like assistance with?
                            Last edited by NinjaTrader_ChelseaB; 01-24-2018, 03:23 PM.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #29
                              "I am advising you assign the account variable to an Account object in OnStateChange() when State is State.DataLoaded outside of a check for null."

                              I have tried that, Chelsea. No matter what I do to get the account, there is always an error within DataLoaded.

                              Perhaps you can provide me with the appropriate code that works for DataLoaded.

                              Thanks

                              Comment


                                #30
                                Hello Rob,

                                If you have tried this, you have not posted it to the forum in this thread in any of your posts.

                                In post #26 I have given an example of what to change in State.DataLoaded.

                                Did you try my advised correction?

                                In other words, did you move the assignment of the variable outside of the check for null while leaving the calls to the variable within the check for null?

                                If so, please post this to the forum so that we may see you have tried this.
                                Chelsea B.NinjaTrader Customer Service

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by bmartz, 03-12-2024, 06:12 AM
                                4 responses
                                31 views
                                0 likes
                                Last Post bmartz
                                by bmartz
                                 
                                Started by Aviram Y, Today, 05:29 AM
                                4 responses
                                12 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  
                                Started by cls71, Today, 04:45 AM
                                1 response
                                7 views
                                0 likes
                                Last Post NinjaTrader_ChelseaB  
                                Working...
                                X