Scripts. Write once, use everywhere: scan, backtest, create alerts.

A Script (a Visual Script) is a set of conditions that translate your criteria into the format that TrendSpider can understand. Building Visual Scripts requires no coding skills. You can use your mouse (or your keyboard, if you're a hotkey guy) to build them. Visual Scripts can be as simple as Price above SMA or as complex as multiple embedded logical AND/OR blocks coding criteria checking mutiple time frames at multiple tickers at once.

Here’s an example of a Visual 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) > Daily Volume(20).SMA
  Any of:
    30min RSI < 60
    30min Price.Close > 30min Bbands.Middle

If you use this script as 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 the average daily volume for the last 20 days, and at the same time either RSI is less than 60, or the recent Close is greater than Middle line of Bollinger Bands(r) on 30 min chart.

Formally, a script is a set of logical blocks (AND, OR, or NOT logical operators). Each block consists of other blocks and actual conditons. Condidion is always one line which has a operator (i.e. ,“Greater Than”,” Less Than”, “Touched by price”) and 2 operands. I.e., in D.SMA(20) greater than D.SMA(50) greater than is an operator, D.SMA(20) is the first operand and D.SMA(50) is the second operand`. Blocks can include other blocks (just like in the example above). We've heard that you love blocks so we put blocks into your blocks so you could execute blocks while executing blocks.

You can use scripts in conjunction with the Strategy Tester, Market Scanner, Smart Checklists or Multi-Factor Alerts.

NOTE: Custom indicators made in the code editor cannot be used in scripts at this time.

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.

For example:

  • 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)

Offsets (condition offsets, X candles ago)

Assume 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. Remember that for practical purposes, in this particular case you actually could use “crossed up” and “crossed down” operators.

You can read more about using offsets here.

Block Offsets (happened within X candles)

In our visual scripts, logical blocks combine criteria using logical operators like "and" and "not." In exampple, a logical block of "and" is considered true when all of its conditions are true at the same time.

Each logical block can have an offset, defined as happened within X candles in the interface. When you are using a logical block with an offset, the offset works differently from offsets on individual conditions. For blocks, instead of defining "how many candles ago it should be true" precisely, you define how far into the past the condition can be true in order for the entire block to be true.

For example, if you have a block named "and" and specify an offset of "happened within 20 candles", this block will be true if all the conditions within it were true at some candle within the last 20 candles. It can be true right now, five candles ago, or seven candles ago, but not 25 candles ago. This condition is useful when you want the conditions to be true somewhere recently without being concerned about the exact timing.

All of the following: 
  D Relative Volume (20, SMA, 1) > 2 
  All of the following: (within 20 candles)
      D SMA (20, 0, close) Crosses ↗ D SMA (50, 0, close) 
      D RSI (14, 70, 30, close) < 30

An alert with a condition like that will trigger if Rel.Vol is above 2 on the current candle, and if there was a case within last 20 candles when there was an MA crossover and RSI was below 30, at the same candle. If you've had a case when there was an MA crosover 10 candles ago but RSI was 35 10 candles ago, then this alert won't fire. Even if RSI was 25 11 candles ago. All of the underlying conditions should be true at the same candle, in order for an entire happened within X candles blcok to be true.

If you're using block offsets, then certain components (like Smart Checklist) will display n/a for the block consituents, but it will stil ldisplay a correct status (Yes/No) for the entire block.

Using multiple symbols in one script

Logical blocks can not only have offsets, they also can have symbols assigned explicitly.

By default, when you're using a script somewhere, it gets computed for a given symbol. In Scanners, it will be computed against every ticker on your list. In Smart Checklist, it will be computed for your current symbol, and so on.

You can assign symbols to embedded logical blocks explicitly if you want your script to account for more than one symbol. This feature is enabled for all the logical blocks except the main block of a script.

In example, this way you can write a strategy which buys SPY when gold goes down, provided oil oversold.

This functionality is available across various components of the system, including backtester, trading bots, smart checklists, and alerts. However, it's important to note that scanners do not support the use of multiple tickers in one script.

We recommend using the same asset classes within a script to avoid complications. For example, if you're backtesting cryptocurrencies, it's advisable to focus on a single asset class or use a high time frame such as daily. One should exercise caution when including different asset types in one script: it's possible and it works unless it explicitly tells you that it does not, but analyzing the results might be tricky.

WARNING: Every scrtipt must always use its "current symbol" as well. I.e., you can't run an alert on AAPL and have this alert using NFLX and META but not using AAPL per se.

Using Candle Patterns in Scripts

TrendSpider includes hundreds of different candlestick patterns that you can use in scripts.

Scripts 1

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.

Scripts 2

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).

Special Indicators

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.

Scripts 3

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”.

Scripts 4

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.

Scripts Manager

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.

Backtesting

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.

Scanning

Click on Market Scanner on 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.

Special Operators

"Within range of" operator

The Is within range of the operator is logically a combination of Greater Than and Less Than operators. So if you have a script like D Price.Close is withing range of D SMA(20) by ±2% then it's going to be true in case if Close is greater than "SMA -2%" and less than "SMA +2%".

The fact that it's a combination nf GT/LT means that the actual "borderline" is excluded from the condition. In example, a case when Close is strictly equal to SMA -2% will result in "false" for the script above.

"Exists in watch list" operator

This operator allows you to dynamically check if a particular symbol exists in a particular watch list, including Smart Watchlists that are generated by the system. One example of how to use this is as follows which would check if a symbol exists within a watch list, which in this example is the Wall Street Bets Smart Watch List: Exists in watch list r/WallStreetBets Top Mentions Last 30 Days

Typical operations when working with Visual Scripts

In general, operations you can do are as follows:

  1. Adding a new condition to a block. You can either add a Visual Script structured condition, or a GPT condition. When creating Visual Script structured conditions, you should pick its elements, one by one. First, you pick a time frame (i.e., Daily), then subject (i.e., an indicator named RSI), operator (i.e., Greater Than) and second operand. You can use your mouse and/or keyboard when doing so. GPT Conditions are a way for you to type your condition in human language. AI will transform your text into a Structured Condition, thereby saving you time.
  2. Adding a new logical block. In cases with some complex scripts you need logical blocks. Blocks will then contain conditions of yours. I.e., assume you want your script identifying bearish reversals. You could structure your script as Price is above sma(200) AND (Bearish Engulfing evolved, OR Evening Start evolved). Logical blocks is how you have these AND/OR implemented.
  3. Editing a condition. Click on any part of a condition to edit it.
  4. Cloning a condition or a block. Clone a block via a menu at a ... button. Clone a condition by pointing your mouse to this condition and then clicking on a Clone button.
  5. Removing a condition or a block. Remove a block via a menu at a ... button. Remove a condition by pointing your mouse to this condition and then clicking on a Remove button.
  6. Temporarily disabling a condition or a block. Disabling (muting) means that a given condition or a block won't participate in computations until you un-mute it back. This is useful whenever you want to check whether your script behavior changes without a given condition. Muted conditions are still a part of a script. So in example, if you share a strategy with some conditions muted, then your subscribers will also see the muted conditions.
Dec 14, 2023

Contact Us

Not finding what you're looking for? Contact Us Directly