Strategy · Pine Script v5 · On the chart

Trix Strategy V2

by CryptoRobotFr

This strategy trades trend continuations using a triple moving average oscillator (TRIX) and a long-term moving average baseline.

View on GitHub

GitHub stars4on the repository
Pine versionv5runs as is
Lines of code203
Settings23inputs
LicenceNone statedfile: MPL-2.0
Last update2 years ago18 Oct 2024

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

203 lines of Pine Script v5 from Prompt GPT Pinescript.txt, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

Prompt GPT Pinescript.txt
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © cryptoRobotFr

//@version=5
strategy("Trix Strategy V2", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=99, initial_capital=1000, commission_type=strategy.commission.percent, commission_value=0.07)

// Global
src = input(title='Source', defval=close)

// Long MA
long_ma_length = input.int(defval=200, title='Long MA length', group="Long MA")
long_ma_type = input.string(defval='SMA', title='Long MA type', options=['EMA', 'SMA'], group="Long MA")

//TRIX
trix_length = input.int(defval=8, title='TRIX length', group="TRIX")
trix_type = input.string(defval='SMA', title='TRIX type', options=['EMA', 'SMA'], group="TRIX")
trix_signa_length = input.int(defval=15, title='TRIX Signal length', group="TRIX")
trix_signal_type = input.string(defval='SMA', title='TRIX Signal type', options=['EMA', 'SMA'], group="TRIX")

use_longs = input.bool(true, 'Long Positions', group = "Backtest") 
use_short = input.bool(false, 'Short Positions', group = "Backtest")
start_date = input.time(timestamp("01 Jan 2017"), "Start date", group="Backtest")
end_date = input.time(timestamp("01 Jan 2025"), "End Date", group="Backtest")

triple_ema = ta.ema(ta.ema(ta.ema(src, trix_length), trix_length), trix_length)
triple_sma = ta.sma(ta.sma(ta.sma(src, trix_length), trix_length), trix_length)
triple_ma = trix_type == 'SMA' ? triple_sma : triple_ema
trix = 1 * ta.roc(triple_ma, 1)

signal_ema = ta.ema(trix, trix_signa_length)
signal_sma = ta.sma(trix, trix_signa_length)
signal_ma = trix_signal_type == 'SMA' ? signal_sma : signal_ema
signal = signal_ma

hist = trix - signal

long_ema = ta.ema(src, long_ma_length)
long_sma = ta.sma(src, long_ma_length)
long_ma = long_ma_type == 'SMA' ? long_sma : long_ema

in_date_range = (time >= start_date) and (time < end_date)

if in_date_range
    if use_longs
        open_long = hist > 0 and close > long_ma and strategy.position_size == 0
        // open_long = close > triple_ma and close > long_ma and strategy.position_size == 0
        if open_long
            strategy.entry('long', strategy.long)
        if hist < 0
            strategy.close('long')

    
    if use_short
        open_short = hist < 0 and close < long_ma and strategy.position_size == 0
        
        if open_short
            strategy.entry('short', strategy.short)
        if hist > 0
            strategy.close('short')


