Strategy · Pine Script v4 · On the chart

Bigbaldy RSI for DCA

by bigbaldy1128· code credited to bigbaldy1129

This strategy simulates Dollar Cost Averaging (DCA) trading bot behavior on the chart.

View on GitHub

GitHub stars4on the repository
Pine versionv4older, still runs
Lines of code94
Settings16inputs
LicenceNone statedfile: MPL-2.0
Last update5 years ago23 Oct 2021

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

94 lines of Pine Script v4 from RSI for DCA.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

RSI for DCA.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © bigbaldy1129

//@version=4
strategy("Bigbaldy RSI for DCA", overlay=true)

startTime = input(timestamp("08 01 2021 00:00 +0800"),type=input.time)
endTime = input(timestamp("01 01 2100 00:00 +0800"),type=input.time)

isDate = time >= startTime and time <= endTime                    
    
showDate   = input(true, title = "Show Date Range", type = input.bool)
showTimeE  = input(true, title = "Show Time Entry", type = input.bool)
showTimeX  = input(true, title = "Show Time Exit", type = input.bool)

length = input( 7 )
overSold = input( 50 )
deviation  = input(1.2)
deviationScale = input(1.0)
fillMaxCount = input(6)
volumeScale = input(1.3)
targetProfit = input(1.2)
maxPrice = input(20.0)
showRange = input(false, title = "Show Range", type = input.bool)
openNewTradeAsap = input(false)
lableHeight = input(10,title= "Lable Height",type = input.integer)

f_print(_text) =>
    var _label = label.new(bar_index, na, _text, xloc.bar_index, yloc.price, color(na), label.style_circle, color.green, size.large, text.align_left)
    label.set_xy(_label, bar_index, highest(10)[0] + lableHeight)
    label.set_text(_label, _text)

price = close
vrsi = rsi(price, length)
buy = false
if vrsi < overSold and close < maxPrice
    buy := true
if openNewTradeAsap and close < maxPrice
    buy := true
var sell = false
var buyPrice = price
var totalPrice = price
var meanPrice = price
var buyCount = 0
var totalVolume = 1.0
var add = false
var _targetProfit = 1 + targetProfit / 100.0
var profit = price - price
var maxVolume = price - price
if not maxVolume
    for i = 1 to fillMaxCount
        maxVolume := maxVolume + pow(volumeScale, i - 1)

if buy and not buyCount and isDate
    buyPrice := price
    totalPrice := price
    meanPrice := price
    buyCount := buyCount + 1
    buy := true
else
    buy := false

currentBuyPrice = price
if deviationScale == 1
    currentBuyPrice := buyPrice * (1 - deviation * buyCount / 100.0)
else
    currentBuyPrice := buyPrice * (1 - deviation * (1 - pow(deviationScale, buyCount))/(1 - deviationScale) / 100.0)
    
if buyCount and buyCount <= fillMaxCount and close < currentBuyPrice
    buyCount := buyCount + 1
    volumeDelta = pow(volumeScale, buyCount - 1)
    totalVolume := totalVolume + volumeDelta
    totalPrice := totalPrice + currentBuyPrice * volumeDelta
    meanPrice := totalPrice / totalVolume
    label.new(bar_index, low, text="F" + tostring(buyCount - 1), style=label.style_label_up, color=color.green, textcolor=color.white, size=size.tiny)

if buyCount and close / meanPrice >= _targetProfit
    profit := profit + totalVolume * (close / meanPrice - 1.00075) / maxVolume
    sell := true
    buyCount := 0
    totalVolume := 1
    meanPrice := 0
else
    sell := false
if (not na(vrsi))
    strategy.entry("buy", true, when = isDate and buy)
    strategy.close("buy", when = isDate and sell, comment='sell')
    
f_print("mean_price: " + tostring(meanPrice) + "\n" + "buy_count: " + tostring(buyCount)+"\n"+"profit: "+tostring(profit))

transp = showRange ? 95: 100
bgcolor(color = showDate and isDate ? color.gray : na,transp = transp)
bgcolor(color = showTimeE and isDate ? color.lime : na,transp = transp)
bgcolor(color = showTimeX and isDate ? color.purple : na,transp = transp)

Overview

Bigbaldy RSI for DCA triggers an initial entry when the RSI drops below a specified oversold limit and price is under a maximum price cap. As price falls, it calculates simulated safety order scale-ins with custom step deviations and volume multipliers to determine an average entry price. The strategy exits when price reaches a percentage profit target relative to the calculated mean price. It draws safety fill labels and a summary text display on the chart, but contains no stop loss.

Details

Published by

bigbaldy1128 on GitHub

Code credit

bigbaldy1129, as written in the script's header

Type

Strategy, Pine Script v4

Draws

On the price chart

Category

Oscillators & momentum, Mean reversion

Markets

Crypto

Timeframes

M15-H1

Risk Notes

  • The strategy enters trades without a stop loss

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
startTimetimetimestamp("08 01 2021 00:00 +0800")startTime
endTimetimetimestamp("01 01 2100 00:00 +0800")endTime
Show Date RangebooltrueshowDate
Show Time EntrybooltrueshowTimeE
Show Time ExitbooltrueshowTimeX
lengthint7length
overSoldint50overSold
deviationfloat1.2deviation
deviationScalefloat1.0deviationScale
fillMaxCountint6fillMaxCount
volumeScalefloat1.3volumeScale
targetProfitfloat1.2targetProfit
Show all 16 settings
SettingTypeDefaultVariable
maxPricefloat20.0maxPrice
Show RangeboolfalseshowRange
openNewTradeAsapboolfalseopenNewTradeAsap
Lable Heightint10lableHeight
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

330 stars

Alert BB + RSI [Alorse] is a multi-symbol mean-reversion strategy designed for cryptocurrency trading. It evaluates Bollinger Bands and the Relative Strength Index (RSI) across up to 20 user-defined ticker symbols simultaneously. The strategy enters long posit

Strategy v4 Oscillators & momentumVolatility & bands
Updated 2 months ago · None stated GitHub repository of Alert BB + RSI [Alorse]
285 stars

Mean Reversion Strategy is an overlay strategy that identifies pullbacks using a long-term EMA, RSI, and candlestick patterns. It generates long signals when a bullish engulfing candle forms below the EMA while RSI is oversold, and short signals when a bearish

Strategy v4 Mean reversionCandlestick patterns
Updated 3 years ago · None stated GitHub repository of Mean Reversion Strategy

This strategy draws on the price chart and combines Stochastic RSI, the Money Flow Index, and an exponential moving average to open long or short trades. Longs use the lower oscillator thresholds and shorts use the upper ones, with direction limited to longs,

Strategy v5 Oscillators & momentumMoving averages Repaints
Updated 2 years ago · GPL-3.0 GitHub repository of 3 Indicator Strategy (StochRSI, MFI & EMA) With Safety Orders
104 stars

This strategy combines Bollinger Bands with a short RSI to fade moves that leave the bands and then return. A long requires the prior bar below the lower band and oversold, then a close back inside the band while RSI is between 30 and 50. Shorts mirror that at

Strategy v5 Oscillators & momentumVolatility & bands
Updated 3 years ago · MIT GitHub repository of Bollinger Bands and RSI Strategy
53 stars

This strategy implements Larry Connors' 2-period RSI mean-reversion algorithm overlaid on the price chart. It filters trades using a long-term Simple Moving Average (SMA), entering long positions when the price trades above the SMA and the short-term RSI drops

Strategy v6 Oscillators & momentumMean reversion
Updated 2 months ago · MPL-2.0 GitHub repository of Larry Connors 2-Period RSI Strategy

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers