// // public override void SetupBegin(IEngineHub myEngineHub, IEngineContainer engineContainer) { base.SetupBegin(myEngineHub, engineContainer); foreach (IEngine ieng in engineContainer.GetEngines()) { // Find order Engine if (ieng is IOrderEngine) { this.m_IOrderEngine = (IOrderEngine)ieng; // find our order engine } } if (m_IOrderEngine is SingleLegExecutor) { // if this is a single leg order engine m_SingleLegExecutor = (SingleLegExecutor)m_IOrderEngine; this.m_Leg = m_SingleLegExecutor.m_PriceLeg; m_MaxPossibleWorkingQuoteQtyPerLot += (int)(Math.Abs(m_Leg.Weight) * 2); // leg ratios * 2 (each side) } // If we subscribe to order book creation, we can be sure to get all the info for all fills and submissions // this only works if create order books after setup beging. m_IOrderEngine.GetExecutionListener().OrderBookCreated += new EventHandler(ExecutionListener_OrderBookCreated); if (m_Hub is ExecutionHub) { // set up ITimer to work ((ExecutionHub)m_Hub).SubscribeToTimer((ITimerSubscriber)m_ExecutionContainer.m_ExecutionListener); m_ExecutionContainer.m_ExecutionListener.SubscribeToTimer(this); // subscrie to updates } }
}//Start() // // // // ***************************************************** // **** TryAddEngine() **** // ***************************************************** /// <summary> /// A nice way for hub to add engines to this container. /// </summary> /// <param name="oEngine"></param> /// <returns></returns> public bool TryAddEngine(Engine oEngine) { if (m_IsLaunched) { return(false); } if (oEngine is IOrderEngine) { if (this.IOrderEngine != null) { return(false); } else { this.IOrderEngine = (IOrderEngine)oEngine; } } if (oEngine is Engine) { Engine engine = (Engine)oEngine; this.EngineList.Add(engine.EngineID, engine); this.m_IEngineList.Add(engine); } return(true); }// TryAddEngine()
// #endregion//Properties #region Public Initialization Methods // ***************************************************************** // **** Public Initialization **** // ***************************************************************** // // // **************************************** // **** Setup Initialize() **** // **************************************** /// <summary> /// The Strategy has been created, and now we add its engines. /// When we call Engine.SetupInitialize() the engine can make NO assumptions /// about the Strategy, except that it and its StrategyHub exists. /// Other Engines may or may not exist. /// What is allowed is that Engines can spawn other Engines and add them /// to the *end* of the Strategy.m_Engines[] list freely. /// </summary> public void SetupInitialize(IEngineHub myHub) { StrategyHub = (StrategyHub)myHub; // store ptr to new parent hub. // First initialize each of my engines. int id = 0; while (id < m_Engines.Count) // Loop using while so that Engines can ADD new engines to end of list. { // Adding new engines spontaneously is allowed here only. Engine engine = m_Engines[id]; engine.SetupInitialize(StrategyHub, this, id); // Tell Engine his new Hub, and owner, and his new id#. // Keep track of important engine ptrs we need. if (engine is Engines.PricingEngine) // using simple "if"s allows one engine to play multiple roles. { m_PricingEngine = (Engines.PricingEngine)engine; } if (engine is IOrderEngine) { m_OrderEngine = (IOrderEngine)engine; } if (engine is ZGraphEngine) { m_GraphEngine = (ZGraphEngine)engine; } id++; } //next engine id m_IsInitializeComplete = true; // after this point if engines are added, they have to manually Initialize } //SetupInitialize()
public OrderLogic( IOrderEngine orderEngine, IExceptionHandlerLogic exceptionHandlerLogic, IVehicleLogic vehicleLogic ) { _orderEngine = orderEngine; _exceptionHandlerLogic = exceptionHandlerLogic; _vehicleLogic = vehicleLogic; }
void IStringifiable.AddSubElement(IStringifiable subElement) { if (subElement is IOrderEngine) { // This is my primary order engine. // Perhaps there should be only one of these. Here is where I send // order requests. IOrderEngine = (IOrderEngine)subElement; } if (subElement is Engine) { // Here we store all engines (including order engine). Engine engine = (Engine)subElement; EngineList.Add(engine.EngineID, engine); } }
// // ***************************************** // **** Setup Begin() **** // ***************************************** /// <summary> /// This is called after all Engines have been created, and added to the master /// list inside the Strategy (IEngineContainer). /// As a convenience, the PricingEngine class locates useful Engines and keeps /// pointers to them. /// </summary> /// <param name="myEngineHub"></param> /// <param name="engineContainer"></param> public override void SetupBegin(IEngineHub myEngineHub, IEngineContainer engineContainer) { base.SetupBegin(myEngineHub, engineContainer); // // Subscribe to economic numbers of legs. // List <InstrumentName> instruments = new List <InstrumentName>(); foreach (IEngine iEngine in engineContainer.GetEngines()) { if (iEngine is PricingEngine) { PricingEngine pricingEngine = (PricingEngine)iEngine; foreach (Lib.MarketHubs.PriceLeg leg in pricingEngine.m_Legs) { if (!instruments.Contains(leg.InstrumentName)) { instruments.Add(leg.InstrumentName); } } } else if (iEngine is IOrderEngine) { // TODO: Extract legs from IOrderEngine. IOrderEngine iOrderEngine = (IOrderEngine)iEngine; } } //QueryBase query = null; //this.ParentStrategy.StrategyHub.RequestQuery(query, this.QueryResponseHandler, this.ParentStrategy); // // Subscribe to start/end trading times // // If StartTime=EndTime => strategy never shuts off. if (ParentStrategy.QueryItem != null && ParentStrategy.StrategyHub.m_Alarm != null && ParentStrategy.QueryItem.StartTime.CompareTo(ParentStrategy.QueryItem.EndTime) != 0) { Random random = new Random(); StrategyQueryItem strategyItem = ParentStrategy.QueryItem; Alarm alarm = ParentStrategy.StrategyHub.m_Alarm; // Collect today's time/date DateTime today = ParentStrategy.StrategyHub.GetLocalTime().Date; // collect nearest day DateTime day = today; if (day.CompareTo(strategyItem.StartDate) < 0) { day = strategyItem.StartDate; } AlarmEventArg eventArg; // my callback event arg. while (day.CompareTo(strategyItem.EndDate) <= 0) // Loop thru each day and set start/end alarms. { // Create start event. DateTime dt = day.Add(strategyItem.StartTime); eventArg = new AlarmEventArg(); eventArg.State = StrategyState.Normal; double secs = m_StartTimeSeconds + m_StartTimeSeconds * (0.01 * random.Next(0, m_RandomSlopPercent[0])); eventArg.TimeStemp = dt.AddSeconds(secs); // start at start time or after. alarm.Set(dt, this.AlarmEventHandler, eventArg); // Create stop event. dt = day.Add(strategyItem.EndTime); // exit time (or earlier) double lastSecs = double.MaxValue; for (int i = 0; i < m_ExitTimeSeconds.Length; ++i) { if (m_ExitTimeSeconds[i] != double.NaN) { StrategyState state = (StrategyState)(i + (int)StrategyState.ExitOnly); eventArg = new AlarmEventArg(); eventArg.State = state; secs = m_ExitTimeSeconds[i] + m_ExitTimeSeconds[i] * (0.01 * random.Next(0, m_RandomSlopPercent[i])); secs = Math.Max(0, Math.Min(secs, lastSecs)); // don't allow this exit level to be earlier than previous one. lastSecs = secs - 1.0; // therefore, states padding is strictly decreasing in seconds! DateTime dt2 = dt.AddSeconds(-secs); alarm.Set(dt, this.AlarmEventHandler, eventArg); } } // Increment day day = day.AddDays(1.0).Date; // increment until past endDate or ... if (day.DayOfWeek == DayOfWeek.Saturday && day != today) // ... its Saturday (but continue if today is Sat.). { break; } } //wend day } } //SetupBegin()