Esempio n. 1
0
        private void ProcessOrderStatusMessage()
        {
            foreach (var accountId in _accountIds.CachedValues)
            {
                int?maxOrderId = null;

                while (true)
                {
                    var orders = _restClient.GetOrders(accountId, maxOrderId);

                    var count = 0;

                    foreach (var order in orders)
                    {
                        count++;

                        maxOrderId = order.Id - 1;

                        OandaOrderCondition condition = null;
                        OrderTypes          orderType;

                        if (order.Type != "market" && order.Type != "limit")
                        {
                            orderType = OrderTypes.Conditional;

                            condition = new OandaOrderCondition
                            {
                                IsMarket               = order.Type == _orderImit,
                                TakeProfitOffset       = (decimal?)order.TakeProfit,
                                StopLossOffset         = (decimal?)order.StopLoss,
                                UpperBound             = (decimal?)order.UpperBound,
                                LowerBound             = (decimal?)order.LowerBound,
                                TrailingStopLossOffset = order.TrailingStop,
                            };
                        }
                        else
                        {
                            orderType = order.Type == "market" ? OrderTypes.Market : OrderTypes.Limit;
                        }

                        SendOutMessage(new ExecutionMessage
                        {
                            OrderType     = orderType,
                            ExecutionType = ExecutionTypes.Order,
                            OrderId       = order.Id,
                            ServerTime    = order.Time.FromOanda(),
                            OrderPrice    = (decimal)order.Price,
                            Volume        = order.Units,
                            Side          = order.Side.To <Sides>(),
                            SecurityId    = order.Instrument.ToSecurityId(),
                            ExpiryDate    = order.Expiry == null ? (DateTimeOffset?)null : order.Expiry.Value.FromOanda(),
                            Condition     = condition,
                            PortfolioName = GetPortfolioName(accountId),
                        });
                    }

                    if (count < 50)
                    {
                        break;
                    }
                }

                int?maxTradeId = null;

                while (true)
                {
                    var trades = _restClient.GetTrades(accountId, maxTradeId);

                    var count = 0;

                    foreach (var trade in trades)
                    {
                        count++;

                        maxTradeId = trade.Id - 1;

                        var takeProfit  = Math.Abs(trade.TakeProfit) < 0.0000001 ? (decimal?)null : (decimal)trade.TakeProfit;
                        var stopLoss    = Math.Abs(trade.StopLoss) < 0.0000001 ? (decimal?)null : (decimal)trade.StopLoss;
                        var tralingStop = trade.TrailingStop == 0 ? (int?)null : trade.TrailingStop;

                        var isConditional = takeProfit != null || stopLoss != null || tralingStop != null;

                        var transId = TransactionIdGenerator.GetNextId();

                        SendOutMessage(new ExecutionMessage
                        {
                            ExecutionType         = ExecutionTypes.Order,
                            OrderType             = isConditional ? OrderTypes.Conditional : OrderTypes.Limit,
                            OrderId               = trade.Id,
                            OriginalTransactionId = transId,
                            ServerTime            = trade.Time.FromOanda(),
                            OrderPrice            = (decimal)trade.Price,
                            Volume        = trade.Units,
                            Balance       = 0,
                            Side          = trade.Side.To <Sides>(),
                            SecurityId    = trade.Instrument.ToSecurityId(),
                            OrderState    = isConditional ? OrderStates.Active : OrderStates.Done,
                            PortfolioName = GetPortfolioName(accountId),
                            Condition     = isConditional ? new OandaOrderCondition
                            {
                                TakeProfitOffset       = takeProfit,
                                StopLossOffset         = stopLoss,
                                TrailingStopLossOffset = tralingStop,
                            } : null,
                        });

                        if (!isConditional)
                        {
                            SendOutMessage(new ExecutionMessage
                            {
                                ExecutionType         = ExecutionTypes.Trade,
                                OrderId               = trade.Id,
                                OriginalTransactionId = transId,
                                TradeId               = trade.Id,
                                TradePrice            = (decimal)trade.Price,
                                Volume        = trade.Units,
                                ServerTime    = trade.Time.FromOanda(),
                                SecurityId    = trade.Instrument.ToSecurityId(),
                                PortfolioName = GetPortfolioName(accountId),
                            });
                        }
                    }

                    if (count < 50)
                    {
                        break;
                    }
                }
            }
        }
		private void ProcessOrderStatusMessage()
		{
			foreach (var accountId in _accountIds.CachedValues)
			{
				int? maxOrderId = null;

				while (true)
				{
					var orders = _restClient.GetOrders(accountId, maxOrderId);

					var count = 0;

					foreach (var order in orders)
					{
						count++;

						maxOrderId = order.Id - 1;

						OandaOrderCondition condition = null;
						OrderTypes orderType;

						if (order.Type != "market" && order.Type != "limit")
						{
							orderType = OrderTypes.Conditional;

							condition = new OandaOrderCondition
							{
								IsMarket = order.Type == _orderImit,
								TakeProfitOffset = (decimal?)order.TakeProfit,
								StopLossOffset = (decimal?)order.StopLoss,
								UpperBound = (decimal?)order.UpperBound,
								LowerBound = (decimal?)order.LowerBound,
								TrailingStopLossOffset = order.TrailingStop,
							};
						}
						else
							orderType = order.Type == "market" ? OrderTypes.Market : OrderTypes.Limit;

						SendOutMessage(new ExecutionMessage
						{
							OrderType = orderType,
							ExecutionType = ExecutionTypes.Order,
							OrderId = order.Id,
							ServerTime = order.Time.FromOanda(),
							OrderPrice = (decimal)order.Price,
							Volume = order.Units,
							Side = order.Side.To<Sides>(),
							SecurityId = order.Instrument.ToSecurityId(),
							ExpiryDate = order.Expiry == null ? (DateTimeOffset?)null : order.Expiry.Value.FromOanda(),
							Condition = condition,
							PortfolioName = GetPortfolioName(accountId),
						});
					}

					if (count < 50)
						break;
				}

				int? maxTradeId = null;

				while (true)
				{
					var trades = _restClient.GetTrades(accountId, maxTradeId);

					var count = 0;

					foreach (var trade in trades)
					{
						count++;

						maxTradeId = trade.Id - 1;

						var takeProfit = Math.Abs(trade.TakeProfit) < 0.0000001 ? (decimal?)null : (decimal)trade.TakeProfit;
						var stopLoss = Math.Abs(trade.StopLoss) < 0.0000001 ? (decimal?)null : (decimal)trade.StopLoss;
						var tralingStop = trade.TrailingStop == 0 ? (int?)null : trade.TrailingStop;

						var isConditional = takeProfit != null || stopLoss != null || tralingStop != null;

						var transId = TransactionIdGenerator.GetNextId();

						SendOutMessage(new ExecutionMessage
						{
							ExecutionType = ExecutionTypes.Order,
							OrderType = isConditional ? OrderTypes.Conditional : OrderTypes.Limit,
							OrderId = trade.Id,
							OriginalTransactionId = transId,
							ServerTime = trade.Time.FromOanda(),
							OrderPrice = (decimal)trade.Price,
							Volume = trade.Units,
							Balance = 0,
							Side = trade.Side.To<Sides>(),
							SecurityId = trade.Instrument.ToSecurityId(),
							OrderState = isConditional ? OrderStates.Active : OrderStates.Done,
							PortfolioName = GetPortfolioName(accountId),
							Condition = isConditional ? new OandaOrderCondition
							{
								TakeProfitOffset = takeProfit,
								StopLossOffset = stopLoss,
								TrailingStopLossOffset = tralingStop,
							} : null,
						});

						if (!isConditional)
						{
							SendOutMessage(new ExecutionMessage
							{
								ExecutionType = ExecutionTypes.Trade,
								OrderId = trade.Id,
								OriginalTransactionId = transId,
								TradeId = trade.Id,
								TradePrice = (decimal)trade.Price,
								Volume = trade.Units,
								ServerTime = trade.Time.FromOanda(),
								SecurityId = trade.Instrument.ToSecurityId(),
								PortfolioName = GetPortfolioName(accountId),
							});
						}
					}

					if (count < 50)
						break;
				}
			}
		}