Skip to content

Commit

Permalink
Handle too many requests exception from market source
Browse files Browse the repository at this point in the history
  • Loading branch information
bleunguts committed Jan 27, 2024
1 parent 2548d1a commit ba22dd6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
32 changes: 20 additions & 12 deletions ProjectX.GatewayAPI/Controllers/BacktestServiceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,25 @@ public async Task<IEnumerable<StrategyChartData>> Get(string ticker, DateTime fr
bool isReinvest = false;
MovingAverageImpl movingAverageImpl = MovingAverageImpl.BollingerBandsImpl;

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 data;
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 data;
}
catch(Exception exp)
{
_logger.LogError($"Cannot GetLongShort Strategy for {ticker}, {fromDate} {toDate} {notional}, Reason: {exp.Message}");
throw;
}
}
}
7 changes: 6 additions & 1 deletion Web/src/TradingStrategyStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ export class TradingStrategyStore
runInAction(() => {
this.transport
.fetchLongShortStrategy(symbol)
.then((res) => this.data = ((res as AxiosResponse<never, never>).data as ChartData[]));
.then((res) => this.data = ((res as AxiosResponse<never, never>).data as ChartData[]))
.catch((e) => {
console.log(`Error occurred whilst loading new symbol... ${e}`);
this.symbol = `ERROR LOADING SYMBOL..`;
this.data = FakeStrategyPlaceholder;
});

console.log(`Symbol: ${symbol} loaded, data length: ${this.data.length}`);
})
Expand Down
17 changes: 14 additions & 3 deletions Web/src/components/layout/StockNews.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,34 @@ export default function StockNews() {
const handleDoubleClick = () => {
setStockSymbol("");
}

function prettify(changesPercentage: string): string {
return changesPercentage.includes('.') ?
changesPercentage.substring(0, changesPercentage.indexOf('.') + 2)
:
changesPercentage;
}

const stockSymbols: Array<string> = [
'AAPL',
'IBM',
'BTCUSD'
];

return (
<>
<Grid container direction="row" spacing={1.5} alignItems="center" justifyContent="flex-end" alignContent='center'>
<Grid item><Typography variant="h6" align="center" gutterBottom>Stock Symbol:</Typography></Grid>
<Grid item>
<input id="stockSymbol" type="text" size={6} disabled={false} value={stockSymbol} onChange={handleStockSymbolChange} list="datalist" onDoubleClick={handleDoubleClick}/>
<datalist id="datalist">
<option value="AAPL">AAPL</option>
<option value="IBM">IBM</option>
<option value="BTCUSD">BTCUSD</option>
{
stockSymbols.map(function(s) {
return (
<option value={s}>{s}</option>
)
})
}
</datalist>
</Grid>
<Grid item><Button disabled={false} onClick={handleStrategize}>STRATEGIZE</Button></Grid>
Expand Down

0 comments on commit ba22dd6

Please sign in to comment.