止损和止盈策略不计算止损
Strategy with Stop Loss and Take Profit not calculating Stop Loss
我是一个新手,真的很努力让我的代码工作。策略计算 TP 但不计算 SL。我看过这个 post 但只是想知道是否有更简单的解决方案可以采用我的代码。谢谢!
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")
首先,您的病情
if (strategy.position_size < 0)
永远不会为真,因为您的脚本中没有空头头寸(strategy.position_size
- 如果该值 < 0,则市场头寸是空头)。
其次,你从不使用你的closePrice
,如果它应该是你的止损那么
closePrice = strategy.position_avg_price * (1 - lossPerc)
所以
//@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)
我是一个新手,真的很努力让我的代码工作。策略计算 TP 但不计算 SL。我看过这个 post
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")
首先,您的病情
if (strategy.position_size < 0)
永远不会为真,因为您的脚本中没有空头头寸(strategy.position_size
- 如果该值 < 0,则市场头寸是空头)。
其次,你从不使用你的closePrice
,如果它应该是你的止损那么
closePrice = strategy.position_avg_price * (1 - lossPerc)
所以
//@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)