Esempio n. 1
0
        //
        // *****************************************
        // ****         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);
                        }
                    }
                }
            }
            //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()
Esempio n. 2
0
 //
 //
 /// <summary>
 ///
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="eventArgs"></param>
 protected void AlarmEventHandler(object sender, EventArgs eventArgs)
 {
     AlarmEventArg e = (AlarmEventArg)eventArgs;
 }//AlarmEventHandler()