Strategy · Pine Script v4 · On the chart

Fear + Greed Reversal Short Strategy

by krosenfeld7

This strategy identifies shorting opportunities by calculating Fear and Greed zones based on price extremums relative to moving averages and standard deviation bands.

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_Short_Strategy.txt, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.

Fear+Greed_Reversal_Short_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 Short Strategy (Not Good)", 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 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

short = (fearful[2] == false and fearful[1] == true and fearful == 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)

shortProfitPercent = input(title = "Profit %",
     type = input.float, minval = 0.0, step = 0.1, defval = 30) / 100
shortLossPercent = input(title = "Loss %",
     type = input.float, minval = 0.0, step = 0.1, defval = 50) / 100

// Calculating exact prices for stop loss and exit prices
shortExitPercentPrice = strategy.position_avg_price * (1 - shortProfitPercent)
shortStopPercentPrice = strategy.position_avg_price * (1 + shortLossPercent)

// Profit & Loss via ATR
shortExitPrice = useAtr ? strategy.position_avg_price - atr * atrMultiplier : shortExitPercentPrice
shortStopPrice = useAtr ? strategy.position_avg_price + atr * atrMultiplier : shortStopPercentPrice

// Plotting profit / loss targets
plot(series = (strategy.position_size < 0) ? shortExitPrice : na, color = #33b802, style = plot.style_linebr, transp = 0, linewidth = 3, title = "Short Target")
plot(series = (strategy.position_size < 0) ? shortStopPrice : na, color = #ff0900, style = plot.style_linebr, transp = 0, linewidth = 3, title = "Short Stop Loss")

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

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

Overview

Fear + Greed Reversal Short Strategy measures price deviation from recent highs or lows to detect extreme market conditions and potential trend reversals. When a sustained fearful condition is identified, the script executes short positions. Strategy trades exit based on target profit percentages, loss thresholds, ATR multipliers, or an order expiration limit defined by a maximum bar count. It plots fear and greed gaps directly on the main chart overlay.

Details

Published by

krosenfeld7 on GitHub

Type

Strategy, Pine Script v4

Draws

On the price chart

Category

Volatility & bands, Mean reversion, Moving averages, Trend

Markets

Any symbol

Timeframes

Any timeframe

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 %float30shortProfitPercent
Loss %float50shortLossPercent
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

This mean-reversion strategy plots Bollinger Bands and a long-term moving average directly on the price chart. It enters trades when price candles penetrate the outer Bollinger Bands beyond a user-defined body percentage, anticipating a price reversal. Entries

Strategy v5 Mean reversionVolatility & bands
Updated 2 months ago · None stated GitHub repository of Bollinger Bands Winner PRO [Alorse]

This strategy combines Volume Weighted Moving Average (VWMA) Bollinger Bands with 200-period and 50-period Exponential Moving Averages (EMAs). It identifies long trade opportunities when the 50 EMA is above the 200 EMA and price falls to or below the lower Fib

Strategy v4 TrendVolatility & bands
Updated 2 years ago · GPL-3.0 GitHub repository of Bollinger Band with Fib Golden Ratio (0.618)

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
257 stars

This overlay strategy targets one-minute ES futures and plots volume-weighted moving-average bands from the open, scaled by a standard-deviation multiple, plus a slower band from ten-minute opens. A higher-timeframe exponential moving average can filter trades

Strategy v6 Moving averagesVolatility & bands
Updated 6 weeks ago · None stated GitHub repository of ES - Futures 1M No Repaint

STRG-BBForce

by harryguiacorn
219 stars

STRG-BBForce is a Bollinger Bands trend-following strategy. It calculates standard Bollinger Bands and tracks directional alignment across the upper, lower, and basis lines. When all three lines slope upward, it opens a long position and closes short positions

Strategy v5 TrendVolatility & bands
Updated 1 year ago · None stated GitHub repository of STRG-BBForce

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