public static void LoadPosition(Position position) { string key = "P_" + position.code + "_A_" + position.account_id + "_U_" + position.unit_id; if (TradeRA.KeyExists(key)) { return; } position.count_sellable = position.count; PositionRA.Add(position, key); }
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(); }