/// <summary> /// проверить, не пора ли высылать новую заявку /// </summary> public void Check() { if (_ordersNeadToCreate == null || _ordersNeadToCreate.Count == 0) { return; } if (_ordersInSystem == null || _ordersInSystem.State == OrderStateType.Done) { _ordersInSystem = _ordersNeadToCreate[0]; _ordersNeadToCreate.Remove(_ordersInSystem); if (_type == AcebergType.Open) { _position.AddNewOpenOrder(_ordersInSystem); } else if (_type == AcebergType.Close) { _position.AddNewCloseOrder(_ordersInSystem); } else if (_type == AcebergType.ModificateBuy) { if (_position.Direction == Side.Buy && _ordersInSystem.Side == Side.Buy || _position.Direction == Side.Sell && _ordersInSystem.Side == Side.Sell) { _position.AddNewOpenOrder(_ordersInSystem); } else { _position.AddNewCloseOrder(_ordersInSystem); } } else if (_type == AcebergType.ModificateSell) { if (_position.Direction == Side.Buy && _ordersInSystem.Side == Side.Buy || _position.Direction == Side.Sell && _ordersInSystem.Side == Side.Sell) { _position.AddNewOpenOrder(_ordersInSystem); } else { _position.AddNewCloseOrder(_ordersInSystem); } } if (NewOrderNeadToExecute != null) { NewOrderNeadToExecute(_ordersInSystem); } } }
/// <summary> /// закрыть позицию /// </summary> private void ClosePosition(Position position, int index, DateTime time, decimal price) { Order newOrder = new Order(); newOrder.SecurityNameCode = position.SecurityName; newOrder.Price = price; newOrder.NumberUser = index; newOrder.Volume = 1; newOrder.NumberMarket = index.ToString(); if (SideInter == Side.Buy) { newOrder.Side = Side.Sell; } else { newOrder.Side = Side.Buy; } newOrder.TimeCallBack = time; newOrder.TimeCreate = time; position.AddNewCloseOrder(newOrder); MyTrade trade = new MyTrade(); trade.Volume = 1; trade.Side = newOrder.Side; trade.NumberOrderParent = index.ToString(); trade.Price = price; trade.NumberTrade = index + 1.ToString(); trade.Time = time; position.SetTrade(trade); }
// внутренние функции управления позицией // internal position management functions private void CloseDeal(Position position, OrderPriceType priceType, decimal price, TimeSpan lifeTime, bool isStopOrProfit) { try { if (position == null) { return; } position.ProfitOrderIsActiv = false; position.StopOrderIsActiv = false; for (int i = 0; position.CloseOrders != null && i < position.CloseOrders.Count; i++) { if (position.CloseOrders[i].State == OrderStateType.Activ && position.CloseOrders[i].TypeOrder != OrderPriceType.LimitStop && position.CloseOrders[i].TypeOrder != OrderPriceType.MarketStop ) { _connector.OrderCancel(position.CloseOrders[i]); } } for (int i = 0; position.OpenOrders != null && i < position.OpenOrders.Count; i++) { if (position.OpenOrders[i].State == OrderStateType.Activ && position.OpenOrders[i].TypeOrder != OrderPriceType.LimitStop && position.OpenOrders[i].TypeOrder != OrderPriceType.MarketStop ) { _connector.OrderCancel(position.OpenOrders[i]); } } if (Securiti == null) { return; } Side sideCloseOrder = Side.Buy; if (position.Direction == Side.Buy) { sideCloseOrder = Side.Sell; } price = RoundPrice(price, Securiti, sideCloseOrder); if (position.State == PositionStateType.Done && position.OpenVolume == 0) { return; } position.State = PositionStateType.Closing; Order closeOrder = _dealCreator.CreateCloseOrderForDeal(position, price, priceType, lifeTime, StartProgram); if (closeOrder == null) { if (position.OpenVolume == 0) { position.State = PositionStateType.OpeningFail; } return; } if (isStopOrProfit) { closeOrder.IsStopOrProfit = true; } position.AddNewCloseOrder(closeOrder); _connector.OrderExecute(closeOrder); } catch (Exception error) { SetNewLogMessage(error.ToString(), LogMessageType.Error); } }