SMA 8 21 Cross Script

← All 56 scripts

  • Trend
  • On price
  • Beginner

Marks the candle on which the 8-period simple moving average crosses the 21-period average and price closes beyond both lines. It is the classic two-average cross with a simple price check.

  • File iqow-sma-8-21-cross.lua
  • Version 1.0.0
  • Size 125 lines · 4 KB
  • Timeframes 1-minute to 5-minute candles
  • License MIT
SMA 8 21 Cross drawn on synthetic candles
How the script draws, shown on synthetic candles generated for testing. It is an illustration of the markers and lines, not real market data or a result.

The rules

HIGHER marker

SMA(8) crosses above SMA(21) on this candle and the candle closes above both averages.

LOWER marker

SMA(8) crosses below SMA(21) on this candle and the candle closes below both averages.

A marker appears after the next candle opens and is drawn on the candle that completed the setup. It is a chart observation, not an instruction to trade.

How to read it

The yellow line is the fast SMA(8) and the blue line is the slow SMA(21). A green HIGHER triangle under a candle means the fast average just crossed above the slow one and price closed above both; a red LOWER triangle above a candle is the mirror case.

Indicators used

  • SMA (8)
  • SMA (21)

Same view with built-in indicators: Add two IQ Option Moving Averages of type SMA with periods 8 and 21.

Default settings

Fast SMA period8
Slow SMA period21

Every value is an input in the script settings. Platform advice is off by default.

Key points

  1. A simple moving average is the plain average of the last N closing prices.
  2. The SMA 8 follows price quickly and the SMA 21 follows it more slowly.
  3. When the fast line crosses above the slow line, short-term direction may be turning up.
  4. We also require the candle to close above both lines, so price has followed the cross.
  5. The mirror cross downward is marked LOWER.
  6. In sideways markets the lines cross back and forth, so test it on a Practice balance first.

Limitations. The article's own 1-minute method trades pullbacks against the trend direction; this script marks the plain average cross it describes first, which is a different, slower setup. Crosses are late and produce many false marks when price moves sideways. Test on the Practice balance before risking real funds.

Learn the strategy: read the full guide on IQOptionWiki →

Install it in four steps

  1. Download and inspectDownload one .lua file and open it in a plain-text editor if you want to read it first. If you downloaded the ZIP, extract it.
  2. Open Chart Analysis → ScriptsIn the IQ Option traderoom, choose the Practice balance, open the chart analysis panel, select Scripts and choose Import script.
  3. Save and add to chartSelect the downloaded file, review the imported code, choose Save, then use the + control to add it to the chart.
  4. Change one input at a timeConfirm the default view first. Then adjust periods, levels or colours one setting at a time and note what changes.

Complete source code

The download and this preview resolve to the same reviewed file. SHA-256: 46f5ec78e3ef53db899a0b506f2ef0fd4a5bf2d490b8234fbc74637a1bbc7062

-- SPDX-License-Identifier: MIT
-- IQOptionWiki QCS Library
-- IQOW SMA 8 21 Cross v1.0.0
--
-- Two simple moving averages (8 and 21). The faster average crossing the slower
-- one marks a change in short-term direction; requiring the candle to close
-- beyond both averages filters out crosses where price has not followed.
--
-- HIGHER: SMA 8 crosses above SMA 21 and the candle closes above both.
-- LOWER:  SMA 8 crosses below SMA 21 and the candle closes below both.
--
-- Markers use the previous completed candle: they appear after the next candle
-- opens and are shifted back one bar onto the candle that qualified.
-- Educational chart tool only. It does not place trades, choose an expiry,
-- predict outcomes, or guarantee results. Test on a Practice balance first.

instrument {
    name = "IQOW SMA 8 21 Cross",
    short_name = "IQOW SMA 8/21",
    overlay = true
}

input_group {
    "1. Moving averages",
    fast_period = input (8, "Fast SMA period", input.integer, 2, 100),
    slow_period = input (21, "Slow SMA period", input.integer, 3, 300)
}

input_group {
    "Display and advice",
    averages_visible = input { default = true, type = input.plot_visibility },
    signals_visible = input { default = true, type = input.plot_visibility },
    advice_enabled = input { default = false, name = "Enable platform advice", type = input.boolean }
}

