Library · Pine Script v5 · Library

MPB

by sourdiesel1202

A Pine library that exports a scoring function for price against two simple moving averages, with periods of 9 and 20.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code326
Settings1inputs
LicenceNone statedfile: MPL-2.0
Last update3 years ago16 Jul 2023

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 library, select all of the template text and paste. Press Save and give it a name.
  4. 4Publish or import itA library draws nothing. Publish it from the editor, then import its functions in your own scripts.

Source Code

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

mpb.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sourdiesel1202

//@version=5

// @description MONEY PRINTER GO BRRRRRRRRRRR
library("MPB")

// obv_percent = input(5, "OBV Percent")
// trend_lookback=input(1,"Trend Lookback")

// obv_percent = input(0.01, "OBV Delta Percent", "The percentage between obv and obv[1], exceed this value to consider OBV trend strong")
_adx = input(14,"ADX Value")
// debug = input(true, "Debug", "Enable Logging")
// [prior_dmilpus, prior_dmineg, prior_adx] = ta.dmi(14, 14)

//[macdLine, signalLine, histLine] = ta.macd(close, fastInput, slowInput, 9)
// obv = ta.obv
// prior_obv = ta.obv[1]
// dmi_delta = input(1, "DMI Delta")
// dmi_rapid=input(5, "DMI Rapid Change", "The delta between DMI[1] and DMI. If this value is exceeded, flag reversal")

// _dmiplus = input(17, "DMI+ Signal")
// _dmineg = input(17, "DMI- Signal")
// adx_rapid=input(0.5, "ADX Rapid Change", "The delta between ADX[1] and ADX. If this value is exceeded, flag reversal")
// is_sinking(_indicator, _lookback)=>
xxs_mpb_value=1
xs_mpb_value=3
s_mpb_value=5
m_mpb_value=8
l_mpb_value=12
xl_mpb_value=15
xxl_mpb_value=20
export calculate_sma_value() =>
    fast_sma = ta.sma(close, 9)
    slow_sma = ta.sma(close, 20)
    // ok so the idea here is check if we have a positive or negative swing over sma
    sma_value = 0.0
    //what the literal fuck
    //bruh this shouldn't be this hard (that's what she said)
    //chec
    if close < open // red candle
        //for the sake of dumbassery we're going to ignore open price
        //indicates downtrend
        if close < slow_sma
            sma_value := (-1*l_mpb_value)+sma_value
        if close < fast_sma
            sma_value := (-1*m_mpb_value)+sma_value

        //indicates selloff in uptren
        if close > slow_sma
            sma_value := s_mpb_value+sma_value
        if close > fast_sma
            sma_value := xs_mpb_value+sma_value
        // if over long SMA

    else //green candle
        //greenily deenily
        //indicates buyup in overall downtrend
        if close  < slow_sma
            sma_value := (-1*m_mpb_value)+sma_value
        if close < fast_sma
            sma_value := (-1*s_mpb_value)+sma_value
            // _value := _value +(xs_mpb_value*-1)

        //indicates uptrend
        if close > slow_sma
            sma_value := l_mpb_value+sma_value
        if close > fast_sma
            sma_value :=m_mpb_value+sma_value

    [sma_value, fast_sma, slow_sma, close, open]

// export calculate_prediction( int _mpb_obv, int _mpb_dm, int _mpb_macd, int prior_bars, int prediction_lookback ) =>
//     prediction=0.0
//     price_averge = 0.0//array.new_float(prediction_lookback)
//     price_delta_average = 0.0//  array.new_float(prediction_lookback)
//     highs_lows =  0.0//array.new_float(prediction_lookback)
//     iterations = 0
//     bars_in_future =3
//     for i = 0 to prediction_lookback
//         iterations := iterations +1
//         price_averge := price_averge + (open[prior_bars+i+1]+close[prior_bars+i])/2
//         price_delta_average := (close[prior_bars+i]-open[prior_bars+i+1]) + price_delta_average
//         // price_delta_average := price_delta_average + (((open[i+1]+close[i])/2)-(((open[i+1]+close[i])/2)))
//         // price_delta_average := price_delta_average + (((close[i]+open[i])/2)-((close[i+1]+open[i+1])/2))
//         // price_delta_average := price_delta_average+open-open[1]
//         highs_lows := highs_lows+(high[prior_bars+i]-low[prior_bars+i])

