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

OnStateChange

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

    OnStateChange

    I would like to be able to change the values below dynamically.

    I want to change the dataseries minutes based on the timeframe of the chart.
    I want to change the profit target and stop loss based on values passed it.

    Can I do this and when should I do this, since configure probably only fires once when a strategy starts.

    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Minute, 240);
    SetProfitTarget("", CalculationMode.Pips, 250);
    SetStopLoss("", CalculationMode.Pips, 50, false);
    }

    Thanks!!

    #2
    Hello,

    Thank you for the post.

    While techically in some cases you can use variables to make adding a series dynamic, it is not suggested. We have a note about this in the helpguide here:


    Arguments supplied to AddDataSeries() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided. Trying to load bars dynamically may result into an error similar to: Unable to load bars series. Your NinjaScript may be trying to use an additional data series dynamically in an unsupported manner.


    I will add a feature request for this item as well.

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

    Comment


      #3
      This is what I did. Thoughts?

      if (State == State.Configure)

      if (BarsPeriod.Value == 60)
      {
      AddDataSeries(Data.BarsPeriodType.Minute, 240);
      }

      if (BarsPeriod.Value == 15)
      {
      AddDataSeries(Data.BarsPeriodType.Minute, 60);
      }

      Comment


        #4
        Hello RiversideDude,

        Thanks for writing back to us.

        What you have outlined still constitutes as dynamically adding a data series. You are adding your additional data series, depending on if the primary data series has a certain period value. I would suggest to add both additional data series and use logic within your strategy to calculate for one data series or the other depending on the BarsPeriod.Value your primary data series specifies.

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

        Comment


          #5
          Good idea.

          Thanks!!

          Comment


            #6
            I added an additional dataseries using the strategy builder, is that dataseries accessible in Ninjascript?

            Also, it possible to name a dataseries so that I can refer to it by name?

            Thanks!!

            Comment


              #7
              Hello RiversideDude,

              Thanks for the reply.

              The Strategy Builder is the NinjaScript code generator for NinjaTrader 8. The code that it generates is NinjaScript and can be referenced by clicking "View Code"or "Unlock Code." Any code that gets generated by the Strategy Builder is usable in NinjaScript.

              Data Series are referenced by BarsInProgress or using the plural form of the common price objects. (Close[barsAgo]] -> Closes[dataSeries][barsAgo]) There is not an applicable way that I am aware of to use names for additional data series.

              Please refer to this documentation for referencing data in a multi series strategy:


              You may also wish to view the reference sample here:
              You can submit orders to different Bars objects. This allows you the flexibility of submitting orders to different timeframes. Like in live trading, taking entry conditions from a 5min chart means executing your order as soon as possible instead of waiting until the next 5min bar starts building. You can achieve this by


              Please let me know if you have any further questions.
              JimNinjaTrader Customer Service

              Comment


                #8
                Thanks Jim!

                Comment


                  #9
                  Situation:
                  I'm building a multiple timeframe strategy. Sometimes it will run with H4 and H1 other times it will run with H1 and M15. I only want to have one version of my strategy.

                  Problem:
                  How can I manually add a dataseries without having a duplicate dataseries when I apply the strategy to a chart?

                  Example: When I run it with H4 and H1. The H4 dataseries is added manually and the primary dataseries will be added based on the timeframe of the chart the strategy is added to. How do I reuse this strategy when I run it with H1 and M15?

                  If I add an H1 dataseries manually, I’ll have two H1 dataseries when I run it with H4 and H1.

                  Thanks in advance!!

                  Comment


                    #10
                    Hello RiversideDude,

                    Thanks for the reply.

                    Additional data series must be hardcoded to the strategy and cannot be added dynamically, or based off of logic.

                    Because of this, you will have to create multiple instances of your strategy so they can be used to match the primary data series that you wish to attach a trade on.

                    For example you can have own strategy that is designed to be added to a 5 minute ES 06-17 chart that adds a 1 hour data series for calculations. Another strategy could be designed to to be used on a 10 minute CL 04-17 chart that adds a 4 hour data series for calculations.

                    Since these data series are added in OnStateChange() under State == State.Configure, they must be hardcoded to the strategy. The only solution is to create multiple strategies that target each instrument and profit-taking goal.

                    Please let me know if you have any further questions.
                    JimNinjaTrader Customer Service

                    Comment


                      #11
                      Is there an issue with having multiple version of the same dataseries in a strategy? I could just add both dataseries and refer to the correct one based on the timeframe of the chart, right?

                      Thanks!!

                      Comment


                        #12
                        Hello RiversideDude,

                        Thanks for writing back.

                        Yes, you can add the same data series as the primary series as a secondary series within a strategy.

                        If you have any further questions, please don't hesitate to ask.
                        JimNinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Kaledus, Today, 01:29 PM
                        1 response
                        4 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Started by frankthearm, Yesterday, 09:08 AM
                        13 responses
                        45 views
                        0 likes
                        Last Post frankthearm  
                        Started by PaulMohn, Today, 12:36 PM
                        2 responses
                        16 views
                        0 likes
                        Last Post PaulMohn  
                        Started by Conceptzx, 10-11-2022, 06:38 AM
                        2 responses
                        53 views
                        0 likes
                        Last Post PhillT
                        by PhillT
                         
                        Started by yertle, Yesterday, 08:38 AM
                        8 responses
                        37 views
                        0 likes
                        Last Post ryjoga
                        by ryjoga
                         
                        Working...
                        X