0bps

Strategy vault / Other

Top Bottom Divergence Indicator Observation System with Take Profit and Stop Loss

Pine Script strategy by Zer3192.

Pine ScriptOtherMACDEMA
Author
Zer3192
Complexity
Starter · 53 lines
Parameters
2
Backtest bar
1d · swing
Tested on
BTC/USDT · futures

The original spec has no written description. Read the source below.

Parameters

NameDefaultWhat it controls
v_input_1trueStop Loss Percentage
v_input_2trueTake profit percentage

Source Pine Script

/*backtest
start: 2022-03-01 00:00:00
end: 2023-02-28 23:59:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
args: [["v_input_1",99]]
*/

//@version=5
strategy("Top/Bottom Divergence Indicator Observation System with Take-Profit and Stop-Loss.", overlay=true)

// Enter stop-loss percentage and take-profit percentage; set here respectively.1%
stop_loss_pct = input(title="Stop Loss Percentage", type=input.float, defval=1.0, step=0.1)
take_profit_pct = input(title="Take profit percentage", type=input.float, defval=1.0, step=0.1)

// CalculationMACDIndicator
fastline = ta.ema(close,12)  
slowline = ta.ema(close,26)
diff = fastline - slowline 
dea = ta.ema(diff,9) 
macd = 2*(diff - dea)

// Determine the top divergence and bottom divergence signals
top_diver = ta.crossunder(macd,0) and close[1] > close
bot_diver = ta.crossover(macd,0) and close[1] < close
plotchar(top_diver, char='Top divergence', location = location.abovebar, size = size.normal, overlay=true)
plotchar(bot_diver, char='Bottom divergence', location = location.belowbar, size = size.normal, overlay=true)
// Determine whether to open a position
if top_diver and strategy.position_size>= 0
    strategy.entry("top_diver_short", strategy.short, comment="Top divergence open long")
else if bot_diver and strategy.position_size <=0
    strategy.entry("bot_diver_long", strategy.long, comment="Bottom divergence open short")
// Calculate take-profit and stop-loss prices
//Long Position Stop Loss
long_stop_price = strategy.position_avg_price * (1 - stop_loss_pct / 100)
//Long Position Take Profit
long_take_profit_price = strategy.position_avg_price * (1 + take_profit_pct / 100)
// Short Position Stop Loss
short_stop_price = (strategy.position_avg_price) * (1 + stop_loss_pct / 100)
// Short Position Take Profit
short_take_profit_price =(strategy.position_avg_price) * (1 - take_profit_pct / 100)
// Determine whether to close a position
if strategy.position_size > 0
    // Long Position Stop Loss
    strategy.exit("long_stop","bot_diver_long", stop=long_stop_price, qty_percent=100)
    // Long Position Take Profit
    strategy.exit("long_tp", "bot_diver_long", limit=long_take_profit_price, qty_percent=100)
else if strategy.position_size < 0
    strategy.exit("short_stop", "top_diver_short", stop=short_stop_price, qty_percent=100)
    strategy.exit("short_tp", "top_diver_short", limit=short_take_profit_price, qty_percent=100)

Published by Zer3192 on the FMZ strategy square · last modified 2023-03-02. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.

Build this with AgenKit

A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Top-Bottom-Divergence-Indicator-Observation-System-with-Take-Profit-and-Stop-Loss.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. Get tests, backtest, risk controls and a paper-trading switch

Understand it first

Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.

Related specs

esc

Try , , or .

↑↓ move↵ open