triple_ma_line = plot(triple_ma, color=color.new(#57008a, 0), linewidth=1, title='Triple MA')
long_ma_line = plot(long_ma, color=#18aac4, linewidth=2, title='Long MA')
trix_line = plot((hist/1+1)*triple_ma, color=#df0606, linewidth=1, title='Trix Hist')
fill_color = hist < 0 ? #df06067e : #40af3183
fill(triple_ma_line, trix_line, color=fill_color)

Envelope Strategy:
//@version=5
strategy("Envelope Strategy by Crypto Robot", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=1000, pyramiding = 5, commission_value=0.007)

// CopyRight Crypto Robot

src = input(close, title="Source", group = "Base MA")
ma_base_window = input.int(4, "Base Mooving Average Window", step = 1, group = "Base MA")
ma_type = input.string(defval='1. SMA', options=['1. SMA', '2. PCMA', '3. EMA', '4. WMA', '5. DEMA', '6. ZLEMA', '7. HMA'], title='MA Type', group = "Base MA")


envelope_1_pct = input.float(0.07, "Envelope 1", step = 0.01, group = "Envelopes")
envelope_2_pct = input.float(0.10, "Envelope 2", step = 0.01, group = "Envelopes")
envelope_3_pct = input.float(0.15, "Envelope 3", step = 0.01, group = "Envelopes")
envelope_4_pct = input.float(0.0, "Envelope 4", step = 0.01, group = "Envelopes")
envelope_5_pct = input.float(0.0, "Envelope 5", step = 0.01, group = "Envelopes")
envelope_6_pct = input.float(0.0, "Envelope 6", step = 0.01, group = "Envelopes")
envelope_7_pct = input.float(0.0, "Envelope 7", step = 0.01, group = "Envelopes")

use_longs = input.bool(true, 'Long Positions') 
use_short = input.bool(true, 'Short Positions')

total_envelope = 0
if envelope_1_pct > 0
    total_envelope := total_envelope + 1
if envelope_2_pct > 0
    total_envelope := total_envelope + 1
if envelope_3_pct > 0
    total_envelope := total_envelope + 1
if envelope_4_pct > 0
    total_envelope := total_envelope + 1
if envelope_5_pct > 0
    total_envelope := total_envelope + 1
if envelope_6_pct > 0
    total_envelope := total_envelope + 1
if envelope_7_pct > 0
    total_envelope := total_envelope + 1

// ---------------------------------------------
// -------------- INDICATORS -------------------
ma_function(MA_type, MA_length) =>
    zlema_lag = (MA_length - 1) / 2
    hma_src = MA_type == '7. HMA' ? 2 * ta.wma(src, math.floor(MA_length / 2)) - ta.wma(src, MA_length) : na
    MA_type == '1. SMA' ? ta.sma(src, MA_length) : MA_type == '2. PCMA' ? (ta.highest(high, MA_length) + ta.lowest(low, MA_length)) / 2 : MA_type == '3. EMA' ? ta.ema(src, MA_length) : MA_type == '4. WMA' ? ta.wma(src, MA_length) : MA_type == '5. DEMA' ? 2 * ta.ema(src, MA_length) - ta.ema(ta.ema(src, MA_length), MA_length) : MA_type == '6. ZLEMA' ? ta.ema(src + src - src[zlema_lag], MA_length) : MA_type == '7. HMA' ? ta.wma(hma_src, math.floor(math.sqrt(MA_length))) : na

    
ma_base_temp = ma_function(ma_type, ma_base_window)
ma_base = ma_base_temp[1]

ma_high_1 = envelope_1_pct > 0 ? ma_base * (1 + (1 / ( 1 - envelope_1_pct) - 1)) : na
ma_high_2 = envelope_2_pct > 0 ? ma_base * (1 + (1 / ( 1 - envelope_2_pct) - 1)) : na
ma_high_3 = envelope_3_pct > 0 ? ma_base * (1 + (1 / ( 1 - envelope_3_pct) - 1)) : na
ma_high_4 = envelope_4_pct > 0 ? ma_base * (1 + (1 / ( 1 - envelope_4_pct) - 1)) : na
ma_high_5 = envelope_5_pct > 0 ? ma_base * (1 + (1 / ( 1 - envelope_5_pct) - 1)) : na
ma_high_6 = envelope_6_pct > 0 ? ma_base * (1 + (1 / ( 1 - envelope_6_pct) - 1)) : na
ma_high_7 = envelope_7_pct > 0 ? ma_base * (1 + (1 / ( 1 - envelope_7_pct) - 1)) : na

// ma_high_1 = envelope_1_pct > 0 ? ma_base * (1 + envelope_1_pct) : na
// ma_high_2 = envelope_2_pct > 0 ? ma_base * (1 + envelope_2_pct) : na
// ma_high_3 = envelope_3_pct > 0 ? ma_base * (1 + envelope_3_pct) : na
// ma_high_5 = envelope_5_pct > 0 ? ma_base * (1 + envelope_5_pct) : na
// ma_high_4 = envelope_4_pct > 0 ? ma_base * (1 + envelope_4_pct) : na
// ma_high_6 = envelope_6_pct > 0 ? ma_base * (1 + envelope_6_pct) : na
// ma_high_7 = envelope_7_pct > 0 ? ma_base * (1 + envelope_7_pct) : na

ma_low_1 = envelope_1_pct > 0 ? ma_base * (1 - envelope_1_pct) : na
ma_low_2 = envelope_2_pct > 0 ? ma_base * (1 - envelope_2_pct) : na
ma_low_3 = envelope_3_pct > 0 ? ma_base * (1 - envelope_3_pct) : na
ma_low_4 = envelope_4_pct > 0 ? ma_base * (1 - envelope_4_pct) : na
ma_low_5 = envelope_5_pct > 0 ? ma_base * (1 - envelope_5_pct) : na
ma_low_6 = envelope_6_pct > 0 ? ma_base * (1 - envelope_6_pct) : na
ma_low_7 = envelope_7_pct > 0 ? ma_base * (1 - envelope_7_pct) : na

// ---------------------------------------------
// --------------- STRATEGY --------------------
if use_longs
    if envelope_1_pct > 0 and strategy.opentrades < 1
        strategy.entry('long 1', strategy.long, limit=ma_low_1, qty=(strategy.equity / ma_low_1) * (1 / total_envelope))
    if envelope_2_pct > 0 and strategy.opentrades < 2
        strategy.entry('long 2', strategy.long, limit=ma_low_2, qty=(strategy.equity / ma_low_2) * (1 / total_envelope))
    if envelope_3_pct > 0 and strategy.opentrades < 3
        strategy.entry('long 3', strategy.long, limit=ma_low_3, qty=(strategy.equity / ma_low_3) * (1 / total_envelope))
    if envelope_4_pct > 0 and strategy.opentrades < 4
        strategy.entry('long 4', strategy.long, limit=ma_low_4, qty=(strategy.equity / ma_low_4) * (1 / total_envelope))
    if envelope_5_pct > 0 and strategy.opentrades < 5
        strategy.entry('long 5', strategy.long, limit=ma_low_5, qty=(strategy.equity / ma_low_5) * (1 / total_envelope))
    if envelope_6_pct > 0 and strategy.opentrades < 6
        strategy.entry('long 6', strategy.long, limit=ma_low_6, qty=(strategy.equity / ma_low_6) * (1 / total_envelope))
    if envelope_7_pct > 0 and strategy.opentrades < 7
        strategy.entry('long 7', strategy.long, limit=ma_low_7, qty=(strategy.equity / ma_low_7) * (1 / total_envelope))


  
if use_short
    if envelope_1_pct > 0 and strategy.opentrades < 1
        strategy.entry('short 1', strategy.short, limit=ma_high_1, qty=(strategy.equity / ma_high_1) * (1 / total_envelope))
    if envelope_2_pct > 0 and strategy.opentrades < 2
        strategy.entry('short 2', strategy.short, limit=ma_high_2, qty=(strategy.equity / ma_high_2) * (1 / total_envelope))
    if envelope_3_pct > 0 and strategy.opentrades < 3
        strategy.entry('short 3', strategy.short, limit=ma_high_3, qty=(strategy.equity / ma_high_3) * (1 / total_envelope))
    if envelope_4_pct > 0 and strategy.opentrades < 4
        strategy.entry('short 4', strategy.short, limit=ma_high_4, qty=(strategy.equity / ma_high_4) * (1 / total_envelope))
    if envelope_5_pct > 0 and strategy.opentrades < 5
        strategy.entry('short 5', strategy.short, limit=ma_high_5, qty=(strategy.equity / ma_high_5) * (1 / total_envelope))
    if envelope_6_pct > 0 and strategy.opentrades < 6
        strategy.entry('short 6', strategy.short, limit=ma_high_6, qty=(strategy.equity / ma_high_6) * (1 / total_envelope))
    if envelope_7_pct > 0 and strategy.opentrades < 7
        strategy.entry('short 7', strategy.short, limit=ma_high_7, qty=(strategy.equity / ma_high_7) * (1 / total_envelope))


strategy.exit('close', limit=ma_base)


// ---------------------------------------------
// ------------------ PLOT ---------------------

ma_base_plot = plot(ma_base, title = "Base MA", color = color.orange, linewidth = 3, offset = 1)

ma_high_1_plot = plot(ma_high_1, title = "MA high 1", color = color.red, offset = 1)
ma_high_2_plot = plot(ma_high_2, title = "MA high 2", color = color.red, offset = 1)
ma_high_3_plot = plot(ma_high_3, title = "MA high 3", color = color.red, offset = 1)
ma_high_4_plot = plot(ma_high_4, title = "MA high 4", color = color.red, offset = 1)
ma_high_5_plot = plot(ma_high_5, title = "MA high 5", color = color.red, offset = 1)
ma_high_6_plot = plot(ma_high_6, title = "MA high 6", color = color.red, offset = 1)
ma_high_7_plot = plot(ma_high_7, title = "MA high 7", color = color.red, offset = 1)

ma_low_1_plot = plot(ma_low_1, title = "MA low 1", color = color.green, offset = 1)
ma_low_2_plot = plot(ma_low_2, title = "MA low 2", color = color.green, offset = 1)
ma_low_3_plot = plot(ma_low_3, title = "MA low 3", color = color.green, offset = 1)
ma_low_4_plot = plot(ma_low_4, title = "MA low 4", color = color.green, offset = 1)
ma_low_5_plot = plot(ma_low_5, title = "MA low 5", color = color.green, offset = 1)
ma_low_6_plot = plot(ma_low_6, title = "MA low 6", color = color.green, offset = 1)
ma_low_7_plot = plot(ma_low_7, title = "MA low 7", color = color.green, offset = 1)


Tu dois coder cette stratégie:

Overview

Trix Strategy V2 opens long positions when price is above the long moving average and the TRIX histogram crosses above zero, and opens short positions when price is below the moving average and the histogram turns negative. Positions exit whenever the histogram flips back against the trade direction. Users can adjust moving average types, smoothing lengths, and backtest date ranges. Plots overlay directly on the price chart. There is no predefined stop loss.

Details

Published by

CryptoRobotFr on GitHub

Draws

On the price chart

Markets

Crypto

Timeframes

Any timeframe

Risk Notes

  • The strategy enters trades without a stop loss

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Sourcesourceclosesrc
Long MA lengthint200long_ma_length
Long MA typestringSMAlong_ma_type
TRIX lengthint8trix_length
TRIX typestringSMAtrix_type
TRIX Signal lengthint15trix_signa_length
TRIX Signal typestringSMAtrix_signal_type
Long Positionsbooltrueuse_longs
Short Positionsboolfalseuse_short
Start datetimetimestamp("01 Jan 2017")start_date
End Datetimetimestamp("01 Jan 2025")end_date
Sourcesourceclosesrc
Show all 23 settings
SettingTypeDefaultVariable
Base Mooving Average Windowint4ma_base_window
MA Typestring1. SMAma_type
Envelope 1float0.07envelope_1_pct
Envelope 2float0.10envelope_2_pct
Envelope 3float0.15envelope_3_pct
Envelope 4float0.0envelope_4_pct
Envelope 5float0.0envelope_5_pct
Envelope 6float0.0envelope_6_pct
Envelope 7float0.0envelope_7_pct
Long Positionsbooltrueuse_longs
Short Positionsbooltrueuse_short
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

330 stars

2 EMA/SMA + RSI Strategy [Alorse] combines three configurable moving averages with a short-period Relative Strength Index filter. It plots fast, slow, and trend moving averages on the price chart. Long entries trigger when the fast moving average is above the

Strategy v4 TrendOscillators & momentum
Updated 2 months ago · None stated GitHub repository of 2 EMA/SMA + RSI Strategy [Alorse]

Acrypto - Weighted Strategy

by AlbertoCuadra
318 stars

This strategy plots on the price chart and opens long or short positions when a weighted score from MACD, Stochastic RSI, RSI, Supertrend and a moving-average crossover exceeds a user-set trigger. Each component has its own weight and candle delay. Entries can

Strategy v5 TrendOscillators & momentum
Updated 3 years ago · Custom licence GitHub repository of Acrypto - Weighted Strategy

This strategy combines Arnaud Legoux Moving Average percentage price change calculations with an adaptive Gaussian Moving Average to generate trend-following signals. Long and short entries are triggered when the ALMA change calculation crosses above or below

Strategy v5 Moving averagesOscillators & momentum
Updated 7 weeks ago · None stated GitHub repository of ESM2024 - ALMA Smoothed Gaussian Moving Average
228 stars

This strategy generates trading signals based on a selectable momentum engine, supporting MACD, RSI, Stochastic, or Rate of Change. Entries trigger on signal crosses, zero-line crosses, or threshold crosses, with an optional moving average trend filter. Risk m

Strategy v6 Oscillators & momentumTrend
Updated 3 months ago · None stated GitHub repository of Momentum Strategy Template

255 EMA Strategy

by hasnocool
106 stars

255 EMA Strategy combines an exponential moving average with Average True Range bands and standard MACD crossovers to generate entry signals. Drawn directly on the price chart, it plots the central 255 EMA along with upper and lower volatility envelopes. Trade

Strategy v4 Moving averagesOscillators & momentum
Updated 2 years ago · GPL-3.0 GitHub repository of 255 EMA Strategy

SMA RSI Strategy

by PythonForForex
64 stars

This strategy evaluates simple moving averages and the Relative Strength Index to generate trading signals. It calculates a 10-period SMA, a 30-period SMA, and a 14-period RSI. Long positions are entered when the fast SMA crosses above the slow SMA while RSI i

Strategy v5 TrendOscillators & momentum
Updated 4 years ago · None stated GitHub repository of SMA RSI Strategy

More Trend scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers