// #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()
}//RequestRepricing() // // // // ************************************************************* // **** GetInstrumentSubscriptions() **** // ************************************************************* /// <summary> /// Called after initialization. Gets all the instruments get mkt data for. /// </summary> /// <returns></returns> public List <InstrumentName> GetInstrumentSubscriptions() { List <InstrumentName> instrNames = new List <InstrumentName>(); if (m_PricingEngine != null && m_PricingEngine is Engines.PricingEngine) { Engines.PricingEngine eng = (Engines.PricingEngine)m_PricingEngine; foreach (PriceLeg leg in eng.m_Legs) // TODO: this should be part of IPricingEngine { instrNames.Add(leg.InstrumentName); } } return(instrNames); }//GetInstrumentSubscriptions()
// // ***************************************** // **** 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()