0

I am a newbie and really struggling to get my code working. Strategy calculates TP but not SL. I have looked at this post link but just wondering if there is a simpler solution to adopt to my code. Thanks!

strategy("Test TP/SL", overlay=true,initial_capital=300, currency=currency.USD, default_qty_value=3000, default_qty_type=strategy.cash, commission_type=strategy.commission.cash_per_order, 
     commission_value=2.25, pyramiding=1, close_entries_rule="ANY")
     

// Getting inputs
fast_length = input(title="Fast Length", type=input.integer, defval=16)
slow_length = input(title="Slow Length", type=input.integer, defval=20)
src = input(title="Source", type=input.source, defval=close)
signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 26)
sma_source = input(title="Simple MA (Oscillator)", type=input.bool, defval=false)
sma_signal = input(title="Simple MA (Signal Line)", type=input.bool, defval=false)
long_entry = input(title="Short Entry", type=input.float, defval= -0.001)
long_exit = input(title="Short Exit", type=input.float, defval= 0.001)
profitPerc = input(title="TP", type=input.float, minval=0.0, step=0.1, defval=0.7) * 0.01
lossPerc = input(title="SL", type=input.float, minval=0.0, step=0.1, defval=1) * 0.01

// Calculating
fast_ma = sma_source ? sma(src, fast_length) : ema(src, fast_length)
slow_ma = sma_source ? sma(src, slow_length) : ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)
hist = macd - signal
exitPrice = strategy.position_avg_price * (1 + profitPerc)
closePrice = strategy.position_avg_price * (1 - profitPerc)

if (hist < long_entry and hist > macd and hist > hist[1] and macd > macd[1])
    strategy.entry("Long", strategy.long, comment="OL")
    
if (strategy.position_size > 0)
    strategy.exit(id="Long", limit=exitPrice, comment="TP")

if (strategy.position_size < 0)
    strategy.exit(id="Long", stop=exitPrice, comment="SL")
Joao Silva
  • 55
  • 1
  • 10

1 Answers1

1

First, your condition

if (strategy.position_size < 0)

is never true, because you don't have short positions in your script (strategy.position_size - if the value is < 0, the market position is short). Second, you never using your closePrice, if it supposed to be your stop loss then

closePrice = strategy.position_avg_price * (1 - lossPerc)

So

//@version=4
strategy("Test TP/SL", overlay=true,initial_capital=300, currency=currency.USD, default_qty_value=3000, default_qty_type=strategy.cash, commission_type=strategy.commission.cash_per_order, commission_value=2.25, pyramiding=1, close_entries_rule="ANY")

// Getting inputs
fast_length = input(title="Fast Length", type=input.integer, defval=16)
slow_length = input(title="Slow Length", type=input.integer, defval=20)
src = input(title="Source", type=input.source, defval=close)
signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 26)
sma_source = input(title="Simple MA (Oscillator)", type=input.bool, defval=false)
sma_signal = input(title="Simple MA (Signal Line)", type=input.bool, defval=false)
long_entry = input(title="Short Entry", type=input.float, defval= -0.001)
long_exit = input(title="Short Exit", type=input.float, defval= 0.001)
profitPerc = input(title="TP", type=input.float, minval=0.0, step=0.1, defval=0.7) * 0.01
lossPerc = input(title="SL", type=input.float, minval=0.0, step=0.1, defval=1) * 0.01


// Calculating
fast_ma = sma_source ? sma(src, fast_length) : ema(src, fast_length)
slow_ma = sma_source ? sma(src, slow_length) : ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)
hist = macd - signal
exitPrice = strategy.position_avg_price * (1 + profitPerc)
closePrice = strategy.position_avg_price * (1 - lossPerc)

if (hist < long_entry and hist > macd and hist > hist[1] and macd > macd[1])
    strategy.entry("Long", strategy.long, comment="OL")
    
if (strategy.position_size > 0)
    strategy.exit(id="Long", limit=exitPrice, stop=closePrice, comment="TP or SL")

plot (exitPrice, color = color.red)
plot (closePrice, color = color.purple)

Starr Lucky
  • 1,578
  • 1
  • 7
  • 8