Skip to content

Instantly share code, notes, and snippets.

@tomas-rampas
Last active June 9, 2022 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomas-rampas/086101c42141fee6907d9ab5c8d2f1e1 to your computer and use it in GitHub Desktop.
Save tomas-rampas/086101c42141fee6907d9ab5c8d2f1e1 to your computer and use it in GitHub Desktop.
Oanda API sample calls Accounts, Products, and Streamer - quick sketchy sample
using System;
using System.IO;
using System.Net;
using System.Text.Json;
using System.Threading;
namespace OandaStreamingConsole
{
class Program
{
static string token = "Bearer ";
static string oanda_account_id = "";
static void Main(string[] args)
{
token = $"{token}{Environment.GetEnvironmentVariable("oanda_token")}";
oanda_account_id = Environment.GetEnvironmentVariable("oanda_account_id");
GetAccounts();
Thread.Sleep(2000);
GetProducts();
Thread.Sleep(2000);
PriceStreamer();
}
static void PriceStreamer()
{
using WebClient client = new();
client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Headers.Add(HttpRequestHeader.Authorization, token);
client.OpenReadCompleted += (_, eventArgs) =>
{
using StreamReader reader = new(eventArgs.Result);
try
{
while (!reader.EndOfStream)
{
Console.Out.WriteLine(FormatJson(reader.ReadLine()));
}
}
catch (IOException e)
{
Console.Error.WriteLine(e.Message);
}
};
var url =
$"https://stream-fxtrade.oanda.com/v3/accounts/{oanda_account_id}/pricing/stream?" +
"instruments=USD_CAD%2CWTICO_USD";
client.OpenReadAsync(new Uri(url));
Console.ReadLine();
}
static void GetAccounts()
{
const string url = "https://api-fxtrade.oanda.com/v3/accounts";
string accountsJson;
var request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add(HttpRequestHeader.Authorization, token);
request.Method = "GET";
request.AutomaticDecompression = DecompressionMethods.GZip;
using (var response = (HttpWebResponse)request.GetResponse())
using (var stream = response.GetResponseStream())
using (StreamReader reader = new(stream))
{
accountsJson = reader.ReadToEnd();
}
Console.WriteLine(FormatJson(accountsJson));
}
static void GetProducts()
{
string url = $"https://api-fxtrade.oanda.com/v3/accounts/{oanda_account_id}/instruments";
string productsJson = "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add(HttpRequestHeader.Authorization, token);
request.Method = "GET";
request.AutomaticDecompression = DecompressionMethods.GZip;
using (var response = (HttpWebResponse)request.GetResponse())
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
productsJson = reader.ReadToEnd();
}
Console.WriteLine(FormatJson(productsJson));
}
static string FormatJson(string json)
{
using var jDoc = JsonDocument.Parse(json);
return JsonSerializer.Serialize(jDoc, new JsonSerializerOptions { WriteIndented = true });
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment