Buy/sell signal using external symbol SPY or .SPX
Author: vvtom
Creation Date: 9/11/2017 8:11 PM
profile picture

vvtom

#1
I want to run various datasets against a rule that says
1. Buy/Maintain 10 stocks when the 10 week EMA on SPY crosses ABOVE the 40 week EMA on SPY.
- use a 18% stop on stocks in the portfolio
2. Sell all stocks when the 10 week EMA on SPY crosses BELOW the 40 week EMA on SPY.

Example chart attached of a crossover on 4/4 2016
profile picture

Eugene

#2
This question is asked pretty frequently. Here's a few matching examples to use external symbols in a code-based strategy:

External Symbol with Indicator
Implementing an external symbol filter

Or you can do the same interactively in a Rule-based strategy after installing Community.Rules. Before you can download it, create a support ticket to verify your account.
profile picture

vvtom

#3
I set up a rules-based Strategy using External .SPX
When I converted it to code it looks like the code below.
It doesn't look right (I haven't code in 6 years).
and it doesn't find any crossovers on .SPX

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

Eugene

#4
Irrespective of what it looks, the code is correct and undoubtedly produces trades (on ^GSPC using Yahoo as I don't have access to Fidelity's .SPX data):

profile picture

vvtom

#5
Thanks Eugene!
profile picture

vvtom

#6
Is there a way to "lock in" weekly (instead of daily) or does it have to be reset every time?
profile picture

Eugene

#7
What does "lock in" the bar scale mean? In this code or for all symbols?
profile picture

vvtom

#8
see .png attached
the red box shows the "Scale". I assume (maybe incorrectly) this applies to the rules as well as graphs.
I want the 10-40 EMAs to be weekly averages.
profile picture

Eugene

#9
Yes, this applies to all DataSeries if you Execute the backtest again after changing Scale to Weekly. But if you want to limit it to just some rules that deal with external symbol's data, this can't be (easily) achieved using Rules (if possible). No obstacle for code-based strategies, though. Below please find some pointers and of course check out the WealthScript Programming Guide > Multi-Time Frame Analysis.

SetScaleWeekly & GetExternalSeries?
Adding a Weekly-bar overlay to a Daily-bar Strategy

Here's how it can be applied to your strategy. Make sure to load enough data (at least 120 bars). The reason is described in the same Guide, Indicators > Stability of Indicators chapter.

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

vvtom

#10
I want to apply the 18% stop during the UP period, so I think it should look like this:

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

Eugene

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

vvtom

#12
Referencing the Programming manual, "XU" must be a comment meaning cross under.