What is a Script on TrendSpider
Scripts are what you expect them to be: A set of conditions which translate your criteria into the format that TrendSpider can understand.
Before we dive into the details, here’s an example of a script with a humanized explanation to give you a sense of what’s what.
All of: 30min SMA(20) Crossed Up 30min SMA(70) Daily Volume(20) Greater Than Daily Volume(20).SMA Any of: 30min RSI Less Than 60 30min Price.Close Greater Than 30min Bbands.Middle
If this is an alert, then it triggers when the 30 min chart SMA(20) crosses up SMA(70), and at the same time volume for the corresponding day is greater than average daily volume for the last 20 days, and at the same time either RSI is greater than 40, or the recent Close is greater than Middle line of Bollinger Bands(r) on 30 min chart.
Now, let’s get to the theory.
A script is a set of conditions, joined with AND, OR or NOT logical operators. Each condition consists of a parameter and an operator, such as “Greater Than”,”Less Than”, “Touched by price” e.t.c.. Conditions can be combined into blocks and you can have multiple blocks in each script—including blocks inside of blocks.
You can use scripts in conjunction with the Strategy Backtester, Market Scanner or Multi-Factor Alerts.
How to use prices action (candles) in scripts
You can use the open, high, low or close price of any candlestick (or a Raindrop, or a Heikin Ashi candle) within a script.
- Price closed above yesterday (
Daily Price.Close > DailyPrice.Close (1 candle ago))
- Price closed higher than the open (
Daily Price.Close > Daily Price.Open)
- Price closed above yesterday’s high
All of: 30min Price.Close > 30min Yesterday’s Range.High 30min Price.Close > Daily Price.High (1 candle ago)
Suppose that you write the script,
1h RSI(14) Crossed 80.The TrendSpider scripting engine would interpret that as “the last value of RSI(14) built on a 1-hour chart has just crossed the level of 80”.
In this case, it refers to the “last value” implicitly, but what if you want to know if the RSI crossed the level 80 upwards or downwards?
One way to do this is to add another condition to your script:
All of: 1h RSI(14) (last) Crossed 80 1h RSI(14) (1 candle ago) Less than 80
The “1 candle ago” is an offset and the example is just one case where you may need offsets to define a strategy. Of course, in this case, we have “crossed up” and “crossed down” operators that may save you some time!
Using Candle Patterns in Scripts
TrendSpider includes hundreds of different candlestick patterns that you can use in scripts.
There are three core settings for each candlestick pattern:
- Timeframe (e.g. “30 min”)
- Candlestick Pattern (e.g. “Abandoned Baby Top”)
- Offset (e.g. “last” or “X candles ago”).
All candlestick scripts make use of the “Evolved” operator, which triggers when the candlestick pattern occurs (means when the last candle of a given pattern finalizes).
Using Indicators in Scripts
TrendSpider includes hundreds of different indicators that you can use in scripts. Every indicator which you can add on your chart, you also can use in scripts.
There are three settings for each indicator:
- Timeframe (e.g. “Daily”)
- Indicator with its input parameters (e.g. “Simple Moving Average(50, close)”)
- Offset (e.g. “last” or “X candles ago”)
Most indicators are compared with other indicator values. For example, the moving average crossover script above looks for a bullish crossover of the SMA(50) and SMA(200).
There are several indicators that may not be very useful on a chart, but they can become tremendously helpful when building scripts. These special indicators include:
Range gives you the highest high, lowest low and average values for a given period of time. This indicator is essentially an alias for the Donchian Channel. In the example below, the script triggers when the price made a 30-day high within five minutes. You could also use `Daily Range(30).High Increased`, but the script wouldn’t trigger until the daily candle closed.
Candle Range gives you the average wick size and body size for a given period of time.
Candle Time gives you the time of each candle in military time format (e.g. “1130”), enabling you to do things like “if time is greater than 10:00 and less than 12:00” in your scripts (especially helpful if you can only trade at certain times). In the example below, the script triggers if the RSI crosses above 30 prior to 11:00 AM if used with “All of the following”.
Change % gives you three out series:
- Change Percent, since X candles ago
- Avg. Change Percent, over the last X candles (use “MA Length” input)
- Cumulative Change, over the last X candles (use “Sum. Length” input)
For example, here's how you identify 3 days in a row when price was growing
All of: Daily Change%(1) (last) Increased Daily Change%(1) (1 cdl ago) Increased Daily Change%(1) (2 cdl ago) Increased
Case study: using a Script in Backtester, Scanner & Alerts
Let’s take a look at how to use scripts as an entry condition for the Strategy Backtester, Market Scanner or for Multi-Factor Alerts. We’ll pretend like we’ve got a strategy (we’ll use a very naive example, sorry!) and see what you can do with that on TrendSpider.
1. Build a script for your entry conditions
Start by clicking Scripts in the top menu bar and search for “MACD Cross Up” — a simple pre-built strategy created by the TrendSpider team. Let’s assume that it’s the entry criteria of your own very secret and powerful strategy.
2. Backtest your strategy to get a sense of how it behaves
The first thing you should do when designing or assessing a strategy is backtest it to get a sense of how it behaves. Click “Send To” and choose “Strategy Explorer: backtest as Entry” to start a backtest using the MACD Cross Up as an entry condition. The popup window will close and you will see the Strategy Tester open at the bottom of your screen.
For simplicity, let’s assume that your strategy uses fixed exit conditions (a combination of Stop Loss and Take Profit). In the Exit Conditions on the right, set the Take Profit to “Exit if gained more than 15%” and the trailing stop to “Trailing stop at 3%”. Click Test to run the backtest and see the result.
In this case, you can see that the strategy generated a 0.37% gain compared to a 1.62% buy-and-hold loss over the time frame, although there were twice as many losers as winners.
This is not looking very promising, and I would not trust this strategy with my account, but for our purposes, let’s assume that you’re happy with the outcome and want to start trading it immediately.
3. Find markets where you can enter immediately
Let’s find the markets where our entry condition is currently active, so we could enter right now. Use the Market Scanner to identify opportunities in the SPX500 or Russell 2K to see if any symbols meet your strategy’s entry conditions.
Click on Market Scanner in the right side of the top menu bar. Next, configure your entry criteria using the script editor (or you can export your script from the Templates Manager, if it’s already saved in there). Finally, choose a group of securities to scan (e.g. Russell 2K) and click Scan to see the results on the right.
4. If you’ve entered, then set up alerts for your exit conditions and never miss an exit.