Zeal Terminal Open in Zeal Terminal

Pine Script v4 · Strategy

Close Trade at end of day

A Pine Script v4 strategy built on Valuewhen (secondCandle, high). It has 10 order rules, 2 entries among them, and trades long only with the default settings. Exits wait for a signal; there is no stop loss. Each trade is one contract and pays no commission. 8 settings; defaults include From Year 2020, From Month 6 and From Day 23.

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 Close Trade at end of day repaint?
No
No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
How did Close Trade at end of day 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.
Can Close Trade at end of day be converted to Pine Script v5?
Not tested
The steps are shown below, but neither version placed a trade on any market tested, so there was nothing to compare and no download is offered.
Does Close Trade at end of day use a stop loss?
No
Every exit waits for a signal; nothing limits a losing trade before it fires.
Does Close Trade at end of day run in Zeal Terminal?
Yes
It compiles in 18 ms and runs 1,000 bars in 51 ms, in the browser.

BTCUSDT · 1D · default settings

  • Valuewhen
  • Valuewhen
  • Winning trade
  • Losing trade
  • Equity
  • Buy and hold
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (8)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$100,000$202,527Equity from $100,000
Binance BTCUSDT 1D candles, 26 Jan 2024 to 21 Oct 2026, 1,000 bars, default settings. Triangles mark entries; circles mark exits. The script plots nothing itself; the lines and the lower pane are its own calculations, drawn for reference. Zeal Terminal's engine currently reads an order side written as long = true as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 41, 49) so the engine trades the way the script says.
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, $100,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.

  • Check

    No commission

    The backtest assumes free trading. Results on any real exchange will be lower.

  • Note

    1 setting has no label

    In the settings dialog open_session (line 25) 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.

  • Note

    Uses an argument v6 removed

    when = on line 41, 49. The upgraded version below moves each call inside an if block.

  • Note

    Uses long =, which v5 renamed line 41

    long = on line 41, 49. v5 writes it as direction = strategy.long or strategy.short.

Upgrade and export

Written in Pine Script v4. The upgrade to v5 takes 1 step, one per version, each applied to the result of the one before. A last clean-v5 pass then types the settings and moves when = into if blocks, which v6 requires. Every step runs before and after on the same candles in Zeal Terminal's engine; a step whose results differ is shown but not offered as a download. Upgrades preserve behaviour: bugs are fixed in the Review, never silently here.

  • Upgrading to v5 renames valuewhen → ta.valuewhen, crossover → ta.crossover, crossunder → ta.crossunder.
  • 2 strategy calls use the when = argument (line 41, 49). It is deprecated in v5 and removed in v6, where each call moves inside an if block.

v4 → v5

Not verified

Neither version placed a trade on any of the 8 markets, so there was nothing to compare.

  • Line 3: //@version=4 → //@version=5
  • Line 25: input(type = input.session) → input.session()
  • Line 30: valuewhen → ta.valuewhen
  • Line 31: valuewhen → ta.valuewhen
  • Line 38: crossover → ta.crossover
  • Line 39: crossover → ta.crossover
6 more edits
  • Line 40: crossunder → ta.crossunder
  • Line 41: long = true → direction = strategy.long
  • Line 46: crossunder → ta.crossunder
  • Line 47: crossunder → ta.crossunder
  • Line 48: crossover → ta.crossover
  • Line 49: long = true → direction = strategy.long