//     // highs_lows/prediction_lookback]7
//     //ok so to calculate the next i bars we need to take the price delta average
//     // for i = 1 to bars_in_future
//     //do first prediction
//     first_pred_mid = (price_averge/(prediction_lookback+1))+(price_delta_average/(prediction_lookback+1))
//     first_pred_high = ((highs_lows/(prediction_lookback+1))/2)+first_pred_mid
//     first_pred_low = first_pred_mid -((highs_lows/(prediction_lookback+1))/2)

//     //second predication
//     second_pred_mid = ((price_averge+first_pred_mid)/(prediction_lookback+2))+(price_delta_average/(prediction_lookback+1))
//     second_pred_high = ((highs_lows/(prediction_lookback+1))/2)+second_pred_mid
//     second_pred_low = second_pred_mid - ((highs_lows/(prediction_lookback+1))/2)

//     //third prediction
//     third_pred_mid = ((price_averge+first_pred_mid+second_pred_mid)/(prediction_lookback+3))+(price_delta_average/(prediction_lookback+1))
//     third_pred_high = ((highs_lows/(prediction_lookback+1))/2)+third_pred_mid
//     third_pred_low =  third_pred_mid -((highs_lows/(prediction_lookback+1))/2)

//     [ first_pred_high,first_pred_mid, first_pred_low, second_pred_high, second_pred_mid, second_pred_low, third_pred_high, third_pred_mid, third_pred_low]

export calculate_macd_value() =>
    slowlength = 26
    fastLength = 12
    MACDLength = 9
    fast_ma = ta.ema(close, fastLength)
    slow_ma = ta.ema(close, slowlength)
    macd = fast_ma - slow_ma
    signal = ta.ema(macd, MACDLength)
    // MACD = ta.ema(close, fastLength) - ta.ema(close, slowlength)
    // aMACD = ta.ema(MACD[1], MACDLength)
    // delta = MACD - aMACD
    // signal=ta.ema(aMACD, MACDLength)
    ema20 = ta.ema(close,20)
    // ok so the idea here is- is the macd over  the ema (signal) line
    _value =0.0
    hist = macd -signal
    //ok so first, add our positive vs negative flag

    if macd > signal //case for long position
        if macd > macd[2] //if the general trend is up
            if hist[1] < hist //so now we look at the histogram,
                if signal-macd <= (signal-macd> 0 ? 1.0 : -1.0)//ensure the delta is actually worth noting
                    _value :=xxl_mpb_value-1
                else
                    _value :=xl_mpb_value
                if hist[2] > hist[1]  //basically this check says was the prior bar indicating reversal on the histo
                    _value := _value -m_mpb_value
            else //otherwise we are
                if signal-macd > (signal-macd> 0 ? 1.0 : -1.0)
                    _value :=m_mpb_value
                else
                    _value :=s_mpb_value
        else //otherwise, the general trend is down
            if signal-macd > (signal-macd> 0 ? 1.0 : -1.0)
                _value :=xs_mpb_value
            else
                _value :=0.0

    else //case for short
        if macd < macd[2] // gemeral trend is down
            if hist[1] > hist //look at histogram
                if macd-signal <= (macd-signal> 0 ? 1.0 : -1.0)
                    _value :=(xxl_mpb_value*-1)+1
                else
                    _value :=(xl_mpb_value*-1)

                if hist[2] < hist[1]  //basically this check says was the prior bar indicating reversal on the histo
                    _value := _value +m_mpb_value
            else
                if macd-signal > (macd-signal> 0 ? 1.0 : -1.0)
                    _value :=(m_mpb_value*-1)
                else
                    _value:=(s_mpb_value*-1)
        else
            // _value := -0.125
            if macd-signal > (macd-signal> 0 ? 1.0 : -1.0)
                _value :=(xs_mpb_value*-1)
            else
                _value :=0.0
    //check for positive vs negative macd values
    if macd > 0
        _value := _value+xxs_mpb_value
    else
        _value := _value+(xxs_mpb_value*-1)

    //ok so if a crossover happened, take this as a strong trend
    if macd > signal and macd[1] < signal[1]
        _value:=xxl_mpb_value
    if macd < signal and macd[1] > signal[1]
        _value:=xxl_mpb_value*-1
    [_value]
