# Bernoulli Process – Binary Entropy Function Amibroker AFL Code

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

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

sell = info2<-range;

/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
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();``````

## 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...