| 1 | /* Example of subscribing/unsubscribing to bars data events from an Add On as well as making bars requests. |
| 2 | The concept can be carried over to any NinjaScript object you may be working on. */ |
| 3 | public class MyAddOnTab : NTTabPage |
| 4 | { |
| 5 | private int daysBack = 5; |
| 6 | private bool barsRequestSubscribed = false; |
| 7 | private BarsRequest barsRequest; |
| 8 | |
| 9 | public MyAddOnTab() |
| 10 | { |
| 11 | // create a new bars request. This will determine the insturment and range for the bars to be requested |
| 12 | barsRequest = new BarsRequest(Cbi.Instrument.GetInstrument("AAPL"), DateTime.Now.AddDays(-daysBack), DateTime.Now); |
| 13 | |
| 14 | // Parametrize your request. |
| 15 | barsRequest.BarsPeriod = new BarsPeriod { BarsPeriodType = BarsPeriodType.Minute, Value = 1 }; |
| 16 | barsRequest.TradingHours = TradingHours.Get("Default 24 x 7"); |
| 17 | |
| 18 | // Attach event handler for real-time events if you want to process real-time data |
| 19 | barsRequest.Update += OnBarUpdate; |
| 20 | |
| 21 | // Request the bars |
| 22 | barsRequest.Request(new Action<BarsRequest, ErrorCode, string>((bars, errorCode, errorMessage) => |
| 23 | { |
| 24 | if (errorCode != ErrorCode.NoError) |
| 25 | { |
| 26 | // Handle any errors in requesting bars here |
| 27 | NinjaTrader.Code.Output.Process(string.Format("Error on requesting bars: {0}, {1}", |
| 28 | errorCode, errorMessage), PrintTo.OutputTab1); |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | // Output the bars we requested. Note: The last returned bar may be a currently in-progress bar |
| 33 | for (int i = 0; i < bars.Bars.Count; i++) |
| 34 | { |
| 35 | // Output the bars |
| 36 | NinjaTrader.Code.Output.Process(string.Format("Time: {0} Open: {1} High: {2} Low: {3} Close: {4} Volume: {5}", |
| 37 | bars.Bars.GetTime(i), |
| 38 | bars.Bars.GetOpen(i), |
| 39 | bars.Bars.GetHigh(i), |
| 40 | bars.Bars.GetLow(i), |
| 41 | bars.Bars.GetClose(i), |
| 42 | bars.Bars.GetVolume(i)), PrintTo.OutputTab1); |
| 43 | } |
| 44 | |
| 45 | // If requesting real-time bars, but there are currently no connections |
| 46 | lock (Connection.Connections) |
| 47 | if (Connection.Connections.FirstOrDefault() == null) |
| 48 | NinjaTrader.Code.Output.Process("Real-Time Bars: Not connected.", PrintTo.OutputTab1); |
| 49 | })); |
| 50 | } |
| 51 | |
| 52 | // This method is fired on real-time bar events |
| 53 | private void OnBarUpdate(object sender, BarsUpdateEventArgs e) |
| 54 | { |
| 55 | /* Depending on the BarsPeriod type of your barsRequest you can have situations where more than one bar is |
| 56 | updated by a single tick. Be sure to process the full range of updated bars to ensure you did not miss a bar. */ |
| 57 | |
| 58 | // Output bar information on each tick |
| 59 | for (int i = e.MinIndex; i <= e.MaxIndex; i++) |
| 60 | { |
| 61 | // Processing every single tick |
| 62 | NinjaTrader.Code.Output.Process(string.Format("Time: {0} Open: {1} High: {2} Low: {3} Close: {4}", |
| 63 | e.BarsSeries.GetTime(i), |
| 64 | e.BarsSeries.GetOpen(i), |
| 65 | e.BarsSeries.GetHigh(i), |
| 66 | e.BarsSeries.GetLow(i), |
| 67 | e.BarsSeries.GetClose(i)), PrintTo.OutputTab1); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Called by TabControl when tab is being removed or window is closed |
| 72 | public override void Cleanup() |
| 73 | { |
| 74 | // Make sure to unsubscribe to the bars request subscription |
| 75 | if (barsRequest != null) |
| 76 | { |
| 77 | barsRequest.Update -= OnBarUpdate; |
| 78 | barsRequest.Dispose(); |
| 79 | barsRequest = null; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // Other required NTTabPage members left out for demonstration purposes. Be sure to add them in your own code. |
| 84 | } |