Tajus
Pine Script Rookie
Pine Script Rookie
Posts: 1
Joined: September 1st, 2021

Stop loss on lowest close between two stochastic crosses

I am trying to make a stoploss order to be the lowest price between the last crossunder of the Stochastic and the enter position signal of the current crossover for long (for example).

How do I recall the lowest price between these two designated crossunder and crossovers?

Best regards

Code: Select all

//@version=4
strategy( "[BACKTEST] Trade Pro Supertrend", overlay=true, default_qty_type =  strategy.percent_of_equity, default_qty_value = 100, initial_capital=25000, currency=currency.USD)
 
//Profit ratio
profitR = 1.5

//--------------------------------EMA--------------------------------------
ema200 = ema(close, 200)

//-----------------------------Stoch----------------------------------
periodK = 14
smoothK = 3
periodD = 3
k = sma(stoch(close, high, low, periodK), smoothK)
d = sma(k, periodD)

//-----------------------------Supertrend
Periods = input(title="ATR Period", type=input.integer, defval=10)
src = input(hl2, title="Source")
Multiplier = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3.0)
changeATR= input(title="Change ATR Calculation Method ?", type=input.bool, defval=true)
showsignals = input(title="Show Buy/Sell Signals ?", type=input.bool, defval=true)
highlighting = input(title="Highlighter On/Off ?", type=input.bool, defval=true)
atr2 = sma(tr, Periods)
atr= changeATR ? atr(Periods) : atr2
up=src-(Multiplier*atr)
up1 = nz(up[1],up)
up := close[1] > up1 ? max(up,up1) : up
dn=src+(Multiplier*atr)
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
buySignal = trend == 1 and trend[1] == -1
greenLong = trend == 1
sellSignal = trend == -1 and trend[1] == 1
redShort = trend == -1
longFillColor = highlighting ? (trend == 1 ? color.green : color.white) : color.white
shortFillColor = highlighting ? (trend == -1 ? color.red : color.white) : color.white
changeCond = trend != trend[1]

//-----------------CONDITIONS------------------------

//EMA
bullema200 = (close > ema200)
bearema200 = (close < ema200)

//Stoch cross
bullstoch = crossover(k,d)
bearstoch = crossunder(k,d)

// -------------ACTIVE CONDITIONS--------------------
longCondition = bullema200 and greenLong and bullstoch
shortCondition = bearema200 and redShort and bearstoch

//Labels
plotshape(longCondition, style=shape.triangleup, location=location.bottom, color=color.green, size=size.small)
plotshape(shortCondition, style=shape.triangledown, location=location.bottom, color=color.red, size=size.small)

// --------------------------Enter the position--------------------------
if longCondition 
    strategy.entry("long", strategy.long, 10000, when=strategy.position_size == 0)

if shortCondition 
    strategy.entry("short", strategy.short, 10000, when=strategy.position_size == 0)

// ---------------------------Exit position--------------------------

//Stoch lowest highest point

stoplow = lowest(close) while k<d between crossover(k,d) and crossunder(k,d)[1] //This is basically what I am trying to script...

//-----------LONG---------------------
if exitLong
    strategy.close("long")


//------------SHORT------------------
if exitShort
    strategy.close("short")

Deep-Wave
Pine Script Master
Pine Script Master
Posts: 44
Joined: September 4th, 2020
Contact: TradingView Profile

Re: Stop loss on lowest close between two stochastic crosses

Hi Tajus,

In your example, you would first need to know how many bars back the last crossunder (and crossover) happened. For this you can use the builtin "barssince" function. If you know that for example the last crossunder happened 7 bars ago, you can paste that value to the builtin "lowest" function (for example: lowest(low, 7)

I hope this points you in the right direction.

bet of luck with your coding :cool:

Return to “Request Scripts”