Skip to content

Commit 8cc279a

Browse files
committed
Refactored tests to improve type safety and ensure proper disposal of CancellationTokenSource instances.
1 parent c5098ed commit 8cc279a

File tree

4 files changed

+49
-47
lines changed

4 files changed

+49
-47
lines changed

tests/Backtest.Net.Tests/EngineTests/EngineTestsV2.cs

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using Backtest.Net.SymbolDataSplitters;
66
using Backtest.Net.SymbolsData;
77
using Backtest.Net.Timeframes;
8-
using Backtest.Tests.EngineTests;
98

109
namespace Backtest.Net.Tests.EngineTests;
1110

@@ -41,7 +40,7 @@ public EngineTestsV2()
4140
/// <param name="symbolData"></param>
4241
protected async Task OnTickMethodV2(SymbolDataV2[] symbolData)
4342
{
44-
var signals = await Strategy.ExecuteV2(symbolData.ToList());
43+
List<IConditionParameter> signals = await Strategy.ExecuteV2(symbolData.ToList());
4544
if (signals.Count != 0)
4645
{
4746
_ = await Trade.Execute(signals);
@@ -57,7 +56,7 @@ public async Task TestRunningEngineWithoutExceptions()
5756
};
5857

5958
// --- Generate fake SymbolData splitter
60-
var data = GenerateSymbolDataList(new DateTime(2023, 1, 1), 500, 1, WarmupCandlesCount);
59+
List<List<SymbolDataV2>> data = GenerateSymbolDataList(new DateTime(2023, 1, 1), 500, 1, WarmupCandlesCount);
6160

6261
await EngineV2.RunAsync(data);
6362

@@ -67,11 +66,12 @@ public async Task TestRunningEngineWithoutExceptions()
6766
[Fact]
6867
public async Task TestCancellationToken()
6968
{
70-
var tokenSource = new CancellationTokenSource();
69+
using var tokenSource = new CancellationTokenSource();
70+
CancellationTokenSource cts = tokenSource;
7171

7272
Strategy.ExecuteStrategyDelegateV2 = _ =>
7373
{
74-
tokenSource.Cancel();
74+
cts.Cancel();
7575
};
7676

7777
EngineV2.OnCancellationFinishedDelegate = () =>
@@ -80,15 +80,16 @@ public async Task TestCancellationToken()
8080
};
8181

8282
// --- Generate fake SymbolData splitter
83-
var data = GenerateSymbolDataList(new DateTime(2023, 1, 1), 500, 1, WarmupCandlesCount);
83+
List<List<SymbolDataV2>> data = GenerateSymbolDataList(new DateTime(2023, 1, 1), 500, 1, WarmupCandlesCount);
8484

8585
await EngineV2.RunAsync(data, tokenSource.Token);
8686
}
8787

8888
[Fact]
8989
public async Task TestCandlesOrder()
9090
{
91-
var tokenSource = new CancellationTokenSource();
91+
using var tokenSource = new CancellationTokenSource();
92+
CancellationTokenSource cts = tokenSource;
9293

9394
Strategy.ExecuteStrategyDelegateV2 = symbols =>
9495
{
@@ -97,32 +98,33 @@ public async Task TestCandlesOrder()
9798

9899
if (symbolsList.Count == 0) Assert.Fail("There is no symbols exist in test data");
99100

100-
var firstSymbol = symbolsList.First();
101-
if (firstSymbol.Timeframes.Any())
101+
SymbolDataV2 firstSymbol = symbolsList.First();
102+
if (firstSymbol.Timeframes.Count != 0)
102103
{
103-
var firstTimeframe = firstSymbol.Timeframes.First();
104+
TimeframeV2 firstTimeframe = firstSymbol.Timeframes.First();
104105

105-
if (firstTimeframe.Candlesticks.Count() >= 2)
106+
if (firstTimeframe.Candlesticks.Count >= 2)
106107
{
107108
var firstCandles = firstTimeframe.Candlesticks.Take(2).ToList();
108109

109110
Assert.True(firstCandles.First().OpenTime > firstCandles.Last().OpenTime);
110111
}
111112
}
112113

113-
tokenSource.Cancel();
114+
cts.Cancel();
114115
};
115116

116117
// --- Generate fake SymbolData splitter
117-
var data = GenerateSymbolDataList(new DateTime(2023, 1, 1), 500, 1, WarmupCandlesCount);
118+
List<List<SymbolDataV2>> data = GenerateSymbolDataList(new DateTime(2023, 1, 1), 500, 1, WarmupCandlesCount);
118119

119120
await EngineV2.RunAsync(data, tokenSource.Token);
120121
}
121122

122123
[Fact]
123124
public async Task TestWarmupCandlesResultCount()
124125
{
125-
var tokenSource = new CancellationTokenSource();
126+
using var tokenSource = new CancellationTokenSource();
127+
CancellationTokenSource cts = tokenSource;
126128

127129
bool allWarmupCandlesResultsAreCorrect = true;
128130

@@ -143,7 +145,7 @@ public async Task TestWarmupCandlesResultCount()
143145
}
144146
}
145147

