Strategy · Pine Script v6 · On the chart

S50 TFEX Strategy LeftBar RightBar

by lumduan

A pivot breakout strategy for the S50 futures on a 30-minute chart.

View on GitHub

Repaints

GitHub stars1on the repository
Pine versionv6current
Lines of code75
Settings7inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago29 Apr 2025

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

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

s50_tfex_strategy_copy.pine
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © lumduan
//@version=6

// For S50 TFEX Strategy Timeframe 30 minutes 

// ————— Constants
float RISK_FREE_RATE = 2.2 // Risk-free rate for calculating Sharpe ratio
HIGH_PRICE_BAR_COLOR = color.green
LOW_PRICE_BAR_COLOR = color.red
int HIGH_PRICE_BAR_WIDTH = 1
int LOW_PRICE_BAR_WIDTH = 1

TABLE_HEADER_COLOR = color.new(color.black, 0)
TABLE_INDEX_COLOR = color.new(color.gray, 0)
TABLE_CELL_COLOR = color.new(color.black, 0)
TABLE_YEAR_CELL_POSITIVE_COLOR = color.new(color.green, 0)
TABLE_YEAR_CELL_NEGATIVE_COLOR = color.new(color.red, 0)

int CONTACT_PER_ORDER = 2
float COMMISSION_PRICE = 23.0 //THB

// Inputs for configuring the pivot point calculation
int leftBars  = input(21) // Number of bars to the left for pivot calculation
int rightBars = input(2) // Number of bars to the right for pivot calculation
int riskPercent = input(1, title="Risk % per Trade") / 100
int atrLength = input(10, title="ATR Length")
int riskRewardRatio = input(2, title="Risk-Reward Ratio")
int prec      = input(2, title = "Return Precision") // Precision of return percentages
bool show_table = input.bool(defval = true, title = "Show Return Rate Table On/Off") // Show or hide the table

strategy(title = "S50 TFEX Strategy LeftBar RightBar",overlay = true, calc_on_every_tick = true, default_qty_type = strategy.fixed, default_qty_value = CONTACT_PER_ORDER, commission_type = strategy.commission.cash_per_contract, commission_value = COMMISSION_PRICE , risk_free_rate = RISK_FREE_RATE)

// ATR Calculation
atr = ta.atr(atrLength)

// Account Equity & Risk Per Trade
strategy_equity = strategy.equity
riskAmount = strategy_equity * riskPercent


swingHigh = ta.pivothigh(leftBars,rightBars)
swingLow = ta.pivotlow(leftBars,rightBars)

// Stop-Loss and Take-Profit Calculation
longStop = swingLow - atr
shortStop = swingHigh + atr
longTakeProfit = swingHigh + atr * riskRewardRatio
shortTakeProfit = swingLow - atr * riskRewardRatio

// Position Sizing: Contracts based on risk
longContracts = riskAmount / (swingHigh - longStop)
shortContracts = riskAmount / (shortStop - swingLow)

// Initialize high and low price variables
float highPrice = 0.0
highPrice := not na(swingHigh) ? swingHigh : highPrice[1] // Update high price if a new swing high is detected, else carry forward previous value

float lowPrice = 0.0
lowPrice := not na(swingLow) ? swingLow : lowPrice[1] // Update low price if a new swing low is detected, else carry forward previous value

// Long Entry
if not na(swingHigh)
    strategy.entry("Long", strategy.long, qty=longContracts, stop=swingHigh + syminfo.mintick, comment = "Entry Long")
    strategy.exit("Long Exit", from_entry="Long", stop=longStop, limit=longTakeProfit, comment = "Exit Long")

// Short Entry
if not na(swingLow)
    strategy.entry("Short", strategy.short, qty=shortContracts, stop=swingLow - syminfo.mintick, comment = "Entry Short")
    strategy.exit("Short Exit", from_entry="Short", stop=shortStop, limit=shortTakeProfit, comment = "Exit Short")


// Plot high and low prices on the chart
plot(highPrice, color = HIGH_PRICE_BAR_COLOR, linewidth = HIGH_PRICE_BAR_WIDTH)
plot(lowPrice, color = LOW_PRICE_BAR_COLOR,   linewidth = LOW_PRICE_BAR_WIDTH)

Overview

Swing highs and lows are found from left and right bar counts, with stop entries placed beyond them. Each entry has an exit order with a stop one ATR past the pivot and a limit set by a risk to reward ratio. Size is based on a percent of equity at risk. It plots the latest pivot levels and an optional return table. Pivot signals arrive late and orders run on every tick.

Details

Published by

lumduan on GitHub

Type

Strategy, Pine Script v6

Draws

On the price chart

Category

Breakout, Support & resistance, Volatility & bands

Markets

Indices

Timeframes

M15-H1

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)

Settings

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

SettingTypeDefaultVariable
leftBarsint21leftBars
rightBarsint2rightBars
Risk % per Tradeint1riskPercent
ATR Lengthint10atrLength
Risk-Reward Ratioint2riskRewardRatio
Return Precisionint2prec
Show Return Rate Table On/Offbooltrueshow_table
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

257 stars

This strategy plots VWMA-based Bollinger bands on the 1-minute ES futures price chart for volatility analysis and potential breakout identification. It calculates dynamic long and short stop loss and take profit levels using percentage inputs relative to entry

Strategy v6 Volatility & bandsSupport & resistance
Updated 6 weeks ago · None stated GitHub repository of V1.6 ES - Futures 1M No Repaint
16 stars

This strategy trades Donchian breakouts measured on weekly bars while orders execute on the chart timeframe. It allows long and short entries when price breaks the prior weekly channel and exits against the shorter weekly channel. Position size risks a set per

Strategy v6 TrendBreakout
Updated 12 months ago · None stated GitHub repository of Seykota Alt 4: Weekly Donchian
11 stars

Calculates upper and lower Donchian Channel bands over a customizable lookback length and plots them on the main price chart. At a specified exchange hour, the strategy places a long buy stop order at the upper Donchian Channel level. Unfilled pending orders a

Strategy v6 BreakoutSupport & resistance
Updated 5 months ago · Apache-2.0 GitHub repository of Estrategia Donchian Breakout
6 stars

This overlay strategy builds independent timed session ranges and marks range, trendline, mean, VWAP, and high-low levels when those displays are enabled. It enters on range breakouts and exits with strategy exits, choosing among range, ATR, fixed-point, and r

Strategy v5 Sessions & timeBreakout
Updated 7 months ago · None stated GitHub repository of MSG Enhanced LEGO System Strategy
3 stars

Donchian Channel Breakout is a Pine Script v5 strategy drawn on the price chart. It enters long when close crosses above the previous upper Donchian band and short when close crosses below the previous lower band, reversing with the opposite signal. An ATR tra

Strategy v5 BreakoutVolatility & bands
Updated 7 months ago · MIT GitHub repository of Donchian Channel Breakout

The Range Percent Breakout Strategy identifies range contraction and inside bar patterns where the current bar's range is within a specified percentage threshold of the previous bar. When this condition is met while flat, breakout levels are established above

Strategy v5 BreakoutVolatility & bands
Updated 5 months ago · None stated GitHub repository of Range Percent Breakout Strategy

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