Strategy · Pine Script v6 · On the chart

Lifecycle Example

by propuchon· code credited to username

This strategy demonstrates a training, testing, and deployment lifecycle for Pine Script strategies.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code50
Settings3inputs
LicenceNone statedfile: MPL-2.0
Last update8 months ago1 Feb 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

50 lines of Pine Script v6 from week 5/lifecycle.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

lifecycle.pine
// 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)

Overview

Lifecycle Example calculates a simple moving average (SMA 20) and generates buy signals on crossover. The runMode input selects between Training, Testing, and Deployment. In Training mode, it only trades within the specified date range. Testing starts after the train end date. Deployment trades continuously. It plots buy signals on the price chart and displays the current mode in a dashboard table. The background highlights the active trading period. It has no stop loss.

Details

Published by

propuchon on GitHub

Code credit

username, as written in the script's header

Type

Strategy, Pine Script v6

Draws

On the price chart

Category

Trend, Moving averages, Screeners & dashboards, Sessions & time

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • The strategy enters trades without a stop loss

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
1. Train Start Datetimetimestamp("2023-01-01")trainStart
2. Train End / Test Starttimetimestamp("2024-01-01")trainEnd
Select Run ModestringTrainingrunMode
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

EMA Trend Surfer

by nxtaiapp
2 stars

EMA Trend Surfer is a dual-timeframe trend-following strategy designed for index futures such as MES and MNQ. It determines overall trend direction using fast and slow exponential moving averages on a higher timeframe while seeking entry signals on a lower tim

Strategy v5 TrendMoving averages
Updated 11 months ago · MIT GitHub repository of EMA Trend Surfer
257 stars

Donald - Test Logic Futures is an overlay strategy designed for intraday futures trading. It combines kernel regression functions with exponential moving averages to evaluate price retracements and entry timing. Trades are filtered by session execution windows

Strategy v5 TrendMoving averages Repaints
Updated 6 weeks ago · None stated GitHub repository of Donald - Test Logic Futures

AC- MY SCRIPT1

by hasnocool
106 stars

This strategy plots an Optimized Trend Tracker and a selectable moving-average support line on the price chart. It builds the average as SMA, EMA, WMA, TMA, VAR, WWMA, ZLEMA, or TSF, then offsets long and short stops by a percent of that average. Optional mark

Strategy v5 TrendMoving averages
Updated 2 years ago · GPL-3.0 GitHub repository of AC- MY SCRIPT1

Calculates a custom moving average (MaMA) by modifying the source price with price momentum, acceleration, and psychological win-rate probability before applying an EMA or SMA. Plots the adjusted moving average directly on the price chart alongside optional re

Strategy v5 Moving averagesTrend
Updated 4 years ago · None stated GitHub repository of Momentum Adjusted Moving Average by DGT

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers