first, i am not an native Speeker in English, my language is German, so i hope i could describ my Problem
I wrote some logik to get a good situation to send a signal to my Server, the server do some magic and build a Order-Object.
After them i will Paint some Informations from this Order-Object in the chart.
After returning fom my server, CurrentBar is not the bar before calling the server, everey access to an Bar before throws an excption
And i had realy many Problems, and i wrote an Indicator for this Post which produce alle errors.
Of course, you can not access to my Server, but i testet this code, you get all errors which i have.
I hope you can help my.
#region Using declarations using System; using System.Net.Http; 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 NinjaTraderSupport : Indicator { private readonly int StartBarsAgo = 18; private int lastPaintedBar = 0; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Indicator für den NinjaTraderSupport"; Name = "NinjaTraderSupport"; Calculate = Calculate.OnBarClose; IsOverlay = true; IsSuspendedWhileInactive = true; } else if (State == State.Configure) { } } protected async override void OnBarUpdate() { if (CurrentBar < StartBarsAgo) return; if (CurrentBar - lastPaintedBar < StartBarsAgo) return; string orderException = string.Empty; try { if (CurrentBar % 100 == 0) { double price = Open[0]; lastPaintedBar = CurrentBar; if (lastPaintedBar <= 10) return; Draw.Text(this, lastPaintedBar + "tag1","Before Call", 10, price, Brushes.White); NinjaTrader.DomainModel.Test.Signal signal = new NinjaTrader.DomainModel.Test.Signal(price, lastPaintedBar); Print(CurrentBar.ToString() + " " + signal.Price); Task<NinjaTrader.DomainModel.Test.Order> orders = signal.Commit(); NinjaTrader.DomainModel.Test.Order orderBuy = await orders; orderException = orderBuy.ExceptionMessage; // change barsAgo to 1 throws an exception Draw.Text(this, lastPaintedBar + "tag2","After Call", 0, price - 500, Brushes.Green); // after returning from HTTPClient call to server, CurrentBar has the last Bar, not the bar which this Method is called. // change Bars[0] to Bars[1] throws an exception Print(CurrentBar.ToString() + " " + orderBuy.Bar + ": " + orderBuy.StopLoss + " - " + Bars[0]); } } catch(Exception ex) { Print("MainTryCatch:" + CurrentBar.ToString() + " " + ex.Message + " " + orderException); } } } } namespace NinjaTrader.DomainModel.Test { public class Signal { public double Price { get; set; } public int Bar { get; set;} public Signal(double price, int bar) { Price = price; Bar = bar; } public async Task<NinjaTrader.DomainModel.Test.Order> Commit() { try { string json = Newtonsoft.Json.JsonConvert.SerializeObject(this); using (HttpClient client = new HttpClient()) { HttpContent content = new StringContent(json); HttpResponseMessage message = await client.PostAsync("http://192.168.80.128:5001", content); if (message.StatusCode != System.Net.HttpStatusCode.OK) { Console.WriteLine("Falschen Status-Code erhalten: {0}.", message.StatusCode); string readAs = await message.Content.ReadAsStringAsync(); Console.WriteLine(string.Format("Gemeldeter Fehler: {0}.", readAs)); NinjaTrader.DomainModel.Test.Order order = new NinjaTrader.DomainModel.Test.Order(); order.Bar = this.Bar; order.ExceptionMessage = string.Format("Gemeldeter Fehler: {0}.", readAs); return order; } NinjaTrader.DomainModel.Test.Order orders = Newtonsoft.Json.JsonConvert.DeserializeObject<Ninj aTrader.DomainModel.Test.Order>( message.Content.ToString()); orders.ExceptionMessage = "Original"; return orders; } }catch(Exception ex) { NinjaTrader.DomainModel.Test.Order order = new NinjaTrader.DomainModel.Test.Order(); order.Bar = this.Bar; order.ExceptionMessage = ex.Message; return order; } } } public class Order { public bool Placed { get; set; } public bool Buy { get; set; } public int Bar { get; set;} public double Price { get; set; } public double StopLoss { get; set; } public double TakeProfit { get; set; } public string ExceptionMessage { get; set;} } } #region NinjaScript generated code. Neither change nor remove. namespace NinjaTrader.NinjaScript.Indicators { public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase { private NinjaTraderSupport[] cacheNinjaTraderSupport; public NinjaTraderSupport NinjaTraderSupport() { return NinjaTraderSupport(Input); } public NinjaTraderSupport NinjaTraderSupport(ISeries<double> input) { if (cacheNinjaTraderSupport != null) for (int idx = 0; idx < cacheNinjaTraderSupport.Length; idx++) if (cacheNinjaTraderSupport[idx] != null && cacheNinjaTraderSupport[idx].EqualsInput(input)) return cacheNinjaTraderSupport[idx]; return CacheIndicator<NinjaTraderSupport>(new NinjaTraderSupport(), input, ref cacheNinjaTraderSupport); } } } namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns { public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase { public Indicators.NinjaTraderSupport NinjaTraderSupport() { return indicator.NinjaTraderSupport(Input); } public Indicators.NinjaTraderSupport NinjaTraderSupport(ISeries<double> input ) { return indicator.NinjaTraderSupport(input); } } } namespace NinjaTrader.NinjaScript.Strategies { public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase { public Indicators.NinjaTraderSupport NinjaTraderSupport() { return indicator.NinjaTraderSupport(Input); } public Indicators.NinjaTraderSupport NinjaTraderSupport(ISeries<double> input ) { return indicator.NinjaTraderSupport(input); } } } #endregion
Comment