
이 전략의 주요 아이디어는 마지막 N개의 K줄의 색에 따라 더하거나 공백을 결정하는 것이다. 마지막 N개의 K줄이 모두 초록이라면 더하고, 마지막 N개의 K줄이 모두 빨간면 공백을 한다. 그것의 독특한 점은, 역 논리 의 변수를 추가하여, 원래의 논리에 반하는 것이다. 역 논리 의 변수가 참일 때, 마지막 N개의 녹색 K줄이 공백하고, 마지막 N개의 빨간 K줄만 더한다.
이 전략은 다음과 같은 몇 가지 중요한 요소에 의존합니다.
핵심 논리는 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")