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

PositionAccount for Strategy Control

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

    PositionAccount for Strategy Control

    As my strategy commonly holds positions overnight and PC reboots are inevitable NT8's PositionAccount methods are very appealing for a strategy to easily understand where it left off. However I may not be understanding them correctly after converting my NT7 to NT8 code and wanted to verify with someone.

    NT7 (Code Theory)
    Code:
    if (Position.Quantity < 1) //Buy Code
    {
        if (conditions are good)
        EnterLong(xShares,"")
    }
    if (Position.Quantity >= 1) //Sell Code
    {
        if (Close[0] > Position.AveragePrice)
        ExitLong("","")
    }
    NT8 (Code Theory)
    Code:
    if (PositionAccount.Quantity < 1) //Buy Code
    {
        if (conditions are good)
        EnterLong(xShares,"")
    }
    if (PositionAccount.Quantity >= 1) //Sell Code
    {
        if (Close[0] > PositionAccount.AveragePrice)
        ExitLong("","")
    }
    It makes sense that I can't back test this strategy because it is pulling from live account info so if the live account is flat then when the strategy tries to EnterLong it doesn't actually happen to the account so the status never changes.

    So in order to test I can just switch back the code to the NT7 like version above, test, and then change again to run it live. Correct?

    The ultimate goal is for the strategy to enter the market if X conditions are true then if the PC must be restarted for it to know these basic values and continue operating so that after restarting when Y conditions are true it exits.

    Am I on the right track or have I fallen off my tricycle?

    #2
    Try running you backtest using account SIM101 and adjust the simulator account balance to match your broker account? You can also forward test your strategy with live data using the SIM101 account until your confident it is ready for trading with real money.

    In the Control Center's Strategies Tab's Ninjascript Tab (or Options Property Group Controls in NT8), there is a 'Handling' setting "Keep Running". The overnight position will be logged at any data service disconnect and maintained until you reconnect the next morning, running again as though there was no disconnect.

    Comment


      #3
      Thanks for the reply borland.
      I added an extra bool user setting called Use Live that changes where quantity, avgprice, cashvalue come from at the top of OnBarUpdate.

      Code:
      // Live or Simulation Setup //
      if (live)
      {
          avgPrice = PositionAccount.AveragePrice;
          quantity = PositionAccount.Quantity;
          currentCash = Account.Get(AccountItem.CashValue, Currency.UsDollar);
      }
      else
      {
          avgPrice = Position.AveragePrice;
          quantity = Position.Quantity;
          currentCash = 10000.00;       // Set currentCash to 10k, used for backtesting
      }
      Back testing works well now with just unchecking the Use Live box in Strategy Settings. However I'm having an issue with the live side of it. The values above work as I can see them in my CSV log file however if an order is manually placed then the strategy is started with Use Live and the sell conditions become true it never sells with ExitLong.

      I think I have to now use the unmanaged order entry and exits. What do you think?

      Comment


        #4
        Finally, it looks like I don't need to do any of this stuff and keep the NT7 Code Theory usage as long as I add IsAdoptAccountPositionAware = true. Then a 5th option appears under Start Behavior which handles this.
        Tested by manually entering long on an instrument in Sim101-Simulated Data Feed, starting the strategy, and using the Trend adjustment to cause the Sell Conditions to become true. The strategies CSV log file showed that it started only from the Sell Conditions portion of code until the sell executed, then it switched back to the Buy Conditions of code.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by junkone, Today, 11:37 AM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by frankthearm, Yesterday, 09:08 AM
        11 responses
        41 views
        0 likes
        Last Post frankthearm  
        Started by quantismo, 04-17-2024, 05:13 PM
        5 responses
        35 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by proptrade13, Today, 11:06 AM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_Clayton  
        Started by love2code2trade, 04-17-2024, 01:45 PM
        4 responses
        34 views
        0 likes
        Last Post love2code2trade  
        Working...
        X