Advertisement
Guest User

https://ninjatrader.com/support/helpGuides/nt8/?exposing_indicator_values_that.htm

a guest
Jul 23rd, 2021
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.04 KB | None | 0 0
  1. //
  2. // Copyright (C) 2015, NinjaTrader LLC <www.ninjatrader.com>.
  3. // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
  4. //
  5. #region Using declarations
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.ComponentModel.DataAnnotations;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Xml.Serialization;
  17. using NinjaTrader.Cbi;
  18. using NinjaTrader.Gui;
  19. using NinjaTrader.Gui.Chart;
  20. using NinjaTrader.Gui.SuperDom;
  21. using NinjaTrader.Data;
  22. using NinjaTrader.NinjaScript;
  23. using NinjaTrader.Core.FloatingPoint;
  24. using NinjaTrader.NinjaScript.DrawingTools;
  25. #endregion
  26.  
  27. // This namespace holds all indicators and is required. Do not change it.
  28. namespace NinjaTrader.NinjaScript.Indicators
  29. {
  30.     public class SampleBoolSeries : Indicator
  31.     {
  32.         /* We declare two Series<bool> objects here. We will expose these objects with the use of
  33.         public properties later. When we want to expose an object we should always use the associated
  34.         ISeries class type with it. This will ensure that the value accessed is kept up-to-date. */
  35.         private Series<bool> bearIndication;
  36.         private Series<bool> bullIndication;
  37.        
  38.         /* If you happen to have an object that does not have an ISeries class that can be used, you
  39.         will need to manually ensure its values are kept up-to-date. This process will be done in the
  40.         "Properties" region of the code. */
  41.         private double exposedVariable;
  42.  
  43.         protected override void OnStateChange()
  44.         {
  45.             if(State == State.SetDefaults)
  46.             {
  47.                 Name                    = "Sample bool series";
  48.                 Calculate               = Calculate.OnBarClose;
  49.                 IsOverlay               = true;
  50.             }
  51.            
  52.             else if(State == State.Configure)
  53.             {
  54.                 /* "this" syncs the Series<bool> to the historical bar object of the indicator. It will generate
  55.                 one bool value for every price bar. */
  56.                 bearIndication          = new Series<bool>(this);
  57.                 bullIndication          = new Series<bool>(this);
  58.             }
  59.         }
  60.  
  61.         protected override void OnBarUpdate()
  62.         {
  63.             // MACD Crossover: Fast Line cross above Slow Line
  64.             if (CrossAbove(MACD(12, 26, 9), MACD(12, 26, 9).Avg, 1))
  65.             {
  66.                 // Paint the current price bar lime to draw our attention to it
  67.                 BarBrushes[0]       = Brushes.Lime;
  68.                
  69.                 /* This crossover condition is considered bullish so we set the "bullIndication" Series<bool> object to true.
  70.                 We also set the "bearIndication" object to false so it does not take on a null value. */
  71.                 bullIndication[0]   = (true);
  72.                 bearIndication[0]   = (false);
  73.             }
  74.            
  75.             // MACD Crossover: Fast Line cross below Slow Line
  76.             else if (CrossBelow(MACD(12, 26, 9), MACD(12, 26, 9).Avg, 1))
  77.             {
  78.                 // Paint the current price bar magenta to draw our attention to it
  79.                 BarBrushes[0]       = Brushes.Magenta;
  80.                
  81.                 /* This crossover condition is considered bearish so we set the "bearIndication" Series<bool> object to true.
  82.                 We also set the "bullIndication" object to false so it does not take on a null value. */
  83.                 bullIndication[0]   = (false);
  84.                 bearIndication[0]   = (true);
  85.             }
  86.            
  87.             // MACD Crossover: No cross
  88.             else
  89.             {
  90.                 /* Since no crosses occured we are not receiving any bullish or bearish signals so we
  91.                 set our Series<bool> objects both to false. */
  92.                 bullIndication[0] = (false);
  93.                 bearIndication[0] = (false);
  94.             }
  95.            
  96.             // We set our variable to the close value.
  97.             exposedVariable = Close[0];
  98.         }
  99.  
  100.         // Important code segment in the Properties section. Please expand to view.
  101.         #region Properties
  102.         // Creating public properties that access our internal Series<bool> allows external access to this indicator's Series<bool>
  103.         [Browsable(false)]
  104.         [XmlIgnore]
  105.         public Series<bool> BearIndication
  106.         {
  107.             get { return bearIndication; }  // Allows our public BearIndication Series<bool> to access and expose our interal bearIndication Series<bool>
  108.         }
  109.        
  110.         [Browsable(false)]
  111.         [XmlIgnore]    
  112.         public Series<bool>  BullIndication
  113.         {
  114.             get { return bullIndication; }  // Allows our public BullIndication Series<bool> to access and expose our interal bullIndication Series<bool>
  115.         }
  116.  
  117.         public double ExposedVariable
  118.         {
  119.             // We need to call the Update() method to ensure our exposed variable is in up-to-date.
  120.             get { Update(); return exposedVariable; }
  121.         }
  122.         #endregion
  123.     }
  124. }
  125.  
  126. #region NinjaScript generated code. Neither change nor remove.
  127.  
  128. namespace NinjaTrader.NinjaScript.Indicators
  129. {
  130.     public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
  131.     {
  132.         private SampleBoolSeries[] cacheSampleBoolSeries;
  133.         public SampleBoolSeries SampleBoolSeries()
  134.         {
  135.             return SampleBoolSeries(Input);
  136.         }
  137.  
  138.         public SampleBoolSeries SampleBoolSeries(ISeries<double> input)
  139.         {
  140.             if (cacheSampleBoolSeries != null)
  141.                 for (int idx = 0; idx < cacheSampleBoolSeries.Length; idx++)
  142.                     if (cacheSampleBoolSeries[idx] != null &&  cacheSampleBoolSeries[idx].EqualsInput(input))
  143.                         return cacheSampleBoolSeries[idx];
  144.             return CacheIndicator<SampleBoolSeries>(new SampleBoolSeries(), input, ref cacheSampleBoolSeries);
  145.         }
  146.     }
  147. }
  148.  
  149. namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
  150. {
  151.     public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
  152.     {
  153.         public Indicators.SampleBoolSeries SampleBoolSeries()
  154.         {
  155.             return indicator.SampleBoolSeries(Input);
  156.         }
  157.  
  158.         public Indicators.SampleBoolSeries SampleBoolSeries(ISeries<double> input )
  159.         {
  160.             return indicator.SampleBoolSeries(input);
  161.         }
  162.     }
  163. }
  164.  
  165. namespace NinjaTrader.NinjaScript.Strategies
  166. {
  167.     public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
  168.     {
  169.         public Indicators.SampleBoolSeries SampleBoolSeries()
  170.         {
  171.             return indicator.SampleBoolSeries(Input);
  172.         }
  173.  
  174.         public Indicators.SampleBoolSeries SampleBoolSeries(ISeries<double> input )
  175.         {
  176.             return indicator.SampleBoolSeries(input);
  177.         }
  178.     }
  179. }
  180.  
  181. #endregion
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement