Strategy · Pine Script v5 · On the chart

BTC Bot

by zhuzp98· code credited to rhapsodyz

A long-only strategy for Bitcoin-style charts that combines an RSI-based momentum signal with a trend filter using one or two simple moving averages.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code123
Settings16inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago17 Jan 2025

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

123 lines of Pine Script v5 from PineScript/Stg_ex_BTC_Bot.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

Stg_ex_BTC_Bot.pine
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © rhapsodyz

//@version=5

strategy(title = "BTC bot", 
     overlay = true, 
     pyramiding = 1, 
     initial_capital = 100000, 
     default_qty_type= strategy.percent_of_equity, 
     default_qty_value = 100, 
     commission_type = strategy.commission.percent, 
     commission_value = 0.04, 
     slippage = 2)

strat_dir_input = 
     input.string(title="Strategy Direction", 
         defval="long", 
         options=["long", "short", "all"])

strat_dir_value = 
     strat_dir_input == "long" ? strategy.direction.long : 
     strat_dir_input == "short" ? strategy.direction.short : 
     strategy.direction.all

strategy.risk.allow_entry_in(strat_dir_value)

// #######################
// === General Numbers ===
// #######################

// === Equity Usage ===
equityUsage = input(0.9, title="Equity Usage Percentage")
_qty = (strategy.equity * equityUsage) / close

C_DownTrend = true
C_UpTrend = true
smaFastLength = input.int(50, title = "SMA Fast Length")
smaSlowLength = input.int(200, title = "SMA Slow Length")

var trendRule1 = "SMA Single"
var trendRule2 = "SMA Double"
var trendRule = input.string(trendRule1, "Detect Trend Based On", options=[trendRule1, trendRule2, "No detection"])

if trendRule == trendRule1
    priceAvg = ta.sma(close, smaFastLength)
    C_DownTrend := close < priceAvg
    C_UpTrend := close > priceAvg

if trendRule == trendRule2
    smaSlow = ta.sma(close, smaSlowLength)
    smaFast = ta.sma(close, smaFastLength)
    C_DownTrend := close < smaFast and smaFast < smaSlow
    C_UpTrend := close > smaFast and smaFast > smaSlow

// === Input A ===
higherTF    = input.timeframe("W")
prevClose   = request.security(syminfo.tickerid, higherTF, close[1])
prevHigh    = request.security(syminfo.tickerid, higherTF, high[1])
prevLow     = request.security(syminfo.tickerid, higherTF, low[1])

factor = input(2, "Factor of Large Border")

PP = 0.0
R1 = 0.0
R2 = 0.0 
R3 = 0.0
S1 = 0.0 
S2 = 0.0 
S3 = 0.0

PP := hlc3
R1 := PP + (PP - prevLow)
S1 := PP - (prevHigh - PP)
R2 := PP + (prevHigh - prevLow)
S2 := PP - (prevHigh - prevLow)

R3 := prevHigh + factor * (PP  - prevLow) 
S3 := prevLow  - factor * (prevHigh - PP) 

// 
length = input(21)
//

closeRSI = ta.rsi(close, length)
emaCRSI  = ta.ema(closeRSI,length)
diffRSI  = (closeRSI - emaCRSI) * 5
sigRSI   = (closeRSI + emaCRSI + diffRSI) / 2
//
longSignal = ta.crossover(sigRSI, 0)

shortSignal = ta.crossover(close[1], R3)
//