⋯// © daytraderph-//@version=4+//@version=5strategy("Close Trade at end of day ", overlay=true,process_orders_on_close=true)⋯    secondCandle :=true -open_session=input(type=input.session,defval="0915-1530")+open_session=input.session(defval="0915-1530")session = time("1", open_session)⋯float l1=na-h1:= valuewhen(secondCandle, high, 0)+h1:= ta.valuewhen(secondCandle, high, 0)-l1:= valuewhen(secondCandle, low, 0)+l1:= ta.valuewhen(secondCandle, low, 0)longEntryPrice = h1 +2⋯shortStopPrice = h1 +10000-longCondition = crossover ( high,longEntryPrice ) and f_tradeDateIsAllowed()+longCondition = ta.crossover ( high,longEntryPrice ) and f_tradeDateIsAllowed()-longTargetHit = crossover (high,longTargetPrice ) and f_tradeDateIsAllowed()+longTargetHit = ta.crossover (high,longTargetPrice ) and f_tradeDateIsAllowed()-longStopHit =  crossunder ( low,longStopPrice ) and f_tradeDateIsAllowed()+longStopHit =  ta.crossunder ( low,longStopPrice ) and f_tradeDateIsAllowed()-strategy.entry( "BNFL", long=true, when=longCondition)+strategy.entry( "BNFL", direction = strategy.long, when=longCondition)if longTargetHit ⋯    strategy.close("BNFL", comment="LSH")-shortCondition =   crossunder ( low,shortEntryPrice )   and f_tradeDateIsAllowed()+shortCondition =   ta.crossunder ( low,shortEntryPrice )   and f_tradeDateIsAllowed()-shortTargetHit = crossunder (low,shortTargetPrice ) and f_tradeDateIsAllowed()+shortTargetHit = ta.crossunder (low,shortTargetPrice ) and f_tradeDateIsAllowed()-shortStopHit = crossover ( high,shortStopPrice ) and f_tradeDateIsAllowed()+shortStopHit = ta.crossover ( high,shortStopPrice ) and f_tradeDateIsAllowed()-strategy.entry( "BNFS", long=true, when=shortCondition)+strategy.entry( "BNFS", direction = strategy.long, when=shortCondition)if shortTargetHit ⋯

Clean v5

Verified

Same 237 trades and the same P&L on BTCUSDT 4h, 1,000 bars, in Zeal Terminal's engine.

  • Line 5: input() → input.bool()
  • Line 6: input() → input.int()
  • Line 7: input() → input.int()
  • Line 8: input() → input.int()
  • Line 9: input() → input.int()
  • Line 10: input() → input.int()
3 more edits
  • Line 11: input() → input.int()
  • Line 41: when = longCondition → if block
  • Line 49: when = shortCondition → if block
⋯strategy("Close Trade at end of day ", overlay=true,process_orders_on_close=true)-i_dateFilter    = input(false,  "═════ Date Range Filtering ═════")+i_dateFilter    = input.bool(false,  "═════ Date Range Filtering ═════")-i_fromYear      = input(2020,   "From Year",    minval = 1900)+i_fromYear      = input.int(2020,   "From Year",    minval = 1900)-i_fromMonth     = input(6,      "From Month",   minval = 1, maxval = 12)+i_fromMonth     = input.int(6,      "From Month",   minval = 1, maxval = 12)-i_fromDay       = input(23,      "From Day",     minval = 1, maxval = 31)+i_fromDay       = input.int(23,      "From Day",     minval = 1, maxval = 31)-i_toYear        = input(2999,   "To Year",      minval = 1900)+i_toYear        = input.int(2999,   "To Year",      minval = 1900)-i_toMonth       = input(1,      "To Month",     minval = 1, maxval = 12)+i_toMonth       = input.int(1,      "To Month",     minval = 1, maxval = 12)-i_toDay         = input(1,      "To Day",       minval = 1, maxval = 31)+i_toDay         = input.int(1,      "To Day",       minval = 1, maxval = 31)fromDate        = timestamp(i_fromYear, i_fromMonth, i_fromDay, 00, 00)⋯longStopHit =  ta.crossunder ( low,longStopPrice ) and f_tradeDateIsAllowed()-strategy.entry( "BNFL", direction = strategy.long, when=longCondition)+if longCondition+    strategy.entry( "BNFL", direction = strategy.long)if longTargetHit ⋯shortStopHit = ta.crossover ( high,shortStopPrice ) and f_tradeDateIsAllowed()-strategy.entry( "BNFS", direction = strategy.long, when=shortCondition)+if shortCondition+    strategy.entry( "BNFS", direction = strategy.long)if shortTargetHit ⋯

