Beispiel #1
0
        public JsonResult EmulateData()
        {
            Common.Entities.Settings settings = null;

            Models.TradeModel tradeModel = new Models.TradeModel();

            IDictionary <Func <Common.Entities.Order, bool>, Action <UnitOfWork, Common.Entities.Order> >         actions      = new Dictionary <Func <Common.Entities.Order, bool>, Action <UnitOfWork, Common.Entities.Order> >();
            IDictionary <Func <Common.Entities.StopOrder, bool>, Action <UnitOfWork, Common.Entities.StopOrder> > stop_actions = new Dictionary <Func <Common.Entities.StopOrder, bool>, Action <UnitOfWork, Common.Entities.StopOrder> >();

            actions.Add((o) => { return(o.OrderOperation == OrderOperationEnum.Sell); },
                        (unit, order) =>
            {
                Common.Entities.Position pos = tradeModel.ApplyOrder(unit, order, settings);
                pos.CurrentBalance          -= order.Count;
            });

            actions.Add((o) => { return(o.OrderOperation == OrderOperationEnum.Buy); },
                        (unit, order) =>
            {
                Common.Entities.Position pos = tradeModel.ApplyOrder(unit, order, settings);
                pos.CurrentBalance          += order.Count;
            });

            stop_actions.Add((o) => { return(o.OrderOperation == OrderOperationEnum.Sell); }, (unit, order) =>
            {
                Models.StopOrderExecute execute = new Models.StopOrderExecute(new Models.SellStrategy());
                execute.ApplyStop(unit, order, settings);
            });

            stop_actions.Add((o) => { return(o.OrderOperation == OrderOperationEnum.Buy); }, (unit, order) =>
            {
                Models.StopOrderExecute execute = new Models.StopOrderExecute(new Models.BuyStrategy());
                execute.ApplyStop(unit, order, settings);
            });

            using (UnitOfWork unit = new UnitOfWork((DbContext) new DataContext()))
            {
                settings = unit.SettingsRepository.All <Common.Entities.Settings>(null).Single();

                var stop_orders = unit.StopOrderRepository.Query <Common.Entities.StopOrder>(o => o.OrderState == OrderStateEnum.IsActive).ToList();
                foreach (Common.Entities.StopOrder order in stop_orders)
                {
                    stop_actions.Single(a => a.Key(order)).Value.Invoke(unit, order);
                }
                unit.Commit();

                var orders_all   = unit.OrderRepository.All <Common.Entities.Order>().ToList();
                var activeOrders = orders_all.Where(o => o.OrderState == OrderStateEnum.IsActive).ToList();
                foreach (Common.Entities.Order order in activeOrders)
                {
                    actions.Single(a => a.Key(order)).Value.Invoke(unit, order);
                }

                unit.Commit();
            }

            return(Json("ok", JsonRequestBehavior.AllowGet));
        }
Beispiel #2
0
        public JsonResult CreateStopOrders()
        {
            Common.Entities.Settings settings = null;

            IDictionary <Func <Common.Entities.Order, bool>, Action <UnitOfWork, Common.Entities.Order, Common.Entities.Transaction> > actions = new Dictionary <Func <Common.Entities.Order, bool>, Action <UnitOfWork, Common.Entities.Order, Common.Entities.Transaction> >();

            actions.Add((o) => { return(o.OrderOperation == OrderOperationEnum.Buy); },
                        (unit, order, trn) =>
            {
                Models.StopOrderCreator creator = new Models.StopOrderCreator(new Models.CreateSellStrategy());
                creator.Create(unit, order, settings, trn);
            });

            actions.Add((o) => { return(o.OrderOperation == OrderOperationEnum.Sell); },
                        (unit, order, trn) =>
            {
                Models.StopOrderCreator creator = new Models.StopOrderCreator(new Models.CreateBuyStrategy());
                creator.Create(unit, order, settings, trn);
            });

            using (UnitOfWork unit = new UnitOfWork((DbContext) new DataContext()))
            {
                settings = unit.SettingsRepository.All <Common.Entities.Settings>(null).Single();

                var transactions = unit.TransactionRepository.Query <Common.Entities.Transaction>(t => !t.Processed && t.Status == 3 && t.Name == "Ввод заявки", null).ToList();

                foreach (Common.Entities.Transaction transaction in transactions)
                {
                    var order = unit.OrderRepository.Query <Common.Entities.Order>(o => o.Number == transaction.OrderNumber && o.OrderState == OrderStateEnum.Executed).SingleOrDefault();

                    if (order != null)
                    {
                        actions.Single(a => a.Key(order)).Value.Invoke(unit, order, transaction);
                    }
                }

                unit.Commit();
            }

            return(Json("ok", JsonRequestBehavior.AllowGet));
        }