/// <summary> /// 多空方向描述。 /// </summary> /// <param name="direction">多空方向。</param> /// <returns>描述字符串。</returns> public static string ToDescription(this USeDirection direction) { switch (direction) { case USeDirection.Long: return("买"); case USeDirection.Short: return("卖"); default: return("未知"); } }
private bool CheckOrderDirection(USeDirection direction, USeOrderSide orderSide) { if (direction == USeDirection.Long && orderSide == USeOrderSide.Buy) { return(true); } else if (direction == USeDirection.Short && orderSide == USeOrderSide.Sell) { return(true); } else { return(false); } }
/// <summary> /// 计算保证金。 /// </summary> /// <param name="instrument">合约。</param> /// <param name="direction">方向。</param> /// <param name="qty">数量。</param> /// <param name="price">价格。</param> /// <returns></returns> private decimal CalculateMargin(USeInstrument instrument, USeDirection direction, int qty, decimal price) { USeMargin margin = m_dataBuffer.GetMargin(instrument); int volumeMultiple = m_dataBuffer.GetVolumeMultiple(instrument); switch (direction) { case USeDirection.Long: return(margin.BrokerLongMarginRatioByMoney * price * qty * volumeMultiple + margin.BrokerLongMarginRatioByVolume * qty); case USeDirection.Short: return(margin.BrokerShortMarginRatioByMoney * price * qty * volumeMultiple + margin.BrokerShortMarginRatioByVolume * qty); default: Debug.Assert(false, "CalculateMargin(),invalid direction"); return(0m); } }
/// <summary> /// 创建委托命令集合。 /// </summary> /// <param name="taskId">任务ID。</param> /// <param name="instrument">合约。</param> /// <param name="orderSide">买卖方向。</param> /// <param name="offsetType">开平方向。</param> /// <param name="orderQty">委托量。</param> /// <param name="orderPrice">委托价格。</param> /// <param name="orderReason">委托原因。</param> /// <returns></returns> private List <OrderCommand> CreateOrderCommands(int taskId, USeInstrument instrument, USeOrderSide orderSide, USeOffsetType offsetType, int orderQty, decimal orderPrice, string orderReason) { List <OrderCommand> commandList = new List <OrderCommand>(); //构造指令 if (offsetType == USeOffsetType.Open) { OrderCommand command = new OrderCommand() { TaskId = taskId, Instrument = instrument, OrderSide = orderSide, OffsetType = USeOffsetType.Open, OrderQty = orderQty, OrderPrice = orderPrice, OrderReason = orderReason }; commandList.Add(command); } else { Debug.Assert(offsetType == USeOffsetType.Close); if (instrument.Market == USeMarket.SHFE) { #region 交所平仓 USeDirection direction = orderSide == USeOrderSide.Buy ? USeDirection.Short : USeDirection.Long; //上海交易所平仓需区分平今还是平昨 USePosition position = m_orderDriver.QueryPositions(instrument, direction); //[yangming] string tmpTestText = string.Format(@"[hanyuClose]Ins:{0} OrderSide:{1} OffsetFlag:{2} OrderQty:{3},NewAvaliablePosition:{4} ,NewFrozonPosition:{5},NewPosition{6}," + "OldAvaliablePosition:{7},OldFrozonPosition:{8},OldPosition:{9}", instrument.InstrumentCode, orderSide, offsetType, orderQty, position.NewAvaliablePosition, position.NewFrozonPosition, position.NewPosition, position.OldAvaliablePosition, position.OldFrozonPosition, position.OldPosition); USeManager.Instance.EventLogger.WriteAudit(tmpTestText); // if (position == null || position.AvaliablePosition < orderQty) { //查询不到仓位,或者仓位不足,直接构造平今指令 OrderCommand command = new OrderCommand() { TaskId = taskId, Instrument = instrument, OrderSide = orderSide, OffsetType = USeOffsetType.CloseToday, OrderQty = orderQty, OrderPrice = orderPrice, OrderReason = orderReason }; commandList.Add(command); } else { //平今 int remainQty = orderQty; if (position.NewAvaliablePosition > 0) { int closeQty = Math.Min(position.NewAvaliablePosition, remainQty); OrderCommand command = new OrderCommand() { TaskId = taskId, Instrument = instrument, OrderSide = orderSide, OffsetType = USeOffsetType.CloseToday, OrderQty = closeQty, OrderPrice = orderPrice, OrderReason = orderReason }; remainQty -= closeQty; commandList.Add(command); } //平昨 if (remainQty > 0) { Debug.Assert(remainQty <= position.OldAvaliablePosition); int closeQty = remainQty; OrderCommand command = new OrderCommand() { TaskId = taskId, Instrument = instrument, OrderSide = orderSide, OffsetType = USeOffsetType.CloseHistory, OrderQty = closeQty, OrderPrice = orderPrice, OrderReason = orderReason }; remainQty -= closeQty; commandList.Add(command); } Debug.Assert(remainQty == 0); } #endregion } else { OrderCommand command = new OrderCommand() { TaskId = taskId, Instrument = instrument, OrderSide = orderSide, OffsetType = USeOffsetType.Close, OrderQty = orderQty, OrderPrice = orderPrice, OrderReason = orderReason }; } } return(commandList); }
/// <summary> /// 查询某一合约指定方向当前持仓。 /// </summary> /// <param name="instrument">合约。</param> /// <param name="direction">持仓方向。</param> /// <returns></returns> public abstract USePosition QueryPositions(USeInstrument instrument, USeDirection direction);
/// <summary> /// 查询某一合约指定方向当前持仓。 /// </summary> /// <param name="product"></param> /// <param name="direction"></param> /// <returns></returns> public override USePosition QueryPositions(USeInstrument instrument, USeDirection direction) { //return m_dataBuffer.GetPosition(instrument, direction); return(null); }
private void button_OK_Click(object sender, EventArgs e) { List <ArbitrageCombineOrderSetting> arbitrageCombineOrderList = new List <ArbitrageCombineOrderSetting>(); try { //保存之前数据校验 foreach (DataRow row in m_dataTable.Rows) { string errorMessage = string.Empty; if (VerifyDataTable(row, out errorMessage) == false) { USeFuturesSpiritUtility.ShowErrrorMessageBox(this, "请填写完整的默认信息:" + errorMessage); return; } } //根据界面生成结果保存 foreach (DataRow row in m_dataTable.Rows) { string productCode = row["ProductName"] as string; Debug.Assert(productCode != string.Empty); int openVolumn = Convert.ToInt32(row["OpenVolumn"]); int perOpenVolumn = Convert.ToInt32(row["PerOpenVolumn"]); USeDirection openDirection = (USeDirection)row["OpenFirstDirectionID"]; USeDirection closeDirection = (USeDirection)row["CloseFirstDirectionID"]; USeDirection stoplossDirection = (USeDirection)row["StopLossFirstDirectionID"]; ArbitrageOrderPriceType nearOpenPriceStyle = (ArbitrageOrderPriceType)row["NearOpenPriceStyleID"]; ArbitrageOrderPriceType farOpenPriceStyle = (ArbitrageOrderPriceType)row["FarOpenPriceStyleID"]; ArbitrageCombineOrderSetting order = new ArbitrageCombineOrderSetting(); USeProduct product = new USeProduct() { ProductCode = productCode }; order.Product = product; order.OpenVolumn = openVolumn; order.OpenVolumnPerNum = perOpenVolumn; order.OpenFirstDirection = openDirection; order.CloseFirstDirection = closeDirection; order.StoplossFirstDirection = stoplossDirection; order.NearPriceStyle = nearOpenPriceStyle; order.FarPriceStyle = farOpenPriceStyle; arbitrageCombineOrderList.Add(order); } } catch (Exception ex) { USeFuturesSpiritUtility.ShowWarningMessageBox(this, ex.Message); return; } string brokerId = USeManager.Instance.LoginUser.BrokerId; string account = USeManager.Instance.LoginUser.Account; USeManager.Instance.DataAccessor.SaveCombineOrderSettings(brokerId, account, arbitrageCombineOrderList); this.DialogResult = DialogResult.Yes; this.Close(); }