'Pinescript -I want to add a trailing stop based on buy conditions candle low of inside bar
if (InSession(sessionTime) and Bullish and strategy.position_size == 0)
noOfTrades := 0
_EntryPrice := high
_StopLoss1 := low
_BullRange := (high -low)
StopValue = high - _BullRange
Bull_Price_Stop := math.max(StopValue, Bull_Price_Stop[1])
_LongTP1 := _EntryPrice + (high -low)
_LongTP2 := _EntryPrice + 2 * (high -low)
_LongTP3 := _EntryPrice + 3 * (high -low)
longCondition := true
float long_sl_level = na
float short_sl_level = na
long_tp1_hit = strategy.position_size[1] > 0 and strategy.position_size[0] > 0 and strategy.position_size[1] > strategy.position_size[0]
long_tp2_hit = strategy.position_size[1] > 0 and strategy.position_size[0] > 0 and strategy.position_size[1] > strategy.position_size[0]
long_sl_level := Bullish ? _StopLoss1 : long_tp1_hit ? _EntryPrice :long_tp2_hit ? _LongTP1: _StopLoss1:long_sl_level[1]
strategy.entry("Long", strategy.long, qty = long_qty, stop=_EntryPrice)
strategy.exit("LE1", qty_percent=50, limit=_LongTP1, stop=long_sl_level)
strategy.exit("LE2", qty_percent=100, limit=_LongTP2, stop=long_sl_level)
Currently, so far entry is correct, when TP1 is hit my SL moves to breakeven, when TP2 is hit my SL is still at breakeven (this has to be incremented to TP1? which is not happening as per my code). here i have not defined in SL & TP in % of input()...My initial condition is when there is an inside bar when high is broken i enter the trade and SL is inside bar low for long position. my TP1 is (high-low) of inside candle from my entry point...can someone guide me. U can try on Banknifty / Nifty indices. I dont have any idea of converting to pips/%of pips...just looking for a simple solution to fix the issue...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
