- ago
This simple strategy takes 15 seconds to run on my data and settings, it generates 50 trades. But if you replace 0.95 with 1 you'll get much more trades (still not more than 1 per bar) and you'll get 12 times longer backtesting time (I mean I did, your numbers may differ).

I fully understand that having a trade causes extra calculations, but 12 times difference looks like too much. And if you subtract permanent time consuming calculations (that are not effected by 0.95->1 changing) the difference will be even bigger. It looks like there is a reserve for emprovement. Could you please look into it?

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; namespace WealthScript1 { public class MyStrategy : UserStrategyBase { //create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) { } //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) { if (true) {             PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, bars.Close[idx] * 0.95); //code your buy conditions here } else if (HasOpenPosition(bars, PositionType.Long)) {             //code your sell conditions here             PlaceTrade(bars, TransactionType.Sell, OrderType.Market); } } //declare private variables below } }


0
243
5 Replies

Reply

Bookmark

Sort
Glitch8
 ( 12.08% )
- ago
#1
I ran it on the Nasdaq 100 for 20 years. The initial run ran in 10 seconds, generating 30 trades but 25,637 NSF positions. Of course, increasing the number to 1 is going to cause the NSF position count to balloon, causing a geometric increase in processing. The reason is your strategy buys indiscriminately almost every bar and never sells. I'm not sure if this is an experiment, or an oversight, but for experiments like this you should use the following line in Initialize to overcome the issue:

CODE:
//create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) { BacktestSettings.RetainNSFPositions = false; }
2
- ago
#2
My strategy performed too slow when adding more trades (like 1 per 1-5 bars). I made this simple strategy to demonstrate - and it played it's role), but yes it's an oversight with not closing trades).

Yes, turning off Retain NSF helped with my initial strategy, thanks. Although it holds positions for 2 bars only. I don't know how it works and why. And I defenetly need to watch a video about retaining NSF and what it is for)).

It took 20 seconds with Retain NSF turned off and 30 minutes with Retain NSF turned on.

Here is the "turned on" Backtesting Report:

CODE:
Metric   Model   Benchmark   Long   Short Summary                Profit   10939060,58192193   10854894,376205804   10939060,58192193   0    Profit %   1093,9060581921929   1085,4894376205805   1093,9060581921929   0    Profit Per Bar   5,356355830782856   10,794869631771725   5,356355830782856   не число    APR   13,275904264394601   13,235627175981746   13,275904264394601   0    Exposure   45,463650854383175   98,74166254723971   45,463650854383175   0    Alpha (α)   2,8772337870484925   не число   2,8772337870484925   не число    Beta (β)   0,7601703408575912   не число   0,7601703408575912   не число    Sharpe Ratio   0,6683082987370295   0,5955062529392637   0,6683082987370295   не число    Sortino Ratio   0,7372567512404727   0,7677305225129176   0,7372567512404727   не число    WL Score   9,040737392630176   3,4919046871769384   9,040737392630176   0                    Interest & Commission                Commission Paid   7598479,559877789   350,23663500000004   7598479,559877789   0    Cash Interest Received   0   0   0   0    Margin Interest Paid   -0   -5698,387159195301   -0   -0    Dividends Received   0   0   0   0                    Positions                Position Count   98670   1   98670   0    Avg Profit   110,86511180624184   10860592,763365   110,86511180624184   не число    Avg Profit %   0,147241399295424   1085,325487774216   0,147241399295424   не число    Profit Factor   1,1162961148975592   не число   1,1162961148975592   не число    Payoff Ratio   1,078746369521354   не число   1,078746369521354   не число    Avg Bars Held   1,999706091010439   5031   1,999706091010439   не число    Avg Trades Per Month   825,581589958159   0,0041841004184100415   825,581589958159   0    NSF Position Count   144791   0   21147   123644                    Drawdown                Max Drawdown   -2539096,9876646604   -4580187,2733780015   -2539096,9876646604   0    Max Drawdown Date   18.03.2020   24.10.2008   18.03.2020   01.01.0001    Max Drawdown %   -69,03977912924125   -73,94936638181827   -69,03977912924125   0    Max Drawdown % Date   21.11.2008   24.10.2008   21.11.2008   01.01.0001    Recovery Factor   4,308248418656568   2,3699673677753466   4,308248418656568   не число                    Profitable Positions                Count   50808   1   50808   0    % Profitable   51,49285497111584   100   51,49285497111584   0    Avg Profit   2066,627168858538   10860592,763365   2066,627168858538   не число    Avg Profit %   2,256007384966205   1085,325487774216   2,256007384966205   не число    Average Bars Held   1,9998228625413321   5031   1,9998228625413321   не число                    Unprofitable Positions                Count   47862   0   47862   0    % Unprofitable   48,50714502888416   0   48,50714502888416   100    Avg Loss   -1965,2779368484867   не число   -1965,2779368484867   не число    Avg Loss %   -2,0913232699612316   не число   -2,0913232699612316   не число    Avg Bars Held   1,9995821319627263   не число   1,9995821319627263   не число   

0
- ago
#3
QUOTE:
It took 20 seconds with Retain NSF turned off and 30 minutes with Retain NSF turned on.

It's not just the additional processing of all the trades that slows you down. The main reason for the slowdown is the larger memory footprint it creates, which causes more processor cache misses and garbage collection--that's the biggest difference.

If you get a server-class machine (Xeon processor) with a very large on-chip cache, that would make a big difference. These server class machines are air cooled, so all their fans are extremely noisy. You'll want to locate it in a machine room. Happy computing.
1
- ago
#4
@superticker

I used my fanless laptop for these tests - it was extremely silent)).
0
- ago
#5
But with a laptop processor, the heat dissipation capability is small, so that limits how much on-chip cache the laptop processor can have. Large memory footprint problems will run really slow on a laptop as you found out.

For your large memory footprint needs, you'll need a server-class Xeon processor with a very large on-chip cache (like we use for Windows Terminal Servers with 20 users). And that big old Xeon processor will require lots of cooling.
1

Reply

Bookmark

Sort