Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Moving average color change when slow ma crosses above or below

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

    #16
    Hello tradingnasdaqprueba,

    It looks like you did not add public properties for your plots. Just as a heads up in the future its best to use the new indicator wizard to create plots for your script, when you create a new indicator you can use the plots section to add your plots. That will also create the public properties.

    Without adding any more code you can use the Values collection like the following:

    Code:
    if (EMA(Input, fastPeriod)[0] < EMA(Input,slowPeriod)[0])
    {
    Values[0][0] = EMA(Input, fastPeriod)[0];
    Values[0][1] = EMA(Input, fastPeriod)[1];
    }
    else
    {
    Values[1][0] = EMA(Input, fastPeriod)[0];
    Values[1][1] = EMA(Input, fastPeriod)[1];
    }​


    If you wanted to use MyEMAGreen and MyEMARed you need to add the public properties for those two plots, the properties look like the following:

    Code:
    [Browsable(false)]
    [XmlIgnore]
    public Series<double> MyEMAGreen
    {
    get { return Values[0]; }
    }
    
    [Browsable(false)]
    [XmlIgnore]
    public Series<double> MyEMARed
    {
    get { return Values[1]; }
    }​

    Comment


      #17
      Hello Again,

      It seems that the problem is that I was missing the public properties. But now comes a new error CS1518:


      Click image for larger version

Name:	image.png
Views:	164
Size:	72.4 KB
ID:	1272799

      What does this mean? I couldn't find Info about it...

      Thanks!

      Comment


        #18
        Hello Jesse,

        I made the changes as you said, still some errors occur (see image below):

        Code:
        [HASHTAG="t3322"]region[/HASHTAG] Using declarations
        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.ComponentModel.DataAnnotations;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        using System.Windows;
        using System.Windows.Input;
        using System.Windows.Media;
        using System.Xml.Serialization;
        using NinjaTrader.Cbi;
        using NinjaTrader.Gui;
        using NinjaTrader.Gui.Chart;
        using NinjaTrader.Gui.SuperDom;
        using NinjaTrader.Gui.Tools;
        using NinjaTrader.Data;
        using NinjaTrader.NinjaScript;
        using NinjaTrader.Core.FloatingPoint;
        using NinjaTrader.NinjaScript.DrawingTools;
        #endregion
        
        //This namespace holds Indicators in this folder and is required. Do not change it.
        namespace NinjaTrader.NinjaScript.Indicators
        {
        public class EMA20DIRECCION : Indicator
        {
        protected override void OnStateChange()
        {
        if (State == State.SetDefaults)
        {
        Description = @"Enter the description for your new custom Indicator here.";
        Name = "EMA20DIRECCION";
        Calculate = Calculate.OnBarClose;
        IsOverlay = false;
        DisplayInDataBox = true;
        DrawOnPricePanel = true;
        DrawHorizontalGridLines = true;
        DrawVerticalGridLines = true;
        PaintPriceMarkers = true;
        ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
        //Disable this property if your indicator requires custom values that cumulate with each new market data event.
        //See Help Guide for additional information.
        IsSuspendedWhileInactive = true;
        AddPlot(Brushes.Green, "MyEMAGreen");
        AddPlot(Brushes.Red, "MyEMARed");
        
        
        }
        else if (State == State.Configure)
        {
        }
        }
        
        [HASHTAG="t3322"]region[/HASHTAG] Variables
        // Wizard generated variables
        private int slowPeriod = 8; // Default setting for SlowPeriod
        private int fastPeriod = 20; // Default setting for FastPeriod
        // User defined variables (add any user defined variables below)
        #endregion
        
        
        protected override void OnBarUpdate()
        {
        //Add your custom indicator logic here.
        if (CurrentBar == 0) return;
        
        if (EMA(Input, fastPeriod)[0] < EMA(Input,slowPeriod)[0])
        {
        Values[0][0] = EMA(Input, fastPeriod)[0];
        Values[0][1] = EMA(Input, fastPeriod)[1];
        }
        else
        {
        Values[1][0] = EMA(Input, fastPeriod)[0];
        Values[1][1] = EMA(Input, fastPeriod)[1];
        }​​
        }
        }
        }
        
        [HASHTAG="t3322"]region[/HASHTAG] Properties
        
        [Browsable(false)]
        [XmlIgnore]
        public Series<double> MyEMAGreen
        {
        get { return Values[0]; }
        }
        
        [Browsable(false)]
        [XmlIgnore]
        public Series<double> MyEMARed
        {
        get { return Values[1]; }
        }​
        
        #endregion
        
        [HASHTAG="t3322"]region[/HASHTAG] NinjaScript generated code. Neither change nor remove.
        
        namespace NinjaTrader.NinjaScript.Indicators
        {
        public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
        {
        private EMA20DIRECCION[] cacheEMA20DIRECCION;
        public EMA20DIRECCION EMA20DIRECCION()
        {
        return EMA20DIRECCION(Input);
        }
        
        public EMA20DIRECCION EMA20DIRECCION(ISeries<double> input)
        {
        if (cacheEMA20DIRECCION != null)
        for (int idx = 0; idx < cacheEMA20DIRECCION.Length; idx++)
        if (cacheEMA20DIRECCION[idx] != null && cacheEMA20DIRECCION[idx].EqualsInput(input))
        return cacheEMA20DIRECCION[idx];
        return CacheIndicator<EMA20DIRECCION>(new EMA20DIRECCION(), input, ref cacheEMA20DIRECCION);
        }
        }
        }
        
        namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
        {
        public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
        {
        public Indicators.EMA20DIRECCION EMA20DIRECCION()
        {
        return indicator.EMA20DIRECCION(Input);
        }
        
        public Indicators.EMA20DIRECCION EMA20DIRECCION(ISeries<double> input )
        {
        return indicator.EMA20DIRECCION(input);
        }
        }
        }
        
        namespace NinjaTrader.NinjaScript.Strategies
        {
        public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
        {
        public Indicators.EMA20DIRECCION EMA20DIRECCION()
        {
        return indicator.EMA20DIRECCION(Input);
        }
        
        public Indicators.EMA20DIRECCION EMA20DIRECCION(ISeries<double> input )
        {
        return indicator.EMA20DIRECCION(input);
        }
        }
        }
        
        #endregion


        Click image for larger version

