Refactor signal generation in TradingService to process indicators individually, improving clarity and performance. Add new API types for refining indicators, including request and response structures.
This commit is contained in:
@@ -580,7 +580,7 @@ public class TradingService : ITradingService
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Generates signals for a date range using a rolling window approach.
|
/// Generates signals for a date range by running each indicator individually and collecting all signals.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private List<LightSignal> GenerateSignalsForDateRange(
|
private List<LightSignal> GenerateSignalsForDateRange(
|
||||||
List<Candle> candles,
|
List<Candle> candles,
|
||||||
@@ -588,47 +588,47 @@ public class TradingService : ITradingService
|
|||||||
Dictionary<IndicatorType, IndicatorsResultBase> preCalculatedIndicatorValues)
|
Dictionary<IndicatorType, IndicatorsResultBase> preCalculatedIndicatorValues)
|
||||||
{
|
{
|
||||||
var allSignals = new List<LightSignal>();
|
var allSignals = new List<LightSignal>();
|
||||||
var previousSignals = new Dictionary<string, LightSignal>();
|
|
||||||
var lightScenario = LightScenario.FromScenario(scenario);
|
var lightScenario = LightScenario.FromScenario(scenario);
|
||||||
|
|
||||||
// Use rolling window approach similar to backtests
|
// Process each indicator individually to get all signals from each
|
||||||
const int RollingWindowSize = 600;
|
foreach (var lightIndicator in lightScenario.Indicators)
|
||||||
var rollingWindowCandles = new List<Candle>(RollingWindowSize);
|
|
||||||
|
|
||||||
foreach (var candle in candles)
|
|
||||||
{
|
{
|
||||||
// Maintain rolling window
|
// Build the indicator instance
|
||||||
if (rollingWindowCandles.Count >= RollingWindowSize)
|
var indicatorInstance = lightIndicator.ToInterface();
|
||||||
{
|
|
||||||
rollingWindowCandles.RemoveAt(0);
|
|
||||||
}
|
|
||||||
rollingWindowCandles.Add(candle);
|
|
||||||
|
|
||||||
// Only process if we have enough candles for indicators
|
// Use pre-calculated indicator values if available
|
||||||
if (rollingWindowCandles.Count < 2)
|
List<LightSignal> indicatorSignals;
|
||||||
|
if (preCalculatedIndicatorValues != null && preCalculatedIndicatorValues.ContainsKey(lightIndicator.Type))
|
||||||
{
|
{
|
||||||
continue;
|
// Use pre-calculated values to avoid recalculating indicators
|
||||||
|
indicatorSignals = indicatorInstance.Run(candles, preCalculatedIndicatorValues[lightIndicator.Type]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Normal path: calculate indicators on the fly
|
||||||
|
indicatorSignals = indicatorInstance.Run(candles);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate signal for current position
|
// Add all signals from this indicator to the collection
|
||||||
var signal = TradingBox.GetSignal(
|
if (indicatorSignals != null && indicatorSignals.Count > 0)
|
||||||
rollingWindowCandles,
|
|
||||||
lightScenario,
|
|
||||||
previousSignals,
|
|
||||||
scenario.LookbackPeriod,
|
|
||||||
preCalculatedIndicatorValues);
|
|
||||||
|
|
||||||
if (signal != null)
|
|
||||||
{
|
{
|
||||||
// Check if this is a new signal (not already in our collection)
|
// Filter signals to only include those within the candle date range
|
||||||
if (!previousSignals.ContainsKey(signal.Identifier))
|
var firstCandleDate = candles.First().Date;
|
||||||
{
|
var lastCandleDate = candles.Last().Date;
|
||||||
allSignals.Add(signal);
|
|
||||||
previousSignals[signal.Identifier] = signal;
|
var filteredSignals = indicatorSignals
|
||||||
}
|
.Where(s => s.Date >= firstCandleDate && s.Date <= lastCandleDate)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
allSignals.AddRange(filteredSignals);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return allSignals.OrderBy(s => s.Date).ToList();
|
// Remove duplicates based on identifier and sort by date
|
||||||
|
return allSignals
|
||||||
|
.GroupBy(s => s.Identifier)
|
||||||
|
.Select(g => g.First())
|
||||||
|
.OrderBy(s => s.Date)
|
||||||
|
.ToList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1469,6 +1469,19 @@ export interface IndicatorRequestDto {
|
|||||||
requesterName: string;
|
requesterName: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface RefineIndicatorsResponse {
|
||||||
|
indicatorsValues?: { [key in keyof typeof IndicatorType]?: IndicatorsResultBase; } | null;
|
||||||
|
signals?: LightSignal[] | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RefineIndicatorsRequest {
|
||||||
|
ticker: Ticker;
|
||||||
|
timeframe: Timeframe;
|
||||||
|
startDate: Date;
|
||||||
|
endDate: Date;
|
||||||
|
indicators: IndicatorRequest[];
|
||||||
|
}
|
||||||
|
|
||||||
export interface LoginRequest {
|
export interface LoginRequest {
|
||||||
name: string;
|
name: string;
|
||||||
address: string;
|
address: string;
|
||||||
|
|||||||
@@ -4137,6 +4137,45 @@ export class TradingClient extends AuthorizedApiBase {
|
|||||||
}
|
}
|
||||||
return Promise.resolve<FileResponse>(null as any);
|
return Promise.resolve<FileResponse>(null as any);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trading_RefineIndicators(request: RefineIndicatorsRequest): Promise<RefineIndicatorsResponse> {
|
||||||
|
let url_ = this.baseUrl + "/Trading/RefineIndicators";
|
||||||
|
url_ = url_.replace(/[?&]$/, "");
|
||||||
|
|
||||||
|
const content_ = JSON.stringify(request);
|
||||||
|
|
||||||
|
let options_: RequestInit = {
|
||||||
|
body: content_,
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Accept": "application/json"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return this.transformOptions(options_).then(transformedOptions_ => {
|
||||||
|
return this.http.fetch(url_, transformedOptions_);
|
||||||
|
}).then((_response: Response) => {
|
||||||
|
return this.processTrading_RefineIndicators(_response);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected processTrading_RefineIndicators(response: Response): Promise<RefineIndicatorsResponse> {
|
||||||
|
const status = response.status;
|
||||||
|
let _headers: any = {}; if (response.headers && response.headers.forEach) { response.headers.forEach((v: any, k: any) => _headers[k] = v); };
|
||||||
|
if (status === 200) {
|
||||||
|
return response.text().then((_responseText) => {
|
||||||
|
let result200: any = null;
|
||||||
|
result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as RefineIndicatorsResponse;
|
||||||
|
return result200;
|
||||||
|
});
|
||||||
|
} else if (status !== 200 && status !== 204) {
|
||||||
|
return response.text().then((_responseText) => {
|
||||||
|
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return Promise.resolve<RefineIndicatorsResponse>(null as any);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class UserClient extends AuthorizedApiBase {
|
export class UserClient extends AuthorizedApiBase {
|
||||||
@@ -5972,6 +6011,19 @@ export interface IndicatorRequestDto {
|
|||||||
requesterName: string;
|
requesterName: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface RefineIndicatorsResponse {
|
||||||
|
indicatorsValues?: { [key in keyof typeof IndicatorType]?: IndicatorsResultBase; } | null;
|
||||||
|
signals?: LightSignal[] | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RefineIndicatorsRequest {
|
||||||
|
ticker: Ticker;
|
||||||
|
timeframe: Timeframe;
|
||||||
|
startDate: Date;
|
||||||
|
endDate: Date;
|
||||||
|
indicators: IndicatorRequest[];
|
||||||
|
}
|
||||||
|
|
||||||
export interface LoginRequest {
|
export interface LoginRequest {
|
||||||
name: string;
|
name: string;
|
||||||
address: string;
|
address: string;
|
||||||
|
|||||||
@@ -1469,6 +1469,19 @@ export interface IndicatorRequestDto {
|
|||||||
requesterName: string;
|
requesterName: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface RefineIndicatorsResponse {
|
||||||
|
indicatorsValues?: { [key in keyof typeof IndicatorType]?: IndicatorsResultBase; } | null;
|
||||||
|
signals?: LightSignal[] | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RefineIndicatorsRequest {
|
||||||
|
ticker: Ticker;
|
||||||
|
timeframe: Timeframe;
|
||||||
|
startDate: Date;
|
||||||
|
endDate: Date;
|
||||||
|
indicators: IndicatorRequest[];
|
||||||
|
}
|
||||||
|
|
||||||
export interface LoginRequest {
|
export interface LoginRequest {
|
||||||
name: string;
|
name: string;
|
||||||
address: string;
|
address: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user