Creating Synthetic ETF
Author: innertrader
Creation Date: 2/14/2012 1:43 PM
profile picture

innertrader

#1
I have 10 equity ETFs in my portfolio. When my strategy generates a signal to sell a position I would like to invest the cash in bond ETFs upon sale. In this case there are 4 different bond ETFs that I will start with equal % of cash but would like to have the option to change the weighting of the bond ETFs in backtest or by optimizing the strategy. Here are my questions:

1. I am not sure how to code the sale of one ETF position to then buy 4 new (ETF) positions (in a given percentage) in an amount equal the cash resulting from the sale. Is the best approach to create a custom "synthetic ETF" that is an almalgam of the 4 ETFs (25% each) and then buy and sell the synthetic?

2. Can you point me to a code set that has accomplished something similar?
profile picture

Eugene

#2
Note that synthetic symbols can't be traded; one has to execute trades on actual symbols which are present in a DataSet.

Re: script-driven equity percentage, see this PosSizer. Starting from version 2011.11, "Position Options" supports passing a double value via Position.Tag or SetShareSize to easily backtest a strategy-driven, flexible percent-equity or Max % Risk approach.
profile picture

Eugene

#3
Maybe this looks somewhat related?

ActiveTrader 2011-05 | Inverse ETF switching system
profile picture

innertrader

#4
QUOTE:
Re: script-driven equity percentage, see this PosSizer.


This link is broken.
profile picture

innertrader

#5
OK. I think I found what the link was supposed to point to. I am trying to figure out the value of the Sell trade, so I can figure out how many ETF Bonds to buy. However the position object always returns 1 for .shares and 1*price for .value. So while I can get the exit price I don't know how to get the value of the exit trade.
profile picture

innertrader

#6
I am not able to get the LastPosition.Tag to change the Buy size. It ignores the value I am attempting to pass and appears to be using the standard 10%. Can you point to working code that uses this feature to set % of equity?
profile picture

Eugene

#7
QUOTE:
This link is broken.

Fixed, thanks.
QUOTE:
I am not able to get the LastPosition.Tag to change the Buy size.

You're not assigning the Tag property correctly. I've added a usage example to the Wiki page above.
QUOTE:
However the position object always returns 1 for .shares and 1*price for .value. So while I can get the exit price I don't know how to get the value of the exit trade.

That's by design. In Strategies, the .Shares property always returns 1. PosSizers run after Strategy's execution has been finished.
profile picture

Cone

#8
I'm not sure if I fully understand. Is the intent of the strategy to always keep "cash" in the bond ETFs? Then, when you want to purchase one of the other instruments, you have to free up cash from the ETFs first. Is this the idea?

profile picture

innertrader

#9
QUOTE:
Is the intent of the strategy to always keep "cash" in the bond ETFs? Then, when you want to purchase one of the other instruments, you have to free up cash from the ETFs first. Is this the idea?
Cone: Yes, that is correct. Ideally, I would like to move all cash resulting from Sell signals in the strategy into an ETF bond portfolio (4 positions). In the code ssection that tests for a Sell condition, I have the sell of the Long equity ETF and purchase of 4 new bond ETFs. Ideally I would like to invest (buy) exactly the same dollar amount in the bond portfolio that was generated from the sale of the Long ETF. When I get a buy signal I will reverse the process and sell enough of the bond portfolio to generate cash to buy the Long equity ETF. At least that's my goal. (I am using SetContext and programmatic sizing to try to accomplish this.)

I'm a bit unclear about how the system views a "position." I have 10 equity positions that can either be in a specific equity or in bonds. There are 4 bond "positions" that will be funded from the sales of the equities. However, I suspect the system will still see it as 10 positions (not 14 or 4) and each of the 10 positions will have a value that is either the equivalent of the individual equity or its portion of the bond portfolio. Am I correct?

BTW, the only reason I bothering to model / backtest the exchange with bonds instead of simply adding a value for "risk-free return of cash" in the preferences is that bond ETFs are not risk-free and they have an impact on overall strategy drawdown and other statistics, including overall returns, in either direction.
profile picture

innertrader

#10
QUOTE:
You're not assigning the Tag property correctly. I've added a usage example to the Wiki page above.
Eugene: I think my code is functionality equivalent to what you posted, although yours is a more general solution I can implement later. Here is my code section:
CODE:
Please log in to see this code.


In the PosSizer am enabling the "Use .Tag" button with "% Equity" option. I would expect it to buy 2% of equity, but it appears to be buying 10% instead (10% is my setting for equities). Can you see anything amiss? Also, can you explain why an "if" construction is necessary to set the Tag?
profile picture

innertrader

#11
This code precedes the above snippet, in case it is relevant.
CODE:
Please log in to see this code.
profile picture

Eugene

