Macro for selecting external symbol in the Editor
Author: wmrgrove
Creation Date: 2/2/2018 3:30 PM
profile picture

wmrgrove

#1
Using several Wealth Lab strategies I have been able to setup a robust back-tester that does a good job of finding Stock Pairs for trading ( 1 to 5 weeks avg ). The robustness of Wealth Lab does a great job at this.
The more difficult part is that after you have selected 50 to 100 possible pairs to monitor for an "Entry Trigger" the limitation of changing the "External Symbol" for each review is very time consuming! An excel Macro helps setup the format for the Wealth Lab editor but I still need to manually select each external symbol in the editor. Example here;

CODE:
Please log in to see this code.


In the keyboard shortcuts for the editor I see a reference too "Play back Macro". My question's are;

Is there a Macro program that is easy to use with WL editor?

What is the best way to automate (semi-automate) the review of 50 to 100 external symbols to their pairs list daily? I can of course easily monitor the individual tickers to look for daily moves but that does not tell me the movement of the pairs. I am using the "RATIO Standard Deviation method" to trigger long/short Pair Trade.

There are 3rd party software products that do this but require both subscription and another database ( limited choices) or use of a different broker for data API . I would like to stay with Fidelity and Wealth Lab!

Any thoughts, guidance, and or links would be much appreciated.

profile picture

Eugene

#2
So if I got it right the idea is to rerun the strategy 50-100 times, driven by choice of your pair symbols. Let's start tossing ideas around.

If your review phase consists of reviewing the charts, then this probably could be automated by saving them to a bitmap file:

Iterate through a DataSetSymbols list and save chart bitmap images, post #5

Which optionally might be directed to an RTF file: Copy to Clipboard > Advanced example

As you understand a WealthScript Strategy would need to be fed with a list of pair symbols, plot the ratio DataSeries, and save the bitmaps. As an alternative, an AutoHotKey or AutoIT script might be pressing the Down arrow key to force chart redraw.

But the key question here is where the pair symbols are coming from? I assume the list is not generated by WLP but comes from Excel, external website or somewhere else. If so, please expand on what stops you from using WLP? And if the source is WLP then we could pass on the symbol names as an argument to GetExternalSymbol.

More information required.
profile picture

wmrgrove

#3
Eugene, thanks for the quick reply and ideas! Yes we would need to run the strategy 50 to 100 times (100 to 200 symbols). One WL strategy that I run has all the results, including chart detailed view. The "Auto" output would most likely be a list / data-file that would identify key metrics (triggers). A symbol list would be all that is need to create a data-set to review charts with triggers or alerts.

More info follows;

I start with Fidelity screen-er and sort for >$10, >X bil mrkt cap, > $X mil trade / day, and so-forth

When I download the screen from Fidelity ( .XLS ) I also get, beta, mktcap, sector, group, and sub-group, info

Then RUN / filter that list in WL for, ATRP100 < X, gaps up/down 1yr < Y, and all is merged in .XLS by symbol as BASE LIST

Then using a selected sub-group ( average 8-15 symbols ) I create a WL data-set and using an .XLS macro, cut & paste list for the WL editor in the correct format ( as prior post ).

Another screen I use is a WL Strategy Chart view of all symbols ( average 8-15 symbols ) ROC, 12mths, 6mths, 3mths, too eliminate outliers. ( .XLS macro sets format to paste into editor in a few keystrokes ) .

Now I can run my Pairs Trader ( WL Strategy ) and back-test for 2 / 3 years performance, one external at a time. In the Trade Tab I can see the profit / loss, for each pair trade, under exit name ( with your previous help ) sorted by symbol, also quantity of trades, and very important the average periods per trade. I have no fixed exit except a cross of a lower #Standard deviation so bad trades go on much longer. The Symbol Tab gives me summary key metrics by symbol on the Pair like, Correlation, quantity of trades, and avg bars held. On the chart I can see the Pair RATIO, RATIOsma, RATIO Stdev, Profit potential at entry, Correlation, RATIO RSI. The purpose of all of this is to select PAIRS to trade going forward that exhibit a very good, consistent history.
For research I exported the trade tab and symbol tab results to excel macros that cleaned up the data for review and to set selection criteria. NOTE: after each run of an external symbol I can delete that symbol in the test data-set for the next run to reduce time required.

