Skip to content

Commit

Permalink
FMP api key is retreved from env variable (Azure Key Vault)
Browse files Browse the repository at this point in the history
  • Loading branch information
bleunguts committed Feb 13, 2024
1 parent a6a78b1 commit 9af9608
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 9 deletions.
3 changes: 2 additions & 1 deletion ProjectX.GatewayAPI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
Console.WriteLine($"ASPNETCORE_HTTP_PORTS={Environment.GetEnvironmentVariable("ASPNETCORE_HTTP_PORTS")}");
Console.WriteLine($"ASPNETCORE_URLS={Environment.GetEnvironmentVariable("ASPNETCORE_URLS")}");
Console.WriteLine($"ASPNETCORE_ENVIRONMENT ={Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}");
Console.WriteLine($"fmpapikey ={Environment.GetEnvironmentVariable("fmpapikey")}");

var builder = WebApplication.CreateBuilder(args);

Expand Down Expand Up @@ -81,7 +82,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddSingleton<FXTasksChannel>();
services.TryAddScoped<IPricingTasksProcessor, PricingTasksProcessor>();
services.AddSingleton<IStockMarketSource>(new FileBackedStockMarketDataSource(
new FMPStockMarketSource(),
new FMPStockMarketSource(Options.Create(new FMPStockMarketSourceOptions() { ApiKey = FMPStockMarketSourceOptions.GetFromEnvironment()})),
Options.Create(new FileBackedStoreMarketDataSourceOptions() { Filename = Configuration.GetSection("MarketData")["CacheFilename"] }))
);
services.AddSingleton<IStockSignalService, StockSignalService>();
Expand Down
4 changes: 3 additions & 1 deletion ProjectX.MarketData.Cache.Tests/BacktestExternalTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public class BacktestExternalTest
const string ticker = "IBM";

