
Ý tưởng chính của chiến lược này là quyết định làm nhiều hoặc làm trống dựa trên màu sắc của N dòng K cuối cùng. Nếu N dòng K cuối cùng là màu xanh lá cây, hãy làm nhiều hơn; Nếu N dòng K cuối cùng là màu đỏ, hãy làm trống.
Chiến lược này phụ thuộc vào một số tham số quan trọng:
Logic quan trọng là đi qua vòng lặp for qua các đường K gần nhất của numCandlesToCheck và thống kê số lần liên tiếp xuất hiện của các đường K xanh và đường K đỏ trong thời gian thực. Đánh dấu lastNCandlesRed là đúng nếu đường K liên tiếp ≥numCandlesToCheck. Đánh dấu lastNCandlesGreen là đúng nếu đường K liên tiếp ≥numCandlesToCheck.
Khi lastNCandlesGreen là đúng, nếu tham số inverseLogic là sai, thì làm thêm; nếu đúng, thì làm trống. Ngược lại, khi lastNCandlesRed là đúng, nếu tham số inverseLogic là sai, thì làm trống; nếu đúng, thì làm thêm.
Bất kể bạn có làm thêm bao nhiêu lần, bộ đếm barsSinceEntry sẽ được đặt lại là 0 khi bạn mở vị trí. Khi barsSinceEntry lớn hơn hoặc bằng với numCandlesToExit, bạn sẽ xóa vị trí hiện tại.
Đây là một chiến lược thú vị để sử dụng quyết định màu K-line, thêm tham số logic reverse , có thể điều chỉnh một cách linh hoạt nhiều logic làm空. Những ưu điểm chính là:
Chiến lược này cũng có một số rủi ro cần lưu ý:
Các biện pháp sau đây có thể được áp dụng để kiểm soát và tối ưu hóa các rủi ro trên:
Chiến lược này có thể được tối ưu hóa theo các hướng sau:
Kỹ thuật tổng thể của chiến lược rất rõ ràng và dễ hiểu, sử dụng màu sắc K đơn giản để xác định hình thành tín hiệu giao dịch. Điều chỉnh các tham số chiến lược có thể tạo ra sự thay đổi kết hợp phong phú, do đó điều chỉnh tối ưu cho các môi trường và giống thị trường khác nhau. Đồng thời, cần chú ý đến một số rủi ro tiềm ẩn và áp dụng các biện pháp cần thiết để kiểm soát rủi ro. Bằng cách liên tục làm phong phú nội dung chiến lược, chiến lược có thể trở thành một chiến lược có giá trị đáng chiến đấu lâu dài và liên tục tối ưu hóa.
/*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")