/// <summary> /// Translate the enum value to an actual server base path /// </summary> /// <param name="connectionType">The connection type enum to translate</param> /// <returns>The server URL for the connection type</returns> public static string ToBasePath(this OandaConnectionType connectionType) { return(connectionType switch { OandaConnectionType.FxTrade => "https://api-fxtrade.oanda.com/v3", _ => "https://api-fxpractice.oanda.com/v3", });
/// <summary> /// OandaApiConnection constuctor /// Sets up the configuration and the datetime format /// </summary> /// <param name="connectionType">Type of the connection</param> /// <param name="accessToken">Access token for the connection</param> /// <param name="dateTimeFormat">Date time format to use for the connection</param> public OandaApiConnection(OandaConnectionType connectionType, string accessToken, DateTimeFormat dateTimeFormat = DateTimeFormat.RFC3339) { // Init configuration Configuration = new Configuration() { UserAgent = $"OandaDotnetSdk/{Assembly.GetExecutingAssembly().GetName().Version}", BasePath = connectionType.ToBasePath(), AccessToken = accessToken }; // Init date time format DateTimeFormat = dateTimeFormat; // Init actual Oanda API connections from the GeriRemenyi.Oanda.V20.Client AccountApi = new AccountApi(Configuration); InstrumentApi = new InstrumentApi(Configuration); OrderApi = new OrderApi(Configuration); TradeApi = new TradeApi(Configuration); PositionApi = new PositionApi(Configuration); TransactionApi = new TransactionApi(Configuration); PricingApi = new PricingApi(Configuration); // Init object caches _accountsCache = new Dictionary <string, IAccount>(); _instrumentCache = new Dictionary <InstrumentName, IInstrument>(); // Fire a really small request synchronously to check that the inited connection is OK // Also fill the available accounts based on this try { Accounts = AccountApi.GetAccounts().Accounts; } catch { throw new ConnectionInitializationException($"Unable to connect to {Configuration.BasePath} with the given token"); } }
/// <summary> /// Create a new Oanda API connection /// </summary> /// <param name="connectionType">Type of the connection to create</param> /// <param name="token">Token to use for the connection</param> /// <param name="dateTimeFormat">Date time format to use for the connection</param> public IOandaApiConnection CreateConnection(OandaConnectionType connectionType, string token, DateTimeFormat dateTimeFormat = DateTimeFormat.RFC3339) { return(new OandaApiConnection(connectionType, token, dateTimeFormat)); }