export  calculate_obv_value(float obv_percent)=>
    obv = ta.obv
    // obv = ta.obv
    obv_delta= 0.0
    // obv_delta_p
    _value = 0.0
    // _obv_percent = 0.0
    delta_percent = 0.0
    delta_percent_prior = 0.0
    // multiplier = 1.0
    if obv > 0
        _value := _value+xs_mpb_value
    else
        _value := _value-xs_mpb_value
    obv_delta := obv-obv[1]
    obv_delta_prior = obv[1]-obv[2]

    if obv > obv[1]
        delta_percent := ((obv_delta > 0) ?obv_delta : obv_delta*-1)/(obv[1] > 0 ? obv[1]: obv[1]*-1)*100

    else
        delta_percent := (((obv_delta > 0) ?obv_delta : obv_delta*-1)/(obv[1] > 0 ? obv[1]: obv[1]*-1)*100) * -1

    if obv>obv[2]
        delta_percent_prior := ((obv_delta_prior > 0) ? obv_delta_prior : obv_delta_prior*-1)/(obv[2] > 0 ? obv[2]: obv[2]*-1)*100
    else
        delta_percent_prior := (((obv_delta_prior > 0) ?obv_delta_prior : obv_delta_prior*-1)/(obv[2] > 0 ? obv[2]: obv[2]*-1)*100) * -1

    if delta_percent > 0
        if delta_percent > obv_percent
            if obv > obv[2]
                if delta_percent_prior > obv_percent
                    _value :=xl_mpb_value
                else
                    _value := l_mpb_value
            else
                if delta_percent_prior > obv_percent
                    _value :=l_mpb_value*-1
                else
                    _value :=xs_mpb_value*-1

        else
            if delta_percent_prior > obv_percent
                _value:=xl_mpb_value
            else
                _value :=l_mpb_value
    else
        if delta_percent < (obv_percent*-1)
            if obv < obv[2]
                if delta_percent_prior < (obv_percent*-1)
                    _value :=xl_mpb_value*-1
                else
                    _value := l_mpb_value
            else
                if delta_percent_prior < (obv_percent*-1)
                    _value :=l_mpb_value
                else
                    _value := xs_mpb_value
        else
            if delta_percent_prior < (obv_percent *-1)
                _value :=(xl_mpb_value*-1)
            else
                _value :=(l_mpb_value*-1)
    // obv = ta.obv

    [_value]


export calculate_adx_value()=>
// [dmilpus, dmineg, adx] = ta.dmi(input(14,"DMI Length"), input(14, "ADX Smoothing"))
    [dmilpus, dmineg, adx] = ta.dmi(14, 3)

    _value=0.0
    if adx > adx[2] //we know trend is strong
        if adx > 20
            _value :=xxl_mpb_value
        else
            _value := xl_mpb_value
    else
        //ok so here we need to do some logic to determine if adx is decreasing as a new trend is forming
        if (dmilpus > dmilpus[2] and dmineg < dmineg[2]) or (dmineg > dmineg[2] and dmilpus[2] >dmilpus)
            _value := xl_mpb_value
        else
            _value := m_mpb_value



    _value :=  dmilpus > dmineg ? 1*_value : -1*_value
    // plotchar(_value, "MPB ADX Value", "", location = location.top)
    [_value]

