结合两种策略.仅当两个方向相同时才输入


  • 我通常将多种策略组合起来并对其进行测试
  • 当我使用and逻辑时,只有当两个策略的buyCondition(无论是ta。crossoverOR>/<value类型逻辑(彼此对齐时,策略才会进入,这不是我想要的。我希望策略在"方向";两种策略的相同,即两者都是多头/空头。我知道这涉及到转换

buyConditiondir = 1

但这是一个挑战,因为我是一个业余爱好者。我想要一个可以应用于所有策略的标准逻辑,这些策略可以将buyCondition更改为dir = 1,我可以将其用作代码中的模板。

  • 它还应该包含,如果信号之前已经到达,则它不会重新输入,即buyCondition and not buyCondition [1]逻辑

我在这里和下面找到了一些示例代码,不知道它是否有帮助:

1.样本代码1

pos = int(na)
iff_3 = close[1] > nz(xATRTrailingStop[1], 0) and close < nz(xATRTrailingStop[1], 0) ? -1 : nz(pos[1], 0)
pos := close[1] < nz(xATRTrailingStop[1], 0) and close > nz(xATRTrailingStop[1], 0) ? 1 : iff_3
color_1 = pos == -1 ? color.red : pos == 1 ? color.lime : color.blue
//patr=plot(xATRTrailingStop, color=color, linewidth=2, title="ATR Trailing Stop", transp=0)
// Deternine if we are currently LONG
isLong = false
isLong := nz(isLong[1], false)
// Determine if we are currently SHORT
isShort = false
isShort := nz(isShort[1], false)
//Trading
// Buy only if the buy signal is triggered and we are not already long
LONG = not isLong and pos == 1

// Sell only if the sell signal is triggered and we are not already short   
SHORT = not isShort and pos == -1
if LONG
isLong := true
isShort := false
isShort
if SHORT
isLong := false
isShort := true
isShort

2.示例代码2(这里我不想回顾(

//@version=5
strategy("Strategy Loop Direction Template, Alam", overlay=true)
lookback = input.int(title = "Lookback Period", defval = 10)
islong  = strategy.position_size > 0 
isshort = strategy.position_size < 0 
isnotin = strategy.position_size == 0
///////////////// MACD Strategy Calculations
fast_length = input.int(title="Fast Length", defval=12)
slow_length = input.int(title="Slow Length", defval=26)
src = input.source(title="Source", defval=close)
signal_length = input.int(title="Signal Smoothing", minval = 1, maxval = 50, defval = 9)
sma_source = input.string(title="Oscillator MA Type", defval="EMA", options=["SMA", "EMA"])
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])
macd_lb    = input.bool(title = "Use MACD Lookback?", defval = false)
// Calculating
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist = macd - signal
macd_L = ta.crossover(macd,signal) and macd < 0
macd_S = ta.crossunder(macd,signal) and macd > 0
///////////////// RSI Strategy Calculations
rsi_len = input.int(title="RSI Length", defval=14)
rsi_lb  = input.bool(title = "Use RSI Lookback?", defval = false)
rsi = ta.rsi(close,rsi_len)
rsi_L = ta.crossover(rsi,30)
rsi_S = ta.crossunder(rsi,70)
/////////////////////////////////////////////////
// Define Variables
bool macd_LC = false
bool macd_SC = false
bool rsi_LC  = false
bool rsi_SC  = false
// Loop Checker
for i = 0 to lookback by 1
// Long Checks
if (macd_lb ? macd_L[i] : macd_L)
macd_LC := true
if (rsi_lb ? rsi_L[i] : rsi_L)
rsi_LC := true        
// Short Checks
if (macd_lb ? macd_S[i] : macd_S)
macd_SC := true
if (rsi_lb ? rsi_S[i] : rsi_S)
rsi_SC := true  
// Long and Short Conditions
longCondition   = macd_LC and rsi_LC
shortCondition  = macd_SC and rsi_SC
// Entries
if longCondition and (isnotin or isshort)
strategy.entry("Enter Long", strategy.long ,comment = "Long Entry", alert_message = "Enter Long Order")
strategy.close("Enter Short", comment = "Close Short", alert_message = "Close Short Order")
if shortCondition and (isnotin or islong)
strategy.entry("Enter Short", strategy.short ,comment = "Short Entry", alert_message = "Enter Short Order")
strategy.close("Enter Long", comment = "Close Long", alert_message = "Close Long Order")

3.样本代码3

// INPUT ============================================================================================================
fastMALen = input.int(defval = 21, title = 'Fast/Slow SMA Length', minval = 1, inline = 'MA Length', group = 'Strategy')
slowMALen = input.int(defval = 49, title = '', minval = 1, tooltip = 'How many candles back to calculte the fast/slow SMA.', inline = 'MA Length', group = 'Strategy')
// LOGIC ============================================================================================================
fastMA = ta.sma(close, fastMALen)
slowMA = ta.sma(close, slowMALen)
bool openLongPosition = longTradesEnabled and ta.crossover(fastMA, slowMA)
bool openShortPosition = shortTradesEnabled and ta.crossunder(fastMA, slowMA)
bool closeLongPosition = longTradesEnabled and ta.crossunder(fastMA, slowMA)
bool closeShortPosition = shortTradesEnabled and ta.crossover(fastMA, slowMA)
// PLOT =============================================================================================================
var fastColor = color.new(color.yellow, 0)
plot(series = fastMA, title = 'Fast SMA', color = fastColor, linewidth = 1, style = plot.style_line)
var slowColor = color.new(color.orange, 0)
plot(series = slowMA, title = 'Slow SMA', color = slowColor, linewidth = 1, style = plot.style_line)
//
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// ENTRY ============================================================================================================
// LOGIC ============================================================================================================
// the open signals when not already into a position
bool validOpenLongPosition = openLongPosition and not (strategy.opentrades.size(strategy.opentrades - 1) > 0)
bool validOpenShortPosition = openShortPosition and not (strategy.opentrades.size(strategy.opentrades - 1) < 0)
bool longIsActive = validOpenLongPosition or strategy.opentrades.size(strategy.opentrades - 1) > 0 and not closeLongPosition
bool shortIsActive = validOpenShortPosition or strategy.opentrades.size(strategy.opentrades - 1) < 0 and not closeShortPosition
// PLOT =============================================================================================================
var buyColor = color.new(color.green, 0)
var sellColor = color.new(color.red, 0)
var textColor = color.new(color.white, 0)
if (validOpenLongPosition)
label.new(x = bar_index, y = na, text = 'Buy', yloc = yloc.belowbar, color = buyColor, style = label.style_label_up, textcolor = textColor)
if (validOpenShortPosition)
label.new(x = bar_index, y = na, text = 'Sell', yloc = yloc.abovebar, color = sellColor, style = label.style_label_down, textcolor = textColor)
// ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// POSITION ORDERS ==================================================================================================
// LOGIC ============================================================================================================
// close on trend reversal
if (closeLongPosition)
strategy.close(id = 'Long Entry', comment = 'Close Long', alert_message = 'Long: Closed at market price')
// close on trend reversal
if (closeShortPosition)
strategy.close(id = 'Short Entry', comment = 'Close Short', alert_message = 'Short: Closed at market price')
// getting into LONG position
if (openLongPosition)
strategy.entry(id = 'Long Entry', direction = strategy.long, alert_message = 'Long(' + syminfo.ticker + '): Started')
// getting into SHORT position
if (openShortPosition)
strategy.entry(id = 'Short Entry', direction = strategy.short, alert_message = 'Short(' + syminfo.ticker + '): Started')

您不能有一个模板,因为每个脚本都不同。

这里有一种解决一般问题的方法,它应该对大多数其他情况都有帮助。

假设脚本1在script1_entry = ta.crossover(a, b)时触发条目,脚本2在script2_entry = ta.crossunder(rsi, c)时触发条目。

假设您的进入条件是脚本1和2处于买入状态,并且价格低于EMA 50。

当价格低于EMA 50时,您可以检查脚本1和脚本2是否处于多头位置的方法是使用comparasion运算符:

script3_entry = (a > b) and (rsi < c) and ta.crossunder(close, ema50)

如果脚本1很长,则(a > b)将是true,如果脚本2很长,那么(rsi < c)将是true

最新更新