// Downloaded From https://www.WiseStockTrader.com _SECTION_BEGIN("Frecuency Distribution Returns"); //Plot histogram of the number of bars which fall between percentiles between 2 data points // Porcentaje de cada barra y periodos a sumar. binpercent=Param("Numero de Barras",15,5,20,1); timeperiod=Param("period for observation",200,50,2000,50); ColorBar=ParamColor("Color barras",colorAqua); ColorText=ParamColor("Color Texto",colorWhite); // Escalares con la suma de apariciones frequency=0; f=0; //Recupera en un array los retornos diarios percent=Nz(100*(log(C/Ref(C,-1)))); //percent=Nz(100*((L-Ref(C,-1))/Ref(C,-1))); //Diferencia porcentual entre el minimo y el cierre anterior // numero de barras dividiendo el 100% entre el % de cada barra mas 1 numbins=binpercent+1; //Obtiene la maxima y minima ganancia en el periodo yMax=SelectedValue(HHV(percent,timeperiod))+0.01; yMin=SelectedValue(LLV(percent,timeperiod))-0.01; //Obtiene el rango del minimo valor al maximo valor yRange=yMax-yMin; //divide el mismo entre el numero de barras de la grafica y asigna entonces el ancho de cada barra. BinSize=yRange/(numbins-1); // primer loop de 1 al numero de barras for(i=0;i<=numbins;i++) //el array f se carga inversamente con el valor de las escalares minimo +el tamoƱo de la barra por el numero de la barra //esto para configurar los limites de cada barra { f[BarCount-i-1]=SelectedValue(ymin+binsize*i); } //Segundo Loop este va a cargar la suma de las frecuencias //Genera el loop uno por cada barra for(i=1;i<=numbins;i++) { frequency[BarCount-numbins+(i-1)]=SelectedValue(Sum(((percent>=f[BarCount-i])AND (percent<=f[BarCount-(i+1)])),timeperiod)); } //Tercer Loop genera el string con los nombres del ejes de las x. XaxisNames = ""; for( y = 1; y < numbins; y++ ) { XaxisNames = XaxisNames + " | " +StrFormat("%.2f", f[BarCount-y])+"," + StrFormat("%.2f", f[BarCount-y]) ; } _N(XaxisNames = XaxisNames + " | " +StrFormat("%.2f", f[BarCount-numbins])) ; ///////////////////////////////////////////////// // Drawing code & helper functions //////////////////////////////////////////////// GfxSetOverlayMode( 0 ); GfxSelectFont( "Tahoma", 12 ); GfxSetTextColor(colortext); GfxSetBkMode( 1 ); YOffset = 25; XOffset = 15; // ========================================= FUNCIONES ================================================= // Pinta las barras ARG ( texto del eje x, barra a pintar, function DrawBar( text, bar, numbars, y, Miny, Maxy ) { BarWidth = (Status("pxwidth") - 4 * XOffset )/( numbars + 1 ); BarHeight = Status("pxheight") - 2 * YOffset; relpos = ( y - Miny ) / (Maxy - Miny ); xp = XOffset + ( bar + 0.5 ) * BarWidth; yp = YOffset + BarHeight * ( 1 - relpos ); xe = XOffset + ( bar + 1 ) * BarWidth; ye = YOffset + BarHeight * ( 1 - ( -miny )/( maxy - miny ) ); if( y > 0 ) { GfxGradientRect( xp, yp, xe , ye, colorBar, colorBar ); } else { GfxGradientRect( xp, ye, xe , yp, ColorHSB( 0, 20, 255 ), ColorHSB( 0, 255 * ( 1 - relpos ), 255 ) ); } GfxSelectFont( "Tahoma", 8 ); GfxTextOut( text, xp, ye );// valores eje x GfxSelectFont( "Tahoma", 12 ); GfxTextOut( StrFormat("%.0f", y ), xp+10, yp-20 ); // valor de la barra } // Pinta el grid y los niveles function DrawLevels( Miny, Maxy ) { range = Maxy - Miny; grid = 100; if( range < 10 ) grid = 1; else if( range < 20 ) grid = 2; else if( range < 50 ) grid = 5; else if( range < 100 ) grid = 10; else if( range < 200 ) grid = 20; else if( range < 500 ) grid = 50; width = Status("pxwidth") - 4 * XOffset; height = Status("pxheight") - 2 * YOffset; GfxSelectPen( colorBlack, 1, 2 ); for( y = grid * ceil( Miny / grid ); y <= grid * floor( Maxy / grid ); y += grid ) { yp = YOffset + Height * ( 1 - ( y - Miny ) / (Maxy - Miny ) ); GfxMoveTo( XOffset, yp ); GfxLineTo( XOffset + width , yp ); GfxTextOut( ""+ y, XOffset + 2 + width, yp ); } GfxSelectPen( colorBlack, 1, 0 ); GfxMoveTo( XOffset, YOffset ); GfxLineTo( XOffset + width, YOffset ); GfxLineTo( XOffset + width, YOffset + Height ); GfxLineTo( XOffset , YOffset + Height ); GfxLineTo( XOffset , YOffset ); } function DisplayFrecuency() { Bar = 0; //Alto MinAvgProf = MaxAvgProf = 0; //determina alto de las frecuencias for( y = 0; y < numbins; y++ ) { Chg = frequency[BarCount-numbins+y]; MinAvgProf = Min( MinAvgProf, Chg ); MaxAvgProf = Max( MaxAvgProf, Chg ); } // Pinta las barras for( y = 0; y < numbins-1; y++ ) { Chg = frequency[BarCount-numbins+y]; DrawBar( StrExtract(XaxisNames, y+1 ), Bar++, numbins-1, Chg, MinAvgProf , MaxAvgProf ); } DrawLevels( MinAvgProf , MaxAvgProf ); //pinta el grid } //////////////////////////// // Main program - chart type switch //////////////////////////// DisplayFrecuency(); _N(Title = StrFormat("Symbol: {{NAME}} Date: {{DATE}} \n\n" + "FRECUENCY DISTRIBUTION RETURNS\n" + "\nPeriods : " + WriteVal(timeperiod,1.0) + " " + Interval(2) + "\n\nMax "+ Interval(2) + " Return : " + WriteVal(yMax-0.01,1.2) + " %%" + "\nMin "+ Interval(2) + " Return : " + WriteVal(yMin+0.01,1.2) + " %%" + "\nAverage "+ Interval(2) + " Return : " + WriteVal(MA(percent,timeperiod)) + " %%" + "\nStandard Deviation : " + WriteVal(StDev(percent,timeperiod)) + "\n\nMedian : " + WriteVal(Median(percent,timeperiod),1.3)+ "\nVaR Level 99 : " + WriteVal(Percentile(percent,timeperiod,1),1.2) + " %%" )); _SECTION_END(); _SECTION_BEGIN("Background Color"); BKswitch = ParamToggle("Background Color","On,Off",1); OUTcolor = ParamColor("Outer Panel Color",colorWhite); TitleColor = ParamColor("Title Color ",ColorRGB(245,245,245)); if (NOT BKswitch) { SetChartBkColor(OUTcolor); // color of outer border Gradient = ParamToggle("Gradient Background Y/N","NO|YES"); INUPcolor = ParamColor("Inner Panel Upper",colorLightYellow); INDNcolor = ParamColor("Inner Panel Lower",colorSeaGreen); if(gradient) SetChartBkGradientFill(INUPcolor,INDNcolor,TitleColor); // color of inner panel } _SECTION_END();