【问题标题】:Strategy with Stop Loss and Take Profit not calculating Stop Loss止损和止盈的策略不计算止损
【发布时间】:2021-08-17 09:03:43
【问题描述】:

我是一个新手,我很难让我的代码正常工作。策略计算 TP 但不计算 SL。我看过这篇帖子link,但只是想知道是否有更简单的解决方案可以用于我的代码。谢谢!

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")

【问题讨论】:

    标签: pine-script tradingview-api


    【解决方案1】:

    首先,你的条件

    if (strategy.position_size < 0)
    

    永远不会正确,因为您的脚本中没有空头头寸(strategy.position_size - 如果值 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)
    
    

    【讨论】:

    • 非常感谢!
    猜你喜欢
    • 2022-06-17
    • 1970-01-01
    • 2022-10-25
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 2022-09-23
    • 2022-11-26
    • 2022-07-30
    相关资源
    最近更新 更多