Exemple #1
0
        public override void Initialize()
        {
            base.Initialize();
            Administrator.CreateEPL("create window PriceWindow.std:groupwin(InstrumentKey).std:lastevent() as PriceEvent");
            Administrator.CreateEPL("insert into PriceWindow select * from PriceEvent");
            var statement = Administrator.CreateEPL("on DailyMarketCloseEvent as MarketCloseEvent select MarketCloseEvent.EventTime as EventTime, Price.InstrumentKey as InstrumentKey, Price.Price as Price from PriceWindow as Price");

            statement.Events += (sender, e) =>
            {
                var prices = (from @event in e.NewEvents
                              select new KeyValuePair <string, double>((string)@event["InstrumentKey"], (double)@event["Price"])).ToDictionary(p => p.Key, p => p.Value);
                var closePricesEvent = new ClosePricesEvent {
                    Prices = prices
                };
                var feedbackEvent = new FeedbackEvent("PriceChanged", closePricesEvent);
                OnFeedback(new EventEventArgs(feedbackEvent));
            };

            statement         = Administrator.CreateEPL("select * from StockSplitEvent");
            statement.Events += (sender, e) =>
            {
                var stockSplitEvent = (StockSplitEvent)e.NewEvents[0].Underlying;
                var feedbackEvent   = new FeedbackEvent("StockSplit", stockSplitEvent);
                OnFeedback(new EventEventArgs(feedbackEvent));
            };

            statement         = Administrator.CreateEPL("select * from TradeEvent");
            statement.Events += (sender, e) =>
            {
                var tradeEvent    = (TradeEvent)e.NewEvents[0].Underlying;
                var feedbackEvent = new FeedbackEvent("Trade", tradeEvent);
                OnFeedback(new EventEventArgs(feedbackEvent));
            };
        }
Exemple #2
0
        private void NewPricesArrived(ClosePricesEvent closePricesEvent)
        {
            var prices    = closePricesEvent.Prices;
            var positions = Context.PortfolioManager.GetPosition();

            var marketValues = (from position in positions
                                let price = prices[position.Key]
                                            select new { InstrumentKey = position.Key, Value = position.Value * price, Price = price }).ToArray();

            var total = marketValues.Sum(v => v.Value);

#if DEBUG
            Console.WriteLine("Strategy");
            foreach (var marketValue in marketValues)
            {
                Console.WriteLine("[{0}] Ratio: {1:P2}", marketValue.InstrumentKey, marketValue.Value / total);
            }
#endif

            var exceptions = (from threshold in _thresholds
                              join marketValue in marketValues on threshold.InstrumentKey equals marketValue.InstrumentKey
                              let ratio = marketValue.Value / total
                                          where ratio <threshold.LowerLimit || ratio> threshold.UpperLimit
                                          select new { Threshold = threshold, MarketValue = marketValue, Ratio = ratio, Direction = ratio < threshold.LowerLimit ? "Lower" : "Upper" }).ToArray();

#if DEBUG
            Console.ForegroundColor = ConsoleColor.Red;
            foreach (var exception in exceptions)
            {
                Console.WriteLine("[{0}] Ratio: {3:P2} Threshold: {1:P2}/{2:P2} ",
                                  exception.MarketValue.InstrumentKey, exception.Threshold.LowerLimit, exception.Threshold.UpperLimit, exception.Ratio);
            }
            Console.ResetColor();
#endif

            var orders = (from exception in exceptions
                          let instrumentKey = exception.MarketValue.InstrumentKey
                                              let threshold = exception.Threshold
                                                              let delta = exception.Direction == "Lower" ? threshold.LowerLimit - exception.Ratio : threshold.UpperLimit - exception.Ratio
                                                                          select new OrderEvent
            {
                EventTime = EventEngine.CurrenTime,
                InstrumentKey = instrumentKey,
                Quantity = delta * positions[instrumentKey],
                Price = prices[instrumentKey]
            }).ToArray();

#if DEBUG
            Console.ForegroundColor = ConsoleColor.Red;
            foreach (var order in orders)
            {
                Console.WriteLine(@order);
            }
            Console.ResetColor();
            Console.WriteLine();
#endif
            if (orders.Any())
            {
                Context.TradingEngine.ExcuteOrders(orders);
            }
        }