export calculate_dmi_value(int dmi_delta, int _dmiplus, int _dmineg,int dmi_rapid)=>

    _value=0.0
    [dmilpus, dmineg, adx] = ta.dmi(14, 3)
    if dmilpus >= dmineg and dmilpus >= _dmiplus
        if dmilpus-dmineg > dmi_delta
            if dmilpus > dmilpus[2]
            // if dmilpus > dmilpus[1]
                _value:=xl_mpb_value
            else
                _value:=0.0 //flag reversal
            // else

                    // _value:=0.25
                // else
                    // _value:=0
    //# check to see if dmi+ is trending down and dmi- is trending up
    // if dmilpus < dmilpus[1] and dmineg > dmineg[1]
        //plot(1,color = color.green)
        else
            _value:=l_mpb_value
        // if  prior_obv> obv and prior_obv/obv*100 < obv_percent
            // value:=0.5


    //case for rapid changes pointing to reversal
        if dmilpus[1]-dmilpus > dmi_rapid
            _value:=0
    // if dmilpus
    // if dmilpus-dmineg <= dmi_delta
        // value:=0
    // if dmilpus-dmineg < input(15, "DMI Delta")
    //     value:=0.5
    // else
    //     value := 0.5
    // []
    if dmineg >= dmilpus and dmineg >= _dmineg
        if dmineg-dmilpus > dmi_delta
            if dmineg > dmineg[2]
                _value:=xl_mpb_value*-1
            else
                // if (dmineg+dmineg[1]+dmineg[2]) / 3 > dmineg
                //     _value:=-0.25
                // else
                _value:=0
        // if adx > prior_adx
        else
            _value:=l_mpb_value*-1
            // if  rsi >= input(60, "RSI High")
                // line.new(bar_index, close, bar_index, close + 1, xloc.bar_index, extend.both, color.red)
            // else
            //     value := -0.5
        if dmineg[1]-dmineg > dmi_rapid
            _value:=0
    [_value]

Overview

Depending on the candle colour and where the close sits relative to each average, it adds or subtracts weighted values and returns the total with both averages, the close and the open. Other helper functions appear in the supplied code, but it is cut off. The library draws nothing by itself and is meant to be imported by other scripts, such as the author's Money Printer indicators.

Details

Published by

sourdiesel1202 on GitHub

Type

Library, Pine Script v5

Draws

Library (no plots)

Markets

Any symbol

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
ADX Valueint14_adx
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

261 stars

AIActivationFunctionsBipolarSigmoid is a Pine Script library that provides mathematical implementations of the bipolar sigmoid activation function and its derivatives for machine learning algorithms. It calculates bipolar sigmoid values bounded between -1 and

Library v5 AI / machine learning
Updated 4 years ago · None stated GitHub repository of AIActivationFunctionsBipolarSigmoid

KernelFunctions

by artificial-intelligence-edge
75 stars

KernelFunctions is a Pine Script v5 library that exports kernel weight functions for Nadaraya-Watson style estimation. It calculates a Rational Quadratic kernel, a Gaussian kernel based on a radial basis function, and a periodic kernel that models repeating be

Library v5
Updated 3 days ago · MIT GitHub repository of KernelFunctions

Ta

by nersent
29 stars

This Pine Script v5 library exports thin wrappers around built-in technical analysis functions. It covers moving averages, momentum measures such as RSI and CCI, volatility tools including ATR and standard deviation, range extremes, cross detection, and runnin

Library v5
Updated 11 months ago · Custom licence GitHub repository of Ta

Bands

by heyphat
26 stars

This library exports a Band type and functions that build a simple-moving-average basis with upper and lower envelopes from standard deviation. It classifies a source as above, below, or inside those envelopes, measures band width, and scales an imported z-sco

Library v6 Volatility & bandsMoving averages
Updated 2 months ago · AGPL-3.0 GitHub repository of Bands

Mylib

by ferranbt
21 stars

This script serves as a minimal utility library written in Pine Script v6. It provides an exported helper function that calculates the sum of two numeric arguments. As a reusable code library, it produces no visual plots, charts, or trading signals, and it con

Library v6
Updated 7 weeks ago · MPL-2.0 GitHub repository of Mylib

HLevel

by stupenkov
14 stars

HLevel is a Pine Script v5 utility library designed to manage horizontal price levels for cryptocurrency charts. It defines custom types for pivots and levels, along with methods to initialize, add, track, and expire price levels based on pivot high and pivot

Library v5
Updated 3 years ago · None stated GitHub repository of HLevel

More libraries

Trade Your Charts With a Regulated Broker

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

Compare brokers