I am a working dad with no coding knowledge and I have a pairs trading strategy I wanted to test, but I can't figure out how to write it. I was wondering if you would be able to help. Here's the idea:
1. If Stock1 goes down > -5% over a 10 trading day period and if Stock2 goes down < -5% (or even goes up) during that same period, buy Stock1 at the close of that 10 day period.
2. Hold Stock1 and sell after 30 trading days.
3. Compare to S&P500 over that same period.
Is that something simple to write? I looked at the pairs strategy scripts, but none seem to be able to do that.
Size:
Color:
At first glance it seems pretty simple. Actually, this doesn't even look like a pair trading strategy where you take the opposite side in Stock2. It's rather an external symbol based strategy. But before coding it I'd like to make sure we're not overlooking something.
Next, this requirement looks perfectly ambiguous:
"goes down < -5% (or even goes up)". So should it go down or up or it's indifferent?
Finally, what does "compare to S&P500" in #3 exactly mean? What should the strategy do?
P.S. Type in your Stock2 in the code where appropriate and then run this on the chart of Stock1:
CODE:
Please log in to see this code.
Size:
Color:
Thanks, Eugene.
1. Here is what the conditions would be. stock1 < -5% down over a 10 day period AND stock2 > -5% over the same 10 day period. If both conditions are met, then buy stock1. Hold stock1 for 30 days. After 30 days, sell stock1.
2. Let's say we backtest the strategy over a 5 year period and assume we invest $5000 in the strategy. What I'd like to do is to display a chart of investing $5000 in the strategy and investing $5000 in the S&P500 index and then charting both returns on the same graph.
Thanks again!
Size:
Color:
Size:
Color:
This is great. Thanks!
How would I set a stop loss? Let's say I want to sell the stock at any point in the 30 trading day period if it dips below 4% of the purchase price?
Also, is it possible to hold multiple positions at once? For instance, I noticed in backtesting this that if the conditions held true during the 30 day period in which the stock was being held, it did not trigger a buy. Is there a setting where multiple positions can be held simultaneously?
Last thing: can I run the strategy without having to highlight a dataset? For instance, let's say I want to run it with AAPL and IBM. I tried changing stock1 to var stk1 = GetExternalSymbol("AAPL",true);
Yet, if I run the strategy and have a different stock highlighted in the dataset, it will still test that stock and not AAPL.
Thanks again for your help! I'm slowly getting the hang of this.
Size:
Color:
CODE:
Please log in to see this code.
Size:
Color:
Thanks. However, it still doesn't allow me to buy the same stock on different bars within the same holding period if the conditions hold true.
Size:
Color:
You probably edited your question while I was in the process of replying. There was just the stop loss question at that time.
This is for multiple positions (and for running on AAPL when IBM is the 2nd symbol):
CODE:
Please log in to see this code.
Size:
Color:
One other thing that wasn't addressed in my last post. What if I want to backtest a dataset of stocks, but I want to pair each individual stock in the dataset with a specific stock in the strategy.
For example:
dataset:
AAPL
GOOG
I want to pair AAPL with IBM and I want to pair GOOG with FB, but the code only allows for one highlighted stock in the dataset. Is there a way to bypass using the dataset and just enter the stocks into the code? For instance, stock1=AAPL, stock2=GOOG, stock3 = IBM, stock4=FB and run the backtest without pulling anything from the dataset?
Size:
Color:
Size:
Color:
Thanks, Eugene. I tried experimenting with your examples, but I get the following errors:
CODE:
Please log in to see this code.
I get the following errors when compiling:
The best overloaded match for 'WealthLab.Indicators.ROC.Series(WealthLab.DataSeries, int)' has some invalid arguments
Argument 1: cannot convert from 'string' to 'WealthLab.DataSeries'
Basically, I want to run the strategy for pairs of stocks. Let's say one pair is AAPL / IBM another pair is GOOG / FB. I want to be able to put all the pairs in the script and run the strategy for a set date range.
Thanks again. I really appreciate your help.
Best,
Dan
Size:
Color:
Dan, please try this out and let me know if it fits:
CODE:
Please log in to see this code.
Size:
Color:
No, unfortunately, it is still pulling from DataSets.
I don't want to pull anything from DataSets. I want to put in a group of n pairs in the code and run the strategy over a date range with a position size.
For instance:
Pair 1: AAPL and IBM
Pair 2: KO and PEP
Pair 3: GOOG and FB
I want to run the strategy and have it summarized for all three pairs (or n pairs assuming I will be adding pairs) simultaneously over the same period.
I feel like this was working for the Vix Pairs strategy, but I can't seem to get it to work for mine.
Thanks again for your help!
Size:
Color:
Let's agree on definitions first. What does it mean to "pull from DataSets"? In this code there's one pair defined (AAPL and IBM). What you've defined is being pulled. Have you tried to add a couple more pairs using code in post #12? Finally, what is to "have it summarized"?
Size:
Color:
QUOTE:
What does it mean to pull from DataSets?
When I run the strategy, it requires me to have a stock selected in the left hand "datasets" window. The trades created from running the strategy reflect the highlighted stock, not stock1 or stock2 from the code. I want to be able to list n sets of pairs in the code, run the strategy, and have the results reflect the stocks listed in the pairs definition from the code.
QUOTE:
Have you tried to add more pairs using code from post #12?
Yes, I created stock 3 and stock 4 using another set of pairs, but the results didn't list trades from those stocks
QUOTE:
What is to "have it summarized"?
When I run the strategy, the results of the trades are listed in the trades tab.
I would like to be able to list n pairs of stocks in the code and have the strategy run for the parameters for the stocks listed in the code.
Thanks,
Dan
Size:
Color:
Run this on a DataSet that includes all the 6 stocks. Is this closer to what you're looking for?
CODE:
Please log in to see this code.
Size:
Color:
Hi Eugene,
Yes, it works! Thanks for all your help. Happy New Year!
Dan
Size:
Color:
Dan, glad to have helped. Merry Christmas and happy New Year to you too.
Size:
Color:
Is it possible to use something like GetTime to change the strategy to buying and selling at 4:00 PM EST instead of Close?
Size:
Color:
Size:
Color:
Thanks. I tried using the code, but I was unable to get it to work. I used TodayCloseTime strategy parameter and set the close time at 3:00 PM EST. What I believe is happening is Wealth Lab is using the actual 4:00 PM EST Close Time price from the DataSets (using Fidelity Investments as a Data Provider).
Here is what I need:
At 3:00 PM EST, I want to be able to run the script and get alerts to make trades at the current price of the stock. Since the script uses the close times of the stock for the previous 5 trading days, I want it to BuyAtClose, but I want BuyAtClose to use the 3:00 PM price of the stock for the previous 5 days, rather than the actual close time. Does that make sense? Is there a BuyAtCurrentPrice command?
Size:
Color:
I was under impression that you were trying to sell a position around the close but now you're asking for timed entries. But unlike your Strategy which is run on daily data, the Wiki page I referred you to is applicable to strategies running on intraday data. If you were to apply the pattern to the strategy in my post #16 you'd have to use SetScaleDaily/RestoreScale for building and plotting your ROC series.
Fortunately, there's a simpler way: just use the Strategy Monitor and set "Execute at: 15:00" using EOD data.
Size:
Color:
The way I envision it is to Run the Strategy at 15:00, Wealth Lab will use the live stock prices at 15:00 rather than EOD and then create BUY or SELL alerts based off the current stock price data. I care less about backtesting now and would like to use the strategy to create actions in the alerts tab where I can place orders. The issue that I'm having is that the strategy creates an alert to buy the stock at EOD price, but the market has already closed. If I place an order to buy the stock, Fidelity would not buy the stock until the next day's open, where the prices has changed, thus invalidating the strategy.
Size:
Color:
QUOTE:
Fortunately, there's a simpler way: just use the Strategy Monitor and set "Execute at: 15:00" using EOD data.
I should've thought twice before saying that: the bar is incomplete.
However, what you envision does not make great sense too for the same reason (peeking / incomplete data). If the close to open difference is powerful enough to invalidate the strategy, why not use limit orders or switch to intraday data instead?
Size:
Color:
How do I switch to intraday data? In the strategy, I would like to use the stock price at 3:00 PM instead of the price at close. I know Fidelity has intraday time scales. Is there a way to do that? Another idea I had is to create an ASCII file and manually replace the close price with the price at the time I want to make the trade. However, since I would need to create separate data files for each stock, it would be nearly impossible to do every day.
Size:
Color:
Is there a way to batch create ASCII files to use for DataSets? I could manually set the EOD price to the price at 3:00 PM and then use those data files. Is there a way for Wealth Lab to import multiple symbols in one ASCII file? How do I format the ASCII file?
Size:
Color:
This thread has developed into complete offtopic and thus had to be splitted from post #21 and below. My bad; I usually manage this better to keep the forum in order and searchable. Here's my closing comment to it.
QUOTE:
At 3:00 PM EST, I want to be able to run the script and get alerts to make trades at the current price of the stock. Since the script uses the close times of the stock for the previous 5 trading days, I want it to BuyAtClose, but I want BuyAtClose to use the 3:00 PM price of the stock for the previous 5 days, rather than the actual close time. Does that make sense?
That still does not make any sense. Your backtesting results and signals based on running it on EOD data will get out of sync with the discretionary signals taken at 3pm.
But if you still wish to try this out, create a new DataSet using Yahoo! data provider instead of Fidelity. Its option "Always return data with partial bar" (Data Manager > Yahoo! Data tab) lets you request an update in the middle of the session and thus trade this incomplete daily bar. At 3pm you could either run the system manually in the Strategy window (update the Y! DataSet prior to doing that) or, as suggested above, set the Strategy Monitor to "Execute at: 15:00" using new Yahoo! DataSet (keep WLP with the SM window open at all times for the scheduler to work).
Please bear in mind that follow-up questions not regarding to the "ROC-based pair trading strategy" directly (if you still have them) should be asked in more appropriate threads, not here.
Size:
Color: