- ago
I have a multi-symbol strategy that I implemented several months ago.

Today I realized, that background coloring and labels are not put on the chart.

It seems that DrawText() function works fine in Execute(), but doesn't in PreExecute().

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using System.Collections.Generic; namespace WealthScript5 {    public class MyTest : UserStrategyBase    {       public override void PreExecute(DateTime date, List<BarHistory> allBars)       {          //this doesn't work          DrawText("X", GetCurrentIndex(allBars[0]), 3000, WLColor.Red, 12);             }              public override void Execute(BarHistory bars, int bar)       {          //this works          DrawText("X", GetCurrentIndex(bars), 3000, WLColor.Red, 12);          }    } }


It seems that it worked before in PreExecute(), but now it doesn't. I tried several workarounds to pass data from PreExecute() to Execute() via global variables and cache, but failed.

Can you suggest any solution to output data on the Chart based on the information I generate inside PreExecute()?

Thank you
2
604
Solved
14 Replies

Reply

Bookmark

Sort
- ago
#1
I would recommend doing as much drawing as possible in Initialize{}. Now if your plots are a function of trade results and Positions, then you'll have to use Cleanup{} or BacktestComplete{} to Chart that.

I would try to avoid performing drawing in Execute{} because it's probably less efficient, but it does work.

I'm not sure if it's even possible to perform drawing in PreExecute{}. Can some of that PreExecute code be moved to Initialize? What are you trying to do in PreExecute anyway that needs plotting? Typically we are collectively taking all stocks in the dataset together (not individually) so how would DrawText know which stock you are referring to when all stocks in the dataset are being examined together on a per bar bases?
1
Glitch8
 ( 9.81% )
- ago
#2
PreExecute is called in the context of one of the symbols in the candidates list, so they will work and appear on that symbol, but there’s really no way to know which symbol that would be. The drawing methods are intended to work in the context of a method that is called for every symbol being processed, like Execute or Initialize.
1
Best Answer
- ago
#3
Thank you for the valid questions and suggestions.

I implemented a multisymbol momentum strategy with regard to the posts on this site. It was an example where all symbols are analyzed and traded in PreExecute(), and Execute() is empty.

Somehow it worked sometime ago, and now it doesn't.

As for the question about how to know the specific symbol - I don't care about the symbol. As the PreExecute() symbol is random (according to Glitch's message), after run this random symbol's prices are depicted on the Chart. Then I manually enter main index ticker (SPY) in the chart window, and all labels are depicted there (I can guess that labels specified with DrawText() are statically depicted in the Chart window, and any switch to other ticker after the backtest completion will not affect the labels, only ticker's prices are re-drawn, but labels remain intact)

According to your advices I will move the labeling logic from PreExecute() to Intialize(), as I definitely can use Cache there.

Thank you very much!
0
- ago
#4
Moved everything to Initialize(), works like a charm! Thank you!

One extra question: is it possible to force to switch to a specific ticker programatically? So that BackTest -> Chart always switches to "SPY"?
0
- ago
#5
QUOTE:
One extra question:

Please start a new topic for each unrelated question. And you can add your vote for the forum guide as well. https://www.wealth-lab.com/Discussion/Add-a-Forum-guide-syntax-rules-of-the-house-etc-10702

QUOTE:
is it possible to force to switch to a specific ticker programmatically? So ... [the] Chart always switches to "SPY"?

I don't think so. Why don't you simply open a second copy of your strategy and point it to SPY all the time? (Or am I missing something?)
0
- ago
#6
QUOTE:
Please start a new topic for each unrelated question.

I do it all the time, but currently the question is related to this thread's quote:

CODE:
but there’s really no way to know which symbol that would be

So if symbol is random, it would be good to somehow override that, e.g. programmatically switch to some specific symbol.

QUOTE:
Why don't you simply open a second copy of your strategy and point it to SPY all the time?

I'm running a mutli-symbol momentum strategy (hundreds of symbols), I always want to see the index (SPY) ticker, as I don't care about any individual symbol, e.g. AAPL or TSLA.

Thank you
0
- ago
#7
QUOTE:
running a multi-symbol momentum strategy (hundreds of symbols), I always want to see the index (SPY) ticker,

So simply include SPY on a separate pane in your multi-symbol strategy so you see it all the time. (Somehow I don't think I understand what you are trying to do.)
1
- ago
#8
You can place both the stock (example below employs AAPL) and the index on the same plot, but you need to transform both so they have compatible units; otherwise, they cannot share a plot. This example uses ROC to transform their units to percentage change, but you can use some other indicator to do the same.

The green line is for ROC(AAPL) and the red line is for ROC(SPY). The PlotIndicatorCloud shading is the difference between them.


CODE:
using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; namespace WealthScript4 {    public class MyStrategy : UserStrategyBase    {       IndicatorBase roc, rocIndex;       public override void Initialize(BarHistory bars)       {          BarHistory spy = GetHistory(bars, "SPY");          rocIndex = ROC.Series(spy.Close, 1);          roc = ROC.Series(bars.Close, 1);          PlotIndicatorCloud(roc, rocIndex);       }       public override void Execute(BarHistory bars, int idx) { }    } }
I still don't understand what any of this has to do with PreExecute{}. You really haven't explained your process to us, so our answers aren't what you need.
0
- ago
#9
Thank you for the example! As I mentioned in #4, I moved all the drawing code from PreExecute() (where I did it bar by bar) to Initalize() where I output all necessary labels in a loop. Now it works fine.

What I was asking in addition: on your last chart you have AAPL as the main Chart, and Roc as an indicator Pane. And AAPL is something that is selected automatically for the main Chart if you backtest on a single ticker. But if you backtest on a group of tickers (DataSet), the specific depicted ticker will be random (AAPL, or TSLA, or META). So I asked if there any method that explicitly forces a ticker to be displayed in the main Chart (e.g. TSLA). As I understand, there is no such method. But it's not a big deal, I can manually enter TSLA into the search box and press Enter.

Thanks again for all suggestions!
0
- ago
#10
QUOTE:
I moved all the drawing code from PreExecute() (where I did it bar by bar) to Initalize() ...

The WL indicators all operate on a bar-by-bar basis by default; they are vector operators. PreExecute{} is for something else. This post should be re-titled.

QUOTE:
I can manually enter TSLA into the search box and press Enter.

No, you can simply step through the stocks (with the mouse) in the dataset tree while in "Single Symbol" mode to run a backtest on individual stocks.



Some people prefer PlotTimeSeriesHistogramTwoColor. Try this code and see if you like it better.

CODE:
   public class MyStrategy : UserStrategyBase    {       IndicatorBase roc, rocIndex;       TimeSeries rocDiff;       public override void Initialize(BarHistory bars)       {          BarHistory spy = GetHistory(bars, "SPY");          rocIndex = ROC.Series(spy.Close, 1);          roc = ROC.Series(bars.Close, 1);          PlotIndicatorCloud(roc, rocIndex);          rocDiff = roc - rocIndex;          PlotTimeSeriesHistogramTwoColor(rocDiff, "ROC difference", "ROC difference");       }       public override void Execute(BarHistory bars, int idx) { }    }
0
- ago
#11
Well, I know that there is the Single Symbol mode. But I'm testing in Portfolio Backtest mode (all stocks from SP500/SP100), and after run I manually enter SPY in the Chart to see additional indicators and labels that regulate investment in individual stocks based on current index's regime (e.g. don't trade stocks momentum if SPY < MA(200))
0
Glitch8
 ( 9.81% )
- ago
#12
I tagged this a #FeatureRequest so anyone interested should "thumbs up" the topic.
1
- ago
#13
QUOTE:
I tagged this a #FeatureRequest

Exactly, what feature is being requested? You need to charge the topic title to reflect that feature (whatever it might be). I can't figure it out.
0
Glitch8
 ( 9.81% )
- ago
#14
For reference, Post #9 is being requested.
1

Reply

Bookmark

Sort