Last active
November 29, 2016 08:00
-
-
Save bboyle1234/c16508460cd21cc55c80afdcb6296283 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region 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; | |
using System.Reflection; | |
using System.Threading; | |
#endregion | |
//This namespace holds Indicators in this folder and is required. Do not change it. | |
namespace NinjaTrader.NinjaScript.Indicators { | |
public static class BarsExtensions { | |
static readonly MethodInfo barsSeriesGetter = typeof(Bars).GetProperty("BarsSeries").GetGetMethod(); | |
public static NinjaTrader.Data.BarsSeries GetBarsSeries(this Bars bars) { | |
return (BarsSeries)barsSeriesGetter.Invoke(bars, new object[0]); | |
} | |
public static bool IsMarketDataSubscribed(this Bars bars) { | |
return bars.GetBarsSeries().IsMarketDataSubscribed; | |
} | |
} | |
public class SubscriptionTest : Indicator { | |
string status; | |
BarsRequest request; | |
Bars requestBars; | |
int initialRequestBarsCount; | |
int numNewTicks = 0; | |
protected override void OnStateChange() { | |
if (State == State.SetDefaults) { | |
Description = @"Enter the description for your new custom Indicator here."; | |
Name = "SubscriptionTest"; | |
Calculate = Calculate.OnBarClose; | |
IsOverlay = true; | |
DisplayInDataBox = true; | |
DrawOnPricePanel = true; | |
DrawHorizontalGridLines = true; | |
DrawVerticalGridLines = true; | |
PaintPriceMarkers = true; | |
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right; | |
} else if (State == State.Configure) { | |
} else if (State == State.DataLoaded) { | |
status = "Loading"; | |
var startDate = DateTime.Now.Date.AddDays(-5); | |
var endDate = DateTime.Now.Date.AddDays(30); | |
request = new BarsRequest(Instrument, startDate, endDate); | |
request.BarsPeriod = new BarsPeriod { BarsPeriodType = BarsPeriodType.Tick, Value = 1, }; | |
request.IsDividendAdjusted = true; | |
request.IsResetOnNewTradingDay = true; | |
request.IsSplitAdjusted = true; | |
request.LookupPolicy = LookupPolicies.Provider | LookupPolicies.Repository; | |
request.MergePolicy = MergePolicy.MergeBackAdjusted; | |
request.TradingHours = TradingHours.Get(TradingHours.SystemDefault); | |
request.Request((r, errorCode, message) => { | |
if (State >= State.Terminated) return; | |
if (errorCode == ErrorCode.NoError) { | |
requestBars = request.Bars; | |
initialRequestBarsCount = requestBars.Count; | |
Task.Run(() => { | |
while (State < State.Terminated) { | |
var format = "Subscribed: {0}, Num New TIck Bars: {1}, Expected Num New Tick Bars: {2}"; | |
status = string.Format(format, requestBars.IsMarketDataSubscribed(), requestBars.Count - initialRequestBarsCount, numNewTicks); | |
ForceRefresh(); | |
Thread.Sleep(500); | |
} | |
}); | |
} else { | |
status = "error: " + errorCode.ToString(); | |
} | |
}); | |
} else if (State == State.Terminated) { | |
if (null != request) { | |
request.Dispose(); | |
} | |
} | |
} | |
protected override void OnBarUpdate() { | |
if (State == State.Realtime && null != requestBars) { | |
numNewTicks++; | |
} | |
} | |
protected override void OnRender(ChartControl chartControl, ChartScale chartScale) { | |
Draw.TextFixed(this, "status", status, TextPosition.BottomRight); | |
base.OnRender(chartControl, chartScale); | |
} | |
} | |
} | |
#region NinjaScript generated code. Neither change nor remove. | |
namespace NinjaTrader.NinjaScript.Indicators { | |
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase { | |
private SubscriptionTest[] cacheSubscriptionTest; | |
public SubscriptionTest SubscriptionTest() { | |
return SubscriptionTest(Input); | |
} | |
public SubscriptionTest SubscriptionTest(ISeries<double> input) { | |
if (cacheSubscriptionTest != null) | |
for (int idx = 0; idx < cacheSubscriptionTest.Length; idx++) | |
if (cacheSubscriptionTest[idx] != null && cacheSubscriptionTest[idx].EqualsInput(input)) | |
return cacheSubscriptionTest[idx]; | |
return CacheIndicator<SubscriptionTest>(new SubscriptionTest(), input, ref cacheSubscriptionTest); | |
} | |
} | |
} | |
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns { | |
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase { | |
public Indicators.SubscriptionTest SubscriptionTest() { | |
return indicator.SubscriptionTest(Input); | |
} | |
public Indicators.SubscriptionTest SubscriptionTest(ISeries<double> input) { | |
return indicator.SubscriptionTest(input); | |
} | |
} | |
} | |
namespace NinjaTrader.NinjaScript.Strategies { | |
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase { | |
public Indicators.SubscriptionTest SubscriptionTest() { | |
return indicator.SubscriptionTest(Input); | |
} | |
public Indicators.SubscriptionTest SubscriptionTest(ISeries<double> input) { | |
return indicator.SubscriptionTest(input); | |
} | |
} | |
} | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment