In the documentation is saids that both PreExecute and PostExecute executes immediate before and after the main iteration.
If I did this on a single asset, it will log the date then the positions, once per day.
But if I do this on a Portfolio, it will print all the dates of the bactest from start to end, then print the positions from start to end.
I am merely inspecting what happens on a day-to-day when running the backtest. How do I get it so that code runs from PreExecute -> Execute -> PostExecute for every loop?
CODE:
public override void PreExecute(DateTime dt, List<BarHistory> participants) { if (dt.ToLongDateString() != date) { date = dt.ToLongDateString(); WriteToDebugLog(date); } } public override void Execute(BarHistory bars, int idx) { } public override void PostExecute(DateTime dt, List<BarHistory> participants) { WriteToDebugLog($"\tTotal Positions: {GetPositionsAllSymbols()}"); }
If I did this on a single asset, it will log the date then the positions, once per day.
But if I do this on a Portfolio, it will print all the dates of the bactest from start to end, then print the positions from start to end.
I am merely inspecting what happens on a day-to-day when running the backtest. How do I get it so that code runs from PreExecute -> Execute -> PostExecute for every loop?
Rename
I like to note that if I pass a false argument to WriteToDebug() that this is corrected so this may be related.
Passing a false to WriteDebugLog switches the output to sequential:
https://www.youtube.com/watch?v=EqxgxLfF4Dw&t=10s
https://www.wealth-lab.com/Discussion/A-Chronological-Debug-Output-Preference-6902
https://www.youtube.com/watch?v=EqxgxLfF4Dw&t=10s
https://www.wealth-lab.com/Discussion/A-Chronological-Debug-Output-Preference-6902
CODE:
private string date = ""; private string datestring = ""; private string target = @"C:\Users\netfe\Desktop\WL Strategies\text.txt"; public override void PreExecute(DateTime dt, List<BarHistory> participants) { if (date != dt.ToLongDateString()) { date = dt.ToLongDateString(); datestring += date; } File.WriteAllText(target, datestring); } public override void Execute(BarHistory bars, int idx) { } public override void BacktestComplete() { Directory.SetCurrentDirectory(@"C:\Users\netfe\Desktop\WL Strategies\"); Process.Start(@"C:\Program Files\Notepad++\notepad++.exe", "text.txt"); }
if I place
CODE:
File.WriteAllText(target, datestring);
in PreExecute, it will write a bunch of dates to file.
if I place it in either BacktestComplete() or Execute(), it will write an empty file.
what is it that I am not understanding about these methods? If I store a string to a variable, shouldn't I be able to write the contents of this variable to a file in the BacktestComplete() method?
The class level date variable is not changing. You should key off bars.DateTimes[idx] in Execute(), for example.
Your Response
Post
Edit Post
Login is required