Now we have selected our pairs for that sub-group and this list of pairs should be good for at least 3 months or so. I create a final WL data-set for that sub-group with only the symbols that have an external symbol "Mate". In the editor I have the following;

CODE:
Please log in to see this code.


Shown in the example above, to the right of the External symbol line is the "pair symbol" ( that is in the data-set list ). Of course the "pair symbol's" can all be in one list / data-set at this point.

I think that covers everything. The selection of the Pairs seems like a lot of work but once the process is set-up it goes pretty fast and you only need to do it every few months or so ( or they are not really pairs ). The hard part is how to mange the watch list for daily trade triggers. My thought is to use a violation ( Cross ) of the Standard deviation threshold for the alert and the cross back inside as the trigger. That can occur the same day or several days later. All trading planned for EOD.

Thanks again for all help! Glad to answer any other questions.
profile picture

Eugene

#4
As I need some time to think it over, I'll get back to you when I finish a task I've been assigned (and maybe also a scheduled task early next week).
profile picture

wmrgrove

#5
Eugene, thanks for giving it some thought and letting me know.

One idea I thought of (with my limited knowledge here), would be to use the Strategy Monitor. Reviewing posts on the Strategy Monitor I saw a post where a user had 125 strategies running in the Strategy Monitor. Interesting!

