Strategy · Pine Script v5 · On the chart

3MA and Fractals Strategy

by geraked

This strategy combines three Exponential Moving Averages with Williams Fractals to execute trend-following or mean-reverting trades.

View on GitHub

GitHub stars104on the repository
Pine versionv5runs as is
Lines of code114
Settings7inputs
LicenceMIT
Last update3 years ago29 Oct 2023

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

114 lines of Pine Script v5 from strategies/3MAF.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

3MAF.pine
// A simple strategy using three Moving Averages and Williams Fractals for scalping.
// The strategy has been explained in this video: https://youtu.be/bKPs2aOsvsk
// It should be noted that the reverse of the strategy works better! :D
// © 2023 Geraked, Rabist
// Licensed under MIT

// Backtesting result:
//      Symbol:             USDCAD
//      Timeframe:          1 MIN
//      Range:              2023-07-09 21:00 — 2023-07-13 13:14
//      Percent Profitable: 70.8%
//      Total Trades:       127
//      Profit Factor:      1.326

// Backtesting result:
//      Symbol:             EURUSD
//      Timeframe:          30 MIN
//      Range:              2023-01-02 00:00 — 2023-07-13 13:00
//      Percent Profitable: 76.7%
//      Total Trades:       172
//      Profit Factor:      1.399


//@version=5
strategy("3MA and Fractals Strategy", "3MAF", overlay=true)

len1 = input.int(20, "EMA1 Len", 1)
len2 = input.int(50, "EMA2 Len", 1)
len3 = input.int(100, "EMA3 Len", 1)
n = input.int(2, "Fractals Len", 2)
tpCoef = input.float(1, "TP Coef")
reverse = input.bool(false, "Reverse Signal")
dispInd = input.bool(false, "Display Indicators")

ma1 = ta.ema(close, len1)
ma2 = ta.ema(close, len2)
ma3 = ta.ema(close, len3)

plot(dispInd ? ma1 : na, title="EMA1", color=color.red)
plot(dispInd ? ma2 : na, title="EMA2", color=color.green)
plot(dispInd ? ma3 : na, title="EMA3", color=color.blue)


// UpFractal
bool upflagDownFrontier = true
bool upflagUpFrontier0 = true
bool upflagUpFrontier1 = true
bool upflagUpFrontier2 = true
bool upflagUpFrontier3 = true
bool upflagUpFrontier4 = true

for i = 1 to n
    upflagDownFrontier := upflagDownFrontier and (high[n-i] < high[n])
    upflagUpFrontier0 := upflagUpFrontier0 and (high[n+i] < high[n])
    upflagUpFrontier1 := upflagUpFrontier1 and (high[n+1] <= high[n] and high[n+i + 1] < high[n])
    upflagUpFrontier2 := upflagUpFrontier2 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+i + 2] < high[n])
    upflagUpFrontier3 := upflagUpFrontier3 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+3] <= high[n] and high[n+i + 3] < high[n])
    upflagUpFrontier4 := upflagUpFrontier4 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+3] <= high[n] and high[n+4] <= high[n] and high[n+i + 4] < high[n])
flagUpFrontier = upflagUpFrontier0 or upflagUpFrontier1 or upflagUpFrontier2 or upflagUpFrontier3 or upflagUpFrontier4

upFractal = (upflagDownFrontier and flagUpFrontier)

// downFractal
bool downflagDownFrontier = true
bool downflagUpFrontier0 = true
bool downflagUpFrontier1 = true
bool downflagUpFrontier2 = true
bool downflagUpFrontier3 = true
bool downflagUpFrontier4 = true

for i = 1 to n
    downflagDownFrontier := downflagDownFrontier and (low[n-i] > low[n])
    downflagUpFrontier0 := downflagUpFrontier0 and (low[n+i] > low[n])
    downflagUpFrontier1 := downflagUpFrontier1 and (low[n+1] >= low[n] and low[n+i + 1] > low[n])
    downflagUpFrontier2 := downflagUpFrontier2 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+i + 2] > low[n])
    downflagUpFrontier3 := downflagUpFrontier3 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+3] >= low[n] and low[n+i + 3] > low[n])
    downflagUpFrontier4 := downflagUpFrontier4 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+3] >= low[n] and low[n+4] >= low[n] and low[n+i + 4] > low[n])
flagDownFrontier = downflagUpFrontier0 or downflagUpFrontier1 or downflagUpFrontier2 or downflagUpFrontier3 or downflagUpFrontier4

downFractal = (downflagDownFrontier and flagDownFrontier)

