private void stop_StatusChanged(StopEventArgs args) { ATSStop atsStop = args.Stop as ATSStop; if (((StopBase)atsStop).Status != null) { this.activeStops[((StopBase)atsStop).Instrument].Remove(atsStop); // ISSUE: method pointer atsStop.StatusChanged -= new StopEventHandler(this.stop_StatusChanged); } if (((StopBase)atsStop).Status != FreeQuant.Trading.StopStatus.Executed) { return; } try { Strategy strategy = (Strategy)null; if (!this.strategies.TryGetValue(((StopBase)atsStop).Instrument, out strategy)) { return; } strategy.OnStopExecuted(Map.FQ_OQ_Stop[(object)atsStop] as OpenQuant.API.Stop); } catch (Exception ex) { this.EmitError(ex); } }
public double GetMatchPrice(Strategy strategy, OrderSide side) { Quote quote = strategy.Quote; Trade trade = strategy.Trade; Bar bar = strategy.Bar; if (quote != null) { if (side == OrderSide.Buy) { if (quote.Ask != 0) return quote.Ask; } else { if (quote.Bid != 0) return quote.Bid; } } if (trade != null) if (trade.Price != 0) return trade.Price; if (bar != null) { if (bar.Close != 0) return bar.Close; if (bar.Open != 0) return bar.Open; } return 0; }
public void SetOrderDone(SingleOrder order) { try { Strategy strategy = (Strategy)null; if (!this.strategies.TryGetValue(order.Instrument, out strategy)) { return; } switch (order.OrdStatus) { case OrdStatus.Filled: strategy.OnOrderFilled(Map.FQ_OQ_Order[(object)order] as Order); break; case OrdStatus.Cancelled: strategy.OnOrderCancelled(Map.FQ_OQ_Order[(object)order] as Order); break; case OrdStatus.Rejected: strategy.OnOrderRejected(Map.FQ_OQ_Order[(object)order] as Order); break; case OrdStatus.Expired: strategy.OnOrderExpired(Map.FQ_OQ_Order[(object)order] as Order); break; } strategy.OnOrderDone(Map.FQ_OQ_Order[(object)order] as Order); } catch (Exception ex) { this.EmitError(ex); } }
private void portfolio_PositionClosed(object sender, PositionEventArgs args) { try { FreeQuant.Instruments.Instrument instrument = args.Position.Instrument; if (this.activeStops.ContainsKey(instrument)) { foreach (ATSStop atsStop in new ArrayList((ICollection)this.activeStops[instrument])) { if (((StopBase)atsStop).Type == FreeQuant.Trading.StopType.Time && ((StopBase)atsStop).Status == null || atsStop.Connected) { atsStop.OnPositionClosed(args.Position); } } } Strategy strategy = (Strategy)null; if (!this.strategies.TryGetValue(instrument, out strategy)) { return; } strategy.OnPositionClosed(); } catch (Exception ex) { this.EmitError(ex); } }
public void SetNewBarOpen(FreeQuant.Instruments.Instrument instrument, FreeQuant.Data.Bar bar) { try { if (this.stops.Count != 0) { List <ATSStop> list = (List <ATSStop>)null; if (this.activeStops.TryGetValue(instrument, out list)) { foreach (ATSStop atsStop in new ArrayList((ICollection)list)) { if (atsStop.Connected) { atsStop.OnNewBarOpen(bar); } } } } Strategy strategy = (Strategy)null; if (!this.strategies.TryGetValue(instrument, out strategy)) { return; } strategy.OnBarOpen((OpenQuant.API.Bar) this.objectConverter.Convert(bar)); } catch (Exception ex) { this.EmitError(ex); } }
public void SetOrderCancelReject(SingleOrder order) { try { Strategy strategy = (Strategy)null; if (!this.strategies.TryGetValue(order.Instrument, out strategy)) { return; } strategy.OnOrderCancelReject(Map.FQ_OQ_Order[(object)order] as Order); } catch (Exception ex) { this.EmitError(ex); } }
private void portfolio_ValueChanged(object sender, PositionEventArgs args) { try { Strategy strategy = (Strategy)null; if (!this.strategies.TryGetValue(args.Position.Instrument, out strategy)) { return; } strategy.OnPositionValueChanged(); } catch (Exception ex) { this.EmitError(ex); } }
public void SetNewMarketDepth(FreeQuant.Instruments.Instrument instrument, MarketDepth depth) { try { Strategy strategy = (Strategy)null; if (!this.strategies.TryGetValue(instrument, out strategy)) { return; } strategy.OnOrderBookChanged((OrderBookUpdate)this.objectConverter.Convert(depth)); } catch (Exception ex) { this.EmitError(ex); } }
// 在对手价上加一定跳数 public double GetMatchPrice(Strategy strategy, OrderSide side, double jump) { double price = GetMatchPrice(strategy, side); if (side == OrderSide.Buy) { price += jump * TickSize; } else { price -= jump * TickSize; } // 修正一下价格 price = FixPrice(price, side); return price; }
public void Online(bool resetState) { this.active = true; if (resetState) { this.strategies.Clear(); this.stops.Clear(); this.activeStops.Clear(); Map.Instruments[this.strategyName] = new List <string>(); } if (!this.enabled) { return; } if (resetState) { string code = (string)this.sampleStrategy.GetType().GetField("Currency", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.SetField).GetValue((object)this.sampleStrategy); if (CurrencyManager.Currencies.Contains(code)) { this.Portfolio.Account.Currency = CurrencyManager.Currencies[code]; } List <string> list = new List <string>(); MethodInfo method = typeof(Strategy).GetMethod("Init", BindingFlags.Instance | BindingFlags.NonPublic); FieldInfo field1 = typeof(Strategy).GetField("startDate", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField); FieldInfo field2 = typeof(Strategy).GetField("stopDate", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField); FieldInfo field3 = typeof(Strategy).GetField("cash", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField); foreach (FreeQuant.Instruments.Instrument key in this.instruments) { Strategy strategy = Activator.CreateInstance(this.sampleStrategy.GetType()) as Strategy; this.SetProxyProperties((object)strategy, (object)this.sampleStrategy); method.Invoke((object)strategy, new object[6] { (object)this.sq_Portfolio, (object)this.sq_MetaPortfolio, (object)key, (object)this.dataRequests, (object)this.strategyName, (object)this.strategyLogManager }); field1.SetValue((object)strategy, (object)this.startDate); field2.SetValue((object)strategy, (object)this.stopDate); field3.SetValue((object)strategy, (object)this.cash); this.strategies.Add(key, strategy); list.Add(key.Symbol); } Map.Instruments[this.strategyName] = list; foreach (Strategy strategy in this.strategies.Values) { strategy.OnStrategyStart(); } } else { OpenQuant.API.Portfolio portfolio = Map.FQ_OQ_Portfolio[(object)this.sq_Portfolio] as OpenQuant.API.Portfolio; foreach (object obj in this.strategies.Values) { typeof(Strategy).GetField("portfolio", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.SetField).SetValue(obj, (object)portfolio); } } this.Connect(); }