// SPY Swing Strategy / Daily Trader
// Free for use. Header credits must be included when any form of the code included in this package is used.
// v1.0 - barbaros - converted for b4signals.com / https://b4indicators.com/threads/spy-swing-strategy.137/
//@version=5
strategy("SPY Swing Strategy", overlay=true, initial_capital=1000)
initialBalance = strategy.initial_capital
atrPeriods = input.int(10, "ATR Periods")
lowest_close = input.int(3, "Lowest Close")
trend_average = input.int(66, "Trend Average")
highest_close = input.int(19, "Highest Close")
stop_var = input.float(7.95, "Stop Var")
targetWinRate = input.float(1, "Target Win Rate")
targetWinLoss = input.float(.50, "Target Win/Loss")
useMoneyManagement = input.bool(true, "Use Money Management")
intradayOnly = input.bool(false, "Intraday Only")
TimeZone = input.string(title="Timezone", defval="America/New_York", options=["America/New_York", "Exchange"], group="Market Hours")
SessionTime = input.session(title="Session", defval="0930-1600", group="Market Hours")
FPL = strategy.grossprofit
entry = strategy.opentrades.entry_price(strategy.opentrades - 1)
isSession = nz(time(timeframe.period, SessionTime, TimeZone == "Exchange" ? syminfo.timezone : TimeZone)) != 0
isTrade = not intradayOnly or isSession
atrA = ta.sma(math.abs(close - close[1]), atrPeriods)
atr = atrA < 1 ? 1 : atrA
portfolioSize = not na(FPL) ? FPL + initialBalance : initialBalance
contractsToTrade = useMoneyManagement ? math.floor(portfolioSize / (atr * close)) : 1
buyCond = close <= ta.lowest(close, lowest_close) and close > ta.sma(close, trend_average)
sellCond = close >= ta.highest(close, highest_close)
direction = 0
direction := bar_index == 1 ? 0 :
direction[1] == 0 and buyCond and contractsToTrade > 0 ? 1 :
direction[1] == 1 and sellCond ? 0 :
direction[1]
stop_price = close
stop_price := ta.cross(direction, 0) ? close - (stop_var * atr) : stop_price[1]
plotshape(isTrade and direction == 1 and direction != direction[1], title="Buy Signal", text="Buy", style=shape.labelup, location=location.belowbar, color=color.green, textcolor=color.black, size=size.normal)
plotshape(isTrade and direction == 0 and direction != direction[1], title="Exit Signal", text="Exit", style=shape.labeldown, location=location.abovebar, color=color.gray, textcolor=color.black, size=size.normal)
var table chartHUD = table.new(position.top_right, 2, 2, bgcolor = color.gray, frame_width = 0)
if barstate.islast
table.cell(chartHUD, 0, 0, "Direction", bgcolor = color.black , text_color = color.white)
table.cell(chartHUD, 1, 0, direction == 1 ? "LONG" : "Neutral", bgcolor = direction == 1 ? color.green : color.gray, text_color = direction == 1 ? color.black : color.white)
table.cell(chartHUD, 0, 1, "Shares To Trade", bgcolor = color.black, text_color = color.white)
table.cell(chartHUD, 1, 1, direction == 1 ? "n/a" : str.tostring(contractsToTrade), bgcolor = color.gray, text_color = color.white)
strategy.order("LONG", strategy.long, contractsToTrade, when = isTrade and ta.crossover(direction, 0))
strategy.close("LONG", when = not isTrade or ta.crossunder(direction, 1), comment = "EXIT")