Pine Script v5 · Strategy
Linear EDCA v1.2
A Pine Script v5 strategy with no technical-analysis calculation. It has 4 order rules and no entry of its own. Exits wait for a signal; there is no stop loss. Each trade commits 1% of equity and pays 0.2% commission. 15 settings; defaults include default invest ratio 0.025, min position size 0.001 and buy slope -35.
Generated from the script's code by a template: every clause is a fact read from the source. Not written by a person or a model, and not taken from the author's description.
- Does Linear EDCA v1.2 repaint?
- No
- No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
- How did Linear EDCA v1.2 do on BTCUSDT 1D with default settings?
- Not measured
- It closed no trades there from 26 Jan 2024 to 21 Oct 2026 with the default settings, so there is no result to report. Its code limits trading to a date window, which can leave this period out. It closed no trades on the other markets either. A backtest, not investment advice.
- Does Linear EDCA v1.2 run in Pine Script v6?
- Not tested
- Neither version placed a trade on any of the 8 markets, so there was nothing to compare. The v6 version is shown for reference and is not offered as a download.
- Does Linear EDCA v1.2 use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Linear EDCA v1.2 run in Zeal Terminal?
- Yes
- It compiles in 43 ms and runs 1,000 bars in 109 ms, in the browser.
BTCUSDT · 1D · default settings
- positionMaxFloatLoss%
- positionMaxFloatGain%
- max_drawdown
- positionPercent%
- strategy.position_size
- strategy.equity
- strategy.openprofit
- strategy.netProfit
- positionValue()
- capitalValue()
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- 0.00%
- $0.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 0
- 0 won · 0 lost
- Profitable
- 0.0%
- of closed trades
- Profit factor
- —
- gross profit ÷ gross loss
- Max drawdown
- 0.00%
- $0.00
One run with the author's default settings, $10,000 starting capital, commission as the script sets it. A single backtest says little about future results and is not a recommendation. Backtests are simulations on historical data and are not investment advice.
Review
What a careful reader of this script would point out, found by checking the source and the backtest.
-
Check
No stop loss
Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.
-
Note
1 setting has no label
In the settings dialog smoothing (line 23) shows an empty name. The labels on this page come from the variable names.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
Settings
Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| default invest ratioThe default fixed investment ratio, the strategy will calculate the position ratio of a single fixed investment based on this ratio and a linear function. The default 0.025 represents 2.5% of the position | 0.025 | float | orderPercent |
| min position size | 0.001 | float · min 0 | resultQty |
| buy slopethe slope of the linear function of the order to buy, used to control the position ratio of a single buy | -35 | float · max 0 | result |
| sell slopethe slope of the linear function of the order to sell, used to control the position ratio of a single sell | -0.7 | float · max 0 | result |
| sell offsetThe offset of the order to sell. If it is greater than 0, it will keep a small buy within a certain range to avoid starting to sell too early | 0.6 | float | result |
| max sell rateControls the maximum sell multiple. The maximum ratio of a single sell position does not exceed defInvestRatio * maxSellRate | 5 | float · min 0 | result |
| max buy rateControls the maximum buy multiple. The maximum ratio of a single buy position does not exceed defInvestRatio * maxBuyRate | 6 | float · min 0 | result |
| MA Period | 1100 | int | movingAverage |
| Smoothing (no label in the script) | SMA | string · options ["EMA", "SMA"] | movingAverage line 34 line 37 |
| Backtest Date Range | |||
| Filter Date Range of Backtest | true | bool | inTradeWindow |
| settle account on end | false | bool | cancel_all close_all line 100 |
| startDate | timestamp("2013-11-30T00:00+00:00") | time | inTradeWindow |
| endDate | timestamp("2022-09-16T00:00+00:00") | time | inTradeWindow |
| invest day of week1 means Sunday, 2 means Monday, 7 means Saturday | 2 | int · min 1 · max 7 | line 32 |
| interval days | 14 | int | line 32 |
What it calculates
No technical-analysis calculation: the script works on prices and settings directly.
Signal flow
How price data and settings reach the orders. Each box is a name in the script, or several computed the same way; each arrow is a value one of them reads. Hover a box for its members.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| order Buy long | orderQty > 0 | On | 93 |
| Close Buy | closeOrderQty > 0 | On | 98 |
| cancel_all | settleOnEnd and not inTradeWindow and inTradeWindow[1] | On | 101 |
| Close | settleOnEnd and not inTradeWindow and inTradeWindow[1] | On | 102 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Does it repaint?
No
No repainting patterns in the source.
- ClearHigher-timeframe dataThe script requests no other symbol or timeframe.
- ClearFuture-bar referencesNo negative history offsets such as close[-1].
- ClearIntrabar recalculationNo varip, calc_on_every_tick or realtime-only state. Orders are decided once per closed bar.
- ClearOrder timingOrders fill at the next bar's open, after the signal bar has closed.
The checks read the source; the replay runs it. Together they catch the common causes, not every possible one.
Other markets
The same script with default settings on 8 Binance markets. Each row is a separate backtest over the most recent 1,000 bars.
| Market | Net | Buy and hold | Trades | Won | Profit factor | Max drawdown | Period |
|---|---|---|---|---|---|---|---|
| BTCUSDT 1D | 0.0% | +102.5% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | 0.0% | +5.5% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | 0.0% | +9.8% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | 0.0% | +8.4% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | 0.0% | +16.7% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | 0.0% | +21.5% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | 0.0% | +148.3% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | 0.0% | +17.1% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
Similar scripts
No other indicator or strategy here shares enough of its calculations.
Upgrade and export
Written in Pine Script v5. The clean-v5 step types the settings and moves when = into if blocks, which v6 requires; it is run before and after like any upgrade.
- 1 setting uses the untyped input() (line 22). It still compiles in v6; input.int, input.float or input.source state the type.
- No v6-only features. v6 is not needed to run this script.
Clean v5
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 22: input() → input.int()
⋯maxBuyRate = input.float(6, title = "max buy rate", minval = 0, tooltip = "Controls the maximum buy multiple. The maximum ratio of a single buy position does not exceed defInvestRatio * maxBuyRate")-maPeriod = input(1100, "MA Period")+maPeriod = input.int(1100, "MA Period")smoothing = input.string(defval="SMA", options=["EMA", "SMA"])⋯Does it run in Pine Script v6?
Not tested
Neither version placed a trade on any of the 8 markets, so there was nothing to compare. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © Stephan-Gao-//@version=5+//@version=6strategy("Linear EDCA v1.2.3", overlay=true, default_qty_type = strategy.percent_of_equity, ⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 43 ms | With a workaround 1,000 bars in 109 ms | Not measured |
| PineTS | Not tested | Not tested | Not measured |
The engine's host API cannot yet declare a chart's symbol and timeframe, so this page sets them on the engine directly for each market.
“Matches TradingView” needs this script's own output exported from TradingView and compared bar by bar, which has not been done for this script. The engine is built on OpenPineScript, whose conformance results are in its conformance report.
Source
Copied verbatim from the author's published script, with its licence header intact. The author may have updated the original since this snapshot.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// // © Stephan-Gao//@version=5strategy("Linear EDCA v1.2.3", overlay=true, default_qty_type = strategy.percent_of_equity, default_qty_value = 1, initial_capital = 10000, currency = currency.USD, commission_type = strategy.commission.percent, commission_value = 0.2)strategy.risk.allow_entry_in(strategy.direction.long)// 单位定投比率defInvestRatio = input.float(0.025, title = "default invest ratio", tooltip = "The default fixed investment ratio, the strategy will calculate the position ratio of a single fixed investment based on this ratio and a linear function. The default 0.025 represents 2.5% of the position")// 最小单笔买卖头寸数量minPositionSize = input.float(0.001, title = "min position size", minval = 0)// 定买的线性函数斜率buySlope = input.float(-35, title = "buy slope", maxval = 0, tooltip = "the slope of the linear function of the order to buy, used to control the position ratio of a single buy")// 定卖的线性函数斜率sellSlope = input.float(-0.7, title = "sell slope", maxval = 0, tooltip = "the slope of the linear function of the order to sell, used to control the position ratio of a single sell")// 定卖的偏移度(如果是正数,则在均线以上一段距离还保持买入;如果是负数,则在偏离度为0时即开始卖出。一般对于牛市较长的市场,一般偏移为正数)sellOffset = input.float(0.6, title = "sell offset", tooltip = "The offset of the order to sell. If it is greater than 0, it will keep a small buy within a certain range to avoid starting to sell too early")// 最大卖出倍率(defInvestRatio * maxSellRate = 最大单次卖出仓位)maxSellRate = input.float(5, title = "max sell rate", minval = 0, tooltip = "Controls the maximum sell multiple. The maximum ratio of a single sell position does not exceed defInvestRatio * maxSellRate")// 最大买入倍率(defInvestRatio * maxBuyRate = 最大单次买入仓位)maxBuyRate = input.float(6, title = "max buy rate", minval = 0, tooltip = "Controls the maximum buy multiple. The maximum ratio of a single buy position does not exceed defInvestRatio * maxBuyRate")maPeriod = input(1100, "MA Period")smoothing = input.string(defval="SMA", options=["EMA", "SMA"])useDateFilter = input.bool(true, title="Filter Date Range of Backtest", group = "Backtest Date Range")settleOnEnd = input.bool(false, title="settle account on end", group = "Backtest Date Range")startDate = input.time(title="startDate", defval=timestamp("2013-11-30T00:00+00:00"), group = "Backtest Date Range")endDate = input.time(title="endDate", defval=timestamp("2022-09-16T00:00+00:00"), group = "Backtest Date Range")investDayofweek = input.int(2, title = "invest day of week", tooltip = "1 means Sunday, 2 means Monday, 7 means Saturday", minval = 1, maxval = 7, group = "Backtest Date Range")intervalDays = input.int(14, title="interval days", group = "Backtest Date Range")inTradeWindow = not useDateFilter or (time >= startDate and time < endDate)var int investBarIndex = 0isInvestDay() => timeframe.period == "D" and dayofweek == investDayofweek and bar_index >= (investBarIndex+intervalDays) and inTradeWindowma(smoothing, src, length) => if smoothing == "EMA" ta.ema(src, length) else if smoothing == "SMA" ta.sma(src, length)calInvestRate(deviation) => float result = 0 if deviation >= 0 result := sellSlope * deviation + sellOffset result := math.abs(result) > maxSellRate ? -maxSellRate : result else result := buySlope * deviation + 1 result := math.abs(result) > maxBuyRate ? maxBuyRate : result result// 当前仓位净值positionValue() => strategy.position_size * close// 当前可动用的本金capitalValue() => strategy.equity - positionValue() > 0 ? strategy.equity - positionValue() : 0// 目标单笔投资的数量(order qty)orderQty(orderPercent) => float resultQty = 0 targetOrderValue = strategy.equity * math.abs(orderPercent) if capitalValue() <= 0 resultQty := 0 else if capitalValue() > 0 and capitalValue() > targetOrderValue resultQty := targetOrderValue / close else if capitalValue() <= targetOrderValue resultQty := capitalValue() / close resultQty := math.round_to_mintick(resultQty) if resultQty > 0 resultQty := resultQty < minPositionSize ? 0 : resultQtycloseOrderQty(orderPercent) => float resultQty = 0 targetCloseValue = strategy.equity * math.abs(orderPercent) if positionValue() <= 0 resultQty := 0 else if positionValue() > 0 and positionValue() > targetCloseValue resultQty := targetCloseValue / close else if positionValue() <= targetCloseValue resultQty := positionValue() / close resultQty := math.round_to_mintick(resultQty) if resultQty > 0 resultQty := resultQty < minPositionSize ? 0 : resultQty//// Main ////movingAverage = ma(smoothing, close, maPeriod)plot(movingAverage, "MA", linewidth = 2, color = color.new(color.black, 0), display = display.all)deviation = (close - movingAverage) / movingAverageinvestRate = calInvestRate(deviation)orderPercent = defInvestRatio * investRateorderQty = orderQty(orderPercent)plot(orderQty, "orderQty", display = display.data_window)closeOrderQty = closeOrderQty(orderPercent)plot(closeOrderQty, "closeOrderQty", display = display.data_window)if isInvestDay() if investRate > 0 if orderQty > 0 orderPer = orderQty * close / strategy.equity strategy.order("Buy", strategy.long, qty = orderQty, comment = "Buy " + str.tostring(orderPer*100, "0.00") + "%") investBarIndex := bar_index else if closeOrderQty > 0 closeOrderPer = closeOrderQty * close / strategy.equity strategy.close("Buy", qty = closeOrderQty, comment = "Sell " + str.tostring(closeOrderPer*100, "0.00") + "%") investBarIndex := bar_indexif settleOnEnd and not inTradeWindow and inTradeWindow[1] strategy.cancel_all() strategy.close_all(comment="Date Range Exit")// for debugror = 100 * (close - strategy.position_avg_price)/strategy.position_avg_pricevar float maxLoss = 0if ror < 0 and maxLoss > ror maxLoss := rorvar float maxGain = 0if ror > 0 and maxGain < ror maxGain := rorplot(ror, "positionReturn%", color = color.new(color.red, 0), style = plot.style_columns, display = display.data_window)plot(maxLoss, "positionMaxFloatLoss%", color = color.new(color.red, 0), style = plot.style_columns, display = display.data_window + display.status_line)plot(maxGain, "positionMaxFloatGain%", color = color.new(color.green, 0), style = plot.style_columns, display = display.data_window + display.status_line)plot(strategy.max_drawdown, "max_drawdown", color = color.new(color.blue, 60), style = plot.style_histogram, display = display.all)plot(100 * deviation, "deviation%", display = display.data_window)plot(investRate, "investRate", display = display.data_window)plot(100*orderPercent, "orderPercent%", display = display.data_window)plot(100*positionValue()/strategy.equity, "positionPercent%", color = color.new(color.red, 0), display = display.data_window)plot(strategy.position_avg_price, "positionAvgPrice", linewidth = 1, color = color.new(color.green, 0), display = display.all)plot(strategy.position_size, "strategy.position_size", display = display.data_window)plot(strategy.equity, "strategy.equity", display = display.data_window)plot(strategy.openprofit, "strategy.openprofit", display = display.data_window)plot(strategy.netprofit, "strategy.netProfit", display = display.data_window)plot(positionValue(), "positionValue()", display = display.data_window)plot(capitalValue(), "capitalValue()", display = display.data_window)