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 Flatten() after instrument name rolls over?

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

    How to Flatten() after instrument name rolls over?

    I can't seem to get around the paradox of needing the instrument name in order to Flatten() the position but I need to supply the instrument name in order to GET the instrument?

    If I have to hard-code the name, that means every three months I have to remember to update the code after a rollover of the ES.

    Flatten() example in help:
    Account.Flatten(new [] { Instrument.GetInstrument("ES 12-15") });

    GetInstrument() example in help:
    if (State == State.Historical) {
    Instrument myInstrument = Instrument.GetInstrument("AAPL");

    Print("AAPL's tick size is " + myInstrument.MasterInstrument.TickSize);
    }

    So, is there a dynamic way to get the name of the instrument that the strategy has been applied to?

    #2
    Hello hillborne,

    You can dynamically access the configured instrument for a NinjaScript by accessing the Instrument object.

    For example you could add this instrument object to an array or a collection to be used in Account.Flatten().

    Code:
    Account.Flatten(new [] { Instrument });
    You can also print the full name of the instrument by accessing Instrument.FullName

    Code:
    Print(Instrument.FullName+"'s tick size is " + Instrument.MasterInstrument.TickSize);
    As noted in the documentation, these should only be accessed after State.DataLoaded has been reached.

    Here is a link to the documentation on the Instrument object - https://ninjatrader.com/support/help...instrument.htm

    Please let me know if I may be of further help.
    JimNinjaTrader Customer Service

    Comment


      #3
      Ah! So is it as simple as:

      Account.Flatten( new [] { Instrument.GetInstrument (Instrument.FullName) });

      If so...that will probably lead me to ask if there's any references on if or how to program the rollover. Since the strategy is attached to the chart of one contract, I wonder how to program the attaching to the next contract?

      The only thing I could think of would be to AddDataSeries() the next several future contracts but that would require a lot of cumbersome coding for BarsInProgress and making sure the order is submitted to the active contract...

      Comment


        #4
        Hello hillborne,

        Thanks for the reply.

        Since additional data series must be hard coded, programmatically switching between contract months would be complicated. You may be better served to assign the strategy to additional contract months when those begin trading, or you could use the Continuous Contract Month (##-##) to keep the strategy running continuously.

        Pre-configuring a list of contract months to use in the strategy is possible, but the strategy would have to have to know when to switch. When it should switch, it would then need to change the data series that are used in the OnBarUpdate() logic. This could get very complex.

        Changing the contract month when the rollover date occurs forces you to check on your strategy so changing the contract month every rollover date can have value in verifying the strategy behavior.

        Please let me know if I may be of further assistance
        JimNinjaTrader Customer Service

        Comment


          #5
          Hi Jim,

          Thank you for all your help so far. I was doing some research through some past questions here regarding the continuous contract for the ES and wanted to confirm that you can't actually place trades on it. You can only use it for continuous reference and trades would have to be placed on the current contract anyway. Is that correct?

          Yes, checking in on the strategy is important but I wanted to program the most flexibility in case I'm traveling or not able to get to a computer for a week or two. I can just program a 'stop trading' date in the worst case.

          Comment


            #6
            Hello hillborne,

            Your research is correct.

            Since the Continuous Contract month has no defined front month, it cannot be used for live trades. It can be only used for sim accounts that fill locally.

            I will submit a feature request on your behalf to request a way to problematically change a contract month. When I get the ticket number, I will update this post/thread.

            Thanks in advance for your patience.
            JimNinjaTrader Customer Service

            Comment


              #7
              Awesome! It does seem a little strange to be unable to have the instrument selected through code so that would be a nice change.

              Lastly, can you confirm the code I wrote is correct for flattening positions? It doesn't seem to work but I haven't investigated it completely, yet:

              Account.Flatten( new [] { Instrument.GetInstrument (Instrument.FullName) });

              Comment


                #8
                Hello hillborne,

                I have tested this line and it works as intended. The position is flattened for that instrument and the strategy is disabled as Flatten() has been called.

                If you would like to keep the strategy alive, you will have to manually flatten the open positions instead of calling Flatten(). You could loop through the Positions object to do this.

                Here is a link to the relevant help documentation: https://ninjatrader.com/support/help...ns_account.htm

                The ticket ID tracking the feature request for changing the contract month programmatically is being tracked with SFT-2259

                Thanks for providing input to help improve NinjaTrader!
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Hi Jim,

                  Could you also change the documentation for Flatten() to mention that it disables strategies?


                  I'd also like to request that it DOES NOT disable strategies or at least have an overload available so that there's a quick and simple way to 'nuke' everything if you want to get to a flat position.

                  I'm using the managed approach and am a little unclear on how to use Positions. Assuming I'm using named entry signals, how would I cancel the following group of orders (1 base order and 2 runners) that consists of:

                  3 long limit orders to enter
                  1 profit target for the 'base' order
                  3 stops that will be adjusted

                  I see the iteration in Positions but I'm not sure if I should use Cancel() on the stops and then ExitLong() x 3? Is there any problem if I call ExitLong() if I'm not in a position or Cancel() on orders that no longer exist?

                  Thanks, Jim!
                  Last edited by hillborne; 05-16-2017, 09:45 PM.

                  Comment


                    #10
                    Hello hillborne,

                    I have submitted a feature request on your behalf to add a note to the Account.Flatten documentation.

                    Before we submit a feature request for Account.Flatten() so it does not disable a strategy, let's model the behavior you would like to see.

                    As the call will flatten the position for the instrument on the account, this could adversely affect other strategies if they are enabled on that instrument and another strategy sends a call to flatten the position. Considering these implications, it might be better to create this logic for your own purposes. Could you describe exactly what you would want this additional Account.Flatten() overload to do and provide insight on how you think the following implications should be handled?
                    • Should the method flatten the position only for that strategy?
                    • Should it flatten every position on that instrument, including those created by other strategies?
                    • What should happen to other strategies when this method is called?


                    If you would like to flatten the position for the strategy, you could call both ExitLong() and ExitShort(). As an example:

                    Code:
                    if (Position.MarketPosition == MarketPosition.Flat)
                    {
                    	ExitShort();
                    	ExitLong();
                    }
                    ExitLong() and ExitShort() will exit all positions in their respective direction when you do not specify a fromEntrySignal. The signalName makes the order unique so you can modify and close it separately when you reference it in the fromEntrySignal argument.

                    You can reference the documentation on these methods for syntax on all of the overloads and how they work.


                    As the help guide states in the CancelOrder() documentation, it is possible for an order to be partially filled before it is canceled. As such it would be advised to use OnOrderUpdate() to catch orders status updates.
                    CancelOrder() - https://ninjatrader.com/support/help...ancelorder.htm

                    We have sample code that demonstrates how to use OnOderUpdate(), OnExecutionUpdate() and Order objects to manage positions. I would suggest to review this sample and the associated documentation, as it could be useful for creating a strategy that does the tasks you have listed:

                    3 long limit orders to enter
                    1 profit target for the 'base' order
                    3 stops that will be adjusted
                    SampleOnOrderUpdate() - http://ninjatrader.com/support/forum...ead.php?t=7499

                    I look forward to being of further assistance.
                    JimNinjaTrader Customer Service

                    Comment


                      #11
                      Hi Jim,

                      Ah, I missed the fact that ExitLong/Short() will flatten the position as well. I though that once you started using entrysignals that you had to keep using them. So, please disregard my request regarding Flaten().

                      Thanks, again!

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by geddyisodin, Today, 05:20 AM
                      0 responses
                      1 view
                      0 likes
                      Last Post geddyisodin  
                      Started by JonesJoker, 04-22-2024, 12:23 PM
                      6 responses
                      32 views
                      0 likes
                      Last Post JonesJoker  
                      Started by GussJ, 03-04-2020, 03:11 PM
                      12 responses
                      3,239 views
                      0 likes
                      Last Post Leafcutter  
                      Started by AveryFlynn, Today, 04:57 AM
                      0 responses
                      5 views
                      0 likes
                      Last Post AveryFlynn  
                      Started by RubenCazorla, 08-30-2022, 06:36 AM
                      3 responses
                      79 views
                      0 likes
                      Last Post PaulMohn  
                      Working...
                      X