FLX8
- ago
In the documentation is saids that both PreExecute and PostExecute executes immediate before and after the main iteration.

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?
0
906
Solved
4 Replies

Reply

Bookmark

Sort
FLX8
- ago
#1
I like to note that if I pass a false argument to WriteToDebug() that this is corrected so this may be related.
0
- ago
#2
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
0
Best Answer
FLX8
- ago
#3
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?
0
- ago
#4
The class level date variable is not changing. You should key off bars.DateTimes[idx] in Execute(), for example.
0

Reply

Bookmark

Sort