Name:	image.png
Views:	201
Size:	64.6 KB
ID:	1272807

        Thanks for your help!​

        Comment


          #19
          Hello tradingnasdaqprueba,

          You placed that code outside of the class, the properties need to go inside the class. You can open the Bollinger Bands indicator to see how the public property code looks and where it should be placed in the file.

          Comment


            #20
            Hello Jesse,

            I see, thanks for the explanation.

            So? I still have an error...

            Click image for larger version

Name:	image.png
Views:	159
Size:	61.5 KB
ID:	1272814

            Comment


              #21
              Hello tradingnasdaqprueba,

              It looks like you have removed some of the curly braces you need, one is from OnBarUpdate. I would suggest to undo the changes you made for the public properties so you go back to where you had the errors from post 15 and then retry adding the properties into the class. Your file should look similar to the bollinger bands in respect to the curly braces } and where the public properties are placed.


              Comment


                #22
                Originally posted by tradingnasdaqprueba View Post
                Hello Brandon,

                The thing is I am trying to copy one indicator that I have from a 3rd Party. What happens here is that sometimes this indicator doesn't color some little MA crosses... but other times it does... and I don't understand the logic here...

                Maybe somebody can tell me which is the logic here, that would be very helpfull. See also that sometimes my indicator changes 1 bar too late or too soon...

                Look at the images:

                3rd Party indicator:



                Here change the color





                Here the color doesnt change… but a Cross happened…





                My indicator:






                Hello Omololu,

                I atached the indicator for NT7 so maybe somebody can convert it to NT8.

                Thanks!

                You may wish to test this "verbatim" conversion to NT8.

                lolu

                [ATTACH]n1273251[/ATTACH]






                Click image for larger version  Name:	EMACrossColorNT8prueba.png Views:	0 Size:	40.9 KB ID:	1272996
                Last edited by omololu; 10-16-2023, 10:29 PM. Reason: I have uploaded it again. Are you able to download it now ?

                Comment


                  #23
                  Hi Omololu, I can't download your file... Can you try to upload again?

                  Thanks!

                  Comment


                    #24
                    Originally posted by tradingnasdaqprueba View Post
                    Hi Omololu, I can't download your file... Can you try to upload again?

                    Thanks!
                    I have uploaded it again. You should be able to download it now.

                    Omololu

                    EMACrossColorNT8prueba.cs
                    Last edited by omololu; 10-17-2023, 04:27 AM.

                    Comment


                      #25
                      Hello Omololu and Jesse,

                      I was able to convert the code to NT8, it works really well... but as you know I am trying to have the same color changes that the 3rd Party indicator. I think that the 3rd Party indicator has another condition.

                      I think that for example, the MA SLOW has be > than the MA FAST, and it has to be a green candle to change the color, if its a Red candle will wait 1 bar more to change "as confirmation".

                      But it takes the cross as confirmation, and sometimes does my indicator change the color at the same time but some times not... I think this can be because the 3rd party changes the color when the cross has already produced??

                      See Picture:




                      Thanks a lot for the help!!!​​​
                      Last edited by tradingnasdaqprueba; 10-20-2023, 06:34 AM.

                      Comment


                        #26
                        Hello tradingnasdaqprueba,

                        Thanks for your notes.

                        Omololu has shared a conversion of the script that is compatible with NinjaTrader 8 on post # 24 and it seems to be working as epxected when testing it on my end.

                        Have you added debugging prints to your script to understand exactly how the logic in the script is behaving?

                        If the indicator is not working as expected then you should add debugging prints to the script one line above the condition to change the color of the plot that prints out the conditions being used to change the color of the plot.

                        Then, you could view the print output in a New > NinjaScript Output window to understand how your logic is evaluating.

                        Below is a link to a forum post that demonstrates how to use prints to understand behavior.
                        https://ninjatrader.com/support/foru...121#post791121

                        If you want to add logic to your script that checks if the bar is an up (green) bar, you could create a condition that checks if the Close[0] price is greater than the Open[0] price.​

                        If the third-party script you are referring to is open source and you could view its code, you could compare the third-party indicator's code to your script to see where differences in the logic might be.
                        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                        Comment


                          #27
                          Originally posted by omololu View Post

                          I have uploaded it again. You should be able to download it now.

                          Omololu

                          [ATTACH]n1273267[/ATTACH]
                          this is great. Thanks!

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                          0 responses
                          607 views
                          0 likes
                          Last Post Geovanny Suaza  
                          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                          0 responses
                          353 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by Mindset, 02-09-2026, 11:44 AM
                          0 responses
                          105 views
                          0 likes
                          Last Post Mindset
                          by Mindset
                           
                          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                          0 responses
                          560 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by RFrosty, 01-28-2026, 06:49 PM
                          0 responses
                          561 views
                          1 like
                          Last Post RFrosty
                          by RFrosty
                           
                          Working...
                          X