Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

Bernoulli Process – Binary Entropy Function Amibroker AFL Code

2 min read

Binary entropy function is a mathematical concept that is used to measure the uncertainty or randomness of a binary system. In a binary system, there are only two possible outcomes, such as heads or tails in a coin toss. The binary entropy function is used to measure the probability of these two outcomes occurring.

In trading, the binary entropy function can be used to analyze the likelihood of a particular event occurring. For example, it can be used to analyze the probability of a stock reaching a certain price level or the probability of a particular economic indicator having a certain value.

The binary entropy function is often used in conjunction with other technical analysis tools, such as statistical analysis and machine learning algorithms, to make more informed trading decisions. It can help traders and investors identify patterns and trends in the market, and make more accurate predictions about the direction of prices.

Here is the Bernoulli Process code snippet translated from Trading pinescript indicator which explores the Bernoulli Function/Distribution), and combined with the Shannon Entropy measurement

In case you need a primer for Bernoulli Distribution. Start here

//////////////////////////////////////////////////////////
//Coded By Rajandran R - Founder - Marketcalls			//
//Co-Creator - www.algomojo.com							//
//Creation Date - 01st Sep 2020							//
//////////////////////////////////////////////////////////

_SECTION_BEGIN("Bernoulli Entropy function");


src = ParamField("Source");
len = Param("Length",22,1,100,1);
range = Param("Range",0.67,0.01,1,0.01);
average = Param("Average",88,1,100,1);
vPR = Param("Percent Rank Limit",5,1,10,1);




cr = src/sum(src,len); //source, typ close, percent of close, measured over summation period
vr = log(volume)/sum(log(volume),len)   ;   //volume data, percent of volume, measured of summation period
vr2 = min(max(percentrank(vr,average)/100,0.001),0.999) ; //cutting out 100% and 0% readings, changing to +/-3sigma
cr2 = min(max(percentrank(cr,average)/100,0.001),0.999) ;
infoc = sum((cr2*log10(cr2)/log10(2)) - (1-cr2)*log10(1-cr2)/log10(2),len); //p(close)*log2(p(close)) - (1-p(close))*log2(1-p(close))
infov = sum((vr2*log10(vr2)/log10(2)) - (1-vr2)*log10(1-vr2)/log10(2),len);
info2 = infoc - infov    ;


color = IIf(info2>range, colorGreen, IIf(info2<-range,colorRed,colorGrey40));
Plot(info2,"Info",color,styleHistogram | styleThick);
Plot(infoc,"Price",colorBlue);
Plot(-infov,"Volume",colorOrange);


hvp = percentrank(info2,average);


PlotShapes(IIf(hvp<vPR, shapeUpArrow, shapeNone),colorlime, 0,info2, Offset=-25);
PlotShapes(IIf(hvp>(100-vPR), shapeDownArrow, shapeNone),colorred, 0,info2, Offset=-25);

SetChartOptions(0,chartShowArrows|chartShowDates);



_SECTION_END();

Simple Trading System Based on Bernoulli Entropy function

//////////////////////////////////////////////////////////
//Coded By Rajandran R - Founder - Marketcalls			//
//Co-Creator - www.algomojo.com							//
//Creation Date - 01st Sep 2020							//
//////////////////////////////////////////////////////////
_SECTION_BEGIN("Trading System Based on Bernoulli Entropy function");


src = ParamField("Source");
len = Param("Length",22,1,100,1);
range = Param("Range",0.67,0.01,1,0.01);
average = Param("Average",88,1,100,1);
vPR = Param("Percent Rank Limit",5,1,10,1);




cr = src/sum(src,len);                                                               //source, typ close, percent of close, measured over summation period
vr = log(volume)/sum(log(volume),len)   ;                                            //volume data, percent of volume, measured of summation period
vr2 = min(max(percentrank(vr,average)/100,0.001),0.999) ;                                //cutting out 100% and 0% readings, changing to +/-3sigma
cr2 = min(max(percentrank(cr,average)/100,0.001),0.999) ;
infoc = sum((cr2*log10(cr2)/log10(2)) - (1-cr2)*log10(1-cr2)/log10(2),len);    //p(close)*log2(p(close)) - (1-p(close))*log2(1-p(close))
infov = sum((vr2*log10(vr2)/log10(2)) - (1-vr2)*log10(1-vr2)/log10(2),len);
info2 = infoc - infov    ;


color = IIf(info2>range, colorGreen, IIf(info2<-range,colorRed,colorGrey40));
//Plot(info2,"Info",color,styleHistogram | styleThick);
//Plot(infoc,"Price",colorBlue);
//Plot(-infov,"Volume",colorOrange);


hvp = percentrank(info2,average);

Buy = info2>range;

sell = info2<-range;


Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", color, styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 


_SECTION_END();
Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

Interactive Brokers – Smart Order Chart Trading Module using…

The IB Controller is an interface designed to facilitate automatic trading with AmiBroker and Interactive Brokers (IB) TWS (Trader Workstation). It serves as a...
Rajandran R
8 min read

Introducing OpenAlgo V1.0: The Ultimate Open-Source Algorithmic Trading Framework…

OpenAlgo V1.0 is an open-source algorithmic trading platform to automate your trading strategies using Amibroker, Tradingview, Metatrader, Python etc. Designed with the modern trader...
Rajandran R
2 min read

[Live Coding Webinar] Build Your First Trading Bridge for…

In this course, you will be learning to build your own trading bridge using Python. This 60-minute session is perfect for traders, Python enthusiasts,...
Rajandran R
1 min read

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More