• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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
    JoshNinjaTrader 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 Atilla, Today, 04:05 PM
      0 responses
      6 views
      0 likes
      Last Post Atilla
      by Atilla
       
      Started by Displacer, Today, 02:57 PM
      2 responses
      13 views
      0 likes
      Last Post Displacer  
      Started by SAILIKITY, 05-22-2019, 05:41 PM
      1 response
      8 views
      0 likes
      Last Post NinjaTrader_PaulH  
      Started by rmillar, 01-09-2019, 06:02 PM
      6 responses
      71 views
      1 like
      Last Post eDanny
      by eDanny
       
      Started by mlarocco, Today, 11:39 AM
      4 responses
      23 views
      0 likes
      Last Post mlarocco  
      Working...
      X