Rajandran R Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

Bernoulli Process – Binary Entropy Function Amibroker AFL Code

1 min read

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							//
//////////////////////////////////////////////////////////

//Translated from Tradingview Pinescript
//url : https://www.tradingview.com/script/bvYZ1CdF-Bernoulli-Process-Binary-Entropy-Function/

_SECTION_BEGIN("Bernoulli Entropy function");

SetChartOptions(0,chartShowArrows|chartShowDates);

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);

_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 Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

Mini Certification Course on Algorithmic Trading Strategies

If you are new to Automated trading? This certification course will help you to kickstart your automated trading with your broker and get to...
Rajandran R
1 min read

HalfTrend – Amibroker AFL Code – Better than Supertrend

Marketcalls had published Supertrend with multiple variations from 2012 onwards. And this time a very popular ATR based strategy Halftrend which is considered a...
Rajandran R
3 min read

AllinOneAlerts – Amibroker Alerts Module for Amibroker Users

AllinOneAlerts is one of the most important and most requested modules to bring all sorts of alerts with no extra coding efforts. Now even...
Rajandran R
5 min read

Leave a Reply

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