Strategy · Pine Script v5 · On the chart

SPY Options Trading - Alert

by dearvn· code credited to binancash

This overlay strategy marks short-term turning points on the price chart and enters SPY option-style trades from them.

View on GitHub

Repaints

GitHub stars257on the repository
Pine versionv5runs as is
Lines of code113
Settings5inputs
LicenceNone statedfile: MPL-2.0
Last update6 weeks ago11 Sep 2026

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

113 lines of Pine Script v5 from alert-options-trading-spy.txt, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

alert-options-trading-spy.txt
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © binancash

//@version=5
strategy("SPY Options Trading - Alert", overlay=true, pyramiding=1, default_qty_type = strategy.fixed, default_qty_value = 1 )

//****************************************************************//
// please set up alert in 5M to controll options trading on SPY   //
//****************************************************************//


stopPerCall = input(0.09, title='Stop Loss Call %') / 100 
float takePerCall = input(0.02, title='Stop Loss Call %') / 100 

stopPerPut = input(0.07, title='Stop Loss Put %') / 100 
takePerPut = input(0.02, title='Take Profit Put %') / 100

longStop = strategy.position_avg_price * (1 - stopPerCall)
shortStop = strategy.position_avg_price * (1 + stopPerPut)
shortTake = strategy.position_avg_price * (1 - takePerPut)
longTake = strategy.position_avg_price * (1 + takePerCall)


//Inputs
ma_length = input(3)

//Three Bar High/Low Moving Average
ma_high = ta.sma(high, ma_length)
ma_low = ta.sma(low, ma_length)
plot(ma_high, linewidth = 1)
plot(ma_low, linewidth = 1)

//Trend Reversal - Up
trend_reversal_up = low[2] > low[1] and low[1] < low and high > high[1]
plotshape(trend_reversal_up ? 1: na, color = color.orange, location = location.belowbar, style = shape.arrowup, text = 'U')

//Trend Reversal - Down
trend_reversal_down = high[2] < high[1] and high[1] > high and low < low[1]
plotshape(trend_reversal_down ? -1: na, color = color.navy, location = location.abovebar, style = shape.arrowdown, text = 'D')

