# Bayes Probability Index by DGT

3410浏览
What is Probability?
It is a measure for calculating the chances or the possibilities of the occurrence of a random event. In simple words, it calculates the chance of the favorable outcome amongst the entire possible outcomes. Mathematically, if you want to answer what is probability, it is defined as the ratio of the number of favorable events to the total number of possible outcomes of a random events.

Is this enough? May be or may be not

Let’s consider an example,
A simple probability question may ask: "What is the probability of Amazon.com's stock price falling?"
How about if we extend our question a step further by asking: "What is the probability of AMZN stock price falling given that the Dow Jones Industrial Average (DJIA) index fell earlier?"
Now we are ready to consider conditional probability and Bayes' Theorem is where we could find answer to this question

Bayes' Theorem
Bayes' theorem, named after 18th-century British mathematician Thomas Bayes, is a mathematical formula for determining conditional probability. Conditional probability is the likelihood of an outcome occurring, based on prior knowledge of conditions or another related event occurring. Bayes' theorem provides a way to revise existing predictions or theories (update probabilities) given new or additional evidence. Bayes' theorem thus gives the probability of an event based on new information that is, or may be related, to that event

Formula For Bayes' Theorem
P(A|B) = P(B∣A) * P(A) /P(B)
= P(B∣A) * P(A) / (P(B∣A)* P(A) + P(B∣A’)* P(A’) )

where
A and B are events and P is probability
P(A|B) is the posterior probability, the probability of A after taking into account B
P(A) is the prior probability, the probability of A belief
P(A’) is the prior probability, the probability of A disbelief : P(A’)=1- P(A)
P(B) is the prior probability, the probability of B belief
P(B∣A) is the conditional probability or likelihood, the degree of belief in B given that proposition of A belief (A true)
P(B∣A’) is the conditional probability or likelihood, the degree of belief in B given that proposition of A disbelief (A false)

Bitcoin was the first-ever cryptocurrency, designed by Satoshi Nakamoto. In its likeness, all other cryptocurrencies were then created. The relationship between Bitcoin and altcoins remains something crypto analyst watch closely. This study aims to display the likelihood of bullish movement for ALTS-USDT pairs taking into consideration of bullish move probability of BTC-USDT pair

What to look for:
Percentage Value of the Conditional Probability and/or Simple Probability. When value is above %50 than bullish move is more probable, conversely when the value is below %50 bearish move is more likely

Limitations: Conditional Probability Line will be shown for daily time frame only, Simply Probability Line would be available for all time frames

Conditional Probability is calculated with the condition of BTC-USDT pair so using Conditional Probability is suggested with ALTS-USDT pairs.

Indicators aim to generate a potential signal/indication of an upcoming opportunity, but, the Indicators themselves do not guarantee the future movement of a given financial instrument, and are most useful when used in combination with other techniques.

Trading success is all about following your trading strategy and the indicators should fit within your trading strategy, and not to be traded upon solely

Disclaimer: The script is for informational and educational purposes only. Use of the script does not constitutes professional and/or financial advice. You alone the sole responsibility of evaluating the script output and risks associated with the use of the script. In exchange for using the script, you agree not to hold dgtrd TradingView user liable for any possible claim for damages arising from any decision you make based on use of the script

cases are clearly stated in the description of this study. warning is added in case the description not taken into account

additionally the base line moved to 0, so values above 0 indicates the probability of bull move is higher and values below 0 indicates the probability of bull move is lower

## 评论

Good explanation, hope you will make it public some day so we can learn math under the hood in details.

Pratik_4Clover
Hi @Pratik_4Clover, with pleasure, here it is

//@version=4
study(title="Bayes Conditional Probability by DGT", shorttitle="BAYES by DGT", overlay=false, format=format.percent, max_bars_back=100)

// -Input ======================================================================================= //

showX = input(false, title = "Show Simple Probability")
showXandY = input(true, title = "Show Bayesian Conditional Probability based on a Given Symbol")
symbol = input(title="Conditional Symbol", type=input.symbol, defval="BTCUSDT")
//sourceX = input(title="Source", type=input.source, defval=close)
length = input(title="Probability Length", type=input.integer, minval=1, defval=20)
sourceX=close