If I take a simple version of my strategy that just looks for RATIO STDev cross-overs / cross-unders, and using both of the PAIR Symbols as External Symbols for the RATIO we could create a Strategy for each PAIR. ( saw the notes on Internet clock, don't use screen-er, and raw profit mode)

THEN:

No need to set up a Data-set for each PAIR?

Just enter the pair symbols in a Strategy copy, save with pair name, and load to Strategy Monitor

Run Strategy Monitor daily after update and there are your signals, valid as long as pair is valid, many months or years

What do you think? Not elegant but uses existing tools without new coding.

I will try it with a few pairs and then wait for your thought process and better ideas.

Thanks again!
profile picture

Eugene

#6
William, thanks for providing detailed descriptions of your workflow. But I'd like to clarify one thing in your original post first:
QUOTE:
I still need to manually select each external symbol in the editor.

What is this used for? I was under impression you're using an external source to deliver the pair symbols to Wealth-Lab which then is used for the visual inspection of signals generated by your system. You described how you would use the Trades tab for the additional performance analysis and subsequent screening.

Now that the Strategy Monitor emerges this suggests a totally "headless" scenario. Re: "there are your signals, valid as long as pair is valid, many months or years", yes that's true.

QUOTE:
No need to set up a Data-set for each PAIR?

Please clear this up. Why is this required to set up a DataSet for each pair? Not arguing with the usage of Strategy Monitor but you could for example loop through DataSetSymbols to go through each pair symbol. Both solutions lack the benefits of the Trades tab.

The list of pairs could be imported from a text file (just a speculation, not an advice now). Moreover, the whole DataSet's *.XML file (with WLP shut down, of course) could be created by an external app, batch file or maybe even macro in your Excel spreadsheet which is fed with the list of pair symbols. This is again a speculation - I'm just trying to understand your objectives better.
profile picture

wmrgrove

#7
Eugene; yes re-reading my posts I can see the need for clarity.

The Pairs Trading process I am trying to develop has three key parts.

The first part is to develop a large list of Stock Pairs ( 100+ pairs ). The reason is that each Pair on average will have 1 to 3 signal opportunities per year. Ideally you would have 5 to 10 active pair trades on at any time with a 5 to 25 period duration.
This process currently is detailed in my POST#3 starting at, "More info follows;" The FINAL output of that process currently is;

1. A data-set in WL that has all of the "PAIR MATES" to all of the "PAIR EXTERNAL"'s

2. A "list" in the Strategy Code that looks like this example below; ( symbol to the right of the external is the "Pair mate or mate's" in the data-set, for reference )
CODE:
Please log in to see this code.


To summarize and clarify this process (detailed in my POST#3), I am using a Fidelity stock screen-er and WL screen-er to find sub-industry lists for possible pairs. I am using .XLS macro's only to reduce the keystrokes to get the symbols into WL editor, and to aid in reviewing the data from Trade Tab for filtering poor candidates. Then I run a WL strategy one External at a time (each against all others in that sub-group). This back-test shows the actual performance of all potential pairs one External at a time. I again use .XLS macro's to aid in review the results of the symbol and trade tabs. From this process I pick the final pairs and create the view you see above. ( AUTO PARTS VENDORS )

This completes the first part of the three key parts of the PAIR Trading process. IMPORTANT: I understand this process can be automated from its current state but I am fine with it for now. So it is not important to improve this step now. The manual process has helped establish criteria and set parameters. ( It is a VALUE ADD process currently )

STEP 2 is where I really need the help now. The monitoring of the 100+ pairs daily is not doable manually! What I need to do in step 2 is, after a download / update of WL data-sets, create a symbol list / alert list of all Pairs where the RATIO exceeded (or is currently outside) the Standard Deviation threshold established in the strategy. ( no chart review, or complex strategy criteria, except using External Symbols )

Step 3 is simply to review the alert list of pairs for trades and execute.

As mentioned in my last post one thought (with my limited expertise here) was to use the Strategy Monitor with 100+ strategies. Copy and paste is a few keystrokes. Forget my comment on putting both sides of the pair as Externals, no value in that. I now see you just select the pair ticker in the data-set and go, with the other side of the pair as External. After several hours yesterday I am struggling with the Strategy Monitor. May just be a problem in my reduced code for the Strategy Monitor application. I will run Cone's basic code (from a forum post) this morning and verify that is working. Are there any particular issues with External Symbol's in strategies in Strategy Monitor that you know of?

Hope all of this helps and I answered your questions. Bottom line is that all the value add is in WL. Fidelity is just used as a symbol list source by sector / industry / sub-industry and screen-er. Excel .xls and macro's are just used to review / analyze data ( WL Trade and Symbol tabs ) and reduce key strokes for WL editor set-up.
profile picture

superticker

#8
I'm not sure what you're trying to do, and I'm not familiar with paired trading, so my solution may be way off. But perhaps it would be helpful if you explained why the brute force solution below will not work for processing Step 2. Be sure to create the WL DataSet {"AXL","CPS","ALV","BWA","MGA"} before running the code.
CODE:
Please log in to see this code.
profile picture

Eugene

#9
The solution to your step #2 proposed by superticker looks reasonable. It's a good pattern for systems operating on external symbols pairs - see my code for an article: ActiveTrader 2011-05 | Inverse ETF switching system.

P.S.
QUOTE:
I understand this process can be automated from its current state but I am fine with it for now.

I'm pretty sure about that too. If I were in your position I'd try to automate the download of the XLS file with screener results from Fidelity's website. Hardly effortless due to likely website authentication, though.
profile picture

wmrgrove

#10
Thanks Superticker and Eugene for the reply and Strategy idea. It is much appreciated!

I will try to implement the code this morning.
profile picture

superticker

#11
Some comments, especially if you're getting a run-time error or crashing.

1) C# is a strongly typed language. Replace the "var" with the appropriate data type whenever possible so the compiler flags the problem rather than throwing a run-time error. In the above code, most of the "var"s can be changed to "DataSeries".

2) Include a
CODE:
Please log in to see this code.
line near the top of the Execute() block so it returns from the Execute() loop without error, then progressively move it down in the code until the run-time error begins. At that point, tell us which specific line is causing the run-time error.
profile picture

wmrgrove

#12
QUOTE:
Is there a chance that something else has crashed WLP?


Eugene, no I don't think so, I am all up to date win10 & WL, using WL many hours per day for several weeks with no other issues. ( running hundreds of strategy runs with external symbols) Failed a lot on Sunday, less so this morning, but still multi-days with full restart .

