0

After entry, I would like to set the Stop Loss according to 4 different criteria and it will automatically close the position after the criteria are met. But the strategy.exit with stop loss part seems not accurate.

  1. If the price immediately falls below "Vegas channel" after entry and the "Volume Osc" indicator is above the 144 parameter line, then we immediately stop loss at the current price of that time.

  2. If the price immediately falls below "Vegas channel" after entry and the "Volume Osc" indicator is below 144, continue to observe whether there is an accelerated decline (eg: decline 5%) within 24 hours. If there is no acceleration, set the stop loss at the support level.

  3. If there is no significant acceleration after the break, but it still cannot return to the top of the Vegas channel within 24 hours, we also make a stop loss, and the final stop loss position is set below the "Vegas channel".

  4. If the price no falls below "Vegas channel", then set the stop loss at usual at 6%.

Remark: Vegas channel=the track between ema144 & ema169

strategy("Vegas", overlay=true, precision=2, pyramiding=6, initial_capital=6000)

//---Volume Oscillator---
var cumVol = 0.
cumVol += nz(volume)
if barstate.islast and cumVol == 0
    runtime.error("No volume is provided by the data vendor.")
shortlen = input.int(1, minval=1, title = "Short Length")
longlen = input.int(14, minval=1, title = "Long Length")
short = ta.ema(volume, shortlen)
long = ta.ema(volume, longlen)
osc = 100 * (short - long) / long

//---Support and Resistance Levels---
leftBars  = input(15, title = "Left Bars ")
rightBars  = input(15, title = "Right Bars")
volumeThresh  = input(20, title = "Volume Threshold")
highUsePivot = fixnan(ta.pivothigh(leftBars, rightBars)[1])
lowUsePivot = fixnan(ta.pivotlow(leftBars, rightBars)[1])
r1 = plot(highUsePivot, color=ta.change(highUsePivot) ? na : #ff113d,  linewidth=3, offset=-(rightBars+1), title="Resistance")
s1 = plot(lowUsePivot, color=ta.change(lowUsePivot) ? na : #007bff,  linewidth=3, offset=-(rightBars+1), title="Support")

//---Settings---
pc=input.float(title="tpsl percent",defval=6)/100
ordersize=1000/close

vegas_long = (out144>out576 and out169>out576) and (out144>out676 and out169>out676)
vegas_short = (out144<out576 and out169<out576) and (out144<out676 and out169<out676)
vegas_max = math.max(out144,out169)
vegas_min = math.min(out144,out169)
distance_long = low-vegas_max
distance_short = vegas_min-high
vegas_long2 = out12>out144 and out12>out169
vegas_short2 = out12<out144 and out12<out169
opengap = input.int(title="Next Position Gap", defval=1)
dist_min = input.float(title="Min Distance", defval=1)
dist_max = input.float(title="Max Distance", defval=25)

//---Long---
longCondition = vegas_long and vegas_long2 and (low>vegas_max and distance_long>dist_min and distance_long<=dist_max)

if longCondition and (strategy.position_size == 0 or (strategy.position_size > 0 and close<strategy.position_avg_price and strategy.position_avg_price-close>=opengap))

longTP_pc  = strategy.position_avg_price*(1+pc)
longSL_pc  = strategy.position_avg_price*(1-pc)
longSL_support = lowUsePivot

if strategy.position_size > 0 and (barstate.isnew and ta.crossunder(strategy.position_avg_price,vegas_max) and osc > 144)
    strategy.exit("Close Long","Long", limit=longTP_pc, stop=close, alert_message = "Close Long")

if strategy.position_size > 0 and (barstate.isnew and ta.crossunder(strategy.position_avg_price,vegas_max) and osc < 144)
    strategy.exit("Close Long","Long", limit=longTP_pc, stop=longSL_support, alert_message = "Close Long")

if strategy.position_size > 0 and (barstate.isnew and osc < 144)
    strategy.exit("Close Long","Long", limit=longTP_pc, stop=longSL_pc, alert_message = "Close Long")
Ying
  • 15
  • 4
  • Where exactly do you need help? What have you tried so far? What is working and what is not working in your code? – vitruvius Nov 04 '22 at 22:28
  • For the strategy.exit part, to set the stop loss bases on different SL criteria. I not sure whether it can be used this way. If criteria1 strategy.exit("Close Long","Long", limit=longTP_pc, stop=close) If criteria2 strategy.exit("Close Long","Long", limit=longTP_pc, stop=longSL_support) If criteria3 strategy.exit("Close Long","Long", limit=longTP_pc, stop=longSL_pc) – Ying Nov 05 '22 at 11:00

0 Answers0