input_group {
    "Colors",
    fast_color = input { default = "#FFD166", type = input.color },
    slow_color = input { default = "#56CEFF", type = input.color },
    average_width = input { default = 2, type = input.line_width },
    higher_color = input { default = "#25E154", type = input.color },
    lower_color = input { default = "#FF6C58", type = input.color },
    signal_text_color = input { default = "#FFFFFF", type = input.color }
}

fast_sma = sma (close, fast_period)
slow_sma = sma (close, slow_period)

local fast_now = get_value (fast_sma, nil)
local slow_now = get_value (slow_sma, nil)
local fast_previous = get_value (fast_sma [1], nil)
local slow_previous = get_value (slow_sma [1], nil)
local close_now = get_value (close, nil)

local periods_are_valid = fast_period < slow_period

local data_is_ready =
    periods_are_valid and
    fast_now ~= nil and
    slow_now ~= nil and
    fast_previous ~= nil and
    slow_previous ~= nil and
    close_now ~= nil

-- Event: the fast average crosses the slow average on this candle.
local crosses_up = data_is_ready and fast_previous <= slow_previous and fast_now > slow_now
local crosses_down = data_is_ready and fast_previous >= slow_previous and fast_now < slow_now

higher_setup = conditional (crosses_up and close_now > fast_now and close_now > slow_now)
lower_setup = conditional (crosses_down and close_now < fast_now and close_now < slow_now)

-- Confirm only a new setup on the previous completed candle.
confirmed_higher = conditional (
    get_value (higher_setup [1], false) and
    not get_value (higher_setup [2], false)
)

confirmed_lower = conditional (
    get_value (lower_setup [1], false) and
    not get_value (lower_setup [2], false)
)

if averages_visible then
    plot (fast_sma, "Fast SMA", fast_color, average_width)
    plot (slow_sma, "Slow SMA", slow_color, average_width)
end

if signals_visible then
    plot_shape (
        confirmed_higher,
        "SMA 8/21 HIGHER",
        shape_style.triangleup,
        shape_size.small,
        higher_color,
        shape_location.belowbar,
        -1,
        "HIGHER",
        signal_text_color
    )

    plot_shape (
        confirmed_lower,
        "SMA 8/21 LOWER",
        shape_style.triangledown,
        shape_size.small,
        lower_color,
        shape_location.abovebar,
        -1,
        "LOWER",
        signal_text_color
    )
end

-- QCS advice is a platform notification state only; it does not place trades.
if advice_enabled and
    get_value (confirmed_higher, false) and
    not get_value (confirmed_lower, false) then
    advise (advice.buy)
elseif advice_enabled and
    get_value (confirmed_lower, false) and
    not get_value (confirmed_higher, false) then
    advise (advice.sell)
else
    advise (advice.neutral)
end

More trend scripts

  • Trend
  • On price
  • Intermediate

KAMA Trend

Marks closes that cross Kaufman's Adaptive Moving Average in the direction the average is already sloping. The slope check removes crosses where the flat, slow-moving KAMA is merely being touched by price.

Indicators: KAMA (10, 2, 30)

  • Trend
  • On price
  • Intermediate

LRS Williams R

Uses the slope of a 100-candle regression line as the trend filter and Williams %R leaving its extreme zone as the timing event. The regression line is drawn on the price chart so the slope direction is visible.

Indicators: Linear Regression Slope (100) · Williams %R (14, levels -80/-20)

  • Trend
  • On price
  • Intermediate

MACD PSAR EMA

Marks a MACD signal-line cross that is backed by candle colour, the EMA and the Parabolic SAR. Three different views of price have to agree on the same completed candle.

Indicators: MACD (12, 26, 9) · Parabolic SAR (0.02, 0.2) · EMA (10)

Download all 56 scripts as one ZIP · MIT License, provided “as is”, without warranty. IQOptionWiki is independent of the IQ Option platform operator. Nothing here is investment advice or a guarantee of results.

GENERAL RISK WARNING

The financial products offered by the company carry a high level of risk and can result in the loss of all your funds. You should never invest money that you cannot afford to lose.
Kindly note that this article does not provide any investment advice. The information presented regarding past events or potential future developments is solely an opinion and cannot be guaranteed as factual, including the provided examples. We caution readers accordingly.