【发布时间】: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