//Short Term Low
short_term_low = low[2] > low[1] and low[1] < low and high[1] < high[2] and high > high[1]
plotshape(short_term_low ? 1: na, color = #00EEEE, location = location.belowbar, style = shape.arrowup)

//Short Term High
short_term_high = high[2] < high[1] and high[1] > high and low[1] > low[2] and low < low[1]
plotshape(short_term_high ? -1: na, color = #00EEEE, location = location.abovebar, style = shape.arrowdown)

//Trend Change Point - Up
float trend_change_up = na
trend_change_up := high > high[1] ? 1 : na
line1 = short_term_low and trend_change_up
color_up = #FFEB3B
color up = na
up := trend_change_up ? color_up : na
//plot(line1 ? high[1] : na, color = up, linewidth = 2)

//plotshape(line1 ? low : na, title="Buy", text="Buy", style=shape.triangleup, location=location.belowbar, color=color.white, size=size.normal)


//Trend Change Point - Down
float trend_change_down = na
trend_change_down := low < low[1] ? 1 : na
float trend_open_up = na
trend_open_up := low[3] < low[2] and low[1] < low ? 1 : na

//plotshape(trend_open_up, 'UP', style = shape.triangleup)

line2 = short_term_high and trend_change_down
color_down = #E040FB
color down = na
down := trend_change_down ? color_down : na
//plot(line2 ? low[1] : na, color=down, linewidth = 2)

//plotshape(line2 ? high : na, title="Sell", text="Sell", style=shape.triangledown, location=location.abovebar, color=color.blue, size=size.normal)


//Bar Color Trend Change
coloryellow = #FFEB3B
colorfuchsia = #E040FB
barcolor(line1 ? coloryellow : line2 ? colorfuchsia : na)


is_call = 0
is_put = 0

if trend_reversal_up and time>timestamp(2023, 08, 01, 09, 30)
    strategy.entry("call", strategy.long)
    is_call := 1

if trend_reversal_down and time>timestamp(2023, 08, 01, 09, 30)
    strategy.entry("put", strategy.short)
    is_put := 1

if(strategy.position_size > 0 and trend_change_down) and time>timestamp(2023, 08, 01, 09, 30)
    strategy.close(id="call")
    is_call := 2

if(strategy.position_size < 0 and trend_change_up) and time>timestamp(2023, 08, 01, 09, 30)
    strategy.close(id="put")
    is_put := 2

if(strategy.position_size > 0) and time>timestamp(2023, 08, 01, 09, 30)
    strategy.exit(id='call', limit=longTake,stop = longStop) 
    //call_price := 0.0
    is_call := 2

if(strategy.position_size < 0) and time>timestamp(2023, 08, 01, 09, 30)
    strategy.exit(id='put', limit=shortTake,stop = shortStop)
    is_put := 2

if is_call > 0 or is_put > 0
    alert('{"call":'+str.tostring(is_call)+',"put":'+str.tostring(is_put)+'}', alert.freq_once_per_bar)

Overview

SPY Options Trading - Alert plots simple moving averages of highs and lows, draws up and down reversal arrows, and colors bars when a short-term extreme coincides with a one-bar trend change. After a fixed date it enters long on an up reversal and short on a down reversal. Percentage stop and take levels are calculated from average price, but this excerpt does not show those prices sent as exit orders. Signals use the current bar and can change before it closes.

Details

Published by

dearvn on GitHub

Code credit

binancash, as written in the script's header

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Trend, Moving averages, Candlestick patterns, Scalping

Markets

Stocks

Timeframes

M1-M5

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)
  • The strategy enters trades without a stop loss

Topics

futures-markettradingviewtradovatetrading-futureses1best-indicator-tradingviewnq1trading-futurerus-stock-market

Settings

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

SettingTypeDefaultVariable
Stop Loss Call %float0.09stopPerCall
Stop Loss Call %float0.02takePerCall
Stop Loss Put %float0.07stopPerPut
Take Profit Put %float0.02takePerPut
ma_lengthint3ma_length
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

Pin Bar Magic v1

by Alorse
330 stars

This strategy trades price action pin bar patterns aligned with moving average trend alignment on the main price chart. It defines uptrends or downtrends using a fan arrangement of three moving averages (6 EMA, 18 EMA, and 50 SMA). Entry conditions require a b

Strategy v4 Candlestick patternsTrend
Updated 2 months ago · None stated GitHub repository of Pin Bar Magic v1

This strategy generates trading signals on the 5-minute price chart using Williams Fractals for trend reversals, combined with EMA fast and slow trend filters and optional RSI confirmation. Long entries trigger on bullish fractals aligned with rising EMA and R

Strategy v5 TrendOscillators & momentum
Updated 2 years ago · GPL-3.0 GitHub repository of 5min Williams Fractals Scalping (3commas)
53 stars

This strategy trades EUR/USD on the price chart using a fast and slow exponential moving average crossover filtered by RSI. It enters long when the fast EMA crosses above the slow EMA while RSI is above 50 and below the overbought level, and enters short on th

Strategy v6 TrendMoving averages
Updated 2 months ago · MPL-2.0 GitHub repository of EUR/USD EMA9-21 + RSI Scalping Strategy

DI Engulfing A002

by ruckard
13 stars

This strategy executes trades based on candlestick engulfing patterns aligned with a 200-period exponential moving average trend filter. It enters long positions when a bullish engulfing candle follows three consecutive bearish candles while price trades above

Strategy v5 Candlestick patternsTrend
Updated 1 year ago · None stated GitHub repository of DI Engulfing A002

Scalping 3EMA

by rodrigo-brito
9 stars

This long-only strategy calculates three exponential moving averages with lengths of 50, 100, and 150 periods, plotting them directly on the price chart. It also calculates a 14-period RSI and MACD. The strategy opens long positions when the moving averages al

Strategy v4 TrendOscillators & momentum
Updated 5 years ago · MIT GitHub repository of Scalping 3EMA

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