- ago
Apologize in advance for my lack of basic understanding...I'm trying to optimize my computation runs (and reduce crashing)

Does WL8 re-run the C# code for every bar of data in a series, without an explicit loop written in?

I'm trying to set up a backtesting framework and then "flip on" a live trading algo. I would like to run the loop forward from a set point (either first bar of "live" day, or beginning of time series, depending on a switch I set), so that I eliminate any "peeking"/cheating errors.

When I insert my "WriteToDebugLog" line, I would've thought it should only write once, but I end up getting it written what seems like as many times as there are bars. If this does indeed run a new iteration on each bar (making there exponentially more loops being run than necessary), how do I cone down on how many iterations get run to make the code more efficient? (I don't want to necessarily reduce the dataset look-back, in case I want to expand Walk-Forward-Optimization later.)

Thanks!

CODE:
public override void Execute(BarHistory bars, int idx) {          int startbar =20;                    bool LiveTrade=false;             //LiveTrade= false;             LiveTrade = true; //toggle off to just run loop on current day          if (LiveTrade)          {             //find first bar of today if trading live             startbar = bars.Count -1;             while (bars.IntradayBarNumber(startbar) != 0)                startbar--;          }          WriteToDebugLog             ("Starting Bar = "+ startbar+ " because LiveTrade = "+ LiveTrade + ". Most recent bar = "+bars.Count + ". Date/Time = "+ bars.GetTimeAsInt(startbar)); //I expected this to line to write only once, but I get it written so many times          for (int i = startbar; i < bars.Count; i++)          {             //insert whatever buy/sell criteria here             int index = idx;             Position foundPosition0 = FindOpenPosition(0);             bool condition0;             if (foundPosition0 == null)             {                condition0 = false;                {                   if (indicator.CrossesOver(37.00, index))                   {                      condition0 = true;                   }                }                if (condition0)                {                   val = multSource[idx];                   _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, val, 0, "Buy at Limit 0.50% above Lowest(Low,20)");                }             }             else             {                condition0 = false;                {                   condition0 = true;                }                if (condition0)                {                   Backtester.CancelationCode = 42;                   value = (6.5 / 100.0) + 1.0;                   ClosePosition(foundPosition0, OrderType.Limit, foundPosition0.EntryPrice * value, "Sell at 6.50% profit target");                }             }          }
0
718
Solved
3 Replies

Reply

Bookmark

Sort
Cone8
 ( 6.32% )
- ago
#1
QUOTE:
Does WL8 re-run the C# code for every bar of data in a series, without an explicit loop written in?

The Execute() method (and Pre/PostExecute), yes. In the QuickRef, select Class: UserStrategyBase and click on the methods in the Strategy Execution section for more explanations.

QUOTE:
I'm trying to set up a backtesting framework and then "flip on" a live trading algo.
This is the idea. There's usually nothing you need to do. Unless you really, really, really are intentionally peeking, the strategy will not be peeking.

QUOTE:
When I insert my "WriteToDebugLog" line, I would've thought it should only write once,
You already know differently now. Every time a bar is added to the chart (the strategy runs from the first to last bar all over again). It needs to do this to create the hypothetical trades so that the strategy logic runs properly at the right time.

If you need to exit the Execute() method prematurely, just... return; For example, say you were running on the Dow 30 DataSet but didn't want to trade MSFT. You could just add this rule at the start of Execute() -

CODE:
if (bars.Symbol == "MSFT") return;
0
Best Answer
- ago
#2
Don't place startbar=## in the Execute block. It belongs in the Initialize block instead.

Don't place FOR loops in the Execute block either. Execute will automatically run through all the code in the Execute block for each bar so you don't need a FOR loop in there.

Make it easy. Take one of the sample strategies that already works and play with it. That's the fastest way to get things going. If you really want a "deep" understanding, read https://www.wealth-lab.com/blog/anatomy-of-a-wl7-strategy after you had a couple weeks experience with WL playing with the canned strategies. But understand that article is somewhat advanced, so it's not that important right now.
0
- ago
#3
Thanks guys! Appreciate the knowledge shared.
0

Reply

Bookmark

Sort