Strategy · Pine Script v4 · On the chart

Fear + Greed Reversal Long Strategy (Daily)

by krosenfeld7

This strategy identifies potential reversal conditions using statistical bands derived from price extremes relative to moving averages and standard deviations.

View on GitHub

GitHub stars5on the repository
Pine versionv4older, still runs
Lines of code100
Settings12inputs
LicenceApache-2.0
Last update3 years ago12 May 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

100 lines of Pine Script v4 from Fear+Greed_Reversal/Fear+Greed_Reversal_Long_Strategy.txt, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.

Fear+Greed_Reversal_Long_Strategy.txt
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © krosenfeld7

//@version=4
strategy("Fear + Greed Reversal Long Strategy (Daily)", overlay = true, pyramiding = 0, 
         margin_long = 100, margin_short = 100, default_qty_type = strategy.percent_of_equity, 
         default_qty_value = 20, initial_capital = 2000, process_orders_on_close = true)

// Set the starting point for backtesting
start = input(title="Backtesting Start", type=input.time, defval=timestamp("1 Jan 2015"))
// Expiration time on orders
maxTimeOpen = input(title="Order Expiration", type=input.integer, defval=100, minval=1)
useAtr = input(title="Use ATR?", type=input.bool, defval=false)

// Algorithm / Strategy Goes here
// Set long / short variables to execute positions below
// Fear & Greed Section
lowPeriod = input(30, title="Low Period")
highPeriod = input(30, title="High Period")
stdev_period = input(50, title="Stdev Period")
src = input(ohlc4, title="Source")
WMA = input(false, title="Use WMA instead of SMA?")

// Condition One 
greedZone1 = (lowest(src, lowPeriod) - src) / lowest(src, lowPeriod)
greedAvg1 = WMA ? wma(greedZone1, stdev_period) : sma(greedZone1, stdev_period)
greedZoneLimit1 = greedAvg1 - stdev(greedZone1, stdev_period)

fearZone1 = (highest(src, highPeriod) - src) / highest(src, highPeriod)
fearAvg1 = WMA ? wma(fearZone1, stdev_period) : sma(fearZone1, stdev_period)
fearZoneLimit1 = fearAvg1 + stdev(fearZone1, stdev_period)

// Condition Two
greedZone2 = WMA ? wma(src, lowPeriod) : sma(src, lowPeriod)
greedAvg2 = WMA ? wma(greedZone2, stdev_period) : sma(greedZone2, stdev_period)
greedZoneLimit2 = greedAvg2 + stdev(greedZone2, stdev_period)

fearZone2 = WMA ? wma(src, highPeriod) : sma(src, highPeriod)
fearAvg2 = WMA ? wma(fearZone2, stdev_period) : sma(fearZone2, stdev_period)
fearZoneLimit2 = fearAvg2 - stdev(fearZone2, stdev_period)

// GreedZone
inGreedZone = greedZone1 < greedZoneLimit1 and greedZone2 > greedZoneLimit2 
greedZoneOpen = inGreedZone ? low - tr : na
greedZoneClose = inGreedZone ? low - 2 * tr : na

inFearZone = fearZone1 > fearZoneLimit1 and fearZone2 < fearZoneLimit2
fearZoneOpen = inFearZone ? low - tr : na
fearZoneClose = inFearZone ? low - 2 * tr : na

greedGap = inGreedZone ? greedZoneOpen - greedZoneClose : na
fearGap = inFearZone ? fearZoneOpen - fearZoneClose : na

// Fear / Greed Trend reversing
greedy = not na(greedGap) ? greedGap > 0 : false
noLongerGreedy = (greedy[1] == true and greedy == false) ? 1 : 0
fearful = not na(fearGap) ? fearGap > 0 : false
noLongerFearful = (fearful[1] == true and fearful == false) ? 1 : 0

// Extreme cases
extremeFear = (fearGap > fearZoneClose) ? 1 : 0
extremeGreed = (greedGap > greedZoneClose) ? 1 : 0

long = (greedy[2] == false and greedy[1] == true and greedy == true) ? 1 : 0

plot(greedGap, color=color.purple, style=plot.style_linebr)
plotcandle(greedZoneOpen, greedZoneOpen, greedZoneClose, greedZoneClose, color=color.red, bordercolor=color.white,  title="Greedy Candlesticks")

plot(fearGap, color=color.aqua, style=plot.style_linebr)
plotcandle(fearZoneOpen, fearZoneOpen, fearZoneClose, fearZoneClose, color=color.blue, bordercolor=color.white,  title="Fearful Candlesticks")

// Average True Range
length = input(title="ATR Length", defval=14, minval=1)
atrMultiplier = input(title="ATR Multiplier", type=input.float, defval=1, minval=0.1)
atr = atr(length)

longProfitPercent = input(title = "Profit %",
     type = input.float, minval = 0.0, step = 0.1, defval = 40) / 100