Does it run in Pine Script v6?

No

The v6 version runs in Zeal Terminal's engine, but its results differ from the v5 version: Trades differ: 237 before, 237 after on BTCUSDT 4h, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.

⋯// © daytraderph-//@version=5+//@version=6strategy("Close Trade at end of day ", overlay=true,process_orders_on_close=true)⋯

Settings

Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.

SettingDefaultType and rangeFeeds
═════ Date Range Filtering ═════falseboolline 14
From Year2020int · min 1900fromDate
From Month6int · min 1 · max 12fromDate
From Day23int · min 1 · max 31fromDate
To Year2999int · min 1900toDate
To Month1int · min 1 · max 12toDate
To Day1int · min 1 · max 31toDate
Open session (no label in the script)0915-1530sessionsession

What it calculates

  • Valuewhen (valuewhen, ta.valuewhen in v5)secondCandle, high, 0h1 · line 30
  • Valuewhen (valuewhen, ta.valuewhen in v5)secondCandle, low, 0l1 · line 31

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.

highhighprice datalowlowprice datai_fromYearFrom Yearsetting · 2020i_fromMonthFrom Monthsetting · 6i_fromDayFrom Daysetting · 23i_toYearTo Yearsetting · 2999i_toMonthTo Monthsetting · 1i_toDayTo Daysetting · 1open_sessionOpen sessionsetting · 0915-1530timetimeprice datah1Valuewhen→ h1l1Valuewhen→ l1fromDatefromDatevaluetoDatetoDatevaluenumBarsnumBarsvaluesessionsessionvaluevalidSessionvalidSessionvaluelongEntryPrice, shortStopPricelongEntryPrice · shortS…2 valueslongStopPrice, shortEntryPricelongStopPrice · shortEn…2 valueslongTargetPricelongTargetPricevalueshortTargetPriceshortTargetPricevaluebarOpenTime, ttbarOpenTime · tt2 valueslongConditionlongConditionconditionlongTargetHitlongTargetHitconditionlongStopHitlongStopHitconditionshortConditionshortConditionconditionshortTargetHitshortTargetHitconditionshortStopHitshortStopHitconditionentry:BNFLOpen BNFLstrategy.entryclose:BNFLClose BNFLstrategy.closeentry:BNFSOpen BNFSstrategy.entryclose:BNFSClose BNFSstrategy.close

Order rules

OrderFires whenBy defaultLine
Open BNFL longlongConditioncrossover ( high,longEntryPrice ) and f_tradeDateIsAllowed()On41
Close BNFLlongTargetHitOn43
Close BNFLlongStopHitOn45
Open BNFS longshortConditioncrossunder ( low,shortEntryPrice ) and f_tradeDateIsAllowed()On49
Close BNFSshortTargetHitOn51
Close BNFSshortStopHitOn53
Close BNFS( hour(time) == 15 and minute(time) > 25 )On60
Close BNFL( hour(time) == 15 and minute(time) > 25 )On62
Close BNFLvalidSession ==0On64
Close BNFSvalidSession ==0On65

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.

Not measuredNo trades to compareThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, but neither the live nor the historical run produced any trades in them, so the replay says nothing either way.
  • 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 timingprocess_orders_on_close = true: orders fill at the signal bar's close.

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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D0.0%+102.5%00%—0.0%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h0.0%+5.5%00%—0.0%8 May 2026 – 21 Oct 2026
ETHUSDT 1D0.0%+9.8%00%—0.0%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h0.0%+8.4%00%—0.0%8 May 2026 – 21 Oct 2026
SOLUSDT 1D0.0%+16.7%00%—0.0%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h0.0%+21.5%00%—0.0%8 May 2026 – 21 Oct 2026
BNBUSDT 1D0.0%+148.3%00%—0.0%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h0.0%+17.1%00%—0.0%8 May 2026 – 21 Oct 2026

Backtests are simulations on historical data and are not investment advice.

Similar scripts

Indicators and strategies built from the same calculations, run on BTCUSDT 1D with their own defaults so they can be compared directly.