146-
tokenSource.Cancel();
148+
cts.Cancel();
147149
};
148150

149151
// --- Generate fake SymbolData splitter
@@ -157,7 +159,8 @@ public async Task TestWarmupCandlesResultCount()
157159
[Fact]
158160
public async Task TestFirstCandleEqualToStartBacktestingDate()
159161
{
160-
var tokenSource = new CancellationTokenSource();
162+
using var tokenSource = new CancellationTokenSource();
163+
CancellationTokenSource cts = tokenSource;
161164

162165
var backtestingStartingDate = new DateTime(2023, 1, 1);
163166
bool allStartingDatesAreCorrect = true;
@@ -168,11 +171,11 @@ public async Task TestFirstCandleEqualToStartBacktestingDate()
168171
var symbolsList = symbols.ToList();
169172
if (symbolsList.Count == 0) Assert.Fail("There is no symbols exist in test data");
170173

171-
foreach (var symbol in symbolsList)
174+
foreach (SymbolDataV2 symbol in symbolsList)
172175
{
173-
foreach (var timeframe in symbol.Timeframes)
176+
foreach (TimeframeV2 timeframe in symbol.Timeframes)
174177
{
175-
var firstCandle = timeframe.Candlesticks.FirstOrDefault();
178+
CandlestickV2? firstCandle = timeframe.Candlesticks.FirstOrDefault();
176179
if (firstCandle == null)
177180
Assert.Fail("Engine Returned candle equal to null");
178181

@@ -181,11 +184,11 @@ public async Task TestFirstCandleEqualToStartBacktestingDate()
181184
}
182185
}
183186

184-
tokenSource.Cancel();
187+
cts.Cancel();
185188
};
186189

187190
// --- Generate fake SymbolData splitter
188-
var data = GenerateSymbolDataList(backtestingStartingDate, 500, 1, WarmupCandlesCount);
191+
List<List<SymbolDataV2>> data = GenerateSymbolDataList(backtestingStartingDate, 500, 1, WarmupCandlesCount);
189192

190193
await EngineV2.RunAsync(data, tokenSource.Token);
191194

@@ -216,14 +219,14 @@ public virtual async Task TestIfAllIndexesReachedTheEndIndex()
216219
k => k.Index == k.EndIndex)));
217220
Assert.True(allReachedEndIndex);*/
218221

219-
var firstSymbolData = dataList.First();
220-
var timeframes = firstSymbolData.First().Timeframes;
221-
var firstTimeframe = timeframes.First();
222-
var indexCandle = firstTimeframe.Candlesticks.ElementAt(firstTimeframe.Index);
222+
List<SymbolDataV2> firstSymbolData = dataList.First();
223+
List<TimeframeV2> timeframes = firstSymbolData.First().Timeframes;
224+
TimeframeV2 firstTimeframe = timeframes.First();
225+
CandlestickV2 indexCandle = firstTimeframe.Candlesticks.ElementAt(firstTimeframe.Index);
223226

