这个策略的主要思想是根据最后N只K线的颜色来决定做多或做空。如果最后N只K线都是绿色,就做多;如果最后N只K线都是红色,就做空。其独特之处在于加入了一个“逆向逻辑”的参数,可以对原有逻辑取反。当“逆向逻辑”参数为真时,最后N只绿色K线会做空,而最后N只红色K线会做多。
这个策略主要依赖下面几个重要参数: 1. numCandlesToCheck: 用来指定需要检查的K线数量 2. numCandlesToExit: 指定持仓后需要退出头寸的K线数量 3. inverseLogic: 逆向逻辑的参数,为真时对原有多空逻辑取反
关键逻辑是通过for循环遍历最近的numCandlesToCheck只K线,实时统计连续出现的绿色K线和红色K线的数量。如果连续红色K线≥numCandlesToCheck就标记lastNCandlesRed为真。如果连续绿色K线≥numCandlesToCheck就标记lastNCandlesGreen为真。
当lastNCandlesGreen为真时,如果inverseLogic参数为假,就做多;如果为真,就做空。相反,当lastNCandlesRed为真时,如果inverseLogic参数为假,就做空;如果为真,就做多。
无论做多做空,计数器barsSinceEntry在开仓后会重置为0。当barsSinceEntry大于等于numCandlesToExit时,会平掉当前持仓。
这是一个利用K线颜色决策的有趣策略,加入了“逆向逻辑”参数,可以灵活调整做多做空的逻辑。主要优势有:
该策略也存在一些风险需要注意:
针对上述风险,可以采取以下措施加以控制和优化:
该策略主要可以从以下几个方向进行优化:
该策略整体思路清晰易懂,利用简单的K线颜色判定形成交易信号。策略参数的调整可以形成丰富的组合变化,从而针对不同市场环境和品种进行优化调整。同时也需要注意一些潜在的风险,采用必要的措施来控制风险。通过不断丰富策略内容,该策略可以成为一个值得长期实战并不断优化提高的有价值策略。
/*backtest
start: 2022-12-25 00:00:00
end: 2023-12-25 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=4
strategy("Last Number of Candles", overlay=true)
// Define the condition for a green candle
isGreenCandle(candle) =>
close[candle] > open[candle]
// Define the condition for a red candle
isRedCandle(candle) =>
close[candle] < open[candle]
// Input to specify the number of candles to check
numCandlesToCheck = input(5, title="Number of Candles to Check")
numCandlesToExit = input(2, title="Number of Candles To Exit") // Corrected the input title
// Initialize variables to count consecutive green and red candles
var int consecutiveGreenCandles = 0
var int consecutiveRedCandles = 0
// Initialize barsSinceEntry outside the loop
var int barsSinceEntry = 0
// Loop through the last "numCandlesToCheck" candles
for i = 0 to numCandlesToCheck - 1
if isGreenCandle(i)
consecutiveGreenCandles := consecutiveGreenCandles + 1
consecutiveRedCandles := 0 // Reset the count for consecutive red candles
else if isRedCandle(i)
consecutiveRedCandles := consecutiveRedCandles + 1
consecutiveGreenCandles := 0 // Reset the count for consecutive green candles
// Check if the last "numCandlesToCheck" candles are green or red
lastNCandlesGreen = consecutiveGreenCandles >= numCandlesToCheck
lastNCandlesRed = consecutiveRedCandles >= numCandlesToCheck
// Calculate the quantity based on the investment value and current asset price
investmentValue = input(10000, title="Investment Value")
var assetPrice = close
var quantity = investmentValue / assetPrice
inverseLogic = input(false, title="inverseLogic")
// Entry condition: Open a buy order if the last "numCandlesToCheck" candles are green
if lastNCandlesGreen
if inverseLogic
strategy.entry("Short", strategy.long, qty = quantity)
else
strategy.entry("Buy", strategy.long, qty = quantity)// Reset barsSinceEntry when entering a trade
barsSinceEntry := 0
// Entry condition: Open a short order if the last "numCandlesToCheck" candles are red
if lastNCandlesRed
if inverseLogic
strategy.entry("Buy", strategy.long, qty = quantity)
else
strategy.entry("Short", strategy.short, qty = quantity)
// Reset barsSinceEntry when entering a trade
barsSinceEntry := 0
// Increment barsSinceEntry
barsSinceEntry := barsSinceEntry + 1
// Exit condition: Close the position after 2 bars
if barsSinceEntry >= numCandlesToExit
strategy.close("Buy")
strategy.close("Short")