// -Calculation ================================================================================= //

bullX = sourceX > sourceX ? 1:0
sumOfBullX = sum(bullX, length)
probabilityOfBullX = sumOfBullX/length * 100

sourceY = security(symbol, timeframe.period, close)
var probabilityOfBullXandBullY = 0.0

if showXandY //and syminfo.currency == "USDT"
bullY = sourceY > sourceY ? 1:0
sumOfBullY = sum(bullY, length)
probabilityOfBullY = sumOfBullY/length * 100
bullYandBullX = (sourceY > sourceY) and (sourceX > sourceX) ? 1:0
sumOfBullYandBullX = sum(bullYandBullX, length)
probabilityOfBullYandBullX = sumOfBullYandBullX/length * 100
probabilityOfBullXandBullY := probabilityOfBullYandBullX * probabilityOfBullX / probabilityOfBullY

// -Plotting ==================================================================================== //

if showXandY
barstate.islast ? syminfo.prefix + ":" + syminfo.ticker != symbol ?
label.new(bar_index, -13, text="bayesian probality ...", textcolor=color.white, textalign=text.align_left, style=label.style_label_up,tooltip="bayesian conditional probability of " + syminfo.ticker + " is calculated by taking into account " + symbol + " movements") :
label.new(bar_index, 0, text="warning ...", textcolor=color.white, textalign=text.align_left, tooltip="same symbol selected as condition, as a result\nbayesian conditional probability will be equal to simple probability (if same exchange)" ) : na

hline(0, title="Equilibrium Line")
plot(showX ? probabilityOfBullX - 50 : na, color=(probabilityOfBullX > 50 ? color.aqua : color.yellow), title="Simple Probability Line")
plot(showXandY ? probabilityOfBullXandBullY - 50 : na, color=(probabilityOfBullXandBullY >= 50 ? color.green : color.red), title="Conditional Probability Line")

dgtrd
@dgtrd, Thank you very much, looking forward to your new work ^^

Pratik_4Clover
Thanks @Pratik_4Clover, I keep researching and whenever something valuable shows up will try to implement it.

dgtrd
@dgtrd, thanks for sharing but it's not working the value is constant always -50% !
r u sure about the calculations?

Mohamed3nan
@Mohamed3nan, sourceX > sourceX Square Bracket 1 Square Bracket not sourceX > sourceX
this was the error :D

Mohamed3nan
@Mohamed3nan, hmmm it seams tradingview blocks Square Brackets in the comments

Mohamed3nan
Dear @Mohamed3nan,

oops, my mistake I forgot to check it, I was aware that square brackets are removed with copy/paste.

Great you figured it out

if others had the same issue here is the list of lines to be corrected. below, square brackets are replaced with pipe, but if needed with pine script pipe symbols needs to be replaced as square brackets

line 15 : bullX = sourceX > sourceX|1| ? 1:0
line 23 : bullY = sourceY > sourceY|1| ? 1:0
line 26 : bullYandBullX = (sourceY > sourceY|1|) and (sourceX > sourceX|1|) ? 1:0

not so essential needed to be replaced but just to provide as it is in original code
line 35 : label.new(bar_index|55|, -13, text="bayesian probality ...", textcolor=color.white, textalign=text.align_left, style=label.style_label_up,tooltip="bayesian conditional probability of " + syminfo.ticker + " is calculated by taking into account " + symbol + " movements") :
line 36: label.new(bar_index|55|, 0, text="warning ...", textcolor=color.white, textalign=text.align_left, tooltip="same symbol selected as condition, as a result\nbayesian conditional probability will be equal to simple probability (if same exchange)" ) : na

thanks for warning me

dgtrd
@dgtrd, thank you friend, i was planning to implent script that depends on Probability and found you when i searched for "Probability", will send you a demo when i finish it ;)

Mohamed3nan
Hello @Mohamed3nan,

you are welcome, and would be happy to look at your demo

I have used the probaility with some of my studies :

assumed as a multiplier factor and add predictive approach

simplest form:

please keep in mind that the probablity calculations herein is simple probability determined from past information