Skip to content

Commit

Permalink
Added LongShortStrategy Endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bleunguts committed Jan 27, 2024
1 parent 6ce28ce commit cb95b50
Show file tree
Hide file tree
Showing 5 changed files with 885 additions and 1 deletion.
9 changes: 9 additions & 0 deletions ProjectX.Core/Strategy/StrategyChartData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectX.Core.Strategy;

public record StrategyChartData(string time, double amount, double amountHold);
46 changes: 46 additions & 0 deletions ProjectX.GatewayAPI/Controllers/BacktestServiceController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.OpenApi.Any;
using Newtonsoft.Json;
using ProjectX.Core.Services;
using ProjectX.Core.Strategy;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace ProjectX.GatewayAPI.Controllers;

[ApiController]
[Route("[controller]")]
public class BacktestServiceController
{
private readonly ILogger<BacktestServiceController> _logger;
private readonly IStockSignalService _stockSignalService;
private readonly IBacktestService _backtestService;

public BacktestServiceController(ILogger<BacktestServiceController> logger, IStockSignalService stockSignalService, IBacktestService backtestService)
{
this._logger = logger;
this._stockSignalService = stockSignalService;
this._backtestService = backtestService;
}

[HttpGet]
public string Get()
{
return "Hello from BacktestServiceController";
}

[HttpGet("LongShortStrategy")]
public async Task<IEnumerable<StrategyChartData>> Get(string ticker, DateTime fromDate, DateTime toDate, double notional)
{
int movingWindow = 10;
double signalIn = 0.5;
double signalOut = 0.4;

bool isReinvest = false;
MovingAverageImpl movingAverageImpl = MovingAverageImpl.BollingerBandsImpl;

var smoothenedSignals = await _stockSignalService.GetSignalUsingMovingAverageByDefault(ticker, fromDate, toDate, movingWindow, movingAverageImpl);
List<StrategyPnl> pnls = _backtestService.ComputeLongShortPnl(smoothenedSignals, notional, signalIn, signalOut, new TradingStrategy(TradingStrategyType.MeanReversion, isReinvest)).ToList();
var data = pnls.Select(p => new StrategyChartData(p.Date.ToString("ddMMyy"), p.PnLCum, p.PnLCumHold));
return data;
}
}
2 changes: 2 additions & 0 deletions ProjectX.GatewayAPI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public void ConfigureServices(IServiceCollection services)
new FMPStockMarketSource(),
Options.Create(new FileBackedStoreMarketDataSourceOptions() { Filename = "cache.json"}))
);
services.AddSingleton<IStockSignalService, StockSignalService>();
services.AddSingleton<IBacktestService, BacktestService>();

services.AddOptions();
services.Configure<ApiClientOptions>(options => options.BaseAddress = Configuration.GetSection("ExternalServices")["ProjectXUrl"]);
Expand Down
11 changes: 10 additions & 1 deletion ProjectX.GatewayAPI/ProjectX.GatewayAPI.http
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Content-Type: application/json
{"timeSlices":10,"optionType":0,"spot":100.0,"strike":150.0,"rate":1.0,"carry":1.0,"vol":0.3,"CalculatorType":2}
###

POST {{GatewayAPI_HostAddress}}/Pricingtasks/bsPlot
POST {{GatewayAPI_HostAddress}}/PricingTasks/bsPlot
Content-Type: application/json

{
Expand All @@ -34,4 +34,13 @@ Content-Type: application/json
"ZTickDecimalPlaces": 0,
"CalculatorType": 0
}
###
GET {{GatewayAPI_HostAddress}}/BacktestService
Accept: application/json

###

GET {{GatewayAPI_HostAddress}}/BacktestService/LongShortStrategy?ticker=AAPL&fromDate=2023-05-01&toDate=2023-09-25&notional=10000
Accept: application/json

###

0 comments on commit cb95b50

Please sign in to comment.