224-
foreach (var timeframe in timeframes.Skip(1))
227+
foreach (TimeframeV2 timeframe in timeframes.Skip(1))
225228
{
226-
var nextIndexCandle = timeframe.Candlesticks.ElementAt(timeframe.Index);
229+
CandlestickV2 nextIndexCandle = timeframe.Candlesticks.ElementAt(timeframe.Index);
227230

228231
Assert.True(indexCandle.OpenTime >= nextIndexCandle.OpenTime && indexCandle.OpenTime <= nextIndexCandle.CloseTime);
229232

@@ -234,7 +237,7 @@ public virtual async Task TestIfAllIndexesReachedTheEndIndex()
234237
[Fact]
235238
public virtual async Task TestCurrentCandleOhlcAreEqual()
236239
{
237-
var tokenSource = new CancellationTokenSource();
240+
using var tokenSource = new CancellationTokenSource();
238241

239242
bool allCurrentCandleOhlcAreEqual = true;
240243

@@ -292,9 +295,9 @@ public async Task TimeframesAreSorted()
292295

293296
if (symbolsList.Count == 0) Assert.Fail("There is no symbols exist in test data");
294297

295-
foreach (var symbol in symbolsList)
298+
foreach (SymbolDataV2 symbol in symbolsList)
296299
{
297-
var isSorted = symbol.Timeframes.SequenceEqual(symbol.Timeframes.OrderBy(t => t.Timeframe));
300+
bool isSorted = symbol.Timeframes.SequenceEqual(symbol.Timeframes.OrderBy(t => t.Timeframe));
298301
if (!isSorted)
299302
{
300303
Assert.Fail("Timeframes aren't sorted in ascending order");
@@ -303,15 +306,15 @@ public async Task TimeframesAreSorted()
303306
};
304307

305308
// --- Generate fake SymbolData splitter
306-
var data = GenerateSymbolDataList(new DateTime(2023, 1, 1), 500, 1, WarmupCandlesCount);
309+
List<List<SymbolDataV2>> data = GenerateSymbolDataList(new DateTime(2023, 1, 1), 500, 1, WarmupCandlesCount);
307310

308311
await EngineV2.RunAsync(data);
309312

310313
Assert.True(true);
311314
}
312315

313316
/// <summary>
314-
/// Testing that each next TF open and close contains a prior timeframe open close inside its range,
317+
/// Testing that each next TF open and close contain a prior timeframe open close inside its range,
315318
/// For example, if 5m tf openTime = 1/1/23 12:00:00 and closeTime 1/1/23 12:05:00
316319
/// 1d tf can't have openTime 1/2/23 12:00:00 and closeTime 1/2/23 23:59:59
317320
/// </summary>
@@ -326,16 +329,16 @@ public async Task TestPriorTfOpenCloseInsideNewTfOpenClose()
326329

327330
if (symbolsList.Count == 0) Assert.Fail("There is no symbols exist in test data");
328331

329-
foreach (var symbol in symbolsList)
332+
foreach (SymbolDataV2 symbol in symbolsList)
330333
{
331334
// Validation Timeframes count
332-
if(symbol.Timeframes.Count() < 2) Assert.Fail("There must be at least 2 timeframes to continue test");
335+
if(symbol.Timeframes.Count < 2) Assert.Fail("There must be at least 2 timeframes to continue test");
333336

334-
var priorTf = symbol.Timeframes.First();
335-
foreach (var timeframe in symbol.Timeframes.Skip(1))
337+
TimeframeV2 priorTf = symbol.Timeframes.First();
338+
foreach (TimeframeV2 timeframe in symbol.Timeframes.Skip(1))
336339
{
337-
var lowerTfCandle = priorTf.Candlesticks.ElementAt(priorTf.Index);
338-
var higherTfCandle = timeframe.Candlesticks.ElementAt(timeframe.Index);
340+
CandlestickV2 lowerTfCandle = priorTf.Candlesticks.ElementAt(priorTf.Index);
341+
CandlestickV2 higherTfCandle = timeframe.Candlesticks.ElementAt(timeframe.Index);
339342

340343
// Checking fail conditions
341344
if (higherTfCandle.OpenTime > lowerTfCandle.OpenTime
@@ -348,15 +351,15 @@ public async Task TestPriorTfOpenCloseInsideNewTfOpenClose()
348351
};
349352

350353
// --- Generate fake SymbolData splitter
351-
var data = GenerateSymbolDataList(new DateTime(2023, 1, 1), 500, 1, WarmupCandlesCount);
354+
List<List<SymbolDataV2>> data = GenerateSymbolDataList(new DateTime(2023, 1, 1), 500, 1, WarmupCandlesCount);
352355

353356
await EngineV2.RunAsync(data);
354357

355358
Assert.True(true);
356359
}
357360

358361
/// <summary>
359-
/// Testing backtesting progress is 100 at the end of the backtesting for single symbol
362+
/// Testing backtesting progress is 100 at the end of the backtesting for a single symbol
360363
/// </summary>
361364
[Theory]
362365
[InlineData(0, 2)]

tests/Backtest.Net.Tests/EngineTests/EngineV10Tests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public EngineV10Tests()
3434
[Fact]
3535
public override async Task TestCurrentCandleOhlcAreEqual()
3636
{
37-
var tokenSource = new CancellationTokenSource();
37+
using var tokenSource = new CancellationTokenSource();
3838

3939
Strategy.ExecuteStrategyDelegateV2 = symbols =>
4040
{
@@ -116,7 +116,7 @@ public override async Task TestIfAllIndexesReachedTheEndIndex()
116116
[Fact]
117117
public async Task TestCurrentCandleOhlcConsolidation()
118118
{
119-
var tokenSource = new CancellationTokenSource();
119+
using var tokenSource = new CancellationTokenSource();
120120

121121
Strategy.ExecuteStrategyDelegateV2 = symbols =>
122122
{

tests/Backtest.Net.Tests/EngineTests/EngineV8Tests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Backtest.Net.Engines;
2-
using Backtest.Net.Tests.EngineTests;
32

4-
namespace Backtest.Tests.EngineTests;
3+
namespace Backtest.Net.Tests.EngineTests;
54

65
/// <summary>
76
/// Testing backtesting Engine V8
@@ -22,4 +21,4 @@ public EngineV8Tests()
2221
OnTick = OnTickMethodV2
2322
};
2423
}
25-
}
24+
}

tests/Backtest.Net.Tests/EngineTests/EngineV9Tests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public EngineV9Tests()
3535
[Fact]
3636
public override async Task TestCurrentCandleOhlcAreEqual()
3737
{
38-
var tokenSource = new CancellationTokenSource();
38+
using var tokenSource = new CancellationTokenSource();
3939

4040
Strategy.ExecuteStrategyDelegateV2 = symbols =>
4141
{
@@ -117,7 +117,7 @@ public override async Task TestIfAllIndexesReachedTheEndIndex()
117117
[Fact]
118118
public async Task TestCurrentCandleOhlcConsolidation()
119119
{
120-
var tokenSource = new CancellationTokenSource();
120+
using var tokenSource = new CancellationTokenSource();
121121

122122
Strategy.ExecuteStrategyDelegateV2 = symbols =>
123123
{

0 commit comments

Comments
 (0)