Ejemplo n.º 1
0
        /// <summary>
        /// execute orders in the exchange
        /// исполнить ордер на бирже
        /// </summary>
        public async void ExecuteOrder(Order order)
        {
            if (_isConnected == false)
            {
                return;
            }

            _orders.Add(order);

            try
            {
                string expiry = ConvertDateTimeToAcceptDateFormat(DateTime.Now.AddMonths(1));

                decimal volume = order.Volume;

                if (order.Side == Side.Sell)
                {
                    volume = -order.Volume;
                }
                string price = order.Price.ToString(new CultureInfo("en-US"));

                Instrument myInstrument = _allInstruments.Find(inst => inst.name == order.SecurityNameCode);

                // create new pending order
                var request = new LimitOrderRequest(myInstrument)
                {
                    instrument  = order.SecurityNameCode,
                    units       = Convert.ToInt64(volume),
                    timeInForce = TimeInForce.GoodUntilDate,
                    gtdTime     = expiry,
                    price       = price.ToDecimal(),

                    clientExtensions = new ClientExtensions()
                    {
                        id      = order.NumberUser.ToString(),
                        comment = "",
                        tag     = ""
                    },
                    tradeClientExtensions = new ClientExtensions()
                    {
                        id      = order.NumberUser.ToString(),
                        comment = "",
                        tag     = ""
                    }
                };

                var response = await Rest20.PostOrderAsync(Credentials.GetDefaultCredentials().DefaultAccountId, request);

                var orderTransaction = response.orderCreateTransaction;

                if (orderTransaction.id > 0)
                {
                    order.NumberMarket = orderTransaction.id.ToString();
                }
            }
            catch (Exception error)
            {
                SendLogMessage(error.ToString(), LogMessageType.Error);
            }
        }
Ejemplo n.º 2
0
        static async Task <long?> PlaceMarketOrder(string side = "buy")
        {
            WriteNewLine("Creating a EUR_USD market BUY order ...");

            var  oandaInstrument = (await Rest20.GetAccountInstrumentsAsync(AccountID, INSTRUMENT)).First();
            long orderUnits      = side == "buy" ? 10 : -10;

            var request = new MarketOrderRequest(oandaInstrument)
            {
                units = orderUnits
            };

            OrderPostResponse response = null;

            try
            {
                response = await Rest20.PostOrderAsync(AccountID, request);

                WriteNewLine("Congrats! You've put on a trade! Let it run! :)");
            }
            catch
            {
                WriteNewLine("Oops. Order creation failed.");
            }

            return(response?.orderFillTransaction?.tradeOpened?.tradeID);
        }
Ejemplo n.º 3
0
        public async Task <long> CreateMarketOrder(string accountId, string instrument, long units)
        {
            MarketOrderRequest request = new MarketOrderRequest()
            {
                instrument = instrument,
                units      = units,
            };
            OrderPostResponse response = await Rest20.PostOrderAsync(accountId, request);

            return(response.orderFillTransaction.id);
        }
Ejemplo n.º 4
0
        static async Task <long?> PlaceMarketOrder(string side = "buy")
        {
            WriteNewLine("Creating a EUR_USD market BUY order ...");

            var parameters = new AccountInstrumentsParameters()
            {
                instruments = new List <string>()
                {
                    INSTRUMENT
                }
            };
            var     oandaInstrument = (await Rest20.GetAccountInstrumentsAsync(AccountID, parameters)).First();
            decimal orderUnits      = side == "buy" ? 10 : -10;

            var request = new MarketOrderRequest(oandaInstrument)
            {
                units = orderUnits
            };

            PostOrderResponse response = null;

            try
            {
                response = await Rest20.PostOrderAsync(AccountID, request);

                WriteNewLine("Congrats! You've put on a trade! Let it run! :)");
            }
            catch (Exception ex)
            {
                var errorResponse = ErrorResponseFactory.Create(ex.Message);

                WriteNewLine("Oops. Order creation failed.");
                WriteNewLine($"The failure message is: {errorResponse.errorMessage}.");
                WriteNewLine("Try again later.");
            }

            return(response?.orderFillTransaction?.tradeOpened?.tradeID);
        }