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 update account value after a trade in backtest?

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

    How to update account value after a trade in backtest?

    Hi Ninjatraders,

    Is there a sample code of NT 8 to update the account value after every trade?

    I am trying to do the position sizing with my strategy on Forex, and there are several confusions I do not fully understand.
    1) So the PointValue is 1, and TickSize is 1E-05, (I confirmed this in the output window) then, this means by default that the smallest pip is 1E-05, and the PointValue does not means the value of a standard lot-100,000 contracts, but one single contract?
    2) The buying power of the back test account(displayed as "Backtest" in the performance statistics) seems to have no limit? I used these code to use the "SIM101" ($100,000) account value to do the position sizing:

    Code:
    lock(Account.All)  // C# statement to secure a object from changing in a multi-thread processing.
                account=Account.All.FirstOrDefault(a => a.Name == "Sim101"); // assign "Sim101" to "account".  
    
                xAccountSize=Account.Get(AccountItem.CashValue, Currency.UsDollar); // get account value.
    While in a single trade the backtest account made a buy of 2020202 contracts, that is more than 20 standard lots, which will cost more than 2.5 million.
    Code:
    @ 2017-05-18 11:40:00 Bactest Account size is 100000;PointValue is 1;TickSize is 1E-05;Pips to risk is 99; Contracts to risk is 2020202
    This seems to be very complicated area, suggestion of any further readings are highly appreciated.

    Thanks.

    #2
    Hello wolfcuring,
    Thanks for your post.

    Is there a sample code of NT 8 to update the account value after every trade?
    The account value will already update after every trade. If you are trying to track order fills I suggest using OnExecutionUpdate(). A sample that my colleage posted also comes to mind. The sample demonstrates how to tally the total account PnL inside a strategy and you could likely modify this to fit your needs.

    Daily Loss LImit Sample -- https://ninjatrader.com/support/foru...limit-examples
    OnExecutionUpdate() -- https://ninjatrader.com/support/help...tionupdate.htm

    1) So the PointValue is 1, and TickSize is 1E-05, (I confirmed this in the output window) then, this means by default that the smallest pip is 1E-05, and the PointValue does not means the value of a standard lot-100,000 contracts, but one single contract?
    PointValue Indicates the currency value of 1 full point of movement on that instrument.

    PointValue -- https://ninjatrader.com/support/help...pointvalue.htm

    2) The buying power of the back test account(displayed as "Backtest" in the performance statistics) seems to have no limit?
    Correct. You would be expected to track that account value inside the script with your own variables. Can we see the order method that was used here to generate an order of that size?
    Josh G.NinjaTrader Customer Service

    Comment


      #3
      Hi Josh,

      Thank you for your reply. I think I did not make myself clear enough. Actually, what I want to do is to test something like a 2% position sizing on every trade.

      So, I copied some similar code from another example. And I think I should move the get account value part into State==State.Configure so that is checked only once when the strategy is initialized?

      Code:
      else if (State == State.Configure)
                  {            
                      lock(Account.All)  // C# statement to secure a object from changing in a multi-thread processing. 
                      account=Account.All.FirstOrDefault(a => a.Name == "Sim101"); // assign "Sim101" to "account".  
      
                      xAccountSize=Account.Get(AccountItem.CashValue, Currency.UsDollar); // get account value. 
      
                  }
      
      protected override void OnBarUpdate()
              {
                      // position sizing to calculate 2% the account 
                      double risktwopercent= xAccountSize*0.02
      
                      if(...)  EnterLong(risktwopercent, "L");
                      if(...)  ExitLong();
      
               }
      
      protected override void OnPositionUpdate(Cbi.Position position, double averagePrice, int quantity, Cbi.MarketPosition marketPosition)
              {
      
                  if(Position.MarketPosition ==MarketPosition.Flat)
                  {
                  // Check to make sure there is at least one trade in the collection
                      if (SystemPerformance.AllTrades.Count > 0)
                      {
                          // Get the last completed real-time trade (at index 0)
                          Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
      
                          // Calculate the PnL for the last completed real-time trade
                          double lastProfit = lastTrade.ProfitCurrency ;
      
                          xAccountSize= xAccountSize + lastProfit;
      
                      }
                  }
              }
      This worked as I expected. The account value updated after every trade. (If, like you said, the account value will be updated automatically, does it mean that the "OnPositionUpdate()" part is dispensable? )

      While there are still something wired going on. And my guess is still with the buying power. So, I tried several insane position sizing, like risk 10%, 50%, 80% on every trade, but, the equity curve remained unchanged. Only after I tried to risk 80% on every trade, the account finally turned into a negative number. So the output window show something like this:
      Code:
      @ 2017-12-15 18:00:00 Bactest Account size is -7444.7658599601;Pips to risk is 116; Contracts to risk is -5134322
      @ 2017-12-18 18:00:00 Bactest Account size is -7444.7639699601;Pips to risk is 158; Contracts to risk is -3769501
      By the way, if I edit the "sim101" account, and change the buy power from "0", to 100,000*200=20,000,000, is this the only way to set the leverage in Ninjatrader ?

      Comment


        #4
        wolfcuring,

        It sounds like you would not want to check account size inside State.Configure. It sounds like you wout want to do that Inside OnBarUpdate right before you submit an entry, to make sure everything is up to date.

        The account value updated after every trade. (If, like you said, the account value will be updated automatically, does it mean that the "OnPositionUpdate()" part is dispensable? )
        Are you asking if your code is going to work the same after changing the logic? It appears to me like if you removed OnPositionUpdate() your code would be broken.

        If I edit the "sim101" account, and change the buy power from "0", to 100,000*200=20,000,000, is this the only way to set the leverage in Ninjatrader ?
        Correct, that would be the only way to make any changes to a SIM account.
        Josh G.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by GussJ, 03-04-2020, 03:11 PM
        11 responses
        3,221 views
        0 likes
        Last Post xiinteractive  
        Started by andrewtrades, Today, 04:57 PM
        1 response
        10 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by chbruno, Today, 04:10 PM
        0 responses
        6 views
        0 likes
        Last Post chbruno
        by chbruno
         
        Started by josh18955, 03-25-2023, 11:16 AM
        6 responses
        436 views
        0 likes
        Last Post Delerium  
        Started by FAQtrader, Today, 03:35 PM
        0 responses
        9 views
        0 likes
        Last Post FAQtrader  
        Working...
        X