Exemple #1
0
        public OrderAccessor(AccountSettings _settings)
        {
            this.accountSettings = _settings;
            var         client  = new RestClient(string.Format("{0}{1}", accountSettings.BASEURI, EndPoints.order(accountSettings.ID)));
            RestRequest request = new RestRequest()
            {
                Method = Method.GET
            };

            request.AddHeader("Authorization", string.Format("Bearer {0}", accountSettings.APIKEY));
            request.RequestFormat = DataFormat.Json;
            IRestResponse _response = client.Execute(request);

            /*
             * Console.ForegroundColor = ConsoleColor.Green;
             * Console.WriteLine("Order Status:{0}", _response.StatusCode);
             * Console.ResetColor();
             */
        }
Exemple #2
0
        public OandaRootOrderObject SubmitOrder(string _instrument, decimal _profitTarget, decimal _stopTarget, decimal _positionSize)
        {
            //market order

            var         clientMarket  = new RestClient(string.Format("{0}{1}", accountSettings.BASEURI, EndPoints.order(accountSettings.ID)));
            RestRequest marketRequest = new RestRequest()
            {
                Method = Method.POST
            };

            marketRequest.AddHeader("Authorization", string.Format("Bearer {0}", accountSettings.APIKEY));
            marketRequest.AddHeader("Content-Type", "application/json");
            marketRequest.RequestFormat = DataFormat.Json;
            this.marketOrder            = new PostOrder();
            this.marketOrder.order      = new Order()
            {
                instrument = _instrument, units = _positionSize, type = "MARKET", timeInForce = "FOK", triggerCondition = "DEFAULT"
            };
            marketRequest.AddBody(this.marketOrder);
            IRestResponse <OandaRootOrderObject> marketResponse = clientMarket.Execute <OandaRootOrderObject>(marketRequest);

            var transactionID = marketResponse.Data.lastTransactionID;

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Market Order ID:{0}", transactionID);
            Console.ResetColor();
            //see oanda documentation http://developer.oanda.com/rest-live-v20/order-df/

            //stop loss

            var         clientStopLoss  = new RestClient(string.Format("{0}{1}", accountSettings.BASEURI, EndPoints.order(accountSettings.ID)));
            RestRequest stopLossRequest = new RestRequest()
            {
                Method = Method.POST
            };

            stopLossRequest.AddHeader("Authorization", string.Format("Bearer {0}", accountSettings.APIKEY));
            stopLossRequest.AddHeader("Content-Type", "application/json");
            stopLossRequest.RequestFormat = DataFormat.Json;
            this.stopLoss = new PostOrder();

            this.stopLoss.order = new Order()
            {
                tradeID = transactionID, price = Convert.ToString(_stopTarget), type = "STOP_LOSS", timeInForce = "GTC", triggerCondition = "DEFAULT"
            };
            stopLossRequest.AddBody(this.stopLoss);
            IRestResponse <OandaRootOrderObject> stopLossResponse = clientStopLoss.Execute <OandaRootOrderObject>(stopLossRequest);

            //take profit
            var         clientTakeProfit  = new RestClient(string.Format("{0}{1}", accountSettings.BASEURI, EndPoints.order(accountSettings.ID)));
            RestRequest takeProfitRequest = new RestRequest()
            {
                Method = Method.POST
            };

            takeProfitRequest.AddHeader("Authorization", string.Format("Bearer {0}", accountSettings.APIKEY));
            takeProfitRequest.AddHeader("Content-Type", "application/json");
            takeProfitRequest.RequestFormat = DataFormat.Json;
            this.takeProfit       = new PostOrder();
            this.takeProfit.order = new Order()
            {
                tradeID = transactionID, price = Convert.ToString(_profitTarget), type = "TAKE_PROFIT", timeInForce = "GTC", triggerCondition = "DEFAULT"
            };
            takeProfitRequest.AddBody(this.takeProfit);
            IRestResponse <OandaRootOrderObject> takeProfitResponse = clientTakeProfit.Execute <OandaRootOrderObject>(takeProfitRequest);


            return(marketResponse.Data);
        }
        public void OnUpdate(object _sender, ElapsedEventArgs _e)
        {
            var         client  = new RestClient(string.Format("{0}{01}/{02}", accountSettings.BASEURI, EndPoints.accounts(), accountSettings.ID));
            RestRequest request = new RestRequest()
            {
                Method = Method.GET
            };

            request.AddHeader("Authorization", string.Format("Bearer {0}", accountSettings.APIKEY));
            request.RequestFormat = DataFormat.Json;
            IRestResponse <OandaRootAccountObject> _response = client.Execute <OandaRootAccountObject>(request);

            this.data     = _response.Data;
            this.response = _response;

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Current Balance:{0}", data.account.balance);
            Console.WriteLine("Current system time:{0}", DateTime.Now);
            Console.ResetColor();
        }
        public AccountAccessor(AccountSettings _accountSettings)
        {
            this.accountSettings = _accountSettings;

            this.startTime = DateTime.Now;

            var         client  = new RestClient(string.Format("{0}{01}/{02}", accountSettings.BASEURI, EndPoints.accounts(), accountSettings.ID));
            RestRequest request = new RestRequest()
            {
                Method = Method.GET
            };

            request.AddHeader("Authorization", string.Format("Bearer {0}", accountSettings.APIKEY));
            request.RequestFormat = DataFormat.Json;
            IRestResponse <OandaRootAccountObject> _response = client.Execute <OandaRootAccountObject>(request);

            this.response = _response;
            data          = _response.Data;
            //Program.mainTimer.Elapsed += this.OnUpdate;

            /*
             * Console.ForegroundColor = ConsoleColor.Green;
             * Console.WriteLine("Account Status:{0}", this.response.StatusCode);
             * Console.WriteLine("Start  time:{0}",startTime);
             * Console.WriteLine("Starting Balance:{0}", data.account.balance);
             * Console.ResetColor();
             */
        }
