// This source code is subject to the terms of the Mozilla Public License 2.0
// © username
// [CUCEDT-53] Strategy Lifecycle: Train, Test, and Deployment
//@version=6
strategy("[CUCEDT-W53] Lifecycle Example", overlay = true, initial_capital = 100000)
// ————— Inputs
string grp_life = "Strategy Lifecycle"
int trainStart = input.time(timestamp("2023-01-01"), "1. Train Start Date", group = grp_life)
int trainEnd = input.time(timestamp("2024-01-01"), "2. Train End / Test Start", group = grp_life)
string grp_mode = "Mode Selection"
// เปลี่ยนเป็น string เพื่อให้เลือกได้เพียงโหมดเดียว (Exclusive Selection)
string runMode = input.string("Training", "Select Run Mode", options = ["Training", "Testing", "Deployment"], group = grp_mode)
// ————— Logic สำหรับการแยกช่วงเวลาอย่างเด็ดขาด
// [Image of train test split in machine learning]
// 1. โหมด Training: เทรดเฉพาะช่วงวันที่กำหนดเท่านั้น
bool isTraining = (runMode == "Training") and (time >= trainStart and time < trainEnd)
// 2. โหมด Testing: เทรดตั้งแต่จุดสิ้นสุด Training จนถึงปัจจุบัน (ไม่เอาช่วง Training มาปน)
bool isTesting = (runMode == "Testing") and (time >= trainEnd)
// 3. โหมด Deployment: เทรดทุกอย่าง (Live Mode)
bool isDeploy = (runMode == "Deployment")
// ตัวกรองสุดท้าย: จะเป็น True ได้เพียงเงื่อนไขเดียวตามโหมดที่เลือก
bool canTrade = isTraining or isTesting or isDeploy
// ————— Strategy Logic
float ma = ta.sma(close, 20)
bool longSig = ta.crossover(close, ma)
if canTrade and longSig
strategy.entry("Long", strategy.long, comment = runMode)
if ta.crossunder(close, ma)
strategy.close("Long")
// ————— Visuals (Highlight เฉพาะช่วงที่กำลังเทรดจริง)
color highlightColor = isTraining ? color.new(color.green, 90) : isTesting ? color.new(color.orange, 90) : isDeploy ? color.new(color.red, 90) : na
bgcolor(highlightColor)
// แสดง Dashboard สถานะ
var table dash = table.new(position.top_right, 2, 1, bgcolor = color.black, border_width = 1)
if barstate.isconfirmed
table.cell(dash, 0, 0, "Active Mode:", text_color = color.white)
table.cell(dash, 1, 0, runMode, bgcolor = (isDeploy ? color.red : isTesting ? color.orange : color.green), text_color = color.white)