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 check that an instrument is flat before allowing the strategy to continue?

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

    How to check that an instrument is flat before allowing the strategy to continue?


    I want to check upon startup that my code is running in a flat environment for the Instrument. It already is set to wait until flat. But it doesn't actually wait until flat. and this code I created also doesn't work. What I mean is, I create a short position in the chart trade for an instrument... then when I enable the strategy, the strategy runs immediately and won't print this error message.

    also if I remove this code, and just rely on "wait until flat".. that also doesn't care about the flat state of the instrument in the Positions tab beforehand. Both chart trader and my strategy run on Sim101

    This code from OnStateChange I created, I can't get it to work

    else if (State == State.Active)
    {
    if (PositionAccount.Quantity > 0) {
    Print(String,Format("{0} already has open position in the Account\n{1}\nStopping {2}", Instrument.MasterInstrument.Name, PositionAccount, this.GetType().Name));

    CloseStrategy(this.GetType().Name);
    }
    Last edited by NinjaCustomer; 04-18-2019, 07:54 AM.

    #2
    Hello NinjaCustomer,
    Thanks for your post.

    When using 'Wait until flat', if your account position is not flat. the strategy will place virtual trades until the Strategy Position reaches or crosses a flat state. Once a flat state is achieved the strategies position would be assumed to be in sync with the account position and orders would begin to be placed live. Depending on your specific needs 'Wait until flat' may not be the right setting for you. Please see the following link for more details on StartBehavior settings: https://ninjatrader.com/support/help..._positions.htm

    I also suggest checking out our Strategy Position Versus Account Position help guide article as that may clear some things up as well: https://ninjatrader.com/support/help...__account_.htm

    In regards to checking your strategy or account position with code, I suggest moving any logic like this to the top of OnBarUpdate() or perhaps even State.DataLoaded. See the sample on the PositionAccount help guide for a sample on how to check for flat conditions: https://ninjatrader.com/support/help...etposition.htm
    Josh G.NinjaTrader Customer Service

    Comment


      #3
      thanks, yes I have read those - but I believe you might be right .. I was unsure about what Active did just from reading the help, and this is the first time I have put code in there. I have since posting this thread, found that AccountPosition.Quantity and Position.Quantity do work as expected in other areas where I have put them (such as in OnAccountItemUpdate() )

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by ScottWalsh, Today, 04:29 PM
      0 responses
      2 views
      0 likes
      Last Post ScottWalsh  
      Started by rtwave, 04-12-2024, 09:30 AM
      2 responses
      21 views
      0 likes
      Last Post rtwave
      by rtwave
       
      Started by tsantospinto, 04-12-2024, 07:04 PM
      5 responses
      69 views
      0 likes
      Last Post tsantospinto  
      Started by cre8able, Today, 03:20 PM
      0 responses
      7 views
      0 likes
      Last Post cre8able  
      Started by Fran888, 02-16-2024, 10:48 AM
      3 responses
      49 views
      0 likes
      Last Post Sam2515
      by Sam2515
       
      Working...
      X