plotshape(dispInd ? downFractal : na, style=shape.triangledown, location=location.belowbar, offset=-n, color=#F44336, size=size.small)
plotshape(dispInd ? upFractal : na, style=shape.triangleup,   location=location.abovebar, offset=-n, color=#009688, size=size.small)


longSignal = ma1 > ma2 and ma2 > ma3 and low > ma3 and low < ma1 and downFractal
shortSignal = ma3 > ma2 and ma2 > ma1 and high < ma3 and high > ma1 and upFractal

longSL = low > ma2 ? ma2 : ma3
shortSL = high < ma2 ? ma2 : ma3

if longSignal
    if not reverse
        strategy.entry("long", strategy.long)
        inn = strategy.opentrades.entry_price(strategy.opentrades - 1)
        longTP = inn + tpCoef * (inn - longSL)
        strategy.exit("exit", "long", stop=longSL, limit=longTP, comment_loss="SL long", comment_profit="TP long")
    else
        strategy.entry("short", strategy.short)
        inn = strategy.opentrades.entry_price(strategy.opentrades - 1)
        longTP = inn + tpCoef * (inn - longSL)
        strategy.exit("exit", "short", stop=longTP, limit=longSL, comment_loss="SL short", comment_profit="TP short")   

if shortSignal
    if not reverse
        strategy.entry("short", strategy.short)
        inn = strategy.opentrades.entry_price(strategy.opentrades - 1)
        shortTp = inn - tpCoef * (shortSL - inn)
        strategy.exit("exit", "short", stop=shortSL, limit=shortTp, comment_loss="SL short", comment_profit="TP short")
    else
        strategy.entry("long", strategy.long)
        inn = strategy.opentrades.entry_price(strategy.opentrades - 1)
        shortTp = inn - tpCoef * (shortSL - inn)
        strategy.exit("exit", "long", stop=shortTp, limit=shortSL, comment_loss="SL long", comment_profit="TP long")

Overview

3MA and Fractals Strategy analyzes price structure using short, medium, and long-term EMAs along with fractal highs and lows. Entries trigger when fractal patterns align with moving average trends, overlaid directly on the price chart. Users can customize EMA lengths, fractal detection periods, take profit multipliers, signal inversion, and toggle indicator plotting visibility.

Details

Published by

geraked on GitHub

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Trend, Moving averages, Support & resistance

Markets

Forex

Timeframes

M1-M5, M15-H1

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

backtesting-trading-strategiesexpert-advisorexpert-advisorsforex-botgerakedmetatraderpinescriptrabiststock-markettrading-algorithms

Settings

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

SettingTypeDefaultVariable
EMA1 Lenint20len1
EMA2 Lenint50len2
EMA3 Lenint100len3
Fractals Lenint2n
TP Coeffloat1tpCoef
Reverse Signalboolfalsereverse
Display IndicatorsboolfalsedispInd
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

Omar Edited WF

by Alorse
330 stars

Omar Edited WF is a trading strategy overlay built around Williams Fractals and multiple Exponential Moving Averages. It detects high and low fractal pivot points over configurable lookback periods alongside three adjustable EMA trendlines. Long entries are ma

Strategy v5 TrendMoving averages
Updated 2 months ago · None stated GitHub repository of Omar Edited WF
257 stars

This strategy calculates dynamic support and resistance levels using pivot highs and lows alongside a custom smoothed trend filter and a 3-period EMA. It identifies entry opportunities when price breaks out relative to the trend filter and short-term EMA cross

Strategy v5 TrendSupport & resistance Repaints
Updated 6 weeks ago · None stated GitHub repository of 30s - ES 1m Strategy Candle Bar

This strategy overlays the price chart and looks for VWAP pullbacks. It goes long when a short SMA crosses above VWAP after RSI has been oversold within ten bars, and short on the opposite cross after RSI has been overbought. Optional Minervini-style moving-av

Strategy v5 TrendOscillators & momentum
Updated 2 years ago · GPL-3.0 GitHub repository of Advanced VWAP-Momentum Pullback Trend Qualifier

This strategy executes trades based on pivot levels, an exponential moving average, and a multi-stage smoothing trend line. It identifies support and resistance points from pivot highs and lows, combining them with price relationship rules against a custom smo

Strategy v5 TrendMoving averages Repaints
Updated 2 years ago · None stated GitHub repository of Binancash - Candle Strategy Futures on Binance
13 stars

This strategy generates entry signals using pivot points and uses custom Curie moving average closing lines for exiting positions. It features risk management components including percentage-based stop loss, take profit, trailing stops, and break-even levels,

Strategy v5 TrendMoving averages
Updated 1 year ago · None stated GitHub repository of BitLa PivotPoints A002
9 stars

Smooth Trend Radar Strategy calculates a double-smoothed Supertrend baseline using WMA and EMA to identify trend direction and sideway regimes. It plots baseline rejection signals on flips, trend slope filters, and statistical overextension highlights with can

Strategy v6 TrendMoving averages
Updated yesterday · MIT GitHub repository of Smooth Trend Radar Strategy

More strategys

Trade Your Charts With a Regulated Broker

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

Compare brokers