Am I having a context issue due to external symbol use?
Author: wmrgrove
Creation Date: 12/14/2017 3:22 PM
profile picture

wmrgrove

#1
Hello Eugene and or Cone, hope you guys are doing well.

I am having a problem with the attached strategy. It was created in rules and then the "spread" using SPY (external symbol) was added.

My problem is the Buy Long and Sell seams to be working fine but the Sell Short and Cover is not. The Sell Short looks good but the Short Cover does not happen most of the time when there is a signal.

I am 16.9.19.0 build 1, on windows 10, and my guess is I have a Context Issue due to the External symbol use. If I " // " out the Short code the long works fine, if I " // " out the Long code the Short still is not correct.

It has been a few years since I asked for help and was hoping you could help me see what is wrong here.

I have the external symbol set to SPY for testing and you can run on any list like sp500 or Nas100.

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

Eugene

#2
Hello William,

QUOTE:
My problem is the Buy Long and Sell seams to be working fine but the Sell Short and Cover is not. The Sell Short looks good but the Short Cover does not happen most of the time when there is a signal.

I don't know what is "fine" for you and what is "not". Could you clarify why exactly do you think that the short side is not correct after uncommenting the // ? In other words, what are the trading rules for the Correlation conditions in plain English?

P.S.
I've renamed the misleading and loud title "PAIRS TRADING STRATEGY CODING PROBLEM" to something slightly more appropriate and without non-descriptive labels like "coding problem". How would one search the forum for solutions when virtually every poster comes here with a problem of sorts? Also, I doubt this is a "pairs trading" strategy nor you can build one using rules - just a single-position

A pairs trading strategy sets context and trades multiple symbols at once. See what it might look like:

https://www.wealth-lab.com/Strategy/Details/380
https://www.wealth-lab.com/Strategy/Details/189
https://www.wealth-lab.com/Strategy/Details/185
profile picture

Eugene

#3
P.P.S. Made the code more readable:

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

wmrgrove

#4
Eugene, thanks so much for the quick reply and the "much more readable code"! It functions as desired.

Yes my problem description was not very descriptive ( and loud ), and thanks for improving that.

You are correct that the code as it exists is not a pairs trading strategy. I was trying to create a strategy code that would allow me to view the signals on a chart when a "Pairs Ratio's Std Dev" crossed a certain level and the correlation level of the pair was above a certain level for multi time periods ( 100 or 200 periods). Also the Visualizer Tab "By Symbol" will show me trade count/occurrences by symbol, and current levels of key indicators added at the top of the code, such as Std Dev of Pairs Ratio extremes.

I will study the linked strategies in your message for more ideas, thank you.

One last request if I may; In the past in Rotation Strategies I have used the function , LastPosition.Priority I tried adding that at the bottom of your more readable code ( last line ) with LastPosition.Priority = corr200[bar]; without success?

Also I would like to close the Long and or Short after "X" days, if a sell / cover signal does not occur but nt sure the correct method. Any help would again be much appreciated.

Anyway, Happy Holidays to you and yours!
profile picture

Eugene

#5
QUOTE:
In the past in Rotation Strategies I have used the function , LastPosition.Priority I tried adding that at the bottom of your more readable code ( last line ) with LastPosition.Priority = corr200[bar]; without success?

Could this be due to not following the convention?
CODE:
Please log in to see this code.


QUOTE:
Also I would like to close the Long and or Short after "X" days, if a sell / cover signal does not occur but nt sure the correct method.

Please give this a try:

CODE:
Please log in to see this code.


May you have a great holiday too!
profile picture

wmrgrove

#6
Eugene, your changes / improvements function as expected, thank you.

I have searched the Forum trying to understand more about the "By Symbol" visualizer function.

My questions is, why does the "By Symbol" visualizer function tab, not show up, after a back-test on the following strategy code. I used the strategy with this code for a few hours yesterday after minor changes ( adding a Chart Panel and Std-Dev of the Ratio var ) and the "By Symbol" visualizer function tab was there every time. Today when I load the Strategy the "By Symbol" visualizer function tab does not show up. I recall this happening in the past with another strategy.

Is there an add / mod to the code that will always bring up the "By Symbol" visualizer function tab on a multi-symbol back-tests of a data-set? Have I given you enough information?

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

Eugene

#7
QUOTE:
My questions is, why does the "By Symbol" visualizer function tab, not show up, after a back-test on the following strategy code

Please find your question answered in the User Guide > Preferences > Performance Visualizers > By Symbol.

QUOTE:
Is there an add / mod to the code that will always bring up the "By Symbol" visualizer

It applies if your strategy is designed to trade multiple symbols with SetContext - like Rotation and pair trading strategies.
profile picture

wmrgrove

#8
Eugene; As it is Saturday I continued to work or the question in the above code.

I found an error where there were 2 "BUY lines" ;

CODE:
Please log in to see this code.



That is my error and I corrected it below and it does not appear to be related to my "By Symbol" visualizer function tab question.

My questions is still, why does the "By Symbol" visualizer function tab, not show up, after a back-test on the following strategy code. I used the strategy with this code for a few hours yesterday after minor changes ( adding a Chart Panel and Std-Dev of the Ratio var ) and the "By Symbol" visualizer function tab was there every time. Today when I load the Strategy the "By Symbol" visualizer function tab does not show up. I recall this happening in the past with another strategy.
Today after saving and closing it a few times the "By Symbol" visualizer function tab came back.

Also if we want to add a "name" to the buy code that will show up in the TRADE Tab as Entry Name how would we modify this line;

CODE:
Please log in to see this code.


Thanks again for all your help!
profile picture

Eugene

#9
QUOTE:
Also if we want to add a "name" to the buy code that will show up in the TRADE Tab as Entry Name how would we modify this line;

Have you exhausted the QuickRef looking for answer to this basic question? At any rate if you revisit it, you will find it answered straight there as this is where every trading function is documented - with examples. Please look no further.

QUOTE:
My questions is still,

You have probably overlooked my answer above in post #7.
profile picture

wmrgrove

#10
Eugene, yes I have studied the documentation and am still struggling with my code and the "External symbol / set context issue" crashing Wealth-lab. I do almost always try to answer my questions with the documentation, the forum posts, and strategy examples in Wealth-lab.

In the attached Strategy all I am trying to do is Chart View;

an External Symbol (SPY)

any selected Data-set Ticker

and the spread of those two Tickers

Viewing the two Tickers without the spread works, viewing the data-set ticker and the Spread works, but viewing all three crashes Wealth-lab.

Any help on this last question would be appreciated.


CODE:
Please log in to see this code.


profile picture

Eugene

#11
Please start using CODE tags and paste your code inside them (the # button nearby B, I, U, S...) This is how I made the pretty syntax highlighting to appear in your 4 posts above - but now you can do it.

While I leave it up to you where you're going with this code, the lack of curly braces makes it incorrect for whatever it's intended to do:
CODE:
Please log in to see this code.

The foreach only applies to the adjacent line. I've removed it since it doesn't belong here. You probably dropped it here in an attempt to make that visualizer work with your code, isn't it. Your strategy is not designed to trade multiple symbols with SetContext - like Rotation and pair trading strategies do. To make the visualizer appear run the code as a Multi-Symbol portfolio backtest.

Nothing crashes here for me:
CODE:
Please log in to see this code.


P.S. If GetExternalSymbol sometimes won't work try to call it specifying the DataSet name. It does the trick: see this.

CODE:
Please log in to see this code.


Good luck with your venture.
profile picture

wmrgrove

#12
Eugene, thanks got it!
profile picture

wmrgrove

#13
Eugene, I'm looking for a little more help on my strategy code please. ( see message #6 above )

I have studied the documentation, and lots of existing strategies but not able to get my profit sell / cover idea to work.

Here is what I would like to do; ( The full code is in message 6 above )

We have a var;
CODE:
Please log in to see this code.


I would like to add, sell the open long position with;

IF the (( ratio value today - ratio value on buy date ) / ratio value on buy date ) * 100 > 5

And for the short position;

IF the (( ratio value on buy date - ratio value today ) / ratio value on buy date ) * 100 > 5


In this strategy the line above represents the profit of the "Spread" , not the price of the tested ticker. This would allow me to sample a list of like industry tickers quickly to compare which tickers had the most profitable trades in X period of time.

One last question, on a sell / cover rule I thought there was a way to display the value of an indicator (of your choice) when the sell occurred but can't find any reference to it anywhere? I know we can add a name like "profit target" to the rule, but can we add an indicator value? Here I would like to add the value of the ratio.

Thanks for any help, Bill
profile picture

Eugene

#14
QUOTE:
IF the (( ratio value today - ratio value on buy date ) / ratio value on buy date ) * 100 > 5


Bill, you have everything to come up with your idea in code just looking at post #6. Have you noticed this:

CODE:
Please log in to see this code.


The EntryBar property (please refer to the QuickRef for more detail) is what you need to refer to the ratio value on buy date:
CODE:
Please log in to see this code.

Where "p" is the Position variable. Something like this (coding on-the-fly):

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

wmrgrove

#15
Eugene, thanks for quick reply and help, got it!

This works;

CODE:
Please log in to see this code.



My 2nd question from above, could have been clearer

" One last question, on a sell / cover rule I thought there was a way to display the value of an indicator (of your choice) when the sell occurred but can't find any reference to it anywhere? I know we can add a name like "profit target" to the rule, but can we add an indicator value? Here I would like to add the value of the ratio. "

I am looking to see if there is a way, after a back test, to display in the TRADES TAB, under the exit name and enter name column's, the value of "ratio" on those dates ( enter & exit ) ? Hope this is clearer.

Thanks again, Bill
profile picture

Eugene

#16
QUOTE:
I am looking to see if there is a way, after a back test, to display in the TRADES TAB, under the exit name and enter name column's, the value of "ratio" on those dates ( enter & exit ) ? Hope this is clearer.


Here's an example: Trade name based on indicator value
profile picture

wmrgrove

#17
Eugene, thanks for the link, I think I've got it, "very cool"!

This is the way I am using it;

CODE:
Please log in to see this code.
for long position.

and slight mod for short position;

CODE:
Please log in to see this code.


Checked the values and appears correct, this shows the true return of the pairs trade.

Do you see any problem with this as written?

Really appreciate the help
profile picture

Eugene

#18
Looks OK on the surface.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).