Pine Script v5 · Strategy
Loft Strategy V4
A Pine Script v5 strategy with no technical-analysis calculation. It has 20 order rules, 4 entries among them, and trades long and short with the default settings. Exits use a stop loss. Each trade commits 100 in cash and pays 0.05% commission. 28 settings; defaults include Kalman Gain: 100.0, Source: close and Beginning Approach(%) 5.0.
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 Loft Strategy V4 repaint?
- No
- No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
- How did Loft Strategy V4 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 Loft Strategy V4 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 Loft Strategy V4 use a stop loss?
- Yes
- strategy.exit sets a stop on line 280, 282, 299, 301, 304, 306.
- Does Loft Strategy V4 run in Zeal Terminal?
- Yes
- It compiles in 99 ms and runs 1,000 bars in 104 ms, in the browser.
BTCUSDT · 1D · default settings
- Kalman Filter
- Stop Loss Line
- 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.
-
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 219, 220, 225, 226, 230, 231, 239, 240. The upgraded version below moves each call inside an if block.
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 |
|---|---|---|---|
| Kalman Gain: | 100.0 | float · min 1 · max 10000.0 · step 1 | velo smooth |
| Source: | close | source | dk smooth |
| Beginning Approach(%) | 5.0 | float · min 0.1 · max 30.0 · step 0.1 | stopPercentMax stopPercent |
| Final Approach(%) | 1.0 | float · min 0.1 · max 30.0 · step 0.1 | stopPercent line 102 line 117 |
| Approach Decrease Step | 0.001 | float · min 0.0 · max 5 · step 0.001 | stopPercent |
| Base Order Quantity | 100.0 | float · min 0.001 | orderQty maxOrderSize line 216 |
| Max Safe Order Attemp | 4 | int · min 1 | maxOrderSize |
| Safe Order Deviation | 3 | float · min 1.0 · step 0.1 | orderQty maxOrderSize |
| Profit Deviation | 1.0 | float · min 1.0 · max 10 · step 0.1 | tp1 tp2 tp3 |
| Max Take Profit(%) | 25.0 | float · max 100 · step 0.1 | tp1 tp2 tp3 |
| Max Order Quantity | 10000.0 | float · min 0.01 | takeQty1 takeQty2 takeQty3 line 208 line 211 line 214 entry LONG entry SHORT |
| TP1(%) | 1.0 | float · min 0.0 · max 100.0 · step 0.1 | tp1 line 130 line 141 |
| QT1(%): | 40 | int · min 1 · max 100 · step 5 | takeQty1 |
| TP2(%) | 3.0 | float · min 0.0 · max 100.0 · step 0.1 | tp2 |
| QT2(%): | 30 | int · min 1 · max 100 · step 5 | takeQty2 |
| TP3(%) | 5.0 | float · min 0.0 · max 100.0 · step 0.1 | tp3 |
| QT3(%): | 30 | int · min 1 · max 100 · step 5 | takeQty3 |
| Stop Loss(%) | 0.0 | float · min 0.0 · max 100.0 · step 0.1 | stopLoss line 302 |
| Long Entry | true | bool | entry LONG close LONG line 129 line 156 line 206 line 209 line 217 line 229 |
| Short Entry | true | bool | entry SHORT close SHORT line 129 line 164 line 206 line 212 line 217 line 238 |
| Safe Stop After TP2 | true | bool | exit LONG exit SHORT line 279 line 298 |
| Safe Stop After TP1 | false | bool | exit LONG exit SHORT line 281 line 300 |
| From Date: | 1 | int · min 1 · max 31 | start |
| / | 1 | int · min 1 · max 12 | start |
| / | 2021 | int · min 2010 | start |
| To__ Date: | 30 | int · min 1 · max 31 | finish |
| / | 12 | int · min 1 · max 12 | finish |
| / | 2022 | int · min 2010 | finish |
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.
The script has 62 named inputs, calculations, conditions and outputs, too many to draw legibly here; the tables above list them.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Close | buySignallwindow() and long and (not stoppedOutLong) | On | 219 |
| Open LONG long | buySignallwindow() and long and (not stoppedOutLong) | On | 220 |
| Close | sellSignallwindow() and (not long) and (not stoppedOutShort) | On | 225 |
| Open SHORT short | sellSignallwindow() and (not long) and (not stoppedOutShort) | On | 226 |
| Open LONG long | buySignallwindow() and long and (not stoppedOutLong) | On | 230 |
| Close LONG | sellSignallwindow() and (not long) and (not stoppedOutShort) | On | 231 |
| Open SHORT short | sellSignallwindow() and (not long) and (not stoppedOutShort) | On | 239 |
| Close SHORT | buySignallwindow() and long and (not stoppedOutLong) | On | 240 |
| Close LONG | close > longProfitPrice1 and tp1 > 0 and isTakeTP1 == false | On | 268 |
| Close LONG | close > longProfitPrice2 and tp2 > 0 and isTakeTP2 == false | On | 272 |
| Close LONG | close > longProfitPrice3 and tp3 > 0 and isTakeTP3 == false | On | 276 |
| Exit LONG stop | isTakeTP2 == true and useSafeStop2 | On | 280 |
| Exit LONG stop | isTakeTP1 == true and useSafeStop1 | On | 282 |
| Close SHORT | close < shortProfitPrice1 and tp1 > 0 and isTakeTP1 == false | On | 287 |
| Close SHORT | close < shortProfitPrice2 and tp2 > 0 and isTakeTP2 == false | On | 291 |
| Close SHORT | close < shortProfitPrice3 and tp3 > 0 and isTakeTP3 == false | On | 295 |
| Exit SHORT stop | isTakeTP2 == true and useSafeStop2 | On | 299 |
| Exit SHORT stop | isTakeTP1 == true and useSafeStop1 | On | 301 |
| Exit LONG stop | ( strategy.position_size > 0 ) | On | 304 |
| Exit SHORT stop | ( strategy.position_size < 0 ) | On | 306 |
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 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.
| 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.
- 8 strategy calls use the when = argument (line 219, 220, 225, 226, 230, 231, 239, 240). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
- 1 setting uses the untyped input() (line 20). 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 20: input() → input.source()
- Line 219: when = buySignall → if block
- Line 220: when = buySignall → if block
- Line 225: when = sellSignall → if block
- Line 226: when = sellSignall → if block
- Line 230: when = buySignall → if block
3 more edits
- Line 231: when = sellSignall → if block
- Line 239: when = sellSignall → if block
- Line 240: when = buySignall → if block
⋯gain = input.float(title="Kalman Gain:", defval=100.0, minval=1, maxval=10000.0, step=1)-src = input(defval=close, title='Source:')+src = input.source(defval=close, title='Source:')stopPercentBase = input.float(title='Beginning Approach(%)', defval=5.0, minval=0.1, maxval=30.0, step=0.1)⋯ if long- strategy.close_all( when = buySignall, comment = exitShortComment)+ if buySignall+ strategy.close_all( comment = exitShortComment)- strategy.entry("LONG", strategy.long, when = buySignall, qty=math.min(orderQty, maxOrderQty), comment = enterLongComment)+ if buySignall+ strategy.entry("LONG", strategy.long, qty=math.min(orderQty, maxOrderQty), comment = enterLongComment) stoppedOutLong := true⋯ else- strategy.close_all(when=sellSignall, comment = exitLongComment)+ if sellSignall+ strategy.close_all(comment = exitLongComment)- strategy.entry("SHORT", strategy.short, when = sellSignall, qty=math.min(orderQty, maxOrderQty), comment = enterShortComment)+ if sellSignall+ strategy.entry("SHORT", strategy.short, qty=math.min(orderQty, maxOrderQty), comment = enterShortComment) stoppedOutLong := false⋯else if(longEntry)- strategy.entry("LONG", strategy.long, when = buySignall, qty=math.min(orderQty, maxOrderQty), comment = enterLongComment)+ if buySignall+ strategy.entry("LONG", strategy.long, qty=math.min(orderQty, maxOrderQty), comment = enterLongComment)- strategy.close("LONG", when = sellSignall, comment = exitLongComment)+ if sellSignall+ strategy.close("LONG", comment = exitLongComment) if long ⋯else if(shortEntry)- strategy.entry("SHORT", strategy.short, when = sellSignall, qty=math.min(orderQty, maxOrderQty), comment = enterShortComment)+ if sellSignall+ strategy.entry("SHORT", strategy.short, qty=math.min(orderQty, maxOrderQty), comment = enterShortComment)- strategy.close("SHORT", when = buySignall, comment = exitShortComment)+ if buySignall+ strategy.close("SHORT", comment = exitShortComment) if not long⋯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.
⋯ // |___/ -//@version=5+//@version=6strategy(title='Loft Strategy V4', overlay=true, ⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 99 ms | With a workaround 1,000 bars in 104 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/// © BigCoinHunter // ____ _ _____ _ _ _ _ // | _ \(_) / ____| (_) | | | | | | // | |_) |_ __ _| | ___ _ _ __ | |__| |_ _ _ __ | |_ ___ _ __ // | _ <| |/ _` | | / _ \| | '_ \| __ | | | | '_ \| __/ _ \ '__| // | |_) | | (_| | |___| (_) | | | | | | | | |_| | | | | || __/ | // |____/|_|\__, |\_____\___/|_|_| |_|_| |_|\__,_|_| |_|\__\___|_| // __/ | // |___/ //@version=5strategy(title='Loft Strategy V4', overlay=true, pyramiding=0, default_qty_type=strategy.cash, default_qty_value=100, initial_capital=10000, currency=currency.USD, commission_value=0.05, commission_type=strategy.commission.percent, process_orders_on_close=true)//-------------- fetch user inputs ------------------gain = input.float(title="Kalman Gain:", defval=100.0, minval=1, maxval=10000.0, step=1)src = input(defval=close, title='Source:')stopPercentBase = input.float(title='Beginning Approach(%)', defval=5.0, minval=0.1, maxval=30.0, step=0.1)stopPercentMin = input.float(title='Final Approach(%)', defval=1.0, minval=0.1, maxval=30.0, step=0.1)downStep = input.float(title='Approach Decrease Step', defval=0.001, minval=0.0, maxval = 5, step=0.001)//stopPercentDeviation = input.float(title="Approach Deviation", defval=1.0, minval=0.1, maxval = 5.0, step=0.1)baseOrderQty = input.float(title="Base Order Quantity", defval=100.0, minval=0.001)maxOrderCount = input.int(title="Max Safe Order Attemp", defval=4, minval=1)priceDeviation = input.float(title="Safe Order Deviation", defval=3, minval=1.0, step=0.1)profitDeviation = input.float(title="Profit Deviation", defval=1.0, minval=1.0, maxval=10, step=0.1)maxTakeProfit = input.float(title="Max Take Profit(%)", defval=25.0, maxval=100, step=0.1)maxOrderQty = input.float(title="Max Order Quantity", defval=10000.0, minval=0.01)baseTP1 = input.float(title="TP1(%)", defval=1.0, minval=0.0, maxval=100.0, step=0.1, inline="0")qt1 = input.int(title="QT1(%):", defval=40, minval=1, maxval=100, step=5, inline="0")baseTP2 = input.float(title="TP2(%)", defval=3.0, minval=0.0, maxval=100.0, step=0.1, inline="1")qt2 = input.int(title="QT2(%):", defval=30, minval=1, maxval=100, step=5, inline="1")baseTP3 = input.float(title="TP3(%)", defval=5.0, minval=0.0, maxval=100.0, step=0.1, inline="2")qt3 = input.int(title="QT3(%):", defval=30, minval=1, maxval=100, step=5, inline="2")initialStopLoss = input.float(title="Stop Loss(%)", defval=0.0, minval=0.0, maxval=100.0, step=0.1)longEntry = input.bool(defval=true, title= 'Long Entry', inline="3")shortEntry = input.bool(defval=true, title='Short Entry', inline="3")useSafeStop2 = input.bool(defval = true, title="Safe Stop After TP2", inline="6")useSafeStop1 = input.bool(defval = false, title="Safe Stop After TP1", inline="6")//---------- backtest range setup ------------fromDay = input.int(defval = 1, title = "From Date:", minval = 1, maxval = 31, inline="4")fromMonth = input.int(defval = 1, title = "/", minval = 1, maxval = 12, inline="4")fromYear = input.int(defval = 2021, title = "/", minval = 2010, inline="4")toDay = input.int(defval = 30, title = "To__ Date:", minval = 1, maxval = 31, inline="5")toMonth = input.int(defval = 12, title = "/", minval = 1, maxval = 12, inline="5")toYear = input.int(defval = 2022, title = "/", minval = 2010, inline="5")//------------ time interval setup -----------start = timestamp(fromYear, fromMonth, fromDay, 00, 00) // backtest start windowfinish = timestamp(toYear, toMonth, toDay, 23, 59) // backtest finish windowwindow() => time >= start and time <= finish ? true : false // create function "within window of time"//------- define the order comments ------enterLongComment = ""exitLongComment = ""enterShortComment = ""exitShortComment = ""longTPSL = ""longTP = ""longSL = ""shortTPSL = ""shortTP = ""shortSL = ""//--------- Define global variables -----------var bool long = truevar bool stoppedOutLong = falsevar bool stoppedOutShort = falsevar float kf = 0.0var float velo = 0.0var float orderQty = baseOrderQtyvar float stopLoss = initialStopLossvar bool isProfit = falsevar int barindex = 1var int winCounter = 0var int winCounterBuffer = 0var int failCounter = 0var float tp1 = baseTP1var float tp2 = baseTP2var float tp3 = baseTP3var bool isTakeTP1 = falsevar bool isTakeTP2 = false var bool isTakeTP3 = false var bool isLastProfit = truevar float stopPercentMax = stopPercentBasevar float stopPercent = stopPercentBasevar float stopLine = 0.0var labelColor = color.blue//------ kalman filter calculation --------dk = src - nz(kf[1], src)smooth = nz(kf[1], src) + dk * math.sqrt(gain / 10000 * 2)velo := nz(velo[1], 0) + gain / 10000 * dkkf := smooth + velo//--------- calculate the loft stopLoss line ---------//stopPercentMax := isLastProfit ? stopPercentBase : (stopPercentBase * stopPercentDeviation)if long == true stopLine := kf - (kf * (stopPercent / 100)) if long[1] == true and stopLine <= stopLine[1] stopLine := stopLine[1] else if (long[1] == true) stopPercent := stopPercent - downStep if(stopPercent < stopPercentMin) stopPercent := stopPercentMin if(kf < stopLine) long := false stopPercent := stopPercentMax stopLine := kf + (kf * (stopPercent / 100)) else stopLine := kf + (kf * (stopPercent / 100)) if long[1] == false and stopLine >= stopLine[1] stopLine := stopLine[1] else if(long[1] == false) stopPercent := stopPercent - downStep if(stopPercent < stopPercentMin) stopPercent := stopPercentMin if(kf > stopLine) long := true stopPercent := stopPercentMax stopLine := kf - (kf * (stopPercent / 100))//------------------- determine buy and sell points ---------------------buySignall = window() and long and (not stoppedOutLong)sellSignall = window() and (not long) and (not stoppedOutShort) if longEntry and shortEntry if buySignall and baseTP1 <= 0.0 if strategy.position_size < 0 if close < strategy.position_avg_price isLastProfit := true else if strategy.position_size == 0 if strategy.wintrades > winCounter //strategy.wintrades[ barindex ] isLastProfit := true else isLastProfit := false else if sellSignall and baseTP1 <= 0.0 if strategy.position_size > 0 if close > strategy.position_avg_price isLastProfit := true else if strategy.position_size == 0 if strategy.wintrades > winCounter //strategy.wintrades[ barindex ] isLastProfit := true else isLastProfit := false else if isTakeTP2 == true isLastProfit := true else isLastProfit := falseelse if longEntry if sellSignall winCounterBuffer := winCounter if buySignall if winCounter > winCounterBuffer isLastProfit := true else isLastProfit := falseelse if shortEntry if buySignall winCounterBuffer := winCounter if sellSignall if winCounter > winCounterBuffer isLastProfit := true else isLastProfit := false //------------- set the deviations ------------var float maxOrderSize = (baseOrderQty * math.pow(priceDeviation, maxOrderCount - 1))if buySignall or sellSignall if isLastProfit == false orderQty := orderQty * priceDeviation tp1 := tp1 * profitDeviation tp2 := tp2 * profitDeviation tp3 := tp3 * profitDeviation tp1 := math.min(tp1, maxTakeProfit) tp2 := math.min(tp2, maxTakeProfit) tp3 := math.min(tp3, maxTakeProfit) if orderQty > maxOrderSize failCounter := failCounter + 1 orderQty := baseOrderQty tp1 := baseTP1 tp2 := baseTP2 tp3 := baseTP3 else orderQty := baseOrderQty tp1 := baseTP1 tp2 := baseTP2 tp3 := baseTP3// ----------------- put debug labels -------------------if orderQty == maxOrderSize labelColor := color.redelse labelColor := isLastProfit ? color.lime : color.yellowif longEntry and shortEntry if buySignall or sellSignall label.new( x=bar_index, y=high, text="Qty:"+str.tostring(math.min(orderQty, maxOrderQty))+" | Worst Case:"+str.tostring(failCounter) ,color = labelColor )else if longEntry if buySignall label.new( x=bar_index, y=high, text="Qty:"+str.tostring(math.min(orderQty, maxOrderQty))+" | Worst Case:"+str.tostring(failCounter) ,color = labelColor )else if shortEntry if sellSignall label.new( x=bar_index, y=high, text="Qty:"+str.tostring(math.min(orderQty, maxOrderQty))+" | Worst Case:"+str.tostring(failCounter) ,color = labelColor )//---------- execute the strategy -----------------nz(orderQty, baseOrderQty)if longEntry and shortEntry if long strategy.close_all( when = buySignall, comment = exitShortComment) strategy.entry("LONG", strategy.long, when = buySignall, qty=math.min(orderQty, maxOrderQty), comment = enterLongComment) stoppedOutLong := true stoppedOutShort := false else strategy.close_all(when=sellSignall, comment = exitLongComment) strategy.entry("SHORT", strategy.short, when = sellSignall, qty=math.min(orderQty, maxOrderQty), comment = enterShortComment) stoppedOutLong := false stoppedOutShort := trueelse if(longEntry) strategy.entry("LONG", strategy.long, when = buySignall, qty=math.min(orderQty, maxOrderQty), comment = enterLongComment) strategy.close("LONG", when = sellSignall, comment = exitLongComment) if long stoppedOutLong := true stoppedOutShort := false else stoppedOutLong := false stoppedOutShort := trueelse if(shortEntry) strategy.entry("SHORT", strategy.short, when = sellSignall, qty=math.min(orderQty, maxOrderQty), comment = enterShortComment) strategy.close("SHORT", when = buySignall, comment = exitShortComment) if not long stoppedOutShort := true stoppedOutLong := false else stoppedOutShort := false stoppedOutLong := true//--------- calculate the TP/SL entries -----------longProfitPrice1 = strategy.position_avg_price * (1 + tp1 * 0.01)longProfitPrice2 = strategy.position_avg_price * (1 + tp2 * 0.01)longProfitPrice3 = strategy.position_avg_price * (1 + tp3 * 0.01) shortProfitPrice1 = strategy.position_avg_price * (1 - tp1 * 0.01)shortProfitPrice2 = strategy.position_avg_price * (1 - tp2 * 0.01)shortProfitPrice3 = strategy.position_avg_price * (1 - tp3 * 0.01)longStopPrice = strategy.position_avg_price * (1 - stopLoss * 0.01)shortStopPrice = strategy.position_avg_price * (1 + stopLoss * 0.01)shortSafeStopPrice2 = strategy.position_avg_price * (1 - 0.2 * 0.01)longSafeStopPrice2 = strategy.position_avg_price * (1 + 0.2 * 0.01)longSafeStopPrice1 = stopLineshortSafeStopPrice1 = stopLine//----------- calculate TP quantity values -----------takeQty1 = math.min(orderQty, maxOrderQty) * qt1 / 100takeQty2 = math.min(orderQty, maxOrderQty) * qt2 / 100takeQty3 = math.min(orderQty, maxOrderQty) * qt3 / 100//----------------- take profit and stop loss processes -----------------if strategy.position_size > 0 if close > longProfitPrice1 and tp1 > 0 and isTakeTP1 == false strategy.close(id="LONG", qty=takeQty1, comment = "longTP 1") isTakeTP1 := true if close > longProfitPrice2 and tp2 > 0 and isTakeTP2 == false strategy.close(id="LONG", qty=takeQty2, comment = "longTP 2") isTakeTP2 := true if close > longProfitPrice3 and tp3 > 0 and isTakeTP3 == false strategy.close(id="LONG", qty=takeQty3, comment = "longTP 3") isTakeTP3 := true if isTakeTP2 == true and useSafeStop2 strategy.exit(id="LONG", stop=longSafeStopPrice2, comment = "Long Safe Stop2") if isTakeTP1 == true and useSafeStop1 strategy.exit(id="LONG", stop=longSafeStopPrice1, comment = "Long Safe Stop1") if strategy.position_size < 0 if close < shortProfitPrice1 and tp1 > 0 and isTakeTP1 == false strategy.close(id="SHORT", qty=takeQty1, comment = "Short TP 1") isTakeTP1 := true if close < shortProfitPrice2 and tp2 > 0 and isTakeTP2 == false strategy.close(id="SHORT", qty=takeQty2, comment = "Short TP 2") isTakeTP2 := true if close < shortProfitPrice3 and tp3 > 0 and isTakeTP3 == false strategy.close(id="SHORT", qty=takeQty3, comment = "Short TP 3") isTakeTP3 := true if isTakeTP2 == true and useSafeStop2 strategy.exit(id="SHORT", stop=shortSafeStopPrice2, comment = "Short Safe Stop2") if isTakeTP1 == true and useSafeStop1 strategy.exit(id="SHORT", stop=shortSafeStopPrice1, comment = "Short Safe Stop1")if(initialStopLoss>0.0) if ( strategy.position_size > 0 ) strategy.exit(id="LONG", stop=longStopPrice, comment = "Long Stop Loss") else if ( strategy.position_size < 0 ) strategy.exit(id="SHORT", stop=shortStopPrice, comment = "Short Stop Loss") if buySignall or sellSignall isTakeTP1 := false isTakeTP2 := false isTakeTP3 := false winCounter := strategy.wintrades //------------- plot charts ---------------------lineColor1 = long ? color.green : color.redlineColor2 = long ? color.aqua : color.fuchsiakalmanPlot = plot(kf, color=lineColor1, linewidth=3, title = "Kalman Filter")stopPlot = plot(stopLine, color=lineColor2, linewidth=2, title = "Stop Loss Line")