Short SPY ETF With an Opening Range Breakout

barbaros

Administrator
Staff member
Another strategy from Relaxed Trader at https://relaxedtrader.com/opening-range-breakout-strategy/

I decided to code this in Pinescript this time. Although I tried to code as close as I can, results are not reflecting the original author's findings. Please check my code.

Code:
// SPY Short with ORB
// 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

//@version=5
strategy("SPY Short with ORB", overlay=true, initial_capital=100000)

UseMoneyManagement = input.bool(true, "Use Money Management")
ConstractsSet = input.int(1, "Contracts Set")
riskPercent = input.int(10, "Risk Percent")
var1 = input.int(39, "var1")
var2 = input.int(1, "var2")
var3 = input.float(.25, "var3")
var4 = input.int(11, "var4")

atr = ta.atr(var1)
Range = math.abs(close[1] - open[1])
LargestRange = ta.highest(Range[1], var4)

contractsToTrade = UseMoneyManagement ? ((riskPercent * 0.01) * strategy.equity) / (atr * 2.6) : ConstractsSet

float LimitPrice = na
LimitPrice := LimitPrice[1]

SellSignal = strategy.position_size == 0 and Range >= LargestRange and Range >= var2 * atr and close > open
SellExit = strategy.position_size < 0 and high >= LimitPrice

plotshape(SellSignal, text="Sell", style=shape.labeldown, location=location.abovebar, color=color.red, textcolor=color.white)

if SellSignal
    LimitPrice := close + var3 * atr
    strategy.entry("SE", strategy.short, contractsToTrade, limit = LimitPrice)
    
if SellExit
    LimitPrice := na
    strategy.close_all(SellExit)

plot(LimitPrice, "LimitPrice", style=plot.style_linebr)

07rztgf.png
 

Hutchew

New member
Study Error
Quantity for entry or order must be a positive number less or equal to 1000000000.000000, passed value is -2663452859.000000.

I don't normally have problems loading scripts, did I do something wrong? Tried changing the start value to several values from 1000 to the number above, no luck.


Pfffftttttt..........have to set margin. Disregard
 

barbaros

Administrator
Staff member
Study Error
Quantity for entry or order must be a positive number less or equal to 1000000000.000000, passed value is -2663452859.000000.

I don't normally have problems loading scripts, did I do something wrong? Tried changing the start value to several values from 1000 to the number above, no luck.


Pfffftttttt..........have to set margin. Disregard
Were you able to figure it out? I didn't make sure the shares to purchase doesn't go negative. You can use the following for downward protection.

Code:
// SPY Short with ORB
// 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
// v1.1 - barbaros - do not trade when no shares

//@version=5
strategy("SPY Short with ORB", overlay=true, initial_capital=100000)

UseMoneyManagement = input.bool(true, "Use Money Management")
ConstractsSet = input.int(1, "Contracts Set")
riskPercent = input.int(10, "Risk Percent")
var1 = input.int(39, "var1")
var2 = input.int(1, "var2")
var3 = input.float(.25, "var3")
var4 = input.int(11, "var4")

atr = ta.atr(var1)
Range = math.abs(close[1] - open[1])
LargestRange = ta.highest(Range[1], var4)

contractsToTrade = UseMoneyManagement ? ((riskPercent * 0.01) * strategy.equity) / (atr * 2.6) : ConstractsSet

float LimitPrice = na
LimitPrice := LimitPrice[1]

SellSignal = strategy.position_size == 0 and Range >= LargestRange and Range >= var2 * atr and close > open
SellExit = strategy.position_size < 0 and high >= LimitPrice

plotshape(SellSignal, text="Sell", style=shape.labeldown, location=location.abovebar, color=color.red, textcolor=color.white)

if SellSignal and contractsToTrade > 0
    LimitPrice := close + var3 * atr
    strategy.entry("SE", strategy.short, contractsToTrade, limit = LimitPrice)
   
if SellExit
    LimitPrice := na
    strategy.close_all(SellExit)

plot(LimitPrice, "LimitPrice", style=plot.style_linebr)
 
Top