• The future is now
  • Become the real trader with our software
  • Innovative FinTech software
Hello There, Guest! Login Register


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trailing Stop Example with %
#1
TV Strategy engine do not allow to use stops are % of open price, only pips, so we need to hack it around.



The header of strategy, we will set commission fee to 0.15% to make more accurate backtesting
Code:
//@version=3
strategy("http://tvautotrader.com", shorttitle="http://tvautotrader.com", overlay=false, default_qty_type=strategy.percent_of_equity, max_bars_back=10, default_qty_value=100, calc_on_order_fills= true, calc_on_every_tick=true, pyramiding=0,commission_type=strategy.commission.percent,commission_value=0.15)

Here goes your strategy code...

But do not place strategy.entry yet, we will use strategy.entry point to calculate % of tp and sl

Code:
//Variables declarations
inpTakeProfit   = input(defval = 25, title = "Take Profit %", minval = 0)
inpStopLoss     = input(defval = 2, title = "Stop Loss %", minval = 0)
inpTrailStop    = input(defval = 2, title = "Trailing Stop Loss %", minval = 0)
inpTrailOffset  = input(defval = 0, title = "Trailing Stop Loss Offset %", minval = 0)
procent=0.0
openprice=0.0
curprice=close[0]
useTakeProfit   = 0.0
useStopLoss     = 0.0
useTrailStop    =  0.0
useTrailOffset  =  0.0
openprice := nz(openprice[1])
useTakeProfit := nz(useTakeProfit[1])
useStopLoss := nz(useStopLoss[1])
useTrailStop := nz(useTrailStop[1])
useTrailOffset := nz(useTrailOffset[1])
useTakeProfit := useTakeProfit==0.0 ? na : useTakeProfit
useStopLoss := useStopLoss==0.0 ? na : useStopLoss
useTrailStop := useTrailStop==0.0 ? na : useTrailStop
useTrailOffset := useTrailOffset==0.0 ? na : useTrailOffset
openprice := openprice==0.0 ? na : openprice

Now, very important part, open position, remember open price, and calculate stops, strategy.position_size==0 will disable recalculation of values

Code:
if (longCondition)  and (year==2018) and strategy.position_size==0
   
   openprice:=curprice
   procent := nz((curprice/100)*(1/syminfo.mintick))
   useTakeProfit   := inpTakeProfit  >= 1  ? inpTakeProfit*procent  : na
   useStopLoss     := inpStopLoss    >= 1 ? inpStopLoss*procent    : na
   useTrailStop    := inpTrailStop   >= 1 ? inpTrailStop*procent   : na
   useTrailOffset  := inpTrailOffset >= 1 ? inpTrailOffset*procent : na
   strategy.entry('BUY',strategy.long)

strategy.exit("Exit Long SL/TP", from_entry = "BUY", profit = useTakeProfit, loss = useStopLoss, trail_points = useTrailStop, trail_offset = useTrailOffset)

and on the end you can place plots to have view on stops
Code:
plot((useTakeProfit*syminfo.mintick)+openprice[0],color=green,title='TP'    )
plot(close)
plot(openprice[0]-(useStopLoss*syminfo.mintick    ),color=orange,title='SL')

Now you have fully compatible start with stoplosses, take profits and trailings


[Image: h1QxTZnY]
Telegram TVAT Support group:
https://t.me/joinchat/HaYgNRIBi9dXkvh5JC2YiQ
 
Reply
#2
Hi,

And what should be put as a longCondition?
 
Reply
#3
So it depends on your strategy eg. crossing(sma(close,20),ema(closing,50))
If you are not sure, you can always send the code to me and I will recode your strategy
Telegram TVAT Support group:
https://t.me/joinchat/HaYgNRIBi9dXkvh5JC2YiQ
 
Reply
  


Forum Jump:


Browsing: 1 Guest(s)