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

Need help with new strategy please.

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

    Need help with new strategy please.

    Hi, I have attached the strategy I am currently working on and have a question regarding the Kagi bars.
    You will see in the (State == State.Configure) section I have added the Kagi bar type, using user defined parameters from above.
    In the OnBarUpdate() section you can see the Bool statements. "LongSignal" has some of the code commented out because it is causing an error. The semi colon after "LongSignal" is only there temporarily so it would compile. But what I need to know to fix this is how to reference the High and Low of a Kagi Bar? Because Kagi.High doesn't work.

    I will also say that this strategy runs on a Kagi chart but the trend will be determined off the UniRenko with the ATR Trailing indicator.

    I you could look at the rest of my code and give me any feedback or suggestions you may have, I'd really appreciate it, that would help a lot.

    I know I will be back with further questions as I am only doing a bit at a time, get that right, then move on to the next bit. I haven't really started with the Trend part yet.
    If UniRenko is to difficult to use I may swap to the NT8 native renko.

    Cheers,
    Ken.
    Attached Files

    #2
    Hello,

    Thank you for the post.

    The AddKaji would work basically the same as all the other data series, you would still use the Open, High, Low, and Close series to retrieve the values from this bar type. I did note that you are not using BarsInProgress or any of the plural-named series. BarsInProgress would be the property that tells you what bar type is calling OnBarUpdate. The plural-named series allow you to pick from a series what data you want.

    Currently, the script would just be processed the same on all series you have it applied/added so there is no reference to what series the data comes from.

    To know the Kagi is processing to get its values, you would either need to use BarsInProgress:

    Code:
     if (BarsInProgress == 2)
        {
           // do something for kagi
        }
    or reference its series using the index it was added to:

    Code:
    double kagiPrice = Closes[2][0];
    double kagiPriceHigh = Highs[2][0];
    double kagiPriceLow = Lows[2][0];





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

    Comment


      #3
      Still having trouble.

      Hi Jesse, I have read all the links you provided plus more and still have some uncertainty about how it all works. But I have made changes to my code accordingly and am still having some problems. If you open the attached code you will see I have commented out the offending code, really just 2 lines, but when I commented that out it caused another part of the code to have an error.
      Can you please have a look and advise me as to what I have done wrong and how best to fix it up?
      Thanks, looking fwd to you reply.
      Ken.
      Attached Files

      Comment


        #4
        Hello,

        If you are referring to the two properties you created, they are just not formed correctly.

        Code:
        bool LongSignal[B];[/B] { get { return (kagiHigh[0] > kagiHigh[1]) && (kagiLow[1] < kagiLow[2]);}}
        bool ShortSignal { get { return (Low[0] < Low[1]) && (High[1] > High[2]);}}
        The semicolon at the end of LongSignal needs to be removed.
        Code:
        bool LongSignal[B];[/B]
        Please let me know if I may be of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Made that change, still have a problem.

          Hi Jesse,
          Still have a problem with this line:
          bool LongSignal { get { return (kagiHigh[0] > kagiHigh[1]) && (kagiLow[1] < kagiLow[2]);}}
          CS0103 Error.

          If you look at the code you will see the variables kagiHigh & kagiLow have been declared in the OnBarUpdate() section. Could it be this declaration is in the wrong place?
          Or is it something else?

          Cheers,
          Ken.

          Comment


            #6
            Strategy 'MultiMACrossOver': Error on calling 'OnStateChange' method:

            Hi Jesse,
            I also have a problem with the other strategy, the "MultiMACrossOver" strategy that the "MultiMAPlus" strategy was built from. It compiles ok, and I can't see anything wrong with it, but when I run it in the strategy analyser I get the following error in the log file: Strategy 'MultiMACrossOver': Error on calling 'OnStateChange' method: Index was outside the bounds of the array.
            I have attached the strategy here.
            What is the problem? And how do I fix it please?

            Please also answer prior post.

            Cheers,
            Ken.
            Attached Files

            Comment


              #7
              Hello,

              Thank you for the reply.

              Yes, you would need to define variables for items you would be using outside of OnBarUpdate. It looks like you have defined variables in OnBarUpdate rather than the class. I did not catch this before because the properties it appeared you were trying to use Series which are not Doubles.

              Try using the following instead for items that should be used from both OnBarUpdate or other places in the script. This would also entail that you remove the current variables you created in OnBarUpdate and move them into this format:

              Code:
              public class KagiSwinger : Strategy
              {
              private double kagiClose;
              // etc...
              Also, a Double is not a Series meaning you cannot use [2] on kagiLow as an example. You are currently assigning the Lows[2][0] to kagiLow, that would mean where you use the varaible kagiLow its syntax would be:
              Code:
              kagiLow 
              not
               kagiLow[2]
              If you wanted a Series instead, you would need to remove the variables you have and follow the Series syntax listed here: http://ninjatrader.com/support/helpG...us/seriest.htm

              Regarding the strategy, I could suggest that you try to debug the strategy to find what line is causing the problem. What you could do to find the answer to this question would be to comment out the logic you have in OnBarUpdate temporarily. Then comment out the logic in your OnStateChange statements, then compile. Now run the script and ensure it can process without error, it won't place trades we just need to make sure the error is gone.

              Now that the script does not produce an error, only uncomment 1 line at a time in your OnStateChange conditions and compile and re-test. This will allow you to identify the specific line causing the error. If it is not apparent from the syntax what the problem is, please post your finding and we can review that specific syntax together.

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

              Comment


                #8
                Ok, I have done as you suggested, I first tried to go with the series, but couldn't make that work. There was seemingly contradictory information in the help files which confused the issue. Then tried the other way, but I have a problem now with this line:
                "bool LongSignal { get { return (kagiHigh[0] > kagiHigh[1]) && (kagiLow[1] < kagiLow[2]);}}.
                I have uploaded the strategy so you can open it and have a look see what I have done wrong and advise how to fix.
                Thanks,
                Ken.
                Attached Files

                Comment


                  #9
                  Hello,

                  Thank you for the reply.

                  Yes, this is the same problem as before, you are trying to use a BarsAgo [1] with a Double which is not possible. You would need to drop the BarsAgo syntax: [1] and just use the variable name: kagiHigh

                  Here is a sample without the BarsAgo, but this would not be valid:

                  Code:
                  bool LongSignal { get { return (kagiHigh > kagiHigh) && (kagiLow < kagiLow);}}
                  In this case, your syntax would not equate because you are comparing the same variables against themselves, you would need to make a kagiHigh1 and kagiHigh2 variable to compare against each other and set these from OnBarUpdate.

                  Code:
                  kagiHigh1 = Highs[2][0];
                  kagiHigh2 = Highs[2][1];
                  Or, you would need to use the Series syntax correctly and make a series. I would suggest to instead use a Series because you want to use BarsAgo as that would be more flexible, otherwise, you need to make a private double for each different value you need to compare.

                  Alternatively, if you are only setting the value you should instead just use the Highs series:


                  Code:
                  bool LongSignal { get { return (Highs[2][0]> Highs[2][1]) && (Lows[2][0] < Lows[2][1]);}}
                  If you try to create a series and have difficulties, please instead post that example and I could see what may be wrong.

                  I look forward to being of further assistance.
                  Last edited by NinjaTrader_Jesse; 05-16-2017, 07:33 AM.
                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    Hi Jesse,
                    Have done as you suggested.
                    Please see attached errors.

                    Variables: (not sure if these are correct)?
                    I tried 2 different options the errors are there with both.

                    private ISeries <double> kagiClose;
                    private double kagiHigh;
                    private double kagiLow;


                    Assigning the data to the variables:

                    else if (State == State.Configure)
                    {
                    //ATR Trailing indicator runs on the UniRenko chart set at the defaults above. Although trade signals execution and trade management are on the Kagi chart with the above defaults.

                    _ATRTrail = ATRTrailing(this.ATRTrailMultiple, this.ATRTrailPeriod, this.ATRTrailRatchet);

                    AddDataSeries(new BarsPeriod() { BarsPeriodType = (BarsPeriodType)20, BaseBarsPeriodValue = UniRenkoOpenOffset, Value = UniRenkoTrend, Value2 = UniRenkoReversal });
                    AddKagi("6A", Data.BarsPeriodType.Tick, KagiPeriod, KagiReversal, ReversalType.Tick, MarketDataType.Last);

                    base.ClearOutputWindow();
                    }
                    else if (State == State.DataLoaded)
                    {
                    kagiClose = new Series<double> (this.Closes [2][0]);
                    kagiHigh = new Series<double> (this.Highs[2][0]);
                    kagiLow = new Series<double> (this.Lows[2][0]);


                    Bool Statements:

                    bool LongSignal { get { return (kagiHigh[0] > kagiHigh[1]) && (kagiLow[1] < kagiLow[2]);}}
                    bool ShortSignal { get { return (kagiLow[0] < kagiLow[1]) && (kagiHigh[1] > kagiHigh[2]);}}


                    Strategy is attached, some of the text had to be commented out for it to compile.
                    Cheers,
                    Ken.
                    Attached Files

                    Comment


                      #11
                      Hello,

                      Thank you for the reply.

                      I see where you have tried to make a series but it is not like the example we have in the help guide, ISeries would not be correct you would instead need Series<double>.

                      This is the example from the help guide for creating a series:

                      Code:
                      private Series<double> myDoubleSeries;
                      
                      protected override void OnStateChange() 
                      {
                          if (State == State.DataLoaded)
                          {
                              myDoubleSeries = new Series<double>(this, MaximumBarsLookBack.Infinite);
                          }
                      }



                      In your script, if you wanted to make a series you would need to use the above syntax giving that series a unique name like kagiClose as you have.

                      Then from OnBarUpdate, you would set a value to that series:

                      Code:
                      myDoubleSeries[0] = Close[0];
                      After that, you can use the series where you would like, or in the properties, you have made using a BarsAgo:

                      Code:
                      if(myDoubleSeries[0] > myDoubleSeries[1])
                      Alternatively, if you are not doing anything but trying to get the price, you could just use the Price series to avoid creating any variables:

                      Code:
                      bool LongSignal { get { return (Highs[2][0] > Highs[2][1]) && (Lows[2][1] < Lows[2][2]);}}
                      Please let me know if I may be of further assistance.
                      JesseNinjaTrader Customer Service

                      Comment


                        #12
                        Getting there.

                        Hi Jesse,
                        I think I am getting there.
                        I have done as instructed.
                        Variables:
                        public class KagiSwinger : Strategy
                        {
                        ATRTrailing _ATRTrail;
                        private Series<double> kagiClose;
                        private Series<double> kagiHigh;
                        private Series<double> kagiLow;
                        private Series<double> renkoClose;
                        private Series<double> renkoHigh;
                        private Series<double> renkoLow;

                        Add other bar types:
                        else if (State == State.Configure)
                        {
                        //ATR Trailing indicator runs on the UniRenko chart set at the defaults above. Although trade signals execution and trade management are on the Kagi chart with the above defaults.

                        _ATRTrail = ATRTrailing(this.ATRTrailMultiple, this.ATRTrailPeriod, this.ATRTrailRatchet);

                        AddDataSeries(new BarsPeriod() { BarsPeriodType = (BarsPeriodType)20, BaseBarsPeriodValue = UniRenkoOpenOffset, Value = UniRenkoTrend, Value2 = UniRenkoReversal });
                        AddKagi("6A", Data.BarsPeriodType.Tick, KagiPeriod, KagiReversal, ReversalType.Tick, MarketDataType.Last);

                        Assign values to the variables:
                        else if (State == State.DataLoaded)
                        {
                        kagiClose = new Series<double>(this,MaximumBarsLookBack.Infinite);
                        kagiHigh = new Series<double>(this,MaximumBarsLookBack.Infinite);
                        kagiLow = new Series<double>(this,MaximumBarsLookBack.Infinite);
                        renkoClose = new Series<double>(this,MaximumBarsLookBack.Infinite);
                        renkoHigh = new Series<double>(this,MaximumBarsLookBack.Infinite);
                        renkoLow = new Series<double>(this,MaximumBarsLookBack.Infinite);

                        On Bar Update:
                        if (BarsInProgress == 0)
                        {
                        kagiClose[0] = Close[0];
                        kagiHigh[0] = High[0];
                        kagiLow[0] = Low[0];
                        renkoClose[0] = Close[0];
                        renkoHigh[0] = High[0];
                        renkoLow[0] = Low[0];

                        I have attached the strategy so you can open and inspect it properly.
                        I have one question now, that is in the above script I can't see how the kagi variables are getting their values from the Kagi bar type series and likewise the Renko variables getting their values from the Renko bar type series. It would appear to me all the values come from the default bar type series. This can't happen as the highs, lows and closes, are different on each bar type. Please explain?


                        Cheers,
                        Ken.
                        Attached Files

                        Comment


                          #13
                          Hello,

                          In this case, you are setting the Primary prices to the variables you created so you are only using the primary data as you noticed.
                          Code:
                          if (BarsInProgress == [B]0[/B])
                          You would either need to correctly use the BarsInProgress index for the index of the Kagi bars or use the price series as I previously noted. If you are only setting values to the series you created that are the price values with no modification, there is really no point for that. You could just use the price series its self as shown in the example in post #9

                          Code:
                          bool LongSignal { get { return (Highs[2][0]> Highs[2][1]) && (Lows[2][0] < Lows[2][1]);}}

                          The BarsInProgress relates to which series is calling OnBarUpdate, you can set a value to a series like the following:

                          Code:
                          if (BarsInProgress == [B]2[/B])[U] // 2 because you added the Kaji series second, the Primary is 0, the Renko is 1, Kaji is 2 per how you coded this script. [/U]
                          {
                          	kagiClose[0] = Close[0];
                          	kagiHigh[0] = High[0];
                          	kagiLow[0] = Low[0];	
                          	renkoClose[0] = Close[0];
                          	renkoHigh[0] = High[0];
                          	renkoLow[0] = Low[0];					
                          }
                          If you are using series, you would also likely need to use IsValidDataPoint in case a value is not set for a bar:


                          If you are not going to be changing the values and just need access to the price, I would highly suggest to just use the example provided in post #9 as that is the most simple approach.

                          I also noted, if you are trying to access the 6A, you would also need to use a valid contract month and year in the call: "6A 06-17". you will see an error in the log if the instrument 6A is used without an expiry. If you have a different instrument that is not the 6A futures instrument configured you can ignore this.


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

                          Comment


                            #14
                            RE: Last Post

                            In reply to your last post, I have highlighted the text below that I think you are referring to?
                            In blue, I was trying to use a series, please point out what exactly I have misunderstood?
                            Am I getting "Series" & "ISeries", mixed up or something, are they 2 different things or not? In red, I need a more detailed explanation.

                            I just had a further thought, in regard to what was said in post #9. To use the series, I don't need variables, is that correct? Simply reference the price data by calling the plural, and the correct bars ago index, example Highs[2][0], no need for variables. Is it that simple?

                            Originally posted by NinjaTrader_Jesse View Post
                            Hello,

                            Thank you for the reply.

                            Yes, this is the same problem as before, you are trying to use a BarsAgo [1] with a Double which is not possible. You would need to drop the BarsAgo syntax: [1] and just use the variable name: kagiHigh

                            Here is a sample without the BarsAgo, but this would not be valid:

                            Code:
                            bool LongSignal { get { return (kagiHigh > kagiHigh) && (kagiLow < kagiLow);}}
                            In this case, your syntax would not equate because you are comparing the same variables against themselves, you would need to make a kagiHigh1 and kagiHigh2 variable to compare against each other and set these from OnBarUpdate.

                            Code:
                            kagiHigh1 = Highs[2][0];
                            kagiHigh2 = Highs[2][1];
                            Or, you would need to use the Series syntax correctly and make a series. I would suggest to instead use a Series because you want to use BarsAgo as that would be more flexible, otherwise, you need to make a private double for each different value you need to compare.

                            Alternatively, if you are only setting the value you should instead just use the Highs series:


                            Code:
                            bool LongSignal { get { return (Highs[2][0]> Highs[2][1]) && (Lows[2][0] < Lows[2][1]);}}
                            If you try to create a series and have difficulties, please instead post that example and I could see what may be wrong.

                            I look forward to being of further assistance.
                            Last edited by KennyK; 05-24-2017, 09:50 PM. Reason: A further thought

                            Comment


                              #15
                              Hello,

                              Thank you for the reply.

                              In my last post, I tried to highlight what was specifically happening using the code blocks. You are not using the correct BarsInProgress, you have 0 but needed 2.

                              Your use of series looks fine that was resolved with your latest zip file, mainly you made it the same as we have documented in the help guide. ISeries and Series are two different things, for creating a Series that contains prices you would use Series and not ISeries.

                              The reason for the problems would be the other items I had noted in post #13. You had asked why you were only seeing primary prices, this is because of using BarsInProgress 0 instead of 2 in this case.



                              When you work with multiple series, each series calls OnBarUpdate. That means the BarsInProgress value will change depending on which series called OBU. You added the Kagi second so its BarsInProgress would be 2.

                              I had also noted that if you are not making any changes to the prices such as doing some form of math, a series would not be needed and you could just use the already existing series that contains the prices. This would be the Plural version of the series in question where you define the BarsInProgress for the data you want:

                              Code:
                              Highs[2][0]
                              Lows[2][0]
                              Opens[2][0]
                              Closes[2][0]
                              A series would only be needed in a case where you want to change the value for each bar, for example, I want to subtract the Open from the High on each bar and store that value. If I only need the High price, I would use the Plural Highs instead because that already exists.

                              For the property that uses these values, please refer to my post #13 for the specific syntax I had noted.

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

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by helpwanted, Today, 03:06 AM
                              1 response
                              13 views
                              0 likes
                              Last Post sarafuenonly123  
                              Started by Brevo, Today, 01:45 AM
                              0 responses
                              11 views
                              0 likes
                              Last Post Brevo
                              by Brevo
                               
                              Started by aussugardefender, Today, 01:07 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post aussugardefender  
                              Started by pvincent, 06-23-2022, 12:53 PM
                              14 responses
                              242 views
                              0 likes
                              Last Post Nyman
                              by Nyman
                               
                              Started by TraderG23, 12-08-2023, 07:56 AM
                              9 responses
                              387 views
                              1 like
                              Last Post Gavini
                              by Gavini
                               
                              Working...
                              X