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

i want an indicator that will exactly replicate a strategy with particular structure.

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

    i want an indicator that will exactly replicate a strategy with particular structure.




    people with nt,


    regards.

    i have a strategy with a structure similar to the one below:


    Code:
    
                // Set 1
                if ( (SMA1[0] < SMA1[1])
                     && (Stochastics1.D[0] < 20 ) )
                {
                    EnterShort( );
                }
    
                 // Set 2
                if ( (SMA1[0] > SMA1[0]))
                {
                    ExitShort( );
                }
    
                 // Set 3
                if ( (SMA1[0] > SMA1[1])
                     && (Stochastics1.D[0] > 80 ) )
                {
                    EnterLong( );
                }
    
                 // Set 4
                if ( (SMA1[0] < SMA1[0]) )
                {
                    ExitLong( );
                }

    if all the conditions in set 1 are true, the strategy should open a short position and hold it until the condition in set 2 is true. and it is the same story just inverted for sets 3 and 4. on occasion it could happen that both an exit condition and the opposite entry condition become true at the same time. in those cases, the strategy should completely reverse position. as far as i can tell, nt and strategies with structures like these work well.

    however, i want to create indicators that will replicate exactly the same behavior and i haven't been able to. my preferred methodology is to have an indicator that plots an sma and have this sma change colors according to the conditions of the data.

    i have tried to modify smas that i already have that already change colors according to conditions like SMA1[0] < SMA1[1] or SMA1[0] > SMA1[1] but it has been a mess. some won't plot and others won't compile when i try to incorporate a structure like the one above.


    i want an indicator that will plot a yellow sma, if all the conditions in set 1 are true, the color should change to red. it could also be helpful to create a downtrend condition and declare it to be true. when the condition in set 2 becomes true, then the downtrend condition should become false and the color should change back to yellow. and it is the same story for sets 3 and 4, with a condition called uptrend and green color.


    i have been working on this indicator below but it is useless. i think i must be missing additional logic to get the indicator to replicate exactly the strategy above (¿maybe i need to nest each pair of conditions inside one another?. ¿what changes must be made to this code to get it to work just like the strategy i included above?

    Code:
                AddPlot(Brushes.Yellow, " indicator ");
                }
                else if (State == State.Configure)
                {
    
                   SMA1 = new Series<double>(this);
                   Stochastics1 = new Series<double>(this);
    
                }
            }
    
            protected override void OnBarUpdate()
            {
    
                Values[0][0]     = SMA(period1)[0];
    
                SMAc1[0]    = SMA(period1)[0];
                Stochasticsc[0]    = Stochastics(period2)[0];
    
    
    
                if (CurrentBar < 1) return;
    
    
    
                // Set 1
                if ( (SMA1[0] < SMA1[1])
                     && (Stochastics1.D[0] < 20) )
                {
                    PlotBrushes[0][0] = Brushes.red;
                }
    
                 // Set 2
                if ( (SMA1[0] > SMA1[0]))
                {
                    PlotBrushes[0][0] = Brushes.yellow;
                }
    
                 // Set 3
                if ( (SMA1[0] > SMA1[1])
                     && (Stochastics1.D[0] > 80 ) )
                {
                    PlotBrushes[0][0] = Brushes.green;
                }
    
                 // Set 4
                if ( (SMA1[0] < SMA1[0]) )
                {
                    PlotBrushes[0][0] = Brushes.yellow;
                }


    if i could also receive email notifications every time one of these changes has taken place and visual alerts like pop-up windows that would be fantastic. i'm still learning the capabilities that nt has to offer.


    very well, thanks, regards.

    #2
    Hi rtwave, thanks for your post.

    You're never adding any plots or filling the Values[][] array with anything. So no plots are being drawn. The code you have to set the color is fine, but you must set your plot values first. Attached is an exemplar.

    Please let me know if I can assist any further.
    Attached Files
    Chris L.NinjaTrader Customer Service

    Comment


      #3



      people with nt,




      thanks.



      i have continued to work on this matter and have managed to get the indicators to plot. however, a strategy and an indicator with the exact same components and structure have nothing to do with one another.


      these strategy and indicator both include an sma, momentum and adx. these pieces of code are great to illustrate what i have been referring to:

      Click image for larger version

Name:	20200217 nt strategy indicator discrepancy 001.JPG
Views:	493
Size:	104.2 KB
ID:	1087394


      i include the files for the strategy and indicator from this image above. as anyone can verify, the indicator does not change colors at the same time that the strategy opens or closes positions and that is the objective of creating such an indicator with the exact same components and structure.


      ¿could the experts at nt take a look at this and indicate what changes must be made to the indicator so that it will follow a strategy to perfection?


      very well, regards.

      Comment


        #4
        Hello rtwave,

        What is the exact issue that you are having?

        Are you trying to set the PlotBrushes[0][0] of a plot and this is not changing?


        To add an indicator to a chart from a strategy use AddChartIndicator().
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5


          Chelsea,



          thanks.


          yes. i want to be able to convert my strategies to indicators with exactly the same behavior. i want to be able to have a strategy and its indicator in a chart so that i can tell whether a downtrend, uptrend or neutral condition is active just by taking a glance at the color of the sma.


          i have developed several strategies and indicators with the exact same structure and components but the indicators do not replicate the strategies like they should. in the examples i have provided i want the sma be plotted in yellow and to change color to red only for as long as the strategy would hold a short position or green only for as long as the strategy would hold a long position. from the images and code i have provided it is clear that this is not the case. the structure for the strategy and indicator is the same, the components are the same, the values for the inputs are the same but the indicator never changed to red when the strategy opened a short position and it did change to green in the appropriate bar but then it changed back to yellow far earlier than it should have. it seems to me that it must be necessary to make changes to the conditions in the indicator so that it replicates the behavior of its identical strategy to perfection.


          Comment


            #6
            Hello rtwave,

            Have you used prints to understand the behavior?

            If not, use prints to understand the behavior. Print all of the values in the condition that triggers the action.

            Below is a link to forum post that demonstrates with videos.


            What is the condition you are expecting to evaluate as true, and on what bar are you expecting this to be true?

            May I have the output from the prints to assist with analyzing the information?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Chelsea,



              thanks.



              i'm still lost when it comes to this matter and i really need help. if i can't create indicators that replicate my strategies to perfection, i can't use nt to trade manually. (i have also had a horrendous time trying to get nt to create txt files to serve as logs for these same events indicated by my strategies, but i have posted about that issue on another thread).


              the charts i'm using go back all the way to 2013, so it makes no sense to have the nt platform print out statements for every event for 7 years of data.


              and these prints would also be redundant as there would be an adx from the sample strategy and one from the sample indicator, as well as two identical smas and momentum indicators.


              anyway, i have just done as requested and now have added print commands to my sample strategy and indicator. also, i'm working with a chart that only has 4 days of data.


              for some reason nt first generates all the prints for the indicator at once and then all the prints for the strategy.


              i have uploaded the txt file with the prints and the files for the strategy and indicator as attachments.


              as other people on the internet would say: - people with nt, please advise -. jaja.

              teindicatortriprints.cs tesampletriprints.cs NinjaScript Output 26-Feb-20 12_39 sample 3 component prints.txt

              Comment


                #8
                Hello rtwave,

                Are we focusing on the first print at 18:04 on February 23rd?

                Is the value of 1661.75 for the SMAind[0] unexpected? What value are you expecting?

                Please note in the forum post I have linked you the prints include all values used in the condition and has labels to understand the comparison being made.

                For example:

                Print(string.Format("{0} | SMA1[1]: {1} > SMA2[1]: {2} && SMA1[0]: {3} < SMA2[0]: {4}", Time[0], SMA1[1], SMA2[1], SMA1[0], SMA2[0]));

                This would output:

                2/26/2020 11:27:00 AM | SMA1[1]: 3137.57142857143 > SMA2[1]: 3137.96428571429 && SMA1[0]: 3138.35714285714 < SMA2[0]: 3138.375

                The less than, ampersand, and greater than symbol allows us to see how these items are being compared. I can see that SMA1 1 bar ago needs to be less than the SMA2 1 bar ago and I can see the values for these. With this information I can see how the condition is going to evaluate as true or false.


                The indicator does get called and loaded in OnStateChange() and gets up to date before the strategy begins processing its own OnBarUpdate().
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Click image for larger version

Name:	20200226 nt strategy indicator discrepancy prints 001.JPG
Views:	436
Size:	146.1 KB
ID:	1088551



                  the strategy only opens a short position when 3 components meet certain conditions. it holds that position until one single component reverses.


                  i have been completely unable to put together an indicator that will replicate this same structure, even when everything is identical inside both the strategy and the indicator:



                  Code:
                              // Set 1
                              if ( (SMA1[0] < SMA1[1])
                                   && (Momentum1[0] < Moml1)
                                   && (ADX1[0] > Adxp) )
                              {
                                  PlotBrushes[0][0] = Dc;
                                  Print(string.Format("{0} | SMAind[0]: {1} | SMAind[1]: {2} | Momind[0]: {3} | ADXind[0]: {4}", Time[0], SMAic1[0], SMAic1[1], Momentumic1[0], ADXic1[0]));
                              }
                  
                               // Set 2
                              if ( (SMA1[0] > SMA1[1]) )
                              {
                                  PlotBrushes[0][0] = Nc;
                                  Print(string.Format("{0} | SMAind[0]: {1} | SMAind[1]: {2} | Momind[0]: {3} | ADXind[0]: {4}", Time[0], SMAic1[0], SMAic1[1], Momentumic1[0], ADXic1[0]));
                              }

                  Code:
                               // Set 1
                              if ((SMA1[0] < SMA1[1])
                                   && (Momentum1[0] < Moml1)
                                   && (ADX1[0] > Adxp))
                              {
                                  EnterShort(Convert.ToInt32(Posi), @"sp01");
                                  Print(string.Format("{0} | SMAstr[0]: {1} | SMAstr[1]: {2} | Momstr[0]: {3} | ADXstr[0]: {4}", Time[0], SMA1[0], SMA1[1], Momentum1[0], ADX1[0]));
                              }
                  
                               // Set 2
                              if (SMA1[0] > SMA1[1])
                              {
                                  ExitShort(Convert.ToInt32(Posi), @"esp01", @"sp01");
                                  Print(string.Format("{0} | SMAstr[0]: {1} | SMAstr[1]: {2} | Momstr[0]: {3} | ADXstr[0]: {4}", Time[0], SMA1[0], SMA1[1], Momentum1[0], ADX1[0]));
                              }

                  it's all in the cs files i attached, i want an sma to change color to red for as long as the same conditions that would cause the strategy to hold a short position are true and to turn back to yellow when the strategy closes the position.


                  the downtrend color doesn't work at all, while the uptrend color sometimes will become active but it doesn't replicate the strategy either.


                  this is really frustrating, this must be definitely possible in nt. i had the same problems with tradestation but as crappy as that platform is, it was possible to refine the code so that i could create identical strategies and indicators.

                  Comment


                    #10
                    Hello rtwave,

                    While I cannot debug the script on your behalf, I can help you understand what is causing differences by using prints.

                    When you say it is not replicating the strategy you are not referring to actually placing orders correct?

                    Are you instead referring to the execution markers being 3 bars after the plot? (if running with On bar close, orders will appear after the signal bar has closed on the new bar)

                    What is not being replicated?
                    Last edited by NinjaTrader_ChelseaB; 02-26-2020, 04:52 PM.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_ChelseaB View Post
                      Hello rtwave,

                      While I cannot debug the script on your behalf, I can help you understand what is causing differences by using prints?

                      When you say it is not replicating the strategy you are not referring to actually placing orders correct?

                      Are you instead referring to the execution markers being 3 bars after the plot? (if running with On bar close, orders will appear after the signal bar has closed on the new bar)

                      What is not being replicated?


                      i don't think there is a need to debug anything as both the sample strategy and indicator do compile and work.


                      the strategy has no issues, it is a sample strategy with a similar structure to several strategies i do use. it requires several conditions to be true to open a position and then only one single condition to close it.


                      as i have repeated in every post in this thread, i want the indicator to change colors exactly replicating the strategy as it opens and closes positions. as i have also repeated in every post: everything is identical inside both the strategy and indicator that i have shared. please take a look at those files: the structure is the same, the components are the same, the values for the inputs are the same but the indicator doesn't follow the strategy as it is intended to do. ¿how is this possible? ¿why would something like this happen? ¿and how could i change the code so that the sma changes colors exactly as the strategy opens and closes positions?


                      i had this exact same issue in another crappy platform and the solution was to nest some conditions inside other conditions. in the case of nt i'm certain that this must be an identical situation where the code needs to be refined so that an indicator replicates an strategy with this structure to perfection:


                      Code:
                       obviously this is not ninjascript code, i'm using this to illustrate what was necessary.
                      
                      if (smav > smav[1] and momv > momtl and adxv > adxt) then  
                      begin  
                          utc=true;   
                          MyColor = uc ;  
                      end  
                      else if smav < smav[1] then  
                      begin  
                          utc=false;  
                          if mycolor = uc then
                              MyColor = FlatColor ;  
                      end;  
                      
                      if (smav < smav[1] and momv < momts and adxv > adxt) then  
                      begin  
                          dtc=true;  
                          MyColor = dc ;  
                      end  
                      else if smav > smav[1] then  
                      begin  
                          dtc=false;  
                          if MyColor = dc then
                              MyColor = FlatColor ;  
                      end;  
                      
                      SetPlotColor( 1, MyColor ) ;

                      Comment


                        #12
                        Hello rtwave,

                        When using Calculate as on bar close, orders appear at the open of the bar after the signal bar.

                        Is your strategy using Calculate on bar close?

                        Are you drawing the object one bar after the signal bar?


                        Reading through the code and adding prints to understand the behavior is debugging.

                        Below is a link to a forum post that demonstrates how to use prints to understand behavior.


                        Unfortunately, in the support department at NinjaTrader it is against our policy to create, debug, or modify, code or logic for our clients. This is so that we can maintain a high level of service for all of our clients as well as our partners.

                        That said, I am happy to answer any questions you may have about NinjaScript if you decide to debug this yourself.

                        This thread will remain open for any community members that would like to debug this as a convenience to you.

                        You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our business development follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request.


                        Have you added prints to understand the behavior?

                        Are the prints showing the conditions are receiving the same values and triggering actions at the same time?

                        Please provide the output with your reply.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13




                          i have been doing some tests. i still need to be able to create indicators that visually indicate the same conditions of interest as my strategies do.



                          i have been able to identify the reason why the indicator does not replicate the strategy to perfection.


                          these are the conditions for an entry:

                          // Set 1
                          if ((SMA1[0] < SMA1[1])
                          && (Momentum1[0] < Moml1)
                          && (ADX1[0] > Adxp))
                          {
                          EnterShort( );
                          }

                          when a strategy finds these conditions it will open a short position and it will stop evaluating for these entry conditions any further. the strategy will just hold this position and will now evaluate for exit (or reversal) conditions.



                          in my indicator these are conditions for the moving average to be plotted in the color i have designated for downtrends. it's all the exact same thing:

                          // Set 1
                          if ( (SMA1[0] < SMA1[1])
                          && (Momentum1[0] < Moml1)
                          && (ADX1[0] > Adxp) )
                          {
                          PlotBrushes[0][0] = Dc;
                          }


                          yesterday i realized that even if these were the only conditions in my indicator and i deleted the other sets, nt will only plot the moving average in the color for downtrends in all the bars for which all the conditions were true. as soon as any of the conditions are not verified, nt will automatically plot the moving average in the color i designated initially.


                          i want my indicator to be identical to my strategy and for it to plot the moving average in the color for downtrend once the conditions are true and until the conditions for no trend or uptrend are verified. this is exactly what the strategy does, it opens a position and holds it until the conditions for exit (or reversal) are verified.


                          so, ¿how can i structure an indicator that will evaluate for set 1, and when these conditions are true set a (color) variable to true and not evaluate for set 1 anymore, but evaluate for no trend (or uptrend) conditions, and only when any of those are true will the color variable be released?

                          // Set 2
                          if ( (SMA1[0] > SMA1[1]) )
                          {
                          PlotBrushes[0][0] = Nc;
                          }




                          in this case, ¿how could i structure an indicator so that once

                          // Set 1
                          if ( (SMA1[0] < SMA1[1])
                          && (Momentum1[0] < Moml1)
                          && (ADX1[0] > Adxp) )
                          {
                          PlotBrushes[0][0] = Dc;
                          }

                          are true for one bar, the color variable will change permanently for all subsequent bars, irrespective of whether set 1 is still true or not?
                          Last edited by rtwave; 05-08-2020, 06:53 PM.

                          Comment


                            #14
                            Storing the color for the plot in a variable should help

                            Code:
                                public class SomeIndicator : Indicator {
                                    Brush plotBrush;
                                ...    
                                        } else if (State == State.Configure) {
                                            plotBrush = Plots[0].Brush;
                                ...    
                            
                                if ( (SMA1[0] < SMA1[1])
                                    && (Momentum1[0] < Moml1)
                                    && (ADX1[0] > Adxp) )
                                {
                                    plotBrush = Dc;
                                }
                            
                                if ( (SMA1[0] > SMA1[1]) )
                                {
                                    plotBrush = Nc;
                                }
                            
                                PlotBrushes[0][0] = plotBrush;

                            Comment


                              #15
                              Hello rtwave,

                              Yes, PlotBrushes is the brush for each bar of a plot. This will only be set for bars you set this on.

                              You could use a bool once the condition set is true to start setting this on each new bar.
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by judysamnt7, 03-13-2023, 09:11 AM
                              4 responses
                              59 views
                              0 likes
                              Last Post DynamicTest  
                              Started by ScottWalsh, Today, 06:52 PM
                              4 responses
                              36 views
                              0 likes
                              Last Post ScottWalsh  
                              Started by olisav57, Today, 07:39 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post olisav57  
                              Started by trilliantrader, Today, 03:01 PM
                              2 responses
                              21 views
                              0 likes
                              Last Post helpwanted  
                              Started by cre8able, Today, 07:24 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post cre8able  
                              Working...
                              X