コード例 #1
0
        private void JY_OrderCanceled(object sender, OrderItem e)
        {
            string[] keys = TradeRA.KeySearch("O_" + e.order_no + "_*");
            if (keys.Length == 0 || keys[0].EndsWith("U_0") || keys[0].EndsWith("_F"))
            {
                return;
            }

            Order model = new Order()
            {
                order_no     = e.order_no,
                deal_count   = (int)decimal.Parse(e.deal_count),
                cancel_count = (int)decimal.Parse(e.cancel_count),
                status       = e.status,
            };

            OrderRA.UpdateStatus(model, keys[0]);

            //撤单成功取消冻结
            Order order = OrderRA.Get(keys[0]);

            MessageBiz.Send(order.user_id.ToString(), MessageTypeEnum.Order_Canceled, "[" + order.code + "]" + order.name + "撤单成功,撤单数量:" + model.cancel_count);
            if (e.type == "0")
            {
                string key_unit = "U_" + order.unit_id;
                UnitRA.UpdateCapitalOrderBuy(-order.price * model.cancel_count, key_unit);
            }
            else if (e.type == "1")
            {
                string key_position = "P_" + order.code + "_A_" + order.account_id + "_U_" + order.unit_id;
                PositionRA.UpdateSellableOrderSell(order.cancel_count, key_position);
            }
            MonitorRA.Increment("account_" + account_id, "cancel_count");
        }
コード例 #2
0
 public static void RunUnitCapitalMonitor()
 {
     string[] keys = TradeRA.KeySearch("U_*");
     foreach (string key in keys)
     {
         Unit unit = UnitRA.Get(key);
         if (unit.user_id > 0 && unit.ratio_warning > 0)
         {
             if (unit.capital_total < unit.ratio_warning * unit.capital_scale && unit.status_order_enum == StatusOrderEnum.Allowed)
             {
                 StatusOrder model = new StatusOrder(unit.id, StatusOrderEnum.NoBuying);
                 UnitDA.UpdateStatusOrder(model);
                 UnitRA.UpdateStatusOrder(model.status, "U_" + model.id);
                 UserDA.ListParents(unit.user_id).ForEach(user_id =>
                 {
                     MessageBiz.Send(user_id.ToString(), MessageTypeEnum.Unit_Warning, "单元[" + unit.code + "]资产已低于预警线,被禁止买入");
                 });
                 NLog.Info(string.Format("单元[{0}]资产已低于预警线,被禁止买入。总资产:{1}", unit.code, unit.capital_total));
             }
             else if (unit.capital_total > unit.ratio_warning * unit.capital_scale && unit.status_order_enum == StatusOrderEnum.NoBuying)
             {
                 StatusOrder model = new StatusOrder(unit.id, StatusOrderEnum.Allowed);
                 UnitDA.UpdateStatusOrder(model);
                 UnitRA.UpdateStatusOrder(model.status, "U_" + model.id);
                 MessageBiz.Send(unit.user_id.ToString(), MessageTypeEnum.Unit_Recovery, "单元[" + unit.code + "]资产已高于预警线,恢复交易");
             }
         }
         if (unit.user_id > 0 && unit.ratio_close_position > 0 && unit.capital_total < unit.ratio_close_position * unit.capital_scale)
         {
             List <Position> list = PositionRA.List4Unit(unit.id).Where(p => p.count_sellable > 0).ToList();
             if (list.Count > 0)
             {
                 list.ForEach(p =>
                 {
                     Order order = new Order()
                     {
                         code      = p.code,
                         count     = p.count_sellable,
                         type_enum = OrderTypeEnum.Sell,
                         unit_id   = unit.id,
                         price     = DataBiz.GetPriceByPriceType(p.code, OrderPriceEnum.Buy5),
                         user_id   = 1,
                         platform  = 1
                     };
                     Order(order);
                 });
                 UserDA.ListParents(unit.user_id).ForEach(user_id =>
                 {
                     MessageBiz.Send(unit.id.ToString(), MessageTypeEnum.Unit_Close_Position, "单元[" + unit.code + "]资产已低于平仓线,被强制平仓");
                 });
                 NLog.Info(string.Format("单元[{0}]资产已低于平仓线,被强制平仓。总资产:{1}", unit.code, unit.capital_total));
             }
         }
     }
 }
