0bps

Strategy vault / DCA & fixed investment

Dollar Cost Averaging Strategy with Take Profit

Pine Script strategy by Zer3192.

Author
Zer3192
Complexity
Starter · 48 lines
Parameters
4
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_1100Investment Amount
v_input_20Investment Period: W | M
v_input_30.2Stop loss percentage
v_input_40.1Take profit percentage

Source Pine Script

/*backtest
start: 2022-10-18 00:00:00
end: 2023-10-24 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/


//@version=4
strategy("Weekly Dollar Cost Averaging", overlay=true,pyramiding = 5)

// Define investment amount and investment period
investmentAmount = input(100, title="Investment Amount")
investmentPeriod = input("W", title="Investment Period", options=["W", "M"])
// Stop loss percentage
stopLossPercentage = input(0.2, title="Stop loss percentage")
// Take profit percentage
takeProfitPercentage = input(0.1, title="Take profit percentage")

// Calculate stop loss and take profit levels
stopLossLevel = strategy.position_avg_price * (1 - stopLossPercentage/100)
takeProfitLevel = strategy.position_avg_price * (1 + takeProfitPercentage/100)



// Define buy conditions
buyCondition = investmentPeriod == "W" ? change(strategy.position_size) >= 0 : change(strategy.position_size) <= 0

// Calculate buy quantity
buyQuantity = buyCondition ? investmentAmount / close : 0

// Execute buy operation
strategy.entry("Buy", strategy.long, qty=buyQuantity)
//Profit Taking Conditions
if (close>= takeProfitLevel) and (strategy.position_size > 0) 
     strategy.close_all()
else
if(close<=stopLossLevel) and (strategy.position_size<0)
     strategy.close_all()


// Print buy information
plotshape(buyCondition, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="Buy")

// Print holding information
plot(strategy.position_size > 0 ? strategy.position_size : na, title="Position Size", color=color.blue, style=plot.style_line, linewidth=2)

Published by Zer3192 on the FMZ strategy square · last modified 2023-10-25. 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/Dollar-Cost-Averaging-Strategy-with-Take-Profit.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