if not inPosition and greenCount >= 13 inPosition := true buySignal := true else if inPosition and greenCount == 11 and redCount == 9 inPosition := false exitSignal := true
// ฟังก์ชันนับแท่งเทียน getCandleCounts(start, end) => green = 0 red = 0 doji = 0 for i = start to end if close > open green += 1 else if close < open red += 1 else doji += 1 [green, red, doji]
// สร้างตาราง var table results = table.new(position.top_right, columns=2, rows=table_rows+1, border_width=1)
// ฟังก์ชันนับแท่งเทียน getCandleCounts(start, end) => green = 0 red = 0 doji = 0 for i = start to end if close > open green += 1 else if close < open red += 1 else doji += 1 [green, red, doji]
// สร้างตาราง var table results = table.new(position.top_right, columns=2, rows=table_rows+1, border_width=1)
// กำหนดขนาดอักษรที่เล็กลง var reduced_text_size = size.tiny
// ฟังก์ชันนับแท่งเทียน getCandleCounts(start, end) => green = 0 red = 0 doji = 0 for i = start to end if close > open green += 1 else if close < open red += 1 else doji += 1 [green, red, doji]
// สร้างตาราง var table results = table.new(position.top_right, columns=2, rows=table_rows+1, border_width=1)
// ฟังก์ชันนับแท่งเทียน getCandleCounts(start, end) => green = 0 red = 0 doji = 0 for i = start to end if close > open green += 1 else if close < open red += 1 else doji += 1 [green, red, doji]
// ฟังก์ชันตรวจสอบเงื่อนไข checkCondition(green, red, doji) => green == input_green and red == input_red and doji == input_doji
// Function to count candles countCandles(start, end) => var int g = 0 var int r = 0 var int y = 0 for i = start to end if close > open g += 1 else if close < open r += 1 else y += 1 [g, r, y]
// Find the bar index for the start date var int startIndex = 0 if barstate.islast if startDateInput == 0 startIndex := 0 // Use the most recent bar if no date is input else for i = 0 to bar_index if time <= startDateInput startIndex := i break
// Main calculation if barstate.islast var int prev_g = 0 var int prev_r = 0 var int prev_y = 0
for round = 0 to countRounds - 1 start = startIndex + round end = start + countPeriod - 1 [total_g, total_r, total_y] = countCandles(start, end)
// Subtract previous counts to get the current window's count g = total_g - prev_g r = total_r - prev_r y = total_y - prev_y
// Update previous counts for the next iteration prev_g := total_g prev_r := total_r prev_y := total_y
// Create result text count_text = "G" + str.tostring(g) + " \nR" + str.tostring(r) + "\n Y" + str.tostring(y)
// Create labels for all rounds if round < countRounds label_text = count_text label.new(bar_index[start], low[start], label_text, color=color.new(color.white, 100), textcolor=textColor, style=label.style_label_up, size=size.tiny)
// Plot something to keep the script active plot(close, color=color.new(color.blue, 100))