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

Calling an Indicator to a Strategy multiple times

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

    Calling an Indicator to a Strategy multiple times

    Hello,

    I have created a strategy which needs to call on an indicator several times. The indicator has 19 parameters, and the strategy code begins to look cluttered when calling the indicator each time I need it for a calculation. Can I set the indicator as a variable/function and just use that variable/function in the rest of the code? If so, can you give me an example. The results will be output to the chart via a DrawText.

    Thank you!

    #2
    Hello,

    Thank you for the question.

    You can certainly use a variable to make the code look better, here is a simple example using an SMA.


    Code:
    private SMA mySma;
    
    protected override void OnStartUp()
    {
            mySma = SMA(12);	
    }
    protected override void OnBarUpdate()
    {
    	double myValue = mySma[0];
    }
    Please note there is no use of a new keyword, that is normal for calling an indicator because of the code that is appended to each indicator at the bottom.

    I look forward to being of further assistance.
    Last edited by NinjaTrader_Jesse; 02-26-2016, 08:19 AM.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks for that Jesse. I have followed your instructions and it works.

      The only issue is that within OnBarUpdate section:

      Code:
      Value.Set(myBsc[0]);
      Value.Set(myBsl[0]);
      returns a CS0103 error code. If I comment it out, then everything compiles and plot and DrawText are shown on the chart. So I am not sure what that part is for. I was under the impression that there was no need to set to plot when adding an indicator to a strategy, based on this link: http://ninjatrader.com/support/forum...14&postcount=1
      It is set to plot in the original indicators as follows:
      Code:
      BuySellLine.Set(SUM(myDataSeries, Period)[0]);
      BuySellChangePlot.Set(Math.Round(bsl0ComparedBsl1, 2));
      Also, when the indicators are loaded from the strategy, all the parameters of the indicators do not appear in the properties panel for adjustment. Is that the correct operation?

      Comment


        #4
        I'd like to ask a couple more questions related to this thread if I may:
        1. Is it ok to load two strategies for automated trading, one for long positions and one for short?
        2. I have created 2 strategies, and I have then merged them into one. The DrawText results on the chart from the merged strategy are not the same as the individual ones, and I cannot identify why, as none of their calculations should affect each other. If I comment out one of the merged strategies, then the one that remains shows the correct result like the single strategy. Is there any known reason it is behaving in this way?

        Comment


          #5
          Hello,

          Thank you for the questions.

          I have edited my prior example to be more specific to a strategy, the previous version included the Value.Set which is for plotting and also the Add() statement to add a plot which is not needed for a strategy.

          You are correct, indicators are only used when called in the provided example and would not show up in the strategies properties.

          For your other questions,
          Is it ok to load two strategies for automated trading, one for long positions and one for short?

          This is a common question, while technically speaking you can do this it is not advised. The reason is that Strategy 1 does not know what Strategy 2 is doing. If either make a trade and enter a position, the other will still be flat becoming out of sync.

          For any strategy it is recommended that all logic is included in the single strategy so that all logic has the same account values.

          For your second question, based on the outcome and commenting logic out making it return to the correct result it sounds like they share a common variable.

          What my thought would be is that you are using a variable or the variable you are outputting for DrawText is being modified again after the first strategies logic occurs. That would be my guess, if you can provide the strategy it may be more apparent why. If you are not comfortable sharing it publicly you could of course email me at platform support @ ninjatrader.com.


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

          Comment


            #6
            Thanks, Jesse. I think it probably is the sharing of variables. I'll try changing some of them or alternatively, as I have not built in any automated entries at this stage, I may try converting to 2 indicators and importing them to the strategy to see how that works.

            Comment


              #7
              I have changed 5 of the variables for the long signals, and the merged signals now give the same results as the individual ones, which are correct.

              One last question on this. The array values going into my computations are correct, being the Min for the short positions and Max for the long positions. When I attempt to print the array values to the output window using:
              Code:
              Print (Time.ToString()+" "+"ArrayValues:"+sovBear[0]+" "+sovBear[1]+" "+sovBear[2]+" "+sovBear[3]+" "+sovBear[4]+" "+sovBear[5]+" "+sovBear[9]);
              the results return the same Max value for each element, instead of all the elements/values from which the Max is chosen. Is there a way to get it to display all the values from which the Max is chosen? I have tried:
              Code:
              sovBear.ToList().ForEach(i => Console.WriteLine(i.ToString()));
              replacing Console.WriteLine with Print, but it still returns the Max value for each element.
              Thank you!

              Comment


                #8
                Hello,

                Thank you for the reply.

                I wanted to check, are you saying that you are setting both the Min and Max values to sovBear?

                I am not sure by the context of what has been provided if this is a series or an array, if this is a series it would only store 1 value per bar, potentially you are setting both values to the same series and only the Max is showing because it is set second?

                if the values can both be reported on the same bar, likely you would need two series one for Max and one for Min values. If you can provide more to the sample it may be more apparent what is being used and how.

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

                Comment


                  #9
                  No, Max and Min do not both apply to the sovBear.

                  sovBear is set to Max (to find the bar which shows the Max value for my calculation within say a 3min period, and that Max value is then used in the overall calculation)
                  tovBull is set to Min (to find the bar which shows the Min value for my calculation within say a 3min period, and that Min value is then used in the overall calculation)

                  I have a separate Print statement for the array values for the tovBull, but it also only shows the Min one rather than all the values it picks up from which the Min is then selected.

                  Comment


                    #10
                    Hello,

                    Thank you for the reply.

                    Without a further sample that shows exactly how the series are set and printed I could only guess what may be happening.

                    Could you provide a working sample of the print that is not currently printing correctly that I could test on my end? Outside of that I could only suggest tracing back the logic to find what the reason is and to utilize Prints to aid in finding the problem.

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

                    Comment


                      #11
                      18:07:12 lvtBearBar:-1.74
                      18:08:04 sovBearBar:11.86
                      18:08:14 sovBearBar:5.23
                      18:09:06 sovBearBar:1.95
                      18:09:46 First Signal.
                      18:09:46 lvtBullBar:28.5
                      18:09:46 sovBullBar:24.46
                      18:09:46 netLvtSOV:34.58=-1.74 11.86 24.46
                      18:09:46 ArrayValues:11.86 11.86 11.86 11.86 11.86 11.86 11.86

                      Above is a sample of the print. The 1st signal is the lvtBearBar @ 18:07:12. The 2nd signal is the highest sovBearBar (as you can see there are 3 in this example and the calculations pick the highest one sent to the array, which is 11.86). The 3rd signal is the sovBullBar @ 18:09:46 which prompts the calculation of netLvtSOV. Note that the array values at the end are only populated by the Max sovBearBar. I would have thought it should be populated by the 3 that arose in the period with the Max being selected for the calculation.

                      This is the part of the code that tells it to find the Max:
                      Code:
                      //Do a for loop to find the maximum for the sovBear
                      for (int k = 0; k < sovBear.Length; k++)
                      if (sovBear[k] > sovBearBar) sovBearBar = sovBear[k];
                      The Print array code comes after that, which is why I think it is only showing the Max value. But I understand there should be a way to get it to show the 3 values which were pushed into the array before the Max was selected.

                      Comment


                        #12
                        Hello,

                        Unfortunately the provided information is not helpful without the syntax to see the order in which the logic is happening.

                        One way to Print an array out would be to use a for loop, you could use something like the following to do that :

                        for(int i = 0; i < sovBear.Length; i++)
                        {
                        Print(sovBear[i]);
                        }

                        As you said though if you are Printing after you set the Max values, potentially the print needs to be before this point.

                        If you can provide a simple sample that I can compile and run on my end, I would be able to assist further.

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

                        Comment


                          #13
                          Thank you for your reply, and sorry for the delay in responding, but I have found a more crucial problem with the code, which may also be having an effect on the array values printed. I have posted it in the thread that originally dealt with that aspect. http://ninjatrader.com/support/forum...838#post450838

                          Comment


                            #14
                            Hello,

                            Thank you for the reply.

                            I checked the other post, it shows you had solved the problem in the other thread, did this also solve this problem or does it still exist?

                            I would be unsure based on the other thread on what may be happening. Likely you would need to run the script with Prints for each variable you are using, and in each loop to find the order things are happening logically. Once you have this in place, it should be more apparent what is happening in the incorrect order and why the print is happening as it is.

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

                            Comment


                              #15
                              Hello,

                              Although the results are now correct, it did not solve the printing of the values in the array problem. I think the issue is that immediately the array sees another value, it compares it to the others and then replaces them with the Max, based on the way the code is written. There may be a way of seeing them based on some code I have seen at this link: http://stackoverflow.com/questions/1...ray-in-c-sharp
                              Code:
                              foreach(var item in yourArray)
                              {
                                  Console.WriteLine(item.ToString());
                              }
                              But I haven't worked out a way to convert this code so that it can be printed to the NinjaScript Output screen. It may also be the case that as the array values are always being updated to the Max, it will not see the values that the array has used for the comparison also.

                              I know what the values are because I can see them in a calculation outside the array. It's just that I thought the print array would show me all the values it was using for the comparison.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by habeebft, Today, 07:27 AM
                              1 response
                              11 views
                              0 likes
                              Last Post NinjaTrader_ChristopherS  
                              Started by AveryFlynn, Today, 04:57 AM
                              1 response
                              12 views
                              0 likes
                              Last Post NinjaTrader_Erick  
                              Started by Max238, Today, 01:28 AM
                              4 responses
                              37 views
                              0 likes
                              Last Post Max238
                              by Max238
                               
                              Started by r68cervera, Today, 05:29 AM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by geddyisodin, Today, 05:20 AM
                              1 response
                              14 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Working...
                              X