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

Non-Scatered Moving Average to Envelopes?

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

    Non-Scatered Moving Average to Envelopes?

    Hi, i need help with this, i been trying for a month (not exaggerating) to solve this for myself ( with my basic C# knowledge) but well.. it seems i cant..

    The idea was to use a SMA(4)[0] on a secondary Daily series as direction, but trading from an hourly chart.

    I want to be able to call the resultant plot from other indicators or as a directional MA for a strategy, include alerts etc, but it doesn't let me, i mean the indicator works well, it plots as it should, it just doesnt let me call the resulting plot as source for other things, like turning it into a channel, dunno why and cant figure it out on my own

    Here's the indie:
    KBT_XmovAvg_v2.zip

    Other alternatives that didn't worked were:
    Simple average on Hourly chart: Multiply the periods *24 hours= The resultant slope isn't the same as the SMA directly applied to the Daily.

    Just Set the SMA to a secondary series= the result is scattered and the slope is ruined.

    Please help!!
    Last edited by kabott; 05-01-2017, 06:51 PM.

    #2
    Hello,

    Thank you for the post.

    I wanted to check, when you say that you can't use this from another script, is this because you have not programmed it to do so or that you are having some other problem?

    The attached file is a compiled assembly so I would be unable to see what syntax was used to know what may be happening.

    If you can provide a sample of the problem that is unprotected, it doesn't necessarily need to be this specific item just one that shows the problem. I could help further in the case we have some actual syntax in question.

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

    Comment


      #3
      Hi Jesse, the problem im having is when i call the plot from the indicator it looks as if its working with the default series instead of the alternative series i want to work with,

      i mean: Suppose i apply that indicator to a 15m chart, but configured to work with a Daily data series, so far so good, ok, i open the ninjascript and try to add 2 more plots to make it into a channel

      something like: Upper.Set( Trend[0] + ( Trend[0] * stDeviation / 100))

      it doesn't works, no matter where i try to place the code snippet, i always get some kind of error or it compiles but the plot looks as if were working with the default series instead of the secondary (Daily) like the Main plot does, so i cant create alerts nor create a strategy with it. :/

      Comment


        #4
        Hello,

        Thank you for the reply.

        Is it that the script is working with the primary series it is applied to rather than adding its own data series? Or are you expecting it to work based off of an input but you are not using the Input series?

        Without seeing the syntax in question I really couldn't comment aside from observations on your explanation. Could you provide an example of how the indicator is currently plotting and any additional series you are using?

        If you have compile errors, that would be helpful to see what you were trying that did not work along with the error you had received.

        For runtime problems, debugging the script is really the only way to know what is going on but that would require seeing the syntax to better understand why that is the outcome.

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

        Comment


          #5
          hey, yep, i attached the indicator in case you wanna take a look at it.

          KBT_XmovAvg_v2.zip

          The code in blue is not working, could you guide me as to how should i add an Upper / Lower Plot within this code so i can turn the indicator into a Channel? thank you Jesse

          here's part of the code:

          Code:
              //****** VARIABLES ****************************************
           
                      
          #region 01.Parameters Mayor Timeframe 
                  
                  private MovAvg_v1AltTimeframe  selectATS        = MovAvg_v1AltTimeframe.Minute;
                  private int altPeriod                    = 15;
           
                  
          #endregion            
              
                      private xMovAv_v1  maType            = xMovAv_v1.EMA;            
                      private int maPeriod                    = 20;
          
               
          
                      private Color upColor        = Color.Blue;
                      private Color dnColor        = Color.Red;
                      private Color ntColor        = Color.Gold;
                  
          
          
                      
                      private bool useUpDnColors=true;
                      
          
              
          
                      
                      //--------------------------------------
                      private bool firstTick                = false;
                      private double _altMA;
                      private double _altMAprev;
                      private int barcount;
                      private double myslope;
                      private double holdslope;
                      private double fastchngslope;
                      
                      private Color myColor                = Color.Gray;
                      private Color plotColor                = Color.Gray;
                      private Color holdPlotColor            = Color.Gray;
                      
          
                      
                      private bool loopctrl                = false;
                      private int loopint;
                      
          
          
                       
                      
          
                      
                  //******************************************************************************
                  protected override void Initialize()
                  {
                      Add(new Plot(new Pen(Color.FromKnownColor(KnownColor.Yellow), 1), PlotStyle.Line, "Trend"));
          [B][COLOR=Blue]           Add(new Plot(new Pen(Color.FromKnownColor(KnownColor.DarkOrange), 4), PlotStyle.Line, "Upper"));[/COLOR][/B]
          
                      
          
                      
                      CalculateOnBarClose    = false;
                      DisplayInDataBox    = false;
                      Overlay                = true;        
                      PaintPriceMarkers    = false;
                      AutoScale            = false;
                  
                      
                   
                       #region Mayor TF Selection Switcher
                      
                          switch(selectATS)
                          {
                              
                              
                              case MovAvg_v1AltTimeframe.Default:
                                  Add(Instrument.FullName, BarsPeriod.Id, BarsPeriod.Value, MarketDataType.Last);
                                  break;    
                              
                              case MovAvg_v1AltTimeframe.Minute:
                                  Add(PeriodType.Minute, altPeriod);
                                  break;
                                  
                              case MovAvg_v1AltTimeframe.Day:
                                  Add(PeriodType.Day, altPeriod);
                                  break;
                                  
                              case MovAvg_v1AltTimeframe.Week:
                                  Add(PeriodType.Week, altPeriod);
                                  break;
                              
                              case MovAvg_v1AltTimeframe.Range:
                                  Add(PeriodType.Range, altPeriod);
                                  break;                
                                  
                              case MovAvg_v1AltTimeframe.RangeNoGap:
                                  Add(PeriodType.Custom2, altPeriod);
                                  break;
          
                              case MovAvg_v1AltTimeframe.HybridRenko:
                                  Add(PeriodType.Custom6, altPeriod);
                                  break;
                              
          
                                  
          
                                  
                          }
                      
            #endregion            
                      
          
          
                  }
          
                  //**************************************************************************
                  protected override void OnBarUpdate()
                  {
          
                      if  (BarsInProgress == 1) 
                      {
                      
                          {
                              if (CurrentBars[1] < maPeriod)    
                                   return;    
                                  
                              if (CurrentBar < 3)    
                                   return;    
                                  
                              if (FirstTickOfBar)
                              {
                                  firstTick = true;
                                  _altMAprev = _altMA;
                              }
                              else
                                  firstTick = false;
                              
                              
                              
                               if (maType == xMovAv_v1.EMA)
                                  _altMA = (EMA( Closes[1], maPeriod) [0]);
                              
                              else if (maType == xMovAv_v1.SMA)
                                  _altMA = (SMA( Closes[1], maPeriod) [0]);
                              
                              else if (maType == xMovAv_v1.WMA)
                                  _altMA = (WMA ( Closes[1], maPeriod) [0]);
                                          
                              else if (maType == xMovAv_v1.T3)
                                  _altMA = (T3(Closes[1], maPeriod ,3 , 0.7) [0]);                
          
                              
                           
                              
                          }
                      }
                      
          
          
          
                      
                          
                      //=====================================================
                      if (BarsInProgress == 0)    // Plots on Current Chart
                      {
                          if (CurrentBars[0] > maPeriod)
                          {
                      
                              //---------------------------------------------------
                              if (firstTick)
                              {
          
                                  firstTick = false;
                                  double diff; double pertick;
                                  diff = (_altMA -_altMAprev);
                                  pertick =diff / barcount;
                                  //=== PLOT SLOPE & COLORS ======================
                                  holdslope = myslope;
                                  myslope = Math.Round(((_altMA-_altMAprev)*1000),4);
                                  fastchngslope = holdslope / 2;
                                  if (myslope > 0) //---TWO Color Slope
                                  {                            
                                      plotColor = upColor;
          
                                  }
                                  else if (myslope < 0)
                                  {
                                      plotColor=dnColor;
          
                                  }
                                  else
                                  {
                                      plotColor=ntColor;
          
                                  }
                                  
                                  if (CurrentBar > barcount)
                                  {
                                      int stop; int mult; int idx;
                                      stop = barcount; 
                                      idx = barcount;
                                      mult = 0;
                                      loopctrl=true;
                                      for (loopint=0;(loopctrl);loopint++)
                                      {
                                          if (useUpDnColors)  
                                          {
                                              PlotColors[0][idx]=plotColor;
                                              PlotColors[1][idx]=plotColor;
                                          }
                                          else
                                          PlotColors[1][idx]=holdPlotColor;
                                          
                                          Trend[idx]=_altMAprev + (pertick * mult);
                                          
          //                                New.Set( Trend[0]);
          
                                          mult ++;
                                          idx = idx -1;
                                          if (loopint >= stop) loopctrl = false;
                                          
                                          
                                      }                
                                      barcount = 0;
                                          
                                  }
                              }
                              barcount++;    
                          } 
                          else
                              holdPlotColor = PlotColors[1][0];
                          
                      } //END of BarsInProgress == 0
                      
                    [B][COLOR=Blue] Upper.Set(    Trend[0] + ( Trend[0] * 2 / 100));[/COLOR][/B]
                      
                      
                      
                  } //---LAST BRACKET

          Comment


            #6
            Hello,

            Thank you for the reply.

            Unfortunately, the attached file is protected meaning no one else can review the source code. We could run it but that would have little relevance as we don't know what code was used in its entirety.

            From the syntax sample you posted it looks like this is a fairly complicated or large item, For this, I would instead suggest making a small example using a single plot and see if you run into the same problem. The code in blue per your example doesn't provide enough information on what may not be working correctly. I can see that you are plotting a value but without being able to run the syntax and also debug it, I could not say what may be happening.

            Extracting the logic in question and putting that specific logic in a second script could help highlight the problem as it is not apparent from what has been provided.

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

            Comment


              #7
              Im trully sorry about the protected file, really weird cause i didn't exported it using any kind of protection, oh well.. here's the .CS, sorry again Jesse

              KBT_XmovAvg_v2.cs

              Comment


                #8
                Hello,

                Thank you for posting the indicator.

                I reviewed the file but don't see anything that would stick out specifically as the reason for differences. I was able to see you are using dynamic Add syntax which we highly advise against as it is known to cause problems.

                Are you able to still have the problem occur if you remove the dynamic series adding and specify using strings and actual values what you want instead? For example Instaed of:

                Code:
                case MovAvg_v2AltTimeframe.Default:
                    Add(Instrument.FullName, BarsPeriod.Id, BarsPeriod.Value, MarketDataType.Last);
                break;
                Using:

                Code:
                 Add("ES 06-17", PeriodType.Minute, 1, MarketDataType.Last);
                Using dynamics variables in Initialize specifically like this is known to cause problems and not work as expected in a few different situations, this could potentially be leading to the result you are seeing. What I could suggest would be to make a much more simple test and add the secondary series you want to use explicitly like I have shown above instead of using variables/switches to populate the data.

                After doing this, if you are still running into the same problem I would like to see both the strategy/indicator that is calling this item and also the modified sample that removes the dynamic nature of Initialize.

                If you wish to continue using the dynamic Add syntax, all I could really suggest would be to reduce the script to a single plot and setting a single plots value, and then call it from the other script and review the output. Taking away all other logic that is not needed to test and ensure you are getting an expected value would aid in finding the reason the current logic is not working.

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

                Comment


                  #9
                  Im going to check your suggestions Jesse, thank you! if anything you'll have me bugging you again tomorrow ;D

                  Comment


                    #10
                    Hi Jesse, i removed the dynamic Add series, and the dynamic averages, still the same issue tho', i attached the new basic version and a screenshot of the persisting problem.

                    KBT_XMovAvg_V3.cs


                    here you can see the two averages, the one on the top ( "Upper" plot) should look like the one on the bottom ( "Trend" plot) thanks for your help Jesse
                    Click image for larger version

Name:	2017-05-02_180524.jpg
Views:	1
Size:	138.8 KB
ID:	882834

                    Comment


                      #11
                      Hello,

                      Thank you for the reply.

                      I reviewed the following item but cannot see a specific cause. I did note that you have not removed all the dynamic syntax from Initialize which very well may be causing the problem.

                      Code:
                      Add([B]Instrument.FullName[/B], PeriodType.Day, 1, MarketDataType.Last);
                      should instead be something like:

                      Code:
                      Add([B]"ES 06-17"[/B], PeriodType.Day, 1, MarketDataType.Last);
                      I can see that when I comment out all of your logic and add a Specific series like the example above, I can get a result as expected. This is likely due to how you are dynamically adding series and it silently failing. Adding series in this way is known to produce problems and is expected to fail in some cases. If you change the logic to add a specific series and then retest are you able to get a value?

                      Also using a more simple test may help identify if logic is also creating a problem, please try something like the following to ensure the calling script gets a value:

                      Code:
                      protected override void OnBarUpdate()
                              {
                      			if (BarsInProgress == 0)
                      			{
                      				Trend[0] = Close[0];
                      				Upper.Set(Trend[0] + (Trend[0] * 2 /100));
                      			} 
                              }
                      You could call the Trend plot from another script and ensure you get the Close price or some other specific value to ensure you have the connection between scripts working. Once you do this, you could modify the indicators logic to be more complex and re-test as you go.




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

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by TraderBCL, Today, 04:38 AM
                      2 responses
                      8 views
                      0 likes
                      Last Post TraderBCL  
                      Started by martin70, 03-24-2023, 04:58 AM
                      14 responses
                      105 views
                      0 likes
                      Last Post martin70  
                      Started by Radano, 06-10-2021, 01:40 AM
                      19 responses
                      606 views
                      0 likes
                      Last Post Radano
                      by Radano
                       
                      Started by KenneGaray, Today, 03:48 AM
                      0 responses
                      4 views
                      0 likes
                      Last Post KenneGaray  
                      Started by thanajo, 05-04-2021, 02:11 AM
                      4 responses
                      471 views
                      0 likes
                      Last Post tradingnasdaqprueba  
                      Working...
                      X