Hi everyone,
I recently purchased WL8 and ran into problems before even starting to translate my WL6 strategies into WL8.
To begin familiarising myself with WL8, I started with the simplest piece of code I had in WL6 — a program with a single loop that goes through historical data and writes it into an ASCII file, then adds some more data that I need for my strategy. After the file is created, I use it as input for my real strategy.
Translating the code wasn’t an issue — that part was straightforward. The problem is execution. In WL6, I could just click the “Run the Strategy” button in the editor, and the code would execute on the chosen symbol. Alternatively, I could open the strategy, click on a symbol on the left, and the code would run immediately, creating the ASCII file as intended. It didn’t matter that this code wasn’t a real trading strategy.
Not so in WL8. In the code editor window, there’s no simple “Run” button for a one-time execution. The only option is “Run Backtest,” which triggers multiple executions, and that breaks my system because the code tries to create, write, and delete the ASCII file repeatedly.
I noticed there’s an “Execute” button in the chart window, but when I click it, nothing happens — the code doesn’t seem to run. Likewise, clicking a symbol in the DataSet doesn’t execute the code just once as it did in WL6. Instead, it launches a full backtest loop and runs the code multiple times.
Am I missing something? I just want to execute the code once, like in WL6 — not backtest it, it is not a strategy
Thanks,
Branko
I recently purchased WL8 and ran into problems before even starting to translate my WL6 strategies into WL8.
To begin familiarising myself with WL8, I started with the simplest piece of code I had in WL6 — a program with a single loop that goes through historical data and writes it into an ASCII file, then adds some more data that I need for my strategy. After the file is created, I use it as input for my real strategy.
Translating the code wasn’t an issue — that part was straightforward. The problem is execution. In WL6, I could just click the “Run the Strategy” button in the editor, and the code would execute on the chosen symbol. Alternatively, I could open the strategy, click on a symbol on the left, and the code would run immediately, creating the ASCII file as intended. It didn’t matter that this code wasn’t a real trading strategy.
Not so in WL8. In the code editor window, there’s no simple “Run” button for a one-time execution. The only option is “Run Backtest,” which triggers multiple executions, and that breaks my system because the code tries to create, write, and delete the ASCII file repeatedly.
I noticed there’s an “Execute” button in the chart window, but when I click it, nothing happens — the code doesn’t seem to run. Likewise, clicking a symbol in the DataSet doesn’t execute the code just once as it did in WL6. Instead, it launches a full backtest loop and runs the code multiple times.
Am I missing something? I just want to execute the code once, like in WL6 — not backtest it, it is not a strategy
Thanks,
Branko
CODE:.
using WealthLab.Backtest; using WealthLab.Core; using System; using System.Collections.Generic; using System.IO; using WealthLab.Indicators; using ScottPlot.Palettes; namespace WealthScript2 { public class USERXtoASCII : UserStrategyBase { //create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) { } //protected void Execute() public override void Execute(BarHistory bars, int idx) { const string sep = ","; const string fmt = "0.00########"; string dtefmt = "yyyyMMdd"; string path = @"C:\Data\ASCII8\"; if (!Directory.Exists(path)) throw new Exception("You must create the directory " + path); WriteToDebugLog("Exporting: " + bars.Symbol); string file = path + bars.Symbol + ".csv"; List<string> datalist = new List<string>(); double nopen, nhigh, nlow, nclose; string csv; DateTime firstDate, secondDate; string secondDateString = "2024-09-06"; DateTime.TryParse(secondDateString, out secondDate); for (int bar = 0; bar < bars.Count; bar++) { csv = bars.DateTimes[bar].ToString(dtefmt) + sep + bars.Open[bar].ToString(fmt) + sep + bars.High[bar].ToString(fmt) + sep + bars.Low[bar].ToString(fmt) + sep + bars.Close[bar].ToString(fmt) + sep + bars.Volume[bar].ToString("0"); datalist.Add(csv); } // Add synthetic data DateTime dNewDate = bars.DateTimes[bars.Count - 1]; int iCounter = 0; while (iCounter < 77) { dNewDate = dNewDate.AddDays(1); int iDayOfWeek = (int)dNewDate.DayOfWeek; if (iDayOfWeek != 0 && iDayOfWeek != 6) { csv = dNewDate.ToString(dtefmt) + sep + bars.Close[bars.Count - 1].ToString(fmt) + sep + bars.Close[bars.Count - 1].ToString(fmt) + sep + bars.Close[bars.Count - 1].ToString(fmt) + sep + bars.Close[bars.Count - 1].ToString(fmt) + sep + bars.Volume[bars.Count - 1].ToString("0"); datalist.Add(csv); iCounter++; } } File.WriteAllLines(file, datalist); WriteToDebugLog("Complete!"); } } }
Rename
For starters, try moving all your code to the Initialize block so it has a fighting chance of working.
You still need to define an Execute block, but it doesn't need any executable statements.
I assume you realize if you used StreamWriter and WriteLine() to write your file to the disk as you create it, it would be much more efficient on memory usage. StreamWriter buffers the output as it's created, then writes it out once it has a complete disk cluster of output. But if you are creating really small files, it shouldn't matter how you do it. Your current approach is simplest for small files. The StreamWriter approach will require a little more code and a using {block}.
You still need to define an Execute block, but it doesn't need any executable statements.
I assume you realize if you used StreamWriter and WriteLine() to write your file to the disk as you create it, it would be much more efficient on memory usage. StreamWriter buffers the output as it's created, then writes it out once it has a complete disk cluster of output. But if you are creating really small files, it shouldn't matter how you do it. Your current approach is simplest for small files. The StreamWriter approach will require a little more code and a using {block}.
Right. Use Initialize(), which is called once for each symbol in the backtest.
Execute() is called once for every bar - the explicit trading loop of WL6 is gone. See: Anatomy of a Wealth-Lab Strategy
This may help too as a quick reference: Quick-WL6-9-to-WL7-Translation-Guide
Execute() is called once for every bar - the explicit trading loop of WL6 is gone. See: Anatomy of a Wealth-Lab Strategy
This may help too as a quick reference: Quick-WL6-9-to-WL7-Translation-Guide
Great, it worked. Thanks.
Your Response
Post
Edit Post
Login is required