longLossPercent = input(title = "Loss %",
     type = input.float, minval = 0.0, step = 0.1, defval = 30) / 100

// Calculating exact prices for stop loss and exit prices
longExitPercentPrice = strategy.position_avg_price * (1 + longProfitPercent)
longStopPercentPrice = strategy.position_avg_price * (1 - longLossPercent)

// Profit & Loss via ATR
longExitPrice = useAtr ? strategy.position_avg_price + atr * atrMultiplier : longExitPercentPrice
longStopPrice = useAtr ? strategy.position_avg_price - atr * atrMultiplier : longStopPercentPrice

// Plotting profit / loss targets
plot(series = (strategy.position_size > 0) ? longExitPrice : na, color = color.green, style = plot.style_linebr, transp = 0, linewidth = 3, title = "Long Target")
plot(series = (strategy.position_size > 0) ? longStopPrice : na, color = color.red, style = plot.style_linebr, transp = 0, linewidth = 3, title = "Long Stop Loss")

// Submit entry orders
if time >= start
    strategy.entry(id = "Long", long = strategy.long, comment = "Long Open", when = long)

// Submit exit orders
strategy.exit(id = "Long", limit = longExitPrice, stop = longStopPrice, comment = "Long Close", when = strategy.position_size > 0)
strategy.close(id = "Long", comment = "Expired: >" + tostring(maxTimeOpen), when = (barssince(change(strategy.opentrades)) + 1) >= maxTimeOpen)

Overview

Fear + Greed Reversal Long Strategy (Daily) enters long positions when a greed condition confirms over consecutive bars. Exits are handled via percentage-based or ATR-based profit targets and stop losses, or through a maximum order expiration bar threshold. The strategy plots custom fear and greed visual markers beneath the price bars directly on the main chart pane.

Details

Published by

krosenfeld7 on GitHub

Type

Strategy, Pine Script v4

Draws

On the price chart

Category

Mean reversion, Volatility & bands

Markets

Any symbol

Timeframes

H4 and above

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.

Settings

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

SettingTypeDefaultVariable
Backtesting Starttimetimestamp("1 Jan 2015")start
Order Expirationint100maxTimeOpen
Use ATR?boolfalseuseAtr
Low Periodint30lowPeriod
High Periodint30highPeriod
Stdev Periodint50stdev_period
Sourcesourceohlc4src
Use WMA instead of SMA?boolfalseWMA
ATR Lengthint14length
ATR Multiplierfloat1atrMultiplier
Profit %float40longProfitPercent
Loss %float30longLossPercent
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

Alert BB + RSI [Alorse] is a multi-symbol mean-reversion strategy designed for cryptocurrency trading. It evaluates Bollinger Bands and the Relative Strength Index (RSI) across up to 20 user-defined ticker symbols simultaneously. The strategy enters long posit

Strategy v4 Oscillators & momentumVolatility & bands
Updated 2 months ago · None stated GitHub repository of Alert BB + RSI [Alorse]
218 stars

The Mean Reversion Strategy Template calculates a baseline mean using SMA, EMA, or VWAP alongside upper and lower deviation bands based on standard deviation, ATR, or percentage offsets. It generates long and short entries when price touches, bounces from, or

Strategy v6 Mean reversionMoving averages
Updated 3 months ago · None stated GitHub repository of Mean Reversion Strategy Template
106 stars

This strategy backtests a long Bollinger Band deal on the price chart for 3Commas-style bots. It plots short and long simple moving averages, offset bands, and the average entry price of the open deal. A long starts when price crosses the lower band inside a c

Strategy v4 Volatility & bandsMoving averages
Updated 2 years ago · GPL-3.0 GitHub repository of 3Commas Bollinger Strategy

Bollinger Bands Strategy

by cklose2000
105 stars

This strategy generates buy and sell signals based on Bollinger Band crossovers combined with a simple moving average trend filter. It enters a long position when price crosses above the lower band while trading above the SMA filter line, and enters a short po

Strategy v5 Volatility & bandsMoving averages
Updated 1 year ago · None stated GitHub repository of Bollinger Bands Strategy
104 stars

This strategy combines Bollinger Bands with a short RSI to fade moves that leave the bands and then return. A long requires the prior bar below the lower band and oversold, then a close back inside the band while RSI is between 30 and 50. Shorts mirror that at

Strategy v5 Oscillators & momentumVolatility & bands
Updated 3 years ago · MIT GitHub repository of Bollinger Bands and RSI Strategy
37 stars

This strategy plots Bollinger Bands on the price chart using a simple moving average and a multiple of standard deviation. It places a long stop entry when close crosses above the lower band and a short stop entry when close crosses below the upper band. Pendi

Strategy v5 Volatility & bandsMean reversion
Updated 3 years ago · MIT GitHub repository of Bollinger Bands Strategy directed

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