I even took the "code clean-up" you helped me with in mid DEC. and changed buy/sells to AT MARKET bar+1, ran that and it crashed this morning!

Will try your ideas above!
profile picture

superticker

#13
QUOTE:
... sometimes it crashes when I just add the strategy ( not activated ),...
You are trying to tell us the *.xml source file for the strategy code is corrupted. Is that really true? If you honestly believe that (How did it get corrupted in the first place?), then I would open a fresh strategy "from code" window, and I would text copy the old code into the new strategy window and save it under a different name. Use Explorer to delete the old corrupted strategy *.xml source file after you do when WL isn't running.
profile picture

wmrgrove

#14
Superticker;

I do really appreciate the input and help on automating the External Symbol list (Post #8)!

I plan to focus on that now as it is a much better idea than using the Strategy Monitor.
profile picture

wmrgrove

#15
Superticker and Eugene;

I have started to use, and modify, the strategy from superticker in POST# 8 above, and started getting error messages when adding lots of pairs too the strategy. ( System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.)

see image attached, JUST ADD PAIRS AND COMPILE.png

Having added about 90 pairs to the strategy I thought I had exceeded the quantity or had a typo. Long story short the Strategy will not allow the same external symbol to be used twice ( 2 sets of pairs ). My pairs might have symbol A in 2 or 3 sets of pairs. That is fine if they are the "data-set" symbols, but if they are used 2 times in the External Symbol side I get the error.

for example this will compile;

CODE:
Please log in to see this code.


this will not compile;

CODE:
Please log in to see this code.


I can move the Pairs around so that most would not be duplicates, but some must be used twice. Using excel it is easy to find duplicates and I use a Macro to get the correct format to load all to the editor. If there is not a script workaround I can just have 2 or 3 strategies that have all the pairs but no duplicate externals. Not a big problem but if there is a work-around that would be great?

One other thing of interest is that I get a Debug and error message for many, many, symbols each time I run the strategy;

Example; Error processing symbol WEN The given key was not present in the dictionary.
( see attached, RUNNING THE STRATEGY WITH NO CHANGES.png )

All the symbols in the error log ARE IN THE data-set, and the strategy runs as expected, just a curiosity?

Thanks again for the help on this, it is exactly what I needed!
profile picture

Eugene

#16
QUOTE:
Long story short the Strategy will not allow the same external symbol to be used twice ( 2 sets of pairs ). My pairs might have symbol A in 2 or 3 sets of pairs

This is by design. It's easy to make sure that the .NET Dictionary collection does not allow duplicates. There are other collections that do though.

Below please find an example which adjusts for multiple pair symbols by putting them in a List<Bars>. However, you cannot continue using your Strategy unmodified. Having duplicates implies you have to execute the same code twice (or three times). If you put your code in a routine this might work out (not sure).

CODE:
Please log in to see this code.
profile picture

wmrgrove

#17
Eugene, thanks for the quick reply and idea, I will give it a try!

Quick question, in my strategy I was using the line below to establish the RATIO for Pairs;

CODE:
Please log in to see this code.



In superticker's code the source was;

CODE:
Please log in to see this code.



In your code above how do I find the var / Dataseries to use for the Ratio?

Thanks,
profile picture

superticker

#18
QUOTE:
My pairs might have symbol A in 2 or 3 sets of pairs. That is fine if they are the "data-set" symbols, but if they are used 2 times in the External Symbol side I get the error.
Clearly, I don't understand paired trading.

So for one particular DataSet symbol, you want to compare multiple external symbols? (I'm assuming it makes sense to do that.)

If so, how is it possible from the cross reference lookup to return something if there are more than one somethings that need to be returned? Are you saying you want the lookup to return an array of strings with many external symbols in it? Weird.

Rather than creating nested FOR loops (because you'll need an inner FOR loop to loop through the array of external strings returned), why not
simply define multiple dictionary instances? Now you can't have a dataset symbol point to multiple externals within the same dictionary instance, but if you had three dictionary instances, then you could have three repeats, one for each instance.

This creates a new problem. Not all the dictionary instances are going to have entries for all the DataSet symbols. Well, you could use dictInstance.TryGetValue(...) to see if the DataSet symbol exists in a particular dictionary instance. You're going to need some kind of a loop for each dictionary instance to contain the code below:
CODE:
Please log in to see this code.

So I'm not sure if this is that much better than using a nested FOR loop solution. :(

How about just having a single FOR loop and having it iterate through a paired list? Then you can dump the Execute() loop altogether and just have the paired-list FOR loop driving everything. Are you going to miss the WL statistics generated by the Execute() loop? Without the Execute loop, you'll need to trade a SetContext() symbol (Bars.Close) with an External symbol (external.Close)--weird. Is that even possible?

All this additional complexity for a few duplicates. :(
I think it would be easier just to put the duplicates into separate strategies, which you could run independently. How many strategies would you need for that? You'll need a separate DataSet for each strategy.... The nested FOR loop solution is looking better where the dictionary lookup will return a string array of external symbols.
profile picture

Eugene

#19
Mark, I too think it'd be much easier for OP to "just have 2 or 3 strategies that have all the pairs but no duplicate externals" as he said. Borderline cases like that are responsible for ugly workarounds in otherwise elegant and terse code. ;)
profile picture

wmrgrove

#20
Superticker, thanks for the quick reply and helping!

Wish I could understand better what you just said :) , but I think you last comment is the easy and effective answer,

QUOTE:
" I think it would be easier just to put the duplicates into separate strategies, which you could run independently. How many strategies would you need for that? "
Answer; 2 or 3, 4 at the most!

Here is a quick attempt to answer the "why multi externals with the same symbol";

So you are looking at an Industry sub-group that has 15 tickers, you "rule-out" say 5 due to volatility, gaps, other reasons. You take the remaining 10 tickers and test for correlation over X period, then backtest for the ratio ( A / B ) of the tickers to move X standard deviations up and down over time creating repeatable profit opportunities. You analyze the results and find the following;


Ticker 6 is good with tickers 5 & 7 & 9
Ticker 7 is good with tickers 6 & 9 & 5

so you pair up 6 external with 5
so you pair up 7 external with 6
so you pair up 9 external with 6
so far so good
now you pair up 5 external with 7
still ok
now you pair up 9 external with 7 or, 7 external with 9 and either way you have two external's that are the same

In general you only want to have 1 or 2 trades on in the same sub-industry to stay diversified, but first you need too get valid signals from the many good pair possibilities. In Pairs trading you are short one and long the other in equal dollars, dramatically reducing overall market risk ( like yesterday). For this reduced risk you get much lower average returns 4%/5% with 75%/80% winning trades, so maybe 2+% average net win, but in 5 to 25 periods, maybe 15 period average.

profile picture

superticker

#21
QUOTE:
... it'd be much easier for OP to "just have 2 or 3 strategies that have all the pairs but no duplicate externals" as he said. Borderline cases like that are responsible for ugly workarounds ...
I thought of another "ugly workaround" that's not extremely ugly.

For Apple, AAPL, with 3 externals symbols, insert AAPL, AAPL.1, AAPL.2 into the dictionary and DataSet uniquely. Then in the Execute() loop, WL is going to try to find "AAPL.1" in cache, which will fail. So you need to SetContext(AAPL) such that you truncate off the decimal portion of it. Then Bars.Close should point to AAPL.

I agree ugly workarounds create bugs in twisted code. I would get everything running correctly with simplified code first, then try implementing the ugly workaround version. And I'm not even sure this workaround will even agree with WL.

Finally, calling SetContext() will create overhead in WL, so I would avoid calling it unless you really need to.

---
EDIT: Another thought would be to use {"AAPL","AAPL/","AAPL//"} for your duplicates. The Dictionary data type would consider these strings different. And I think WL DataSet type would consider symbols with the "/" as different but would automatically ignore the "/"s when processing so you won't need the SetContext() call.