private readonly BacktestService _backtestService = new();
private readonly FileBackedStockMarketDataSource _marketSource = new FileBackedStockMarketDataSource(new FMPStockMarketSource(), Options.Create<FileBackedStoreMarketDataSourceOptions>(new FileBackedStoreMarketDataSourceOptions { Filename = "externalTests.json" }));
private readonly FileBackedStockMarketDataSource _marketSource = new FileBackedStockMarketDataSource(
new FMPStockMarketSource(Options.Create<FMPStockMarketSourceOptions>(new FMPStockMarketSourceOptions() { ApiKey = FMPStockMarketSourceOptions.GetFromEnvironment()})),
Options.Create<FileBackedStoreMarketDataSourceOptions>(new FileBackedStoreMarketDataSourceOptions { Filename = "externalTests.json" }));
public record ChartData(string time, double amount, double amountHold);

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace ProjectX.MarketData.Tests
{
public class StockSignalServiceExternalTest
{
private static readonly FileBackedStockMarketDataSource _marketSource = new FileBackedStockMarketDataSource(new FMPStockMarketSource(), Options.Create<FileBackedStoreMarketDataSourceOptions>(new FileBackedStoreMarketDataSourceOptions { Filename = "externalTests.json" }));
private static readonly FileBackedStockMarketDataSource _marketSource = new FileBackedStockMarketDataSource(new FMPStockMarketSource(Options.Create<FMPStockMarketSourceOptions>(new FMPStockMarketSourceOptions() { ApiKey = FMPStockMarketSourceOptions.GetFromEnvironment() })), Options.Create<FileBackedStoreMarketDataSourceOptions>(new FileBackedStoreMarketDataSourceOptions { Filename = "externalTests.json" }));
private readonly StockSignalService _signalService = new StockSignalService(_marketSource);

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class NinjaTraderExternalTests
private Client _client = new Client();

[Test]
[Ignore("Requires Ninja Trader GUI to be running")]
public async Task SubscribeTo()
{
const string instrumentReceive = "BTCUSD";
Expand Down
7 changes: 4 additions & 3 deletions ProjectX.MarketData.Tests/FMPStockMarketSourceExternalTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using MatthiWare.FinancialModelingPrep.Model;
using MatthiWare.FinancialModelingPrep.Model.CompanyValuation;
using MatthiWare.FinancialModelingPrep.Model.StockTimeSeries;
using Microsoft.Extensions.Options;
using NuGet.Frameworks;
using ProjectX.Core;
using ProjectX.Core.Strategy;
Expand Down Expand Up @@ -37,7 +38,7 @@ public async Task ExternalFoo()
[Ignore("Tool")]
public async Task WhenGettingHistoricalPricesFromFMP()
{
var marketDataService = new FMPStockMarketSource();
var marketDataService = new FMPStockMarketSource(Options.Create<FMPStockMarketSourceOptions>(new FMPStockMarketSourceOptions() { ApiKey = FMPStockMarketSourceOptions.GetFromEnvironment() }));
var marketPrices = await marketDataService.GetPrices("ACAQ", new DateTime(2023, 1, 1), new DateTime(2023, 10, 1));
Assert.That(marketPrices.Count(), Is.GreaterThan(0));
foreach (var data in marketPrices)
Expand All @@ -52,7 +53,7 @@ public async Task WhenGettingHistoricalPricesFromFMP()
[Ignore("External Tool for testing only")]
public async Task WhenGettingStockMarketMostActiveFromFMP()
{
var marketDataService = new FMPStockMarketSource();
var marketDataService = new FMPStockMarketSource(Options.Create<FMPStockMarketSourceOptions>(new FMPStockMarketSourceOptions() { ApiKey = FMPStockMarketSourceOptions.GetFromEnvironment() }));
var mostActiveStocks = await marketDataService.GetMostActiveStocks();
Assert.That(mostActiveStocks.Count(), Is.GreaterThan(0));
foreach(var data in mostActiveStocks)
Expand All @@ -65,7 +66,7 @@ public async Task WhenGettingStockMarketMostActiveFromFMP()
[Ignore("Use to try out StockIndicator Api features")]
public async Task WhenGettingBollingerBandsFromFMQ()
{
var marketDataService = new FMPStockMarketSource();
var marketDataService = new FMPStockMarketSource(Options.Create<FMPStockMarketSourceOptions>(new FMPStockMarketSourceOptions() { ApiKey = FMPStockMarketSourceOptions.GetFromEnvironment() }));
var quotes = await marketDataService.GetQuote("ACAQ", new DateTime(2023, 1, 1), new DateTime(2023, 10, 1));
Assert.That(quotes.Count(), Is.GreaterThan(0));

Expand Down
6 changes: 3 additions & 3 deletions ProjectX.MarketData/FMPStockMarketSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
using Skender.Stock.Indicators;
using MatthiWare.FinancialModelingPrep.Model;
using MatthiWare.FinancialModelingPrep.Model.StockMarket;
using Microsoft.Extensions.Options;

namespace ProjectX.MarketData;

public class FMPStockMarketSource : IStockMarketSource, IRealStockMarketSource
{
private readonly IFinancialModelingPrepApiClient _api;

public FMPStockMarketSource()
public FMPStockMarketSource(IOptions<FMPStockMarketSourceOptions> options)
{
_api = FinancialModelingPrepApiClientFactory.CreateClient(new FinancialModelingPrepOptions()
{
ApiKey = "35fdfe7c1a0d49e6ca2283bb073fea3a"
ApiKey = options.Value.ApiKey
});
}

Expand Down
20 changes: 20 additions & 0 deletions ProjectX.MarketData/FMPStockMarketSourceOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace ProjectX.MarketData;

public class FMPStockMarketSourceOptions
{
public string ApiKey { get; set; }

public static string GetFromEnvironment()
{
var provider = new Microsoft.Extensions.Configuration.EnvironmentVariables.EnvironmentVariablesConfigurationProvider();
provider.Load();

string? apiKey = null;
if (!provider.TryGet("fmpapikey", out apiKey))
{
throw new Exception($"Blow up: cannot get compulsory market data api key from environment variable 'fmpapikey'");
}

return apiKey;
}
}
1 change: 1 addition & 0 deletions ProjectX.MarketData/ProjectX.MarketData.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<PackageReference Include="MatthiWare.FinancialModelingPrep" Version="0.2.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="System.Reactive" Version="6.0.0" />
</ItemGroup>

Expand Down

0 comments on commit 9af9608

Please sign in to comment.