smiPlot = plot(R3, "R3", color = color.blue)
plot(close[1], "Previous Close", color = color.orange)
plot(sigRSI, title = "sigRSI", style = plot.style_columns, color = (sigRSI >= 0 ? (sigRSI[1] < sigRSI ? #26A69A : #B2DFDB) : (sigRSI[1] < sigRSI ? #FFCDD2 : #FF5252)))


if longSignal and C_UpTrend
    strategy.entry("buy", strategy.long, qty = _qty)
if shortSignal and C_DownTrend
    strategy.entry("sell", strategy.short, qty = _qty)

per(pcnt) =>
    strategy.position_size != 0 ? math.round(pcnt / 100 * strategy.position_avg_price / syminfo.mintick) : float(na)

// stoploss maybe 8
stoploss=input.float(title=" stop loss", defval = 15, minval = 0.01)
los = per(stoploss)
q1=input.int(title=" qty_percent1", defval=25, minval=1)
q2=input.int(title=" qty_percent2", defval=25, minval=1)
q3=input.int(title=" qty_percent3", defval=25, minval=1)
// takeprofit maybe 2 3 4 5
tp1=input.float(title=" Take profit1", defval=3, minval=0.01)
tp2=input.float(title=" Take profit2", defval=5, minval=0.01)
tp3=input.float(title=" Take profit3", defval=7, minval=0.01)
tp4=input.float(title=" Take profit4", defval=10, minval=0.01)
strategy.exit("x1", qty_percent = q1, profit = per(tp1), loss = los)
strategy.exit("x2", qty_percent = q2, profit = per(tp2), loss = los)
strategy.exit("x3", qty_percent = q3, profit = per(tp3), loss = los)
strategy.exit("x4", profit = per(tp4), loss = los)

Overview

BTC Bot buys when the smoothed RSI signal crosses above zero while price is in an uptrend, sizes positions from a share of equity, and scales out at four take-profit levels with a percentage stop loss. Weekly pivot-style levels are plotted on the price chart. Direction, averages, stop and targets are adjustable.

Details

Published by

zhuzp98 on GitHub

Code credit

rhapsodyz, as written in the script's header

Draws

On the price chart

Markets

Crypto

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

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

SettingTypeDefaultVariable
Strategy DirectionstringlongStrategy Direction
Equity Usage Percentagefloat0.9equityUsage
SMA Fast Lengthint50smaFastLength
SMA Slow Lengthint200smaSlowLength
Detect Trend Based OnstringtrendRule1trendRule
higherTFtimeframeWhigherTF
Factor of Large Borderint2factor
lengthint21length
stop lossfloat15stoploss
qty_percent1int25q1
qty_percent2int25q2
qty_percent3int25q3
Show all 16 settings
SettingTypeDefaultVariable
Take profit1float3tp1
Take profit2float5tp2
Take profit3float7tp3
Take profit4float10tp4
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

106 stars

This strategy plots on the price chart and enters when three smoothed moving averages are aligned, RSI is on the matching side of 50, and a Williams fractal appears. An upward stack with RSI above 50 and a down fractal opens a long; the opposite setup opens a

Strategy v5 Moving averagesOscillators & momentum
Updated 2 years ago · GPL-3.0 GitHub repository of 3SmmaCrossUp + Fractal + RSI

This strategy trades 1-minute futures contracts on Binance. It plots pivot high and low resistance and support levels on the price chart, along with an EMA and a custom Cto indicator. Long and short entries trigger on EMA and Cto crossovers combined with price

Strategy v5 TrendMoving averages Repaints
Updated 2 years ago · None stated GitHub repository of Binancash Futures Strategy 1M Candles

KC15

by benso87
24 stars

KC15 is a price-chart strategy that configures two moving averages, a Bollinger Band Width Percentile filter, and PMARP entry and exit thresholds for long and short trades. Users can switch each average between SMA and EMA, set lengths and colors, and toggle l

Strategy v5 Moving averagesVolatility & bands
Updated 4 years ago · MPL-2.0 GitHub repository of KC15

Optimizer v1

by QuantGeekDev
7 stars

This strategy generates long signals on the price chart using multiple technical indicators. It computes EMA 70 and 100, SMA 70, ADX with DMI, CCI, and MACD values. A long entry occurs when EMA crossover condition, SMA condition, ADX above 15, CCI above 30, an

Strategy v5 TrendOscillators & momentum
Updated 3 years ago · None stated GitHub repository of Optimizer v1

This strategy combines MACD momentum signals with support and resistance levels from pivot points and an EMA100 trend filter for long positions. It plots the 100-period EMA on the price chart and signals when price breaks above the EMA or forms bullish pivot p

Strategy v4 TrendOscillators & momentum
Updated 3 years ago · Apache-2.0 GitHub repository of MACD + Support / Resistance Long Strategy

This overlay strategy combines swing price structure with MACD structure and two higher-timeframe exponential moving averages. It finds pivot highs and lows over ten bars, labels higher highs, lower highs, higher lows and lower lows, and builds matching states

Strategy v5 TrendOscillators & momentum Repaints
Updated 5 months ago · None stated GitHub repository of Price Action Candles with MACD Signals Strategy

More Oscillators & momentum scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

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

Compare brokers