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

GetAccountValue not working for some instruments?

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

    #16
    Is nobody using GetAccountValue() as well? Unfortunately, the problem is still existing

    Comment


      #17
      Hello cNuuuuuu,

      Thank you for your response.

      Are you testing GetAccount Value() on Real-Time data or in backtesting? Is the value returning 0 on historical bars?

      Comment


        #18
        Hey Patrick,

        I am testing the script with my Interactive Brokers paper account with RT-data of the DOW30 stocks. Usually I am testing on a 5 Min basis, so I can see the results/effects of code modifications more quickly.

        As it was written in the NT manual that backtests/historical bars will return 0, i honestly didn't even try it

        The weird thing is that my code seems to work for most instruments at the second day. I suppose that the "FirstBar"-Method is then starting to work.

        Assume I start the strategy at 15:45. The statement: "if (Bars.FirstBarOfSession && FirstTickOfBar)" shouldn't be triggered, as the first bar is finished at 15:31. Or even if it is triggered, it should by logic return the value of 0, as it is a historical bar, although it is only 14 minutes ago...

        Nevertheless, I would suppose that OnStartUp() should return the correct account value. I guess that the order quantity of 100 is set by Ninja as default, as the "set order quantity - by strategy" returns a value of 0.

        However, I only want to program "Before you start to trade at that day, check the account value. Do this again the next day." It does not necessarily has to check to be the first bar of the session. So I am also open for other code which executes this command.

        I hope this information helps to understand the problem

        Here again my used codelines.

        Code:
        protected override void OnBarUpdate()         
        { if (Bars.FirstBarOfSession && FirstTickOfBar)                 
          { Print ("The date is:" + String.Format("{0:d/M/yyyy HH:mm:ss}", Time[0]) + "and now is:" + DateTime.Now.ToString());
        Dquantity = (GetAccountValue(AccountItem.BuyingPower)*0.4 / 30);
        Print(Instrument.FullName+": Buying-Power is "+GetAccountValue(AccountItem.BuyingPower));
        Print(Instrument.FullName+": Cash amount available for shares is "+Dquantity);
          }
        }
        Code:
        protected override void OnStartUp()
        {
        Dquantity = (GetAccountValue(AccountItem.BuyingPower)*0.4 / 30);
        Print(Instrument.FullName+": Cash amount available for shares is"+Dquantity);
        }
        Last edited by cNuuuuuu; 01-28-2015, 02:39 AM.

        Comment


          #19
          Hello cNuuuuuu,

          Thank you for your note.

          The Account Values would not be loaded on start up or in historical bars. Yet you have code in place OnBarUpdate() for the first of the first bar, which would produce the needed value should that first bar of the session be a real-time bar. Unfortunately, if you enable the strategy after the first tick of the first bar of the session it your Account Values are going to be zero unless you call them outside of this or the first tick of the first bar occurs after your start the strategy.

          Comment


            #20
            Hey Patrick,
            thanks for your reply. Makes sense...

            Basically, I only want to program "Before you start to trade at a certain day, check the account value." It must not be "FirstbarOfSession". I just don't wan't Ninja is always checking the account value before every trade...

            Any ideas how I might do this?!

            Comment


              #21
              I just had an idea how to solve this issue.

              Under Variables I define private bool AV = false.
              And i use the following code:

              Code:
              protected override void OnBarUpdate()
              {
                      if (AV != true)
                         Dquantity = (GetAccountValue(AccountItem.BuyingPower));
                         AV = true;
                         Print ("AV:"+AV);    
                    
                      if (Bars.FirstBarOfSession && FirstTickOfBar)
                        {
                         AV = false
                        }
              }
              Unfortunately, NT checks the account value on every bar, although Print ("AV:"+AV) returns the value "true. So theoretically the first block of my code shouldn't be executed, right?! Or where is my mistake?

              Comment


                #22
                Hello cNuuuuuu,

                I think you are looking to place the GetAccountValue(), the bool set and the print nested inside the IF statement for AV != true.

                Additionally, you may want to place the FirstBarOfSession above the check, so that it reads out logically, if our reset condition is true then we want to reset our variables, if not then we can continue on.
                Currently, the reset will take place on the next bar because we are setting the bool to false after its checked
                Code:
                 if (Bars.FirstBarOfSession && FirstTickOfBar)
                       {
                        AV = false
                       }
                
                if (AV != true)
                      {
                        Dquantity = (GetAccountValue(AccountItem.BuyingPower));
                        AV = true;
                        Print ("AV:"+AV);    
                      }
                Cal H.NinjaTrader Customer Service

                Comment


                  #23
                  Hey Cal,

                  thank you for your answer. I'll test the code when markets are open again

                  Currently, the reset will take place on the next bar because we are setting the bool to false after its checked
                  Why is it set to false? The if (Bars.FirstBarOfSession && FirstTickOfBar) should only be triggered once per day at session start. From my understanding this should not be triggered at the next bar...

                  But we will see, if it works now

                  Comment


                    #24
                    mmmmh again the same problem arises...

                    When I restart the strategy during the trading session, NT only buys the default size of 100 stocks...

                    I define under variables private bool AV = false; then it should logically trigger condition 2 and use the right account value. However, when starting the strategy outside trading hours, it seems to get the right account value.

                    Code:
                    //condition 1
                     if (Bars.FirstBarOfSession && FirstTickOfBar)
                     {
                                    AV = false;
                     }
                    
                    //condition 2
                    if (AV != true)  
                    {
                    Dquantity = (GetAccountValue(AccountItem.BuyingPower)); 
                    AV = true;
                    Print ("AV:"+AV);
                    }
                    I don't understand why it is not working, because the logic is so simple...
                    Attached Files

                    Comment


                      #25
                      Hello,

                      Thank you for your response.

                      You would need to ensure you were on a real-time bar and not on a historical bar, then proceed to check the first tick of the first bar of a session. Please refer to the code below and let me know if you have any questions.
                      Code:
                              #region Variables
                              private double Dquantity = 0;
                      		private bool initRTbar = false; // initial real-time bar
                              #endregion
                              protected override void Initialize()
                              {
                      			
                              }
                              protected override void OnBarUpdate()
                              {	
                      			// If we are now on a real-time bar, set the Dquantity, but make sure this only occurs once.
                                  if(!Historical && !initRTbar)
                      			{
                      				Dquantity = (GetAccountValue(AccountItem.BuyingPower));
                      				initRTbar = true;
                      			}
                      			// On the first tick of the first bar of a new session after the initial real-time bar set the Dquantity...
                      			// ...and do so for each following first tick of the first bar of a new session.
                      			if(Bars.FirstBarOfSession
                      				&& FirstTickOfBar
                      				&& initRTbar)
                      			{
                      				Dquantity = (GetAccountValue(AccountItem.BuyingPower));
                      			}
                              }

                      Comment


                        #26
                        Hey PatrickH,

                        you are awesome!
                        This was what I was searching for. Code works as it should!

                        Just a question of understanding:
                        What is the exclamation mark (!) in front of Historical or initRTbar exactly meaning?
                        I would guess sth like "is not true"?! So could I also write instead " initRTbar != true?

                        Thanks for your answer and again thank you all very much for finally solving this problem

                        Comment


                          #27
                          Correct, or you could say myBool == false.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Orion815, 05-02-2024, 08:39 AM
                          2 responses
                          15 views
                          0 likes
                          Last Post Orion815  
                          Started by suroot, 02-25-2017, 04:43 AM
                          11 responses
                          2,548 views
                          0 likes
                          Last Post Zilvercat  
                          Started by Rogers101, 05-05-2024, 11:30 AM
                          16 responses
                          50 views
                          0 likes
                          Last Post Rogers101  
                          Started by ninza33, Today, 12:31 PM
                          2 responses
                          10 views
                          0 likes
                          Last Post ninza33
                          by ninza33
                           
                          Started by Bobin, 03-12-2024, 08:51 AM
                          15 responses
                          482 views
                          0 likes
                          Last Post fiddich
                          by fiddich
                           
                          Working...
                          X