コード例 #3
0
        private void JY_NewDeal(object sender, DealItem e)
        {
            Order order   = null;
            int   unit_id = 0;

            string[] keys = TradeRA.KeySearch("O_" + e.order_no + "_*");
            if (keys.Length > 0)
            {
                order   = OrderRA.Get(keys[0]);
                unit_id = order.unit_id;
            }

            string key = "D_" + e.deal_no + "_O_" + e.order_no + "_U_" + unit_id;

            if (TradeRA.KeyExists(key))
            {
                return;
            }

            Deal deal = new Deal()
            {
                code        = e.code,
                name        = e.name,
                type        = int.Parse(e.type),
                count       = (int)decimal.Parse(e.count),
                money       = decimal.Parse(e.money),
                time_dt     = DateTime.Parse(e.date.ToDate() + " " + e.time.ToTime()),
                deal_no     = e.deal_no,
                order_no    = e.order_no,
                price       = decimal.Parse(e.price),
                unit_id     = unit_id,
                account_id  = account_id,
                transferred = unit_id > 0 ? 0 : 1,
            };

            DealRA.Add(deal, key);

            //更新成交均价
            DealAveragePrice(deal);

            //系统内成交
            if (unit_id > 0)
            {
                MessageBiz.Send(order.user_id.ToString(), MessageTypeEnum.Order_Dealt, "[" + deal.code + "]" + deal.name + "已成交,成交数量:" + deal.count);
                TradeBiz.NewDeal(deal, order.price);
                MonitorRA.Increment("account_" + account_id, "deal_count");
            }
        }
コード例 #4
0
 public static void RunAccountCapitalMonitor()
 {
     string[] keys = TradeRA.KeySearch("A_*");
     foreach (string key in keys)
     {
         Account account = AccountRA.Get(key);
         if (account.ratio_capital_warning > 0 && account.capital_total > 0 && account.capital_total < account.ratio_capital_warning * account.capital_initial)
         {
             UserDA.ListParents(account.created_by).ForEach(user_id =>
             {
                 MessageBiz.Send(user_id.ToString(), MessageTypeEnum.Account_Warning, "主账户[" + account.code + "]资产已低于预警线");
             });
             NLog.Info(string.Format("主账户[{0}]资产已低于预警线。总资产:{1}", account.code, account.capital_total));
         }
     }
 }
コード例 #5
0
        private void JY_Order_Done(object sender, OrderResult e)
        {
            string[] keys = TradeRA.KeySearch("O_" + e.request_id + "_*");
            if (keys.Length == 0)
            {
                return;
            }

            Order order = OrderRA.Get(keys[0]);

            if (order.state_enum != OrderStatusEnum.Submitted)
            {
                return;
            }

            //委托失败解除冻结
            if (!e.result)
            {
                if (order.type_enum == OrderTypeEnum.Buy)
                {
                    string key_unit = "U_" + order.unit_id;
                    UnitRA.UpdateCapitalOrderBuy(-order.price * order.count, key_unit);
                }
                else if (order.type_enum == OrderTypeEnum.Sell)
                {
                    string key_position = "P_" + order.code + "_A_" + order.account_id + "_U_" + order.unit_id;
                    PositionRA.UpdateSellableOrderSell(order.count, key_position);
                }
                OrderRA.UpdateOrderFailed(keys[0], e.message);
                MessageBiz.Send(order.user_id.ToString(), MessageTypeEnum.Order_Failed, "[" + order.code + "]" + order.name + "委托失败。" + e.message);
                MonitorRA.Increment("account_" + account_id, "failed_count");
            }
            else
            {
                OrderRA.UpdateOrderSuccess(keys[0], order.unit_id, order.trade_no, e.order_no);
                MonitorRA.Increment("account_" + account_id, "success_count");
            }
        }