public Result UpdateUnits(UserUnits model) { ApiResultEnum result = UnitDA.UpdateUserID(model); if (result == ApiResultEnum.Success && MonitorRA.GetStatusTrade() != 0) { string[] keys = TradeRA.KeySearch("U_*"); foreach (string key in keys) { string _user_id = TradeRA.Get(key, "user_id"); if (model.unit_ids.Contains(int.Parse(key.Substring(2)))) { if (model.id != _user_id) { UnitRA.UpdateUserID(model.id, key); } } else { if (model.id == _user_id) { UnitRA.UpdateUserID("0", key); } } } } return(Result(result)); }
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"); }
public Result UpdateStatus(Status model) { ApiResultEnum result = UnitDA.UpdateStatus(model); if (result == ApiResultEnum.Success && MonitorRA.GetStatusTrade() != 0) { UnitRA.UpdateStatus(model.status, "U_" + model.id); } return(Result(result)); }
public Result UpdateRatioFreezing(Model.DB.Unit model) { ApiResultEnum result = UnitDA.UpdateRatioFreezing(model); if (result == ApiResultEnum.Success && MonitorRA.GetStatusTrade() != 0) { UnitRA.UpdateRatioFreezing(model.ratio_freezing, "U_" + model.id); } return(Result(result)); }
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)); } } } }
public Result CapitalInOut(LogCapital model) { model.@operator = user_id; decimal delta; ApiResultEnum result = LogCapitalDA.Add(model, out delta); if (result == ApiResultEnum.Success && MonitorRA.GetStatusTrade() != 0) { UnitRA.UpdateCapitalInOut(delta, "U_" + model.unit_id); } return(Result(result)); }
public Result <UnitCapital> GetUnitCapital(int unit_id) { Model.DB.Unit unit = UnitRA.Get("U_" + unit_id); UnitCapital unitCapital = new UnitCapital(); if (unit != null) { unitCapital.available = unit.capital_available; unitCapital.scale = unit.capital_scale; unitCapital.balance = unit.capital_balance; unitCapital.value = unit.capital_stock_value; unitCapital.assets = unit.capital_total; unitCapital.profit = unit.capital_profit; } return(Result(unitCapital)); }
//将单元的下单数量分配到主账户 public static Dictionary <int, int> GetOrderAccount(Order order) { Dictionary <int, int> order_items = new Dictionary <int, int>(); Unit unit = UnitRA.Get("U_" + order.unit_id); List <AccountGroupItem> items = AccountGroupRA.List4Unit(order.unit_id); int total = 0; if (order.type_enum == OrderTypeEnum.Buy) { if (unit.priority_strategy_enum == PriorityStrategyEnum.Natural) { items = items.OrderBy(i => i.sort_buy).ToList(); } else { items = items.OrderByDescending(i => i.capital_available).ToList(); } foreach (AccountGroupItem item in items) { int buyable = (item.capital_available == decimal.MaxValue) ? (order.count - total) : (int)(item.capital_available / 100 / order.price) * 100; int ct = Math.Min(buyable, (order.count - total)); order_items.Add(item.account_id, ct); total += ct; if (ct == order.count) { break; } } } else { items = items.OrderBy(i => i.sort_sell).ToList(); foreach (AccountGroupItem item in items) { int sellable = PositionRA.GetSellable(order.unit_id, order.code, item.account_id); int ct = Math.Min(sellable, (order.count - total)); order_items.Add(item.account_id, ct); total += ct; if (ct == order.count) { break; } } } return(order_items); }
public static void LoadUnit(Unit unit, bool add = true) { string key = "U_" + unit.id; if (add && TradeRA.KeyExists(key)) { return; } if (add) { UnitRA.Add(unit, key); } else { UnitRA.Update(unit, key); } }
public static void SaveUnitCapital(Dictionary <int, decimal[]> dic) { string[] keys = TradeRA.KeySearch("U_*"); foreach (string key in keys) { Unit unit = UnitRA.Get(key); UnitDA.UpdateCapital(unit); Statement statement = new Statement() { unit_id = unit.id, capital_total = unit.capital_total, capital_stock_value = unit.capital_stock_value, capital_inout = unit.capital_inout, fee = dic.ContainsKey(unit.id) ? dic[unit.id][0] : 0, profit = dic.ContainsKey(unit.id) ? dic[unit.id][1] : 0, }; StatementDA.Add(statement); } }
public Result UpdateItems(Model.DB.AccountGroup model) { model.items = model.items.OrderBy(i => i.sort_buy).AsEnumerable().DistinctBy(i => i.account_id).ToList(); ApiResultEnum result = AccountGroupDA.UpdateItems(model); if (result == ApiResultEnum.Success && MonitorRA.GetStatusTrade() != 0) { var units = UnitDA.List4AccountGroup(model.id); foreach (var kvp in units) { UnitRA.UpdatePriorityStrategy(model.priority_strategy, "U_" + kvp.id); } AccountGroupRA.Delete4AccountGroup(model.id); List <Model.DB.AccountGroupItem> items = AccountGroupDA.ListItems4AccountGroup(model.id); foreach (Model.DB.AccountGroupItem item in items) { OpenCloseBiz.LoadAccountGroupItem(item); } TradeBiz.RunCalculateValue(); } return(Result(result)); }
public static void NewDeal(Deal deal, decimal order_price, int type = 0) { //计算手续费,佣金最少收5元 string key_unit = "U_" + deal.unit_id; Unit unit = UnitRA.Get(key_unit); deal.commission = Math.Max(Math.Round(deal.money * unit.ratio_commission, 2), 5); deal.management_fee = Math.Round(deal.money * unit.ratio_management_fee, 2); //卖单计算盈亏 Position position = PositionRA.Get("P_" + deal.code + "_A_" + deal.account_id + "_U_" + deal.unit_id); if (deal.type == 1 && position != null) { deal.profit = Math.Round((deal.price - position.price_cost) * deal.count - deal.commission, 2); } //更新手续费和盈亏 DealRA.UpdateFee(deal, "D_" + deal.deal_no + "_O_" + deal.order_no + "_U_" + deal.unit_id); AdjustPosition(position, deal, order_price, type); }
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"); } }
public static void RunCalculateValue() { //单元的市值 Dictionary <int, decimal[]> dic_unit = new Dictionary <int, decimal[]>(); //主账户的市值 Dictionary <int, decimal> dic_account = new Dictionary <int, decimal>(); //单元下主账户组中的主账户市值 Dictionary <string, decimal> dic_account_group = new Dictionary <string, decimal>(); string[] keys = TradeRA.KeySearch("P_*"); foreach (string key in keys) { Position position = PositionRA.Get(key); HQItem hq = HQService.Get(position.code); decimal price = hq == null ? position.price_latest : hq.Last; decimal value = price * position.count; decimal profit = (price - position.price_cost) * position.count; if (dic_unit.ContainsKey(position.unit_id)) { dic_unit[position.unit_id][position.block] += value; dic_unit[position.unit_id][4] += value; dic_unit[position.unit_id][5] += profit; } else { decimal[] values = new decimal[6] { 0, 0, 0, 0, value, profit }; values[position.block] = value; dic_unit.Add(position.unit_id, values); } //主账户市值暂只计算创业板 if (position.block_enum == BlockEnum.gem) { if (dic_account.ContainsKey(position.account_id)) { dic_account[position.account_id] += value; } else { dic_account.Add(position.account_id, value); } } string ua = "U_" + position.unit_id + "_A_" + position.account_id; if (dic_account_group.ContainsKey(ua)) { dic_account_group[ua] += value; } else { dic_account_group.Add(ua, value); } } foreach (var kvp in dic_unit) { UnitRA.UpdateCapitalStockValue(kvp.Value, "U_" + kvp.Key); } foreach (var kvp in dic_account) { AccountRA.UpdateCapitalStockValue(kvp.Value, "A_" + kvp.Key); } foreach (var kvp in dic_account_group) { AccountGroupRA.UpdateCapitalStockValue(kvp.Key, kvp.Value); } }
//检查单元是否满足下单条件 public static ApiResultEnum CheckUnit(Order order) { Unit unit = UnitRA.Get("U_" + order.unit_id); if (unit == null || unit.user_id != order.user_id) { return(ApiResultEnum.Order_Unit_Null); } if (order.type_enum == OrderTypeEnum.Buy && !string.IsNullOrWhiteSpace(unit.limit_no_buying) && unit.limit_no_buying.Contains(order.code)) { return(ApiResultEnum.Order_Unit_NoBuying_Code); } if (unit.status_enum == StatusEnum.Forbidden) { return(ApiResultEnum.Order_Unit_Forbidden); } if (unit.status_order_enum == StatusOrderEnum.Forbidden) { return(ApiResultEnum.Order_Unit_Forbidden_Order); } if (order.type_enum == OrderTypeEnum.Buy && unit.status_order_enum == StatusOrderEnum.NoBuying) { return(ApiResultEnum.Order_Unit_NoBuying_Order); } string[] keys = TradeRA.KeySearch("P_" + order.code + "_A_*_U_" + order.unit_id); if (unit.limit_stock_count > 0) { if (PositionRA.GetCodeCount(order.unit_id, order.code) > unit.limit_stock_count) { return(ApiResultEnum.Order_Unit_Limit_Count); } } if (unit.limit_order_price_enum == OrderPriceLimitEnum.In5LeverPrice) { decimal price_min = DataBiz.GetPriceByPriceType(order.code, OrderPriceEnum.Buy5); decimal price_max = DataBiz.GetPriceByPriceType(order.code, OrderPriceEnum.Sell5); if (order.price < price_min || order.price > price_max) { return(ApiResultEnum.Order_Unit_Limit_Price); } } if (order.type_enum == OrderTypeEnum.Buy) { if (unit.capital_available < order.price * order.count) { return(ApiResultEnum.Order_Unit_Negative_Amount); } BlockInfo block = StockInfoBiz.GetBlock(order.code); if (block.block_type_enum == BlockEnum.mbm) { if (unit.limit_ratio_mbm_single > 0 && order.price * order.count > unit.capital_scale * unit.limit_ratio_mbm_single) { return(ApiResultEnum.Order_Unit_Limit_MBM_Single); } } else if (block.block_type_enum == BlockEnum.gem) { if (unit.limit_ratio_gem_single > 0 && order.price * order.count > unit.capital_scale * unit.limit_ratio_gem_single) { return(ApiResultEnum.Order_Unit_Limit_GEM_Single); } if (unit.limit_ratio_gem_total > 0 && order.price * order.count + unit.capital_stock_value_gem > unit.capital_scale * unit.limit_ratio_gem_total) { return(ApiResultEnum.Order_Unit_Limit_GEM_Total); } } else if (block.block_type_enum == BlockEnum.sme) { if (unit.limit_ratio_sme_single > 0 && order.price * order.count > unit.capital_scale * unit.limit_ratio_sme_single) { return(ApiResultEnum.Order_Unit_Limit_SME_Single); } if (unit.limit_ratio_sme_total > 0 && order.price * order.count + unit.capital_stock_value_sme > unit.capital_scale * unit.limit_ratio_sme_total) { return(ApiResultEnum.Order_Unit_Limit_SME_Total); } } else if (block.block_type_enum == BlockEnum.star) { if (unit.limit_ratio_star_single > 0 && order.price * order.count > unit.capital_scale * unit.limit_ratio_star_single) { return(ApiResultEnum.Order_Unit_Limit_STAR_Single); } if (unit.limit_ratio_star_total > 0 && order.price * order.count + unit.capital_stock_value_star > unit.capital_scale * unit.limit_ratio_star_total) { return(ApiResultEnum.Order_Unit_Limit_STAR_Total); } } if (block.block_type_enum == BlockEnum.sme || block.block_type_enum == BlockEnum.gem) { if (unit.limit_ratio_smg_total > 0 && order.price * order.count + unit.capital_stock_value_sme + unit.capital_stock_value_gem > unit.capital_scale * unit.limit_ratio_smg_total) { return(ApiResultEnum.Order_Unit_Limit_SMG_Total); } } } else { if (PositionRA.GetSellable(order.unit_id, order.code) < order.count) { return(ApiResultEnum.Order_Unit_Negative_Position); } } return(ApiResultEnum.Success); }
//检查主账户是否满足下单条件 public static ApiResultEnum CheckAccount(Order order) { Account account = AccountRA.Get("A_" + order.account_id); if (order.type_enum == OrderTypeEnum.Buy && !string.IsNullOrWhiteSpace(account.limit_no_buying) && account.limit_no_buying.Contains(order.code)) { return(ApiResultEnum.Order_Account_NoBuying_Code); } if (account.status_enum == StatusEnum.Forbidden) { return(ApiResultEnum.Order_Account_Forbidden); } if (account.status_order_enum == StatusOrderEnum.Forbidden) { return(ApiResultEnum.Order_Account_Forbidden_Order); } if (order.type_enum == OrderTypeEnum.Buy && account.status_order_enum == StatusOrderEnum.NoBuying) { return(ApiResultEnum.Order_Account_NoBuying_Order); } if (order.type_enum == OrderTypeEnum.Buy) { if (account.limit_ratio_single > 0 && order.price * order.count > account.capital_initial * account.limit_ratio_single) { return(ApiResultEnum.Order_Account_Limit_Single); } BlockInfo block = StockInfoBiz.GetBlock(order.code); if (block.block_type_enum == BlockEnum.gem) { if (account.limit_ratio_gem_single > 0 && order.price * order.count > account.capital_initial * account.limit_ratio_gem_single) { return(ApiResultEnum.Order_Account_Limit_GEM_Single); } if (account.limit_ratio_gem_total > 0 && order.price * order.count + account.capital_stock_value_gem > account.capital_initial * account.limit_ratio_gem_total) { return(ApiResultEnum.Order_Account_Limit_GEM_Total); } } string[] keys = TradeRA.KeySearch("G_*_U_" + order.unit_id + "_A_" + order.account_id); AccountGroupItem item = AccountGroupRA.Get(keys[0]); if (item.capital_available > 0 && item.capital_available < order.price * order.count + item.capital_stock_value) { return(ApiResultEnum.Order_Account_Negative_Amount); } } else { if (PositionRA.GetSellable(order.unit_id, order.code, order.account_id) < order.count) { return(ApiResultEnum.Order_Account_Negative_Position); } } //风控通过进行冻结 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); } return(ApiResultEnum.Success); }
public static void AdjustPosition(Position position, Deal deal, decimal order_price, int type) { string key_unit = "U_" + deal.unit_id; string key_position = "P_" + deal.code + "_A_" + deal.account_id + "_U_" + deal.unit_id; //更新单元余额和冻结 if (deal.type_enum == OrderTypeEnum.Buy) { UnitRA.UpdateCapitalDealBuy(order_price * deal.count, type == 0, key_unit); } else if (deal.type_enum == OrderTypeEnum.Sell) { UnitRA.UpdateCapitalDealSell(deal.price * deal.count, key_unit); } //更新持仓 decimal amount = deal.money + deal.commission; if (position != null) { if (deal.type == 0) { position.price_cost = Math.Round((position.price_cost * position.count + amount) / (deal.count + position.count), 6); position.count = position.count + deal.count; if (type == 0 || type == 1) { position.price_cost_today_buy = Math.Round((amount + position.price_cost_today_buy * position.count_today_buy) / (deal.count + position.count_today_buy), 6); position.count_today_buy = deal.count + position.count_today_buy; } PositionRA.UpdateBuy(position, key_position); } else { position.count = position.count - deal.count; if (type == 0 || type == 1) { position.price_cost_today_sell = Math.Round((amount + position.price_cost_today_sell * position.count_today_sell) / (deal.count + position.count_today_sell), 6); position.count_today_sell = deal.count + position.count_today_sell; } PositionRA.UpdateSell(position, key_position); } } else { HQItem hq = HQService.Get(deal.code); BlockInfo block = StockInfoBiz.GetBlock(deal.code); if (block == null) { return; } position = new Position() { code = deal.code, name = deal.name, unit_id = deal.unit_id, account_id = deal.account_id, block_enum = block.block_type_enum, price_latest = hq != null ? hq.Last : 0 }; if (deal.type == 0) { position.price_cost = Math.Round(amount / deal.count, 6); position.count = deal.count; if (type == 0 || type == 1) { position.price_cost_today_buy = position.price_cost; position.count_today_buy = position.count; } else if (type == 2) { position.count_sellable = position.count; } } else { position.price_cost = Math.Round(amount / deal.count, 6); position.count = position.count_sellable = -deal.count; if (type == 0 || type == 1) { position.price_cost_today_sell = position.price_cost; position.count_today_sell = deal.count; } } PositionRA.Add(position, key_position); } RunCalculateValue(); }
//获取单元最大可下单数量 public static int GetUnitCount(Order order) { Unit unit = UnitRA.Get("U_" + order.unit_id); if (unit == null) { return(0); } if (order.type_enum == OrderTypeEnum.Buy && !string.IsNullOrWhiteSpace(unit.limit_no_buying) && unit.limit_no_buying.Contains(order.code)) { return(0); } if (unit.status_enum == StatusEnum.Forbidden) { return(0); } if (unit.status_order_enum == StatusOrderEnum.Forbidden) { return(0); } if (order.type_enum == OrderTypeEnum.Buy && unit.status_order_enum == StatusOrderEnum.NoBuying) { return(0); } string[] keys = TradeRA.KeySearch("P_" + order.code + "_A_*_U_" + order.unit_id); if (unit.limit_stock_count > 0) { if (PositionRA.GetCodeCount(order.unit_id, order.code) > unit.limit_stock_count) { return(0); } } if (order.type_enum == OrderTypeEnum.Buy) { decimal count = unit.capital_available / order.price; BlockInfo block = StockInfoBiz.GetBlock(order.code); if (block.block_type_enum == BlockEnum.mbm) { if (unit.limit_ratio_mbm_single > 0) { count = Math.Min(count, unit.capital_scale * unit.limit_ratio_mbm_single / order.price); } } else if (block.block_type_enum == BlockEnum.gem) { if (unit.limit_ratio_gem_single > 0) { count = Math.Min(count, unit.capital_scale * unit.limit_ratio_gem_single / order.price); } if (unit.limit_ratio_gem_total > 0) { count = Math.Min(count, (unit.capital_scale * unit.limit_ratio_gem_total - unit.capital_stock_value_gem) / order.price); } } else if (block.block_type_enum == BlockEnum.sme) { if (unit.limit_ratio_sme_single > 0) { count = Math.Min(count, unit.capital_scale * unit.limit_ratio_sme_single / order.price); } if (unit.limit_ratio_sme_total > 0) { count = Math.Min(count, (unit.capital_scale * unit.limit_ratio_sme_total - unit.capital_stock_value_sme) / order.price); } } else if (block.block_type_enum == BlockEnum.star) { if (unit.limit_ratio_star_single > 0) { count = Math.Min(count, unit.capital_scale * unit.limit_ratio_star_single / order.price); } if (unit.limit_ratio_star_total > 0) { count = Math.Min(count, (unit.capital_scale * unit.limit_ratio_star_total - unit.capital_stock_value_star) / order.price); } } if (block.block_type_enum == BlockEnum.sme || block.block_type_enum == BlockEnum.gem) { if (unit.limit_ratio_smg_total > 0) { count = Math.Min(count, (unit.capital_scale * unit.limit_ratio_smg_total - unit.capital_stock_value_sme - unit.capital_stock_value_gem) / order.price); } } return((int)Math.Ceiling(count / 100) * 100); } else { return(PositionRA.GetSellable(order.unit_id, order.code)); } }