internal void MasterOrderCreated(IOrderInfo masterOrder) { RefreshRates(); double orderPrice = Marketinfo(masterOrder.GetSymbol(), (masterOrder.GetTradeOperation() == TradeOperation.OP_BUY) ? MarketInfo.MODE_ASK : MarketInfo.MODE_BID); TradeOperation orderType = masterOrder.GetTradeOperation(); Console.WriteLine("Creating an order - order.closePrice: {0} market.bid: {1} market.ask: {2} order.OrderType: {3}", masterOrder.GetClosePrice(), Marketinfo(masterOrder.GetSymbol(), MarketInfo.MODE_BID), Marketinfo(masterOrder.GetSymbol(), MarketInfo.MODE_ASK), orderType); //Info(String.Format("Executing order for account {0}", _acc)); try { int ticket = OrderSend( masterOrder.GetSymbol(), masterOrder.GetTradeOperation(), masterOrder.GetLots(), //masterOrder.GetOpenPrice(), // todo: adjust price to current Bid/Ask if OrderType==OP_BUY/SELL orderPrice, 20, masterOrder.GetStopLoss(), masterOrder.GetTakeProfit(), _master.Acc + "@" + _master.Broker + "-> _acc:" + _acc, masterOrder.GetTicket(), masterOrder.GetExpiration(), 0 ); if (ticket != 0) { _ordersMap.Add(masterOrder.GetTicket(), ticket); Info(String.Format("Master order {0} is mapped to {1}", masterOrder, ticket)); } } catch (ErrTradeDisabled) { Info(String.Format("Trade disabled for accoutn: {0}", _acc)); } catch (ErrInvalidPrice) { Info(String.Format("## ErrInvalidPrice - Price slippage ## - order.closePrice: {0} market.bid: {1} market.ask: {2} order.OrderType: {3}", masterOrder.GetClosePrice(), Marketinfo(masterOrder.GetSymbol(), MarketInfo.MODE_BID), Marketinfo(masterOrder.GetSymbol(), MarketInfo.MODE_ASK), orderType)); } /* * catch(ErrInvalidPrice) * { * Info(String.Format("Price invalid: {0} ", masterOrder.GetOpenPrice())); * * } * catch(ErrInvalidTradeVolume) * { * Info(String.Format("Trade volume invalid: {0} ", masterOrder.GetLots())); * * } * catch(ErrTradeDisabled) * { * Info(String.Format("Trade disabled!!")); * * } * catch(ErrOffQuotes) * { * Info(String.Format("This trade is off quotes!!")); * * } **/ }
private bool CloseOrder(IOrderInfo order) { try { TradeOperation orderType = order.GetTradeOperation(); Console.WriteLine("Closing an order - order.closePrice: {0} market.bid: {1} market.ask: {2} order.OrderType: {3}", order.GetClosePrice(), Marketinfo(order.GetSymbol(), MarketInfo.MODE_BID), Marketinfo(order.GetSymbol(), MarketInfo.MODE_ASK), orderType); switch (orderType) { case TradeOperation.OP_BUY: case TradeOperation.OP_SELL: RefreshRates(); if (order.GetTicket() > 0 && OrderClose(order.GetTicket(), order.GetLots(), Marketinfo(order.GetSymbol(), orderType == TradeOperation.OP_BUY ? MarketInfo.MODE_BID : MarketInfo.MODE_ASK), 20, 0)) { Info(String.Format("Order {0} has been closed", order)); return(true); } Info(String.Format("Order {0} to be closed; todo", order)); return(false); default: if (OrderDelete(order.GetTicket(), Color.Black)) { Info(String.Format("Order {0} has been deleted", order)); return(true); } else { Info(String.Format("Can not delete order {0}", order)); return(false); } } } catch (ErrInvalidTicket) { Info(String.Format("Looks like order {0} has been deleted manually", order)); return(true); } catch (ErrInvalidPrice) { Info(String.Format("## ErrInvalidPrice - Price slippage ## - order.closePrice: {0} market.bid: {1} market.ask: {2} order.OrderType: {3}", order.GetClosePrice(), Marketinfo(order.GetSymbol(), MarketInfo.MODE_BID), Marketinfo(order.GetSymbol(), MarketInfo.MODE_ASK), order.GetTradeOperation())); return(false); } }