ScriptSharesTypeNetTradesMax drawdown
Close Trade at end of day this page—v4 strategy0.0%00.0%
[LunaOwl] Support Resistance Strategy V4 by LunaOwlvaluewhenv4 strategy−1.3%725.5%
1St Bar GAP+BkOut Screener v2 by RM by raul3429valuewhenv4 strategyNo trades on this market
Pivot Points Standard by Zeal Terminalvaluewhenv5 indicatorindicator
Reinforced RSI - The Quant Science by thequantsciencevaluewhenv5 strategy−3.9%73.9%
MACD 50x Leveraged Short Strategy with Real Equity by Noldovaluewhenv4 strategy+15,109.2%392,974.0%

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 18 msWith a workaround 1,000 bars in 51 msNot measured
PineTSNo v5 and v6 only—Not measured

Zeal Terminal's engine currently reads an order side written as long = true as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 41, 49) so the engine trades the way the script says.

“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. PineTS runs Pine Script v5 and v6 only.

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/// © daytraderph//@version=4strategy("Close Trade at end of day ", overlay=true,process_orders_on_close=true)i_dateFilter    = input(false,  "═════ Date Range Filtering ═════")i_fromYear      = input(2020,   "From Year",    minval = 1900)i_fromMonth     = input(6,      "From Month",   minval = 1, maxval = 12)i_fromDay       = input(23,      "From Day",     minval = 1, maxval = 31)i_toYear        = input(2999,   "To Year",      minval = 1900)i_toMonth       = input(1,      "To Month",     minval = 1, maxval = 12)i_toDay         = input(1,      "To Day",       minval = 1, maxval = 31)fromDate        = timestamp(i_fromYear, i_fromMonth, i_fromDay, 00, 00)toDate          = timestamp(i_toYear, i_toMonth, i_toDay, 23, 59)f_tradeDateIsAllowed() => not i_dateFilter or (time >= fromDate and time <= toDate)numBars = 1t = time('D')if t == t[1]    numBars := nz(numBars[1]) + 1else    numBars := 1bool secondCandle= false if numBars == 1    secondCandle :=true open_session=input(type=input.session,defval="0915-1530")session = time("1", open_session)validSession=(na(session) ? 0 : 1)float h1=nafloat l1=nah1:= valuewhen(secondCandle, high, 0)l1:= valuewhen(secondCandle, low, 0)longEntryPrice = h1 +2longStopPrice = l1 -10000longTargetPrice = longEntryPrice + 10000shortEntryPrice= l1 -2 shortTargetPrice = shortEntryPrice - 10000shortStopPrice = h1 +10000longCondition = crossover ( high,longEntryPrice ) and f_tradeDateIsAllowed()longTargetHit = crossover (high,longTargetPrice ) and f_tradeDateIsAllowed()longStopHit =  crossunder ( low,longStopPrice ) and f_tradeDateIsAllowed()strategy.entry( "BNFL", long=true, when=longCondition)if longTargetHit     strategy.close("BNFL", comment="LTH")if longStopHit     strategy.close("BNFL", comment="LSH")shortCondition =   crossunder ( low,shortEntryPrice )   and f_tradeDateIsAllowed()shortTargetHit = crossunder (low,shortTargetPrice ) and f_tradeDateIsAllowed()shortStopHit = crossover ( high,shortStopPrice ) and f_tradeDateIsAllowed()strategy.entry( "BNFS", long=true, when=shortCondition)if shortTargetHit     strategy.close("BNFS", comment="STH")if shortStopHit     strategy.close("BNFS", comment="SSH")        barOpenTime = time tt=timestamp(year(time), month(time), dayofmonth(time), hour(time), 2)    if ( hour(time) == 15 and minute(time) > 25  )      strategy.close("BNFS", comment="SCM")if ( hour(time) == 15 and minute(time) > 25 )       strategy.close("BNFL", comment="SCM")if validSession ==0     strategy.close("BNFL", comment="SC")    strategy.close("BNFS", comment="SC")