Skip to content

Commit

Permalink
Tidy up LongShortStrategyChartData endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bleunguts committed Jan 28, 2024
1 parent 499b6da commit 260258f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
21 changes: 10 additions & 11 deletions ProjectX.GatewayAPI/Controllers/BacktestServiceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace ProjectX.GatewayAPI.Controllers;

[ApiController]
[Route("[controller]")]
public class BacktestServiceController
public class BacktestServiceController : ControllerBase
{
private readonly ILogger<BacktestServiceController> _logger;
private readonly IStockSignalService _stockSignalService;
Expand All @@ -32,36 +32,35 @@ public string Get()
return "Hello from BacktestServiceController";
}

[HttpGet("LongShortStrategy")]
public async Task<Results<NoContent, NotFound, Ok<IEnumerable<StrategyChartData>>>> GetAsync(string ticker, DateTime fromDate, DateTime toDate, double notional)
{
[HttpGet("LongShortStrategyChartData")]
public async Task<ActionResult<IEnumerable<StrategyChartData>>> ComputeLongShortPnlStrategy(string ticker, DateTime fromDate, DateTime toDate, double notional, MovingAverageImpl movingAverageImpl = MovingAverageImpl.BollingerBandsImpl)
{
bool isReinvest = false;
MovingAverageImpl movingAverageImpl = MovingAverageImpl.BollingerBandsImpl;

try
{
var marketPrices = await _stockMarketSource.GetPrices(ticker, fromDate, toDate);
var pnlRanking = _backtestService.ComputeLongShortPnlFull(marketPrices, notional, new TradingStrategy(TradingStrategyType.MeanReversion, isReinvest));

var maximumProfitStrategy = pnlRanking.OrderByDescending(p => p.pnlCum).First();

int movingWindow = maximumProfitStrategy.movingWindow;
double signalIn = maximumProfitStrategy.zin;
double signalOut = maximumProfitStrategy.zout;

var smoothenedSignals = await _stockSignalService.GetSignalUsingMovingAverageByDefault(ticker, fromDate, toDate, movingWindow, movingAverageImpl);
List<StrategyPnl> pnlForMaximumProfit = _backtestService.ComputeLongShortPnl(smoothenedSignals, notional, signalIn, signalOut, new TradingStrategy(TradingStrategyType.MeanReversion, isReinvest)).ToList();
var data = pnlForMaximumProfit.Select(p => new StrategyChartData(p.Date.ToString("ddMMyy"), p.PnLCum, p.PnLCumHold));
return TypedResults.Ok(data);
return Ok(data);
}
catch(Exception e) when (e.Message.Contains("TooManyRequests"))
{
_logger.LogError($"Cannot GetLongShort Strategy for {ticker}, {fromDate} {toDate} {notional}, Reason: {e.Message}");
return TypedResults.NoContent();
_logger.LogError($"Error executing LongShortStrategy for {ticker}, {fromDate} {toDate} {notional}, Reason: {e.Message}");
return NoContent();
}
catch (Exception e)
{
_logger.LogError($"Cannot GetLongShort Strategy for {ticker}, {fromDate} {toDate} {notional}, Reason: {e.Message}");
return TypedResults.NotFound();
_logger.LogError($"CannotError executing LongShortStrategy for {ticker}, {fromDate} {toDate} {notional}, Reason: {e.Message}");
return NotFound();
}
}
}
2 changes: 1 addition & 1 deletion Web/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const api: BackenedApi = {
return await axios.get(endpoint);
},
fetchLongShortStrategy: async (ticker: string) => {
const endpoint = `${backendServer}/BacktestService/LongShortStrategy?ticker=${ticker}&fromDate=2023-05-01&toDate=2023-09-25&notional=10000`;
const endpoint = `${backendServer}/BacktestService/LongShortStrategyChartData?ticker=${ticker}&fromDate=2023-05-01&toDate=2023-09-25&notional=10000`;
console.log(`Fetching from ${endpoint}`);
return await axios.get(endpoint);
},
Expand Down

0 comments on commit 260258f

Please sign in to comment.