public static void StrategyUpdateTickets(StrategyBase strategy, SpreadTraderWindow stw, Queue <SingleSpread> spreads, double displayedprice, string strratio, LegEntryType leg1entrytype, LegEntryType leg2entrytype, object ieh) { List <SpreadOrderTicket> ticketstoremove = new List <SpreadOrderTicket>(); lock (stw.tickets) { if (stw.tickets.Count > 0) { foreach (SpreadOrderTicket ticket in stw.tickets) { if (ticket.EntryConditionSatisfied(displayedprice)) { Guid newguid = Guid.NewGuid(); ticket.Guid = newguid; ticketstoremove.Add(ticket); ClickhandlerArgs A = new ClickhandlerArgs(ticket.ClickedButton, ticket.Units, newguid); Zweistein.SpreadTraderWindow.StrategyHandleClickedButton(strategy, A, spreads, strratio, leg1entrytype, leg2entrytype, ieh); // clickcustomhandler(A); } } } } if (ticketstoremove.Count > 0) { foreach (SpreadOrderTicket t in ticketstoremove) { stw.Dispatcher.Invoke(new Action(() => stw.DeleteTicket(t))); } } }
public static void StrategyTransition(SpreadTraderWindow stw, Queue <SingleSpread> spreads, List <SpreadOrderTicket> Tickets) { stw.Dispatcher.Invoke(new Action(() => stw.FillGrid(Tickets))); foreach (SingleSpread s in spreads) { s.RebuildPositions(stw._strategy); } }
static SpreadTraderWindow CreateFromBaml(Assembly localAssembly) { SpreadTraderWindow obj = null; try { string assname = localAssembly.FullName; MemoryStream ms = new MemoryStream(); using (MemoryStream fstream = new MemoryStream(baml, false)) //using (FileStream fstream = new FileStream(@"Properties\\SpreadTraderWindow.baml", FileMode.Open)) { long len = fstream.Length; byte[] buffer = new byte[1024]; len -= fstream.Read(buffer, 0, 40); ms.Write(buffer, 0, 39); int i = buffer[39]; len -= fstream.Read(buffer, 0, i); byte[] bytes = Encoding.Default.GetBytes(assname); byte b = (byte)bytes.Length; ms.WriteByte(b); ms.Write(bytes, 0, bytes.Length); while (len > 0) { long chunk = (len > 1024L) ? 1024L : len; len -= fstream.Read(buffer, 0, (int)chunk); ms.Write(buffer, 0, (int)chunk); } } ms.Seek(0, SeekOrigin.Begin); XamlReaderSettings settings = new XamlReaderSettings(); settings.LocalAssembly = localAssembly; Baml2006Reader reader = new Baml2006Reader(ms, settings); obj = System.Windows.Markup.XamlReader.Load(reader) as SpreadTraderWindow; LinearGradientBrush btnbackgroundBrush = Application.Current.TryFindResource("SuperDom.ButtonBackground") as LinearGradientBrush; obj.btnReverse.Background = btnbackgroundBrush; obj.btnClose.Background = btnbackgroundBrush; obj.btnGoLong.Background = btnbackgroundBrush; obj.btnGoShort.Background = btnbackgroundBrush; QtyLongBackgroundBrush = Application.Current.TryFindResource("BasicEntry.PositionQtyLongBackground") as SolidColorBrush; QtyShortBackgroundBrush = Application.Current.TryFindResource("BasicEntry.PositionQtyShortBackground") as SolidColorBrush; SolidColorBrush PnLBackgroundBrush = Application.Current.TryFindResource("BasicEntry.PnLBackground") as SolidColorBrush; obj.unrealizedPNL.Background = PnLBackgroundBrush; obj.btnAddTicket.IsEnabled = false; } catch (Exception ex) { Zweistein.Diagnostics.Trace(ex.Message); } return(obj); }
public static SpreadTraderWindow StrategyDataLoaded(StrategyBase _strategybase, double Left, double Top, Queue <SingleSpread> spreads, string DisplayName) { SpreadTraderWindow stw = null; System.Reflection.Assembly ass = System.Reflection.Assembly.GetAssembly(typeof(SpreadTraderWindow)); // System.Reflection.Assembly ass = _strategybase.GetType().Assembly; //stw = SpreadTraderWindow.CreateFromBaml(ass); stw = (SpreadTraderWindow)NinjaTrader.Core.Globals.RandomDispatcher.Invoke((stwDelegate) delegate(){ return(stw = SpreadTraderWindow.CreateFromBaml(ass)); }); //stw.setCurrency(Zweistein.MoneyString.FromString(System.Globalization.RegionInfo.CurrentRegion.ISOCurrencySymbol)); if (stw != null) { stw.Dispatcher.Invoke(new Action(() => stw.setCurrency(_strategybase.Account.Denomination))); } if (Left != -1) { if (stw != null) { stw.Dispatcher.Invoke(new Action(() => stw.Left = Left)); } } if (Top != -1) { if (stw != null) { stw.Dispatcher.Invoke(new Action(() => stw.Top = Top)); } } if (stw != null) { stw.Dispatcher.Invoke(new Action(() => stw.fromStrategy(_strategybase))); } foreach (SingleSpread s in spreads) { s.setStrategyBase(_strategybase); } if (stw != null) { stw.Dispatcher.Invoke(new Action(() => stw.Show())); } if (stw != null) { stw.Dispatcher.Invoke(new Action(() => stw.Title = DisplayName)); } return(stw); }
public static void StrategyTerminated(SpreadTraderWindow stw, Queue <SingleSpread> spreads, List <SpreadOrderTicket> Tickets) { try { foreach (SingleSpread spread in spreads) { spread.setIExitHandling(spread.getIExitHandling()); // update iexithandling params prior to serialize } if (spreads.Count > 0) { var stacktrace = new StackTrace(false); int i = 0; foreach (StackFrame f in stacktrace.GetFrames()) { bool found = false; if (f.GetMethod().ToString().Contains("FlattenEverything()")) { found = true; } if (found) { spreads.Clear(); break; } i++; if (i > 5) { break; } } } } catch (Exception ex) { Zweistein.Diagnostics.Trace(ex.Message); } if (stw != null) { stw.Dispatcher.Invoke(new Action(() => { if (stw.tickets != null) { lock (stw.tickets){ Tickets.Clear(); foreach (SpreadOrderTicket sot in stw.tickets) { Tickets.Add(sot); } } } })); //stw.Destroy(); stw.Dispatcher.Invoke(new Action(() => stw.Destroy())); stw = null; } }
public static double StrategySetPositionAndEntryLabels(SpreadTraderWindow stw, Queue <SingleSpread> spreads) { double netunits = 0; double avgentryprice = 0; lock (spreads) { foreach (SingleSpread s in spreads) { int n = s.NUnits; int sign = 0; if (s.MarketPosition == MarketPosition.Long) { sign = 1; } if (s.MarketPosition == MarketPosition.Short) { sign = -1; } if (sign * n != 0 && (sign * n + netunits) != 0) { double old = avgentryprice * netunits; double newp = s.EntryPricePerUnit(stw.ptr2members.Leg1PriceDisplayMultiplier, stw.ptr2members.Leg2PriceDisplayMultiplier) * n; avgentryprice = (old + newp) / (sign * n + netunits); } netunits += sign * n; } } string txt = ""; if (netunits == 0) { txt = "FLAT"; } //this.form1.ENTRY.Text="ENTRY"; if (netunits > 0) { txt = "LONG " + netunits.ToString(); } if (netunits < 0) { txt = "SHORT " + (-netunits).ToString(); } //is.form1.Position.TextBackColor(txt,netunits,ChartControl); // if(TraceOrders) Print("SetPositionAndEntryLabels: "+txt+" ,"+avgentryprice.ToString()); if (stw != null) { stw.Dispatcher.Invoke(new Action(() => stw.SetPositionTextAndValue(txt, avgentryprice))); } return(netunits); }
public static void UpdateForm(SpreadTraderWindow stw, Queue <SingleSpread> spreads, double realizedpnl, string pricestring, double displaymarketprice) { double unrealizedpnl = 0; try { lock (spreads) { foreach (SingleSpread s in spreads) { int n = s.NUnits; unrealizedpnl += s.PnlPerUnit() * n; } } } catch (Exception ex) { } //Print("unrealized="+unrealizedpnl.ToString()); if (stw != null) { stw.Dispatcher.Invoke(new Action(() => stw.SetUnrealizedPNL(unrealizedpnl))); } List <double> pnls = new List <double>(); List <string> pnlxunit = new List <string>(); lock (spreads) { foreach (SingleSpread s in spreads) { string status = s.OnBarUpdateStatus(); double d = s.PnlPerUnit(); if (!string.IsNullOrEmpty(status)) { continue; } pnls.Add(d); status = " x Units(" + s.NUnits.ToString() + ") " + s.MarketPosition.ToString(); pnlxunit.Add(status); } } if (stw != null) { stw.Dispatcher.Invoke(new Action(() => stw.SetDetailsPnl(realizedpnl, pricestring, displaymarketprice, pnls, pnlxunit))); } }
private void button_Click(object sender, RoutedEventArgs e) { System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); //if (stw==null) stw = SpreadTraderWindow.CreateFromBaml(assembly); // Zweistein.Diagnostics.doSnoopWindow(this); if (stw == null) { stw = new SpreadTraderWindow(); stw.fromStrategy(strategy); stw.InitializeComponent(); var l = strategy.GetType().GetProperties().ToList(); stw.FillGrid(l); } if (stw != null) { stw.Show(); } }
private void button_Click(object sender, RoutedEventArgs e) { System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); //if (stw==null) stw = SpreadTraderWindow.CreateFromBaml(assembly); //Zweistein.Diagnostics.doSnoopWindow(this); if (stw == null) { stw = new SpreadTraderWindow(); NinjaTrader.NinjaScript.Strategies.SpreadExitStrategy strategy = new NinjaTrader.NinjaScript.Strategies.SpreadExitStrategy(); stw.fromStrategy(strategy); stw.InitializeComponent(); stw.FillGrid(); } if (stw != null) { stw.Show(); } }