Exemple #5
0
        public InstrumentAccessor(AccountSettings _accountSettings, string _currencyPair)
        {
            this.currencyPair    = _currencyPair;
            this.accountSettings = _accountSettings;
            var         client  = new RestClient(string.Format("{0}{1}/{2}/candles", accountSettings.BASEURI, EndPoints.instruments(), this.currencyPair));
            RestRequest request = new RestRequest()
            {
                Method = Method.GET
            };

            request.AddHeader("Authorization", string.Format("Bearer {0}", accountSettings.APIKEY));
            request.AddParameter("count", this.period);
            request.AddParameter("granularity", this.timeFrame);  //5 minute chart
            request.RequestFormat = DataFormat.Json;
            IRestResponse <OandaRootInstrumentObject> _response = client.Execute <OandaRootInstrumentObject>(request);

            //this.data = _response.Data;  //set public field to deserialized data
            this.bar     = _response.Data;
            this.allBars = new List <OandaRootInstrumentObject>();
            this.allBars.Add(new OandaRootInstrumentObject {
                instrument = this.currencyPair, granularity = this.timeFrame, candles = new List <Candle>()
            });
            this.allBars[0].candles.Add(this.bar.candles[0]);

            this.response = _response;


            //Program.mainTimer.Elapsed += this.OnUpdate;  //add Onupdate method to timer invocation list

            /*
             * Console.ForegroundColor = ConsoleColor.Green;
             * Console.WriteLine("Instrument Status:{0}", _response.StatusCode);
             * Console.WriteLine("Using {0} currency pair", this.currencyPair);
             * Console.ResetColor();
             */
        }
Exemple #6
0
        private OandaRootInstrumentObject  OandaGetBars()
        //get bars from Oanda endpoint
        {
            var         client  = new RestClient(string.Format("{0}{1}/{2}/candles", accountSettings.BASEURI, EndPoints.instruments(), this.currencyPair));
            RestRequest request = new RestRequest()
            {
                Method = Method.GET
            };

            request.AddHeader("Authorization", string.Format("Bearer {0}", accountSettings.APIKEY));
            request.AddParameter("count", this.period);
            request.AddParameter("granularity", this.timeFrame);
            request.RequestFormat = DataFormat.Json;
            IRestResponse <OandaRootInstrumentObject> _response = client.Execute <OandaRootInstrumentObject>(request);

            if (_response.Data.candles.Count() == 0)
            {
                throw new ProgramException("no bars returned from end point");
            }

            return(_response.Data);
        }
Exemple #7
0
        private OandaRootInstrumentObject OandaGetBars(DateTime _startTime, DateTime _endTime)
        {
            //get bars time range from Oanda endpoint
            {
                //really we have to convert to unix epoch BS?
                DateTimeOffset startOffset = new DateTimeOffset(_startTime);
                DateTimeOffset endOffset   = new DateTimeOffset(_endTime);

                var         client  = new RestClient(string.Format("{0}{1}/{2}/candles", accountSettings.BASEURI, EndPoints.instruments(), this.currencyPair));
                RestRequest request = new RestRequest()
                {
                    Method = Method.GET
                };
                request.AddHeader("Authorization", string.Format("Bearer {0}", accountSettings.APIKEY));
                request.RequestFormat = DataFormat.Json;
                request.AddParameter("from", startOffset.ToUnixTimeSeconds());
                request.AddParameter("to", endOffset.ToUnixTimeSeconds());
                request.AddParameter("granularity", this.timeFrame);

                IRestResponse <OandaRootInstrumentObject> _response = client.Execute <OandaRootInstrumentObject>(request);
                if (_response.Data.candles.Count() == 0)
                {
                    throw new ProgramException(string.Format("no bars returned from end point:{0}", _startTime.ToString()));
                }

                return(_response.Data);
            }
        }