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

Premium

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

    Premium

    I am new to working with multi data streams in a single chart. I've set a chart up with the front month ES as the 1st data stream and the S&P cash index as the second. The NinjaScript code below is not doing exactly what I'd like it to do, and I have a feeling that I'm not quite fully understanding how to create code for multiple datastreams. There are two problems that I'd like to fix:

    1. It will sometimes give me results that are outside the normal range, which is corrected once I reload the data. Do I need to "sync" the two datastreams somehow so that this doesn't happen?

    2. I want the audio alerts to fire off just once per bar, but it isn't firing at all. Yes, I am sure that the sound file is appropriately named and resides where it needs to be.

    Any help would be appreciated.

    Code:
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Gui.Chart;
    #endregion
    
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
        [Description("Premium Trading Levels for the ES front month & S&P 500 Spread Difference")]
        public class Premium : Indicator
        {
            #region Variables
                private double pTBuy = -2.89; // PTBuy Input
                private double pTSell = -5.69; // PTSell Sell
                private bool audioalerts = true;
                private double difference = 0;
                private string secondInstrument = @"^SP500"; // Default setting for SecondInstrument
                private bool AlreadyAlertThisBar=false;
            
            #endregion
    
            protected override void Initialize()
            {    
                   Add(new Plot(new Pen(Color.Green, 2), "Buy Level"));
                Add(new Plot(new Pen(Color.Red, 2), "Sell Level"));
                Add(new Plot(new Pen(Color.Orange, 2), "Difference"));
                Add(secondInstrument, PeriodType.Minute, 1);
                CalculateOnBarClose    = false;
                Overlay                = false;
                DrawOnPricePanel    = false;
                PriceTypeSupported    = false;
            }
    
            protected override void OnBarUpdate()
            {
                // Use this method for calculating your indicator values. Assign a value to each
                // plot below by replacing 'Close[0]' with your own formula.
                            
                PTBuyLine.Set(PTBuy);
                PTSellLine.Set(PTSell);
                
                difference = Closes[0][0]-Closes[1][0];  
                
                DifferenceSeries.Set(difference);
                
                if(FirstTickOfBar) AlreadyAlertThisBar=false;
                            
                if (CrossAbove(DifferenceSeries,PTBuy,1))
                {
                    DrawDot("Buy Dot" + CurrentBar, false, 0, PTBuy, Color.Green);
                    if (AlreadyAlertThisBar=false && AudioAlerts) 
                        {PlaySound(@"C:\Sounds\Premium Buy.wav");
                         AlreadyAlertThisBar=true;}
                }
                
                if (CrossBelow(DifferenceSeries,PTSell,1))
                {    
                    DrawDot("Sell Dot" + CurrentBar, false, 0, PTSell, Color.Maroon);
                    if (AlreadyAlertThisBar=false && AudioAlerts) 
                        {PlaySound(@"C:\Sounds\Premium Sell.wav");
                         AlreadyAlertThisBar=true;}
                }
            }
    
            #region Properties
            [Browsable(false)]
            [XmlIgnore()] 
            public DataSeries PTBuyLine
            {
                get { return Values[0]; }
            }
    
            [Browsable(false)] 
            [XmlIgnore()] 
            public DataSeries PTSellLine
            {
                get { return Values[1]; }
            }
            
             [Browsable(false)]
            [XmlIgnore()] 
            public DataSeries DifferenceSeries
            {
                get { return Values[2]; }
            }
    
            [Description("Program Trading Buy Level")]
            [GridCategory("Parameters")]
            public double PTBuy
            {
                get { return pTBuy; }
                set { pTBuy = Math.Max(-100, value); }
            }
    
            [Description("Program Trading Sell Level")]
            [GridCategory("Parameters")]
            public double PTSell
            {
                get { return pTSell; }
                set { pTSell = Math.Max(-100, value); }
            }
            
            [Description("True=play audio alerts, False=don't play audio alerts.")]
            [Category("Parameters")]
            public bool AudioAlerts
            {
                get { return audioalerts; }
                set { audioalerts = value; }
            }
                    
            #endregion
        }
    }
    
    #region NinjaScript generated code. Neither change nor remove.
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
        public partial class Indicator : IndicatorBase
        {
            private Premium[] cachePremium = null;
    
            private static Premium checkPremium = new Premium();
    
             /// Premium Trading Levels for the ES front month & S&P 500 Spread Difference
            /// <returns></returns>
            public Premium Premium(bool audioAlerts, double pTBuy, double pTSell)
            {
                return Premium(Input, audioAlerts, pTBuy, pTSell);
            }
    
            /// Premium Trading Levels for the ES front month & S&P 500 Spread Difference
             /// <returns></returns>
            public Premium Premium(Data.IDataSeries input, bool audioAlerts, double pTBuy, double pTSell)
            {
                if (cachePremium != null)
                    for (int idx = 0; idx < cachePremium.Length; idx++)
                        if (cachePremium[idx].AudioAlerts == audioAlerts && Math.Abs(cachePremium[idx].PTBuy - pTBuy) <= double.Epsilon && Math.Abs(cachePremium[idx].PTSell - pTSell) <= double.Epsilon && cachePremium[idx].EqualsInput(input))
                            return cachePremium[idx];
    
                lock (checkPremium)
                {
                    checkPremium.AudioAlerts = audioAlerts;
                    audioAlerts = checkPremium.AudioAlerts;
                    checkPremium.PTBuy = pTBuy;
                    pTBuy = checkPremium.PTBuy;
                    checkPremium.PTSell = pTSell;
                    pTSell = checkPremium.PTSell;
    
                    if (cachePremium != null)
                        for (int idx = 0; idx < cachePremium.Length; idx++)
                            if (cachePremium[idx].AudioAlerts == audioAlerts && Math.Abs(cachePremium[idx].PTBuy - pTBuy) <= double.Epsilon && Math.Abs(cachePremium[idx].PTSell - pTSell) <= double.Epsilon && cachePremium[idx].EqualsInput(input))
                                return cachePremium[idx];
    
                    Premium indicator = new Premium();
                    indicator.BarsRequired = BarsRequired;
                    indicator.CalculateOnBarClose = CalculateOnBarClose;
    #if NT7
                    indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
                    indicator.MaximumBarsLookBack = MaximumBarsLookBack;
    #endif
                    indicator.Input = input;
                    indicator.AudioAlerts = audioAlerts;
                    indicator.PTBuy = pTBuy;
                    indicator.PTSell = pTSell;
                    Indicators.Add(indicator);
                    indicator.SetUp();
    
                    Premium[] tmp = new Premium[cachePremium == null ? 1 : cachePremium.Length + 1];
                    if (cachePremium != null)
                        cachePremium.CopyTo(tmp, 0);
                    tmp[tmp.Length - 1] = indicator;
                    cachePremium = tmp;
                    return indicator;
                }
            }
        }
    }
    
    // This namespace holds all market analyzer column definitions and is required. Do not change it.
    namespace NinjaTrader.MarketAnalyzer
    {
        public partial class Column : ColumnBase
        {
            [Gui.Design.WizardCondition("Indicator")]
            public Indicator.Premium Premium(bool audioAlerts, double pTBuy, double pTSell)
            {
                return _indicator.Premium(Input, audioAlerts, pTBuy, pTSell);
            }
    
            public Indicator.Premium Premium(Data.IDataSeries input, bool audioAlerts, double pTBuy, double pTSell)
            {
                return _indicator.Premium(input, audioAlerts, pTBuy, pTSell);
            }
        }
    }
    
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
        public partial class Strategy : StrategyBase
        {
            [Gui.Design.WizardCondition("Indicator")]
            public Indicator.Premium Premium(bool audioAlerts, double pTBuy, double pTSell)
            {
                return _indicator.Premium(Input, audioAlerts, pTBuy, pTSell);
            }
    
            public Indicator.Premium Premium(Data.IDataSeries input, bool audioAlerts, double pTBuy, double pTSell)
            {
                if (InInitialize && input == null)
                    throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");
    
                return _indicator.Premium(input, audioAlerts, pTBuy, pTSell);
            }
        }
    }
    #endregion

    #2
    Hi TrendTracker,

    Best practice with multiple series is to hard code the series in Initialize() method. Some users report success using inputs, but the timing will not always work to add it when the script needs.

    There is different behavior referencing multiseries bars historically compared to real time with CalculateOnBarClose = false. This is usually the first place to look when values aren't returning what you expect. These differences are highlighted here in the section How Bar Data is Referenced


    You'll want to use BarsInProgress filters appropriately to define the context of bar updates. A couple examples are available here:


    You should separate the two tasks of troubleshooting sound playback with the condition evaluating.

    To troubleshoot sound playback, you should first try with a simplified script that is made to only play sound with a simple condition. This is a real time only action. Once you verify it's working you can integrate it into your script.

    To evaluate the condition triggering, use a simple Print() statement and check in Tools > Output Window.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Thank you Ryan - this at least gives me a place to start.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by KennyK, 05-29-2017, 02:02 AM
      2 responses
      1,278 views
      0 likes
      Last Post marcus2300  
      Started by fernandobr, Today, 09:11 AM
      0 responses
      0 views
      0 likes
      Last Post fernandobr  
      Started by itrader46, Today, 09:04 AM
      1 response
      3 views
      0 likes
      Last Post NinjaTrader_Clayton  
      Started by timmbbo, Today, 08:59 AM
      0 responses
      2 views
      0 likes
      Last Post timmbbo
      by timmbbo
       
      Started by bmartz, 03-12-2024, 06:12 AM
      5 responses
      33 views
      0 likes
      Last Post NinjaTrader_Zachary  
      Working...
      X