Trade Specific External Symbol if Conditions are met / not met
Author: cicerotullius
Creation Date: 10/27/2013 11:54 AM
profile picture

cicerotullius

#1
Hi,

I want to do the following:

Have a certain Tardings system that trades certain stocks or ETF. (I have this)

And then Have a condition that "if active positions is 0" or in the more complicated version if not all positions are filled (pe 5 positions but only 4 trades open), then buy for the remaining position something else. Sounds maybe more complicated then it is, so simple example with only 1 position and 2 Symbols.

I have the Dow30 and my strategy trades 1 position of the dow30, for this I have certain entry and exit conditions. If there is no position, then I would want to buy a Moneymarket ETF, as soon as I get a new signal out of Dow stocks, I sell the Money ETF and take the regular position.

Curcial is here: the money market ETF is not part of the DOW30! so its not sz´ymbol rotation --> I also looked at Symbol rotation strategies, but in my case this alternative position / Money ETF is NOT selected from certain conditions inside of the data set (like Symbol rotation would pe: take the one with the lowest RSI or whatever) here its clear: the others or this.

Similar thing I would want to try with:

have a external symbol as a filter, pe VIX and depending if the VIX is above or below a certain value only Trades on set A shall be taken (like all dow stocks, or in simple version like SPY ETF) and if it is below a certain value only Trades on Set B shall be taken (like money ETF ,or whatever)

I know I can do this with two separate strategies and then have one combination strategy, but I would want to know is there some more simple function, like:

for first condition add "if symbol is NOT "ETF"" then... continue with conditions for Dow30 or
if Symbol = ETF then .....

but I cannot find any code that would address directly one symbol.

thx
profile picture

Eugene

#2
Hi,

QUOTE:
And then Have a condition that "if active positions is 0" or in the more complicated version if not all positions are filled (pe 5 positions but only 4 trades open), then buy for the remaining position something else.


Since you're interested in checking whether there are active positions in the portfolio (not in the same symbol), you might find this SymbolIsActive function useful:

Any Open Position (in Other Symbols) in Portfolio Mode

Note, however, that your strategy has to be rewritten for looping through DataSetSymbols in single-symbol mode for checking active positions on portfolio level.

QUOTE:
have a external symbol as a filter


For this, there are numerous examples and pointers. Check out GetExternalSymbol in the QuickRef, DataSeries > Accessing Secondary Symbols in the WealthScript Programming Guide, and downloadable strategies inside the "Intermarket" folder.
profile picture

Supersol

#3
Hi Eugene,

I have a (I guess) similar problem / question.

The strategy I wrote runs as expected in the Strategy Monitor - but it trades not very often, so my idea was to add some other (more or less risk free) instrument (ETF, ...) to the portfolio and let the strategy trade it whenever there is no trade.

In my opinion it is not necessary to use GetExternalSymbol as I am able to simple put the additional instrument into the portfolio.

QUOTE:
Note, however, that your strategy has to be rewritten for looping through DataSetSymbols in single-symbol mode for checking active positions on portfolio level.


This worrys me a little bit as the code from your example is not self explanatory, but apart from this I am not sure if I can reach my goal with this code, because the goal is:

If all symbols do not meet the criteria for an entry (or there are only 4 open from a maximum of 5 positions) - then it is time to take an entry for the ETF.

Can this be accomplished with your single-symbol mode?

profile picture

Cone

#4
In this sense the Strategy Window or Strategy Monitor works essentially the same way: if your strategy depends on knowing the position status of other symbols in the portfolio, you need to process all the symbols in the same run; executing the script on one symbol only. A script's complexity rises significantly when you need to do this (for example, see the RSI Rotation strategy).

Your strategy may not need to be that complex, but the devil is in the details and it would be hard to help more without knowing the strategy's requirements.
profile picture

cicerotullius

#5
Hi,

I still have my problem and could not solve it so far, my strategy is a counter trend strategy that is very very picky, meaning around 100 Trades with multiposition code. Of course good winning percentage, but at the end only a small amount of time invested.

Mz hypothesis is now: since it is designed so picky and is hitting the dips quite well, there should be a big chance that during the time when its not invested, the market is going up. Which can mean either the SPY or QQQ ETF or some Bond or Money ETF. Of course I cannot know until I backtest if this is true.

So I want to do exactlz what I described originally: whenever Im not invested (or not fully invested) I buy the ETF and as soon as I get a signal for tomorrow, sell the ETF and release the money for the signal. The ETF itself shall NOT be traded with the signals, only when there is no signal.

I tried to find some similar strategy among the preinstalled examples to make it more tangible for you and I think one of those 17liner or 11 liner are quite close, they trade a bit more often then my strategy but the principle is the same: a seldom trading counter trend and I want to utilize the money during those non trading times. (if at least with money market ETF to get interest, but maybe even Index ETF)

br
profile picture

Cone

#6
"Snap in" solutions don't exist for this. The logic has to be integrated into your code. Also, if the strategy trades multiple symbols and idea is to use all the free cash to purchase an ETF, so that you're always 100% in the market, then the complexity rises a bit more.

Just thinking about one way you could go about that, and assuming that market orders must be used...

1. Use the "Position Priority" PosSizer to size positions according to the priority value you assign to a position (See Position Object: Priority in the QuickRef, F11). Assuming that you use 20% sizing for 5 max "signal" positions, you'd configure the sizer like this:

CODE:
Please log in to see this code.


2. For "signal" positions, you always assign a priority between 0.01 and 1.01

ETF logic
3. To always make full use of capital for the ETF, the logic buys the ETF everyday and sells it the next day (even though you may buy fewer or more shares again). The priority that you assign to the ETF will determine how much you buy as follows:

3a. Count how many signals (alerts) you have and add it to how many active positions you'll continue to hold tomorrow, which is the ActivePosition.Count minus exits. Be careful to store the AP.Count before actually exiting positions on bar + 1, since executing exits will cause the count to change during backtesting.

3b. Subtract the number above from the number of max Positions (which is 5 in our example) to obtain the priority for the ETF Position.

Example:
Lets say you'll have 3 signals positions (e.g., 1 active position and 2 alerts). That means you need to buy a 40% ETF position. The priority is 5 minus 3 = 2, which corresponds to the 40% position in the PosSizer.

If the number is 0 or less, then the priority assigned to the ETF trade is 0 (for 0% sizing).

Hopefully that helps (and makes sense).

profile picture

cicerotullius

#7
Hi,

Actually Cones Idea with the priority does help. because I anyways use a priority function in my Code.
My Idea would be the following: I add the SPY to the Data Set (thus it will be anyways calculated with all the other symbols) and then I play it though the priority function:

My Prio assgins anayway prio from 0 to whatever to any real prio that shall be taken.

Now I would want to assign to SPY simply a fixed Prio, like 0,0001
Thus it will be always taken when there is no other higher prio.
Of course I have to buy and sell SPY every day, otherwise it would block other positions.

But my problem stays: how can I directly access a symbol in the code to assign certain values? I haven't found anything there.

What I would now more or less need. (only example code I didn't find such functions)

if symbol = "SPY" (Prio = 0,0001);

But I don't know how to dress the symbol...

thx
profile picture

Eugene

#8
QUOTE:
But my problem stays: how can I directly access a symbol in the code to assign certain values? I haven't found anything there.


Have you already checked the numerous examples and pointers?

* GetExternalSymbol and SetContext/RestoreContext in the QuickRef,
* DataSeries > Accessing Secondary Symbols in the WealthScript Programming Guide,
* downloadable strategies inside the "Intermarket" folder
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).