Trying to Buy from a Sorted List
Author: robert-9876
Creation Date: 11/9/2010 12:54 AM
profile picture

robert-9876

#1
I'm trying to buy from a Sorted List
I'm trying to implement a strategy that uses a "Market Timer" and a Search/Sort.
I've looked at the Weak-stock Rotation Strategy and Dogs of the Dow Strategy. (very helpful)
In my example, an 8-day ema crossing a 20-day ema of symbol VTI is used to signal entry & exit of Long Positions at the Next Day's Open (NDO).
The search puts all symbols from the DataSet whose Price/Sales Ratio is > 0 into a List.
Then, sort the List in ascending order.
Enter = Buy the 5 stocks in the List with the lowest Price/Sales Ratio at the NDO.
Exit = Sell all positions at NDO.
This code seems to be working in that, on each buy signal, 5 positions are being bought, and on each sell signal, all positions are being sold,

But ... on the buy signal one or two of the symbols are being bought more than once, (multiple positions of a symbol are being bought). I would like only one position to be bought per symbol, so I'd like 5 different stocks to be bought on each buy signal.

I'm using Position Simulation Mode
Position size = 20% of Equity.

Margin factor 1 to 1.
WealthLab Pro 6.0.55.0
Scale: Daily
Date Range: 2 yrs
Computer: Intel P4 Dual Core, running Vista (32-bit), 3 GB Ram, 200GB HDD.

Any advice would be appreciated.


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

robert-9876

#2
Any thoughts?
Do I need to restate or simplify my question?

Best regards, Robert
profile picture

Cone

#3
What's NDO?

You've made 2 big mistakes (at least):
1. DataSeries psr, used in the numeric sort, is created only once for the primary symbol. In other words, psr is the same DataSeries for all symbols.

2. The {} statement block delimiters are out-of-whack, so it's difficult to see that you've put the entry logic inside the DataSetSymbols loop. I don't know how you can get the blocks so out of line since the Editor automatically corrects them, but that's the problem.
profile picture

Cone

#4
Try this -
CODE:
Please log in to see this code.
profile picture

robert-9876

#5
Cone,
Thanks very much! I'll check my DataSeries & try your code.

NDO = Next Day's Open

The {} weren't out-of-whack, until I tried to make the comments easier to read after I pasted code between the [code tags]. I guess I got them messed-up at that time. I didn't notice the whackiness until after it was posted. sorry.

Best regards, Robert
profile picture

robert-9876

#6
Cone, Thanks again.

FYI - Your code seems to be working fine. Sometimes it still bought multiple positions in some symbols, but I changed the Position Size

from: Percent of Equity = 20%

to: PosSizer = One Trade per Symbol
Configure, Pct of Equity = 20

And now it only gives one position per stock symbol, just like I want.

I also had to increase margin from 1 to 1, to 1.2 to 1, so I'd always get to buy all 5 stocks.

Thank you and best regards,
Robert
profile picture

Eugene

#7
QUOTE:
Sometimes it still bought multiple positions in some symbols

Just wondering, are you sure the code is being run in single symbol mode?
profile picture

robert-9876

#8
Hi Eugene, I'm not really sure, I'll have to look at that part of it again (I'm still new at this stuff).

I thought that since I removed all but 5 stocks from my sort_list (confirmed by PrintDebug), and then only bought from sort_list, that those 5 stocks & only those 5 stocks (one position for each) would be bought. But that doesn't happen with Portfolio Simulation Mode: Percent of Equity = 20%. It bought from sort_list, but sometimes one or two stock symbols would get multiple positions.

From DebugPrinting, it seems that the whole trading loop is run for each symbol in the DataSet, rather than going through the trading loop once and checking each symbol in the DataSet every bar. Anyway, I'll go back to some of my older Strategies, which were definately single symbol mode, and figure out what I removed. You're probably right.

Thanks & Best regards, Robert
CODE:
Please log in to see this code.
profile picture

Cone

#9
When you run a script like this one (with a DataSetSymbols loop), you should click on only ONE symbol in the DataSet, not on the DataSet name. (See the instructions for RSI Rotation, for example.) Otherwise, the script is repeated unnecessarily (and incorrectly) for every symbol in the DataSet. That's why you're getting duplicate trades. The symbol that you click on doesn't matter, but it should be the one with the most complete history.
profile picture

robert-9876

#10
Cone, OOOhh. I'm sure I'm doing that wrong. Thanks! I'll also go back and re-read the RSI Rotation Strategy. Best regards, Robert
profile picture

robert-9876

#11
I think this will be my last comment on this topic. Everything seems to be wotking properly now. That fixed it when I only selected one symbol in the DataSet, then ran the strategy. I couldn't find the RSI Rotation example, but the Dogs of the Dow Rotation Strategy, does state that only one symbol in the DataSet should be selected before running the strategy. Also, good point to select the symbol with the most history.

Thank you for all of the help. It was very,.. helpful.
profile picture

Eugene

#12
Just for the record, this has been put on the Wealth-Lab FAQ many months ago:

FAQ | Strategies and WealthScript > Rotation strategies > A Rotation strategy isn't working like it should.
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).