我正在写一份策略,我需要避免重复两次多头交易或连续两次短期交易。这是多空交易交替进行的。我试着使用strategy.closedtrades.size,但是没有用,或者我漏掉了一些东西。
我也加了金字塔,但没什么用。
// Davydov Strategy
//@version=5
strategy("Davydov Strategy", overlay=true, pyramiding=0)
// Declaring stop loss (SL) take profit (TP)
SL = input.float(0.5, "Stop loss", minval=0.1, maxval=100, step=0.1)
TP = input.float(1, "Take profit", minval=0.1, maxval=100,step=0.1)
// Interrupt the Long-Long / Short-Short cycle
var count = 0
longLong = strategy.closedtrades.size(0)>=0
if longLong
count := count + 1
plot(count)
// Condition Long
// Price calculation for stop loss and take profit (Long)
longStop = strategy.position_avg_price*(100-SL)/100
longProfit = strategy.position_avg_price*(100+TP)/100
// Condition check
if trend_strength > 0
if Greenbar1 and Greenbar2 == 1 ? RsiMa2 - 50 : na
if direction < 0 ? supertrend : na
strategy.entry("Long", strategy.long, na)
// Conditions for closing a deal
strategy.exit("Close","Long", stop=longStop, limit=longProfit,
when=strategy.position_size>=0)
// Condition Short
//Price calculation for stop loss and take profit
shortStop = strategy.position_avg_price*(100+SL)/100
shortProfit = strategy.position_avg_price*(100-TP)/100
// Condition check
if trend_strength < 0
if Redbar1 and Redbar2 == 1 ? RsiMa2 - 50 : na
if direction < 0? na : supertrend
strategy.entry("Short", strategy.short, na)
// Conditions for closing a deal
strategy.exit("exit","Short", stop=shortStop,limit=shortProfit,
when=strategy.position_size<=0)
[1]: https://i.stack.imgur.com/WuCXU.png发布于 2021-11-26 15:19:19
getLastPosSign()是您需要的助手函数:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © adolgov
//@version=5
strategy("My Strategy", overlay=true, margin_long=100, margin_short=100)
longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
if (longCondition)
strategy.entry("My Long Entry Id", strategy.long)
shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))
if (shortCondition)
strategy.entry("My Short Entry Id", strategy.short)
getLastPosSign() =>
strategy.closedtrades > 0 ? math.sign(strategy.closedtrades.size(strategy.closedtrades-1)) : na
plot( getLastPosSign(), style = plot.style_linebr )https://stackoverflow.com/questions/70028933
复制相似问题