Opening multiple (stand alone) orders, scaling out

Viewed 17

I'm struggling with my pine script strat. The script I want to test is based on band Standard deviations (momentum and mean reversion).

The goal is to launch multiple (separate) long and short positions that don't interfere with another but run on their own. When taking profit the position should scale out based on give price and qty.

    //@version=5
strategy(title="Double BB StdDev", shorttitle="BBs StdDev", close_entries_rule='ANY', pyramiding=20, overlay=true)

//---- BACKTEST PERIOD ----//

fromDay    = input.int(defval = 17, title = "From Day", minval = 1, maxval = 31)
fromMonth  = input.int(defval = 9, title = "From Month", minval = 1, maxval = 12)
fromYear   = input.int(defval = 2022, title = "From Year", minval = 2017)

toDay   = input.int(defval = 18, title = "To Day", minval = 1, maxval = 31)
toMonth = input.int(defval = 9, title = "To Month", minval = 1, maxval = 12)
toYear  = input.int(defval = 2022, title = "To Year", minval = 2017)

startDate  = timestamp(fromYear, fromMonth, fromDay, 00, 00)
finishDate = timestamp(toYear, toMonth, toDay, 00, 00)
time_cond  = time >= startDate and time <= finishDate


//---- IMPUTS ----//

length= input.int(20, minval=1)
src = input(hlc3, title="source")
basis = ta.sma(src, length)
Band1 = input.float(1, minval=0.001, maxval=50)
StdDev1 = Band1 * ta.stdev(src, length)
Upper1 = basis + StdDev1
Lower1 = basis - StdDev1

Band2 = input.float(2, minval=0.001, maxval=50)
StdDev2 = Band2 * ta.stdev(src, length)
Upper2 = basis + StdDev2
Lower2 = basis - StdDev2

// ---- POSITION LOGIC & EXECUTION ----//

// -- Momentum / Trend --//


LS1 = close >= Upper1  
if(time_cond and LS1)
    strategy.entry("LS1", strategy.long, limit=close, qty=0.0025)
    strategy.exit("Close Long SL", from_entry="LS1", stop=Upper1 * 0.9995, qty_percent=100)                    
    strategy.exit("Close Long TP1", from_entry="LS1", limit=close * 1.0005, qty_percent=25)
    strategy.exit("Close Long TP2", from_entry="LS1", limit=close * 1.001, qty_percent=25)
    strategy.exit("Close Long TP3", from_entry="LS1", limit=close * 1.0015, qty_percent=25)
    strategy.exit("Close Long TP4", from_entry="LS1", limit=close * 1.002, qty_percent=25)

SS1 = close <= Lower1
if(time_cond and SS1)
    strategy.entry("SS1", strategy.short, limit=close, qty=0.0025)
    strategy.exit("Close Short SL", from_entry="SS1", stop=Lower1 * 1.0005, qty_percent=100)
    strategy.exit("Close Short TP1", from_entry="SS1", limit=close * 0.01, qty_percent=25)
    strategy.exit("Close Short TP2", from_entry="SS1", limit=close * 0.02, qty_percent=25)
    strategy.exit("Close Short TP3", from_entry="SS1", limit=close * 0.03, qty_percent=25)
    strategy.exit("Close Short TP4", from_entry="SS1", limit=close * 0.04, qty_percent=25)

//-- Mean Reversion --//

BS2 = close >= Lower2 and close <=Lower1
if(time_cond and BS2)
    strategy.entry("BS1", strategy.long, limit=close, qty=0.0025)
    strategy.exit("Close Long SL mr", from_entry="BS2", stop=Lower2 * 0.9995, qty_percent=100)
    strategy.exit("Close Long TP1 mr", from_entry="BS2", limit=Upper1, qty_percent=50)
    strategy.exit("Close Long TP2 mr", from_entry="BS2", limit=Upper2, qty_percent=50)

SS2 = close <=Upper2 and close >=Upper1
if(time_cond and SS2)
    strategy.entry("SS2", strategy.short, limit=close, qty=0.0025)
    strategy.exit("Close Short SL mr", from_entry="SS2", stop=Upper2 * 1.0005, qty_percent=100)
    strategy.exit("Close Short TP1 mr", from_entry="SS2", limit=Lower1, qty_percent=50)
    strategy.exit("Close Short TP2 mr", from_entry="SS2", limit=Lower2, qty_percent=50)

The issue I'm having is that the orders mix with each other what creates a mess within exiting a position. Advise would be appreciated

0 Answers
Related