如何进行交替 long/short 交易
How to have alternating long/short deals
我正在编写一个策略,我需要避免连续重复两次多头交易或两次空头交易。即多空交易交替进行。我尝试使用 strategy.closedtrades.size
但它没有帮助,或者我错过了一些东西。
我也添加了金字塔,但没有帮助。
[![在此处输入图片描述][1]][1]
// 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
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 )
我正在编写一个策略,我需要避免连续重复两次多头交易或两次空头交易。即多空交易交替进行。我尝试使用 strategy.closedtrades.size
但它没有帮助,或者我错过了一些东西。
我也添加了金字塔,但没有帮助。
[![在此处输入图片描述][1]][1]
// 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
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 )