#12
QUOTE:
I would expect it to buy 2% of equity, but it appears to be buying 10% instead (10% is my setting for equities).

Try disabling the "Skipped trade solution" and see if it makes difference. I looked at the code and think that currently, this option can override "Use .Tag". Let me know how it goes.
QUOTE:
Also, can you explain why an "if" construction is necessary to set the Tag?

In the first place to avoid exception when assigning a value to a Position which is null.
profile picture

innertrader

#13
QUOTE:
Try disabling the "Skipped trade solution" and see if it makes difference. I looked at the code and think that currently, this option can override "Use .Tag". Let me know how it goes.
It didn't go well. If I disable STS all trades are rejected (none execute) regardless of margin set.
profile picture

Eugene

#14
Well, your position sizing must be making every effort to run out of capital - and I don't have enough information to judge how is that possible.

I'll take care of the "Use .Tag" option (next release) to integrate it with STS.
profile picture

innertrader

#15
The error only occurs when I enable Use .Tag. Otherwise the strategy runs fine with 1.1:1 margin setting, even with STS disabled. (Remember, STS doesn't make any difference for me either, so maybe there is a connection.) I even removed the code that does the bond trade and sets LastPosition.Tag = 2 so that the only difference between the working and non working version is enabling the Use .Tag button. Enabling Use .Tag seems to be the only change that makes it fail completely (drop all trades). Any other ideas about what I could look at further?
profile picture

Eugene

#16
STS only works for AtMarket(bar+1) orders, for anything else it has no effect.
profile picture

innertrader

#17
BTW, I had not been setting Use .Tag programmatically for the purchase (Buy) of my equity positions. I included that in case the value that was being passed was random but it didn't make any difference.

I commented out all the strategy code except a single rule for buy and one for sell. The strategy is not buying bonds anymore, but staying in cash. The only place I now set LastPosition.Tag is here:
CODE:
Please log in to see this code.
When I enable Use .Tag it drops all trades. I don't know what else I can try to assist with troubleshooting. If you have any additional suggestions, please let me know.
profile picture

innertrader

#18
QUOTE:
STS only works for AtMarket(bar+1) orders, for anything else it has no effect.
I am only using BuyAtMarket and SellAtMarket currently.
profile picture

Eugene

#19
QUOTE:
I don't know what else I can try to assist with troubleshooting.

No problem, please follow this checklist:

* PosSizer settings
* Margin factor
* Symbol(s), DataSet, data provider, bar scale
* A watered-down code that demonstrates the effect
profile picture

innertrader

#20
I think I've completed this list, the last one being the most recent code I posted. If you run that code in a simulation, are you able get the position to buy 2% of available equity?

Is there some other setting or feature enablement I might have that could causing this failure to trade? If you send me a link to a complete running strategy that uses this feature, I will download or copy it and confirm that it does or does not run for me. If it does run for me I may be able to troubleshoot futher.
profile picture

Eugene

#21
The position size passed via the .Tag property should be a double value. Like shown in my example and in your 2/15/2012 11:41 AM post.

The size drops to zero because you assigned an integer value (2/15/2012 2:02 PM). Change the value to a double and it will work:
CODE:
Please log in to see this code.

I'll handle integer values passed to .Tag (next release); for now, make sure the value is defined as double explicitly:
CODE:
Please log in to see this code.
profile picture

innertrader

#22
Yes, Eugene, that was the problem. Thanks.
QUOTE:
Try disabling the "Skipped trade solution" and see if it makes difference.
You were also correct that STS has to be disabled for this to work. It does appear to override "Use .Tag"

I am now trying to figure out how to sell the bonds when I want to buy equities. I only want to sell enough bonds to fund the purchase at hand. Does programatic Equity % or SetShareSize work for sell or only for buy?

QUOTE:
That's by design. In Strategies, the .Shares property always returns 1. PosSizers run after Strategy's execution has been finished.
Is there a way to get the following data to construct a trade?

1. Position value (to construct a sell using SetShareSize)
2. Account value (to construct a buy or sell using % Equity)

I believe you previously responded (but later edited and removed the response) that what I am doing is causing the system to set up 14 unique positions (10 equity and 4 bond). If this is the case, then will the system will cycle through each one of these positions in executing the strategy, and I will need to add a test to check whether it is operating on a bond position or an equity position to take the appropriate action?

profile picture

Eugene

#23
QUOTE:
Is there a way to get the following data to construct a trade?

Yes but this solution will only become truly usable next month:

WealthScript Techniques | Interacting Dynamically with Portfolio Level Equity

Currently, it suffers from some issues (like incompatibility with SetShareSize and PosSizers, results not always repeatable etc.) which will be fully resolved in the upcoming release 2012.03.
profile picture

Eugene

#24
FYI

Starting from library version 2012.03, the "Use .Tag" option has become STS-friendly.
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).