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

Pass Value Generated from BarsType to a Strategy

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

    Pass Value Generated from BarsType to a Strategy

    Hi, I am trying to do something similar as described in this post, but passing a value generated within a custom BarType, not from an indicator, to a strategy; adopting the same approach as described here does not work. Could you please suggest how to proceed? Thank you, Martin

    #2
    Originally posted by martin70 View Post
    Hi, I am trying to do something similar as described in this post, but passing a value generated within a custom BarType, not from an indicator, to a strategy; adopting the same approach as described here does not work. Could you please suggest how to proceed? Thank you, Martin
    Hello Martin,

    Thank you for your post.

    This thread is in the NinjaTrader 7 portion of the forum. Are you trying to accomplish this in NinjaTrader 8? If so, I would be glad to move your post into its own thread in the appropriate section of the forum and assist you from there. Please clarify your version of NinjaTrader, which may be found at Control Center > Help > About.

    I look forward to your reply and assisting you further.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Hi Emily, yes sorry, I am using NT8. Thank you

      Comment


        #4
        Hello martin70,

        No worries! Thank you for clarifying; I have moved your post to its own thread in the appropriate topic.

        So I may better assist you and understand if what you are describing may be achieved, please provide more details. I understand you want to pass a value generated within a custom BarType to a strategy. What kind of value are you looking to pass over to the strategy? How is this value currently being set in the BarsType script?

        I appreciate your patience and look forward to your reply.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Hi Emily, within OnDataPoint a double value is calculated, that represents the high and low where the bar would be closed before creating a new one. I need to pass these values to the startegy in order to place stop and limit orders at those values. I want to avoid streamwriter as working on tick data. Thank you

          Comment


            #6
            Hello martin70,

            Thank you for your reply.

            You should be able to access the desired BarsType value by calling AddDataSeries() in your strategy and adding the custom BarsType. The following tips from the AddDataSeries() page are relevant to using a custom BarsType:
            2. You can add a custom BarsType which is installed on your system by casting the registered enum value for that BarsPeriodType. For example: AddDataSeries((BarsPeriodType)14, 10);

            3. You can specify optional BarsPeriod values (such as Value2) of a custom BarsType in the BarsPeriod object initializer. For example: AddDataSeries(new BarsPeriod() { BarsPeriodType = (BarsPeriodType)14, Value = 10, Value2 = 20 });
            https://ninjatrader.com/support/help...dataseries.htm

            From there, you could access the high and low from the added series using Highs and Lows and reference the proper barsSeriesIndex for the added series:For more information about working with scripts that have added bars objects, please see the following page:


            Please let me know if I may be of further assistance.
            Emily C.NinjaTrader Customer Service

            Comment


              #7
              Hi Emily,
              sorry this is not what I need. I have a double value calculated within the BarType code that needs to be passed to the strategy BEFORE highs or lows are formed. This needs probably to be done through a method, but I am not suceeding in making that happen. Thank you, Martin

              Comment


                #8
                Hello martin70,

                Thank you for your reply.

                Is the double being calculated in OnDataPoint() within your BarsType script? OnDataPoint() is solely used to adjust data points for your series. The reason the Update() method would not work to try and get the value is because that forces OnBarUpdate() to be called for indicator values to be updated to the current bar index. For more information regarding Update() and how it works:Otherwise, how is the double being calculated and where in your BarsType script? Is there any way to perform the same calculation within the strategy itself rather than trying to pull the value from the BarsType script?

                I appreciate your patience and look forward to your reply.
                Emily C.NinjaTrader Customer Service

                Comment


                  #9
                  Hi, no,,the value is a variable within the BarType script that needs to to be passed to a Strategy without using StreamWriter.

                  Comment


                    #10
                    Hello martin70,

                    Thank you for your reply.

                    How is the value of the variable being calculated within the BarsType script? If possible, please provide a snippet that demonstrates how the variable is set.

                    I look forward to your reply.
                    Emily C.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi, I am coming back to an unsolved issue.

                      Referring to this post



                      I created a Series<T> within my custom BarType:

                      public class MyTestBarType : BarsType
                      {
                      Series<double> mySeries;

                      [...
                      ....]

                      protected override void OnDataPoint(Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isBar, double bid, double ask)
                      {

                      MyValue = CalcMyValue(bars, time); //calls method

                      mySeries = new Series<double>(bars, MaximumBarsLookBack.Infinite);
                      mySeries[0] = MyValue;
                      ​....

                      The code compiles correctly.

                      Where I am asking your support now is: how do I get the mySeries[0] value from outside the custom BarType script, e.g. from a strategy script?
                      Does this eventually work in backtest as well?

                      Thank you
                      Martin


                      Comment


                        #12
                        Hello martin70,

                        You would need to use the code from this post and use the name of the public property that you made instead of the property name used in the sample.



                        In your code you did not define a public property so you need to fix that:
                        Series<double> mySeries;

                        needs to be

                        public Series<double> mySeries { :get; set; }

                        As long as you select the bars type in the backtest then yes that would also work there.

                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          Thank you for your reply.
                          I am replicating the example of the link you provided to run a test, adding
                          public double MyTestValue
                          {
                          get; set;
                          }
                          and
                          protected override void OnDataPoint(Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isBar, double bid, double ask)
                          {
                          if (high!=null && low !=null)
                          MyTestValue = (high+low)/2;
                          in the custom BarType (I used UniRenko for the test)

                          Then I did a Test strategy with the following:
                          protected override void OnBarUpdate()
                          {
                          NinjaTrader.NinjaScript.BarsTypes.UniRenkoBarsType testBarType = Bars.BarsType as NinjaTrader.NinjaScript.BarsTypes.UniRenkoBarsType ;
                          if (testBarType != null)
                          {
                          Print(testBarType.MyTestValue);
                          }
                          }
                          It works well on real time, but it does not work in backtest: it is repetedly printing out the first value.

                          Is there a way to get this to work also in backtest?

                          I attach the scripts of the UniRenkoBarType with the a.m. changes ad of the Test strategy.

                          Thank you
                          Martin


                          ​​
                          Attached Files

                          Comment


                            #14
                            Hello martin70,

                            Casting a bars type and having it work would require that you cast it and then use it. Are you trying the math in the strategy to confirm the calculation is happening?
                            JesseNinjaTrader Customer Service

                            Comment


                              #15
                              Hi, yes the calculation is happening, in fact in real time it works correctly. Only backtest is not working

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by geddyisodin, Today, 05:20 AM
                              0 responses
                              3 views
                              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