public void close_all_position() { RestoreDb(); MatchService ms = new MatchService(); var t1 = ms.Model.Traders.Where(a => a.Id == 1).First(); var t2 = ms.Model.Traders.Where(a => a.Id == 2).First(); var c = ms.Model.Contracts.Where(a => a.Id == 1 && a.IsDel == false).First(); var sl = new List <OrderResult>(); for (int i = 0; i < 6; i++) { var t = ms.AddOrder(t1.Id, c.Id, TradeDirectType.卖, OrderType.开仓, OrderPolicy.限价申报, i + 1, i + 1); sl.Add(t); } var b1 = ms.AddOrder(t2.Id, c.Id, TradeDirectType.买, OrderType.开仓, OrderPolicy.限价申报, 21, 6); var t1bt = t1.Account.BailAccount.Total; var t2bt = t2.Account.BailAccount.Total; var sc = ms.AddOrder(t1.Id, c.Id, TradeDirectType.买, OrderType.平仓, OrderPolicy.限价申报, 21, 6); var bc = ms.AddOrder(t2.Id, c.Id, TradeDirectType.卖, OrderType.平仓, OrderPolicy.限价申报, 21, 6); vo_s_dp_dc(sc.Order, OrderState.已成交, 6, 21); vo_s_dp_dc(bc.Order, OrderState.已成交, 6, 21); var ps = t1.GetPositionSummary(c.Code, PositionType.义务仓); var pb = t2.GetPositionSummary(c.Code, PositionType.权利仓); vp_c_bp_m(ps, 0, 6, 0); vp_c_bp_m(pb, 0, 6, 0); vb_t_f_m(t1.Account.BailAccount, t1bt - (decimal)(21 * 6) - (decimal)(21 * 6 * 0.001), 0, 0); vb_t_f_m(t2.Account.BailAccount, t2bt + (decimal)(21 * 6) - (decimal)(21 * 6 * 0.001), 0, 0); }
public void s_o_1_1_2_2_b_o_3_2() { RestoreDb(); MatchService ms = new MatchService(); var t1 = ms.Model.Traders.Where(a => a.Id == 1).First(); var t2 = ms.Model.Traders.Where(a => a.Id == 2).First(); var c = ms.Model.Contracts.Where(a => a.Id == 1 && a.IsDel == false).First(); //1块钱卖1个 var r1 = ms.AddOrder(t1.Id, 1, TradeDirectType.卖, OrderType.开仓, OrderPolicy.限价申报, 1, 1); //2块钱卖2个 var r2 = ms.AddOrder(t1.Id, 1, TradeDirectType.卖, OrderType.开仓, OrderPolicy.限价申报, 2, 2); //3块钱买2个 var r3 = ms.AddOrder(t2.Id, 1, TradeDirectType.买, OrderType.开仓, OrderPolicy.限价申报, 2, 3); //成交 vo_s_dp_dc(r1.Order, OrderState.已成交, 1, 1); vo_s_dp_dc(r2.Order, OrderState.部分成交, 2, 1); vo_s_dp_dc(r3.Order, OrderState.已成交, 2, 1); //持仓 var p1 = t1.GetPositionSummary(c.Code, PositionType.义务仓); vp_c_bp_m(p1, 2, 1.5m, 0); var p2 = t2.GetPositionSummary(c.Code, PositionType.权利仓); vp_c_bp_m(p2, 2, 1.5m, 0m); //金额 var delta = (decimal)(1 * 1 + 2 * 1); var commission = (decimal)(0.001 * 3); vb_t_f_m(t1.Account.BailAccount, orignalSum + delta - commission, 2m, 0); vb_t_f_m(t2.Account.BailAccount, (decimal)(orignalSum - delta - commission), 0m, 0m); }
public void one_deal_ok() { var ms = new MatchService(); var r = ms.AddOrder(3, 2, Core.TradeDirectType.卖, OrderType.开仓, OrderPolicy.限价申报, 10, 10); Assert.AreEqual(true, r.IsSuccess); Assert.AreEqual(ms.Matcher.Container.Orders.Count, 1); var r1 = ms.AddOrder(2, 2, Core.TradeDirectType.买, OrderType.开仓, OrderPolicy.限价申报, 10, 10); Assert.AreEqual(true, r1.IsSuccess); Assert.AreEqual(ms.Matcher.Container.Orders.Count, 1); }
public void anti_sell_and_has_position()//买卖平仓 { var ms = new MatchService(); var t = ms.Model.Traders.Where(a => a.Id == 1).FirstOrDefault(); // ms.dataCenter.Db.Set<Trader>().Find(1); var t2 = ms.Model.Traders.Where(a => a.Id == 2).FirstOrDefault(); //ms.dataCenter.Db.Set<Trader>().Find(2); var orginalCount = t.Positions.Count; var orginalCount2 = t2.Positions.Count; var r = ms.AddOrder(1, 1, Core.TradeDirectType.买, OrderType.开仓, OrderPolicy.限价申报, 10, 10); var r1 = ms.AddOrder(2, 1, Core.TradeDirectType.卖, OrderType.开仓, OrderPolicy.限价申报, 10, 10); Assert.AreEqual(1, t.Positions.Count - orginalCount); Assert.AreEqual(1, t2.Positions.Count - orginalCount2); }
public void sell_close_fail_because_no_position() { var ms = new MatchService(); var r = ms.AddOrder(1, 1, Core.TradeDirectType.买, OrderType.平仓, OrderPolicy.限价申报, 10, 10); Assert.AreEqual(false, r.IsSuccess); }
public void sell_open_ok() { var ms = new MatchService(); var r = ms.AddOrder(1, 1, Core.TradeDirectType.卖, OrderType.开仓, OrderPolicy.限价申报, 10, 10); Assert.AreEqual(true, r.IsSuccess); Assert.AreEqual(ms.Matcher.Container.Orders.Count, 1); }
public void s_o_i_to10_i_to10_b_o_10_55() { RestoreDb(); MatchService ms = new MatchService(); var t1 = ms.Model.Traders.Where(a => a.Id == 1).First(); var t2 = ms.Model.Traders.Where(a => a.Id == 2).First(); var c = ms.Model.Contracts.Where(a => a.Id == 1 && a.IsDel == false).First(); var sl = new List <OrderResult>(); for (int i = 0; i < 10; i++) { var t = ms.AddOrder(t1.Id, c.Id, TradeDirectType.卖, OrderType.开仓, OrderPolicy.限价申报, i + 1, i + 1); sl.Add(t); } var b1 = ms.AddOrder(t2.Id, c.Id, TradeDirectType.买, OrderType.开仓, OrderPolicy.限价申报, 55, 10); //成交确认 for (int i = 0; i < 10; i++) { vo_s_dp_dc(sl[i].Order, OrderState.已成交, i + 1, i + 1); } vo_s_dp_dc(b1.Order, OrderState.已成交, 10, 10); //持仓确认 var p1 = t1.GetPositionSummary(c.Code, PositionType.义务仓); decimal bt = 0; decimal bm = 0; for (int i = 0; i < 10; i++) { bt += (decimal)((i + 1) * (i + 1)); bm += 0; } vp_c_bp_m(p1, 55, bt / 55m, bm); var p2 = t2.GetPositionSummary(c.Code, PositionType.权利仓); vp_c_bp_m(p2, 55, bt / 55m, 0); //金额确认 vb_t_f_m(t1.Account.BailAccount, orignalSum + bt - (decimal)(0.001m * bt), 0, bm); vb_t_f_m(t2.Account.BailAccount, orignalSum - bt - (decimal)(0.001m * bt), 0, 0); }
public void multi_trader_sell_buy_count_not_eaque() { var ms = new MatchService(); List <OperationResult> l = new List <OperationResult>(); for (int i = 0; i < 10; i++) { var r = ms.AddOrder(i + 1, 1, Core.TradeDirectType.卖, OrderType.开仓, OrderPolicy.限价申报, (i + 1) * 10, 10); l.Add(r); } for (int i = 0; i < 10; i++) { var r = ms.AddOrder(i + 1, 1, Core.TradeDirectType.买, OrderType.开仓, OrderPolicy.限价申报, (i + 1) * 11, 10); l.Add(r); } foreach (var v in l) { Assert.AreEqual(true, v.IsSuccess); } }
public void close_all() { var t1bt = t1.Account.BailAccount.Total; var t2bt = t2.Account.BailAccount.Total; var sc = ms.AddOrder(t1.Id, c.Id, TradeDirectType.买, OrderType.平仓, OrderPolicy.限价申报, 21, 6); var bc = ms.AddOrder(t2.Id, c.Id, TradeDirectType.卖, OrderType.平仓, OrderPolicy.限价申报, 21, 6); vo_s_dp_dc(sc.Order, OrderState.已成交, 6, 21); vo_s_dp_dc(bc.Order, OrderState.已成交, 6, 21); var ps = t1.GetPositionSummary(c.Code, PositionType.义务仓); var pb = t2.GetPositionSummary(c.Code, PositionType.权利仓); vp_c_bp_m(ps, 0, 6, 0); vp_c_bp_m(pb, 0, 6, 0); vb_t_f_m(t1.Account.BailAccount, t1bt - (decimal)(21 * 6) - (decimal)(21 * 6 * 0.001), 0, 0); vb_t_f_m(t2.Account.BailAccount, t2bt + (decimal)(21 * 6) - (decimal)(21 * 6 * 0.001), 0, 0); }