Wednesday 23 October 2019

VierBox.afl for Amibroker for Amibroker (AFL)

VierBox.afl for Amibroker for Amibroker (AFL)





/*
VierBox ver 0.1. Messy programming, rough plotting, barely usable :D
         ver 0.2. * Parameter VierBox per saham bisa di save. Tidak perlu lagi memakai
                        Draw Rectangle. Big time saver :D
                    * Plot yang lebih halus
                    * fitur untuk menampilkan/menyembunyikan VierBox
                    * fitur untuk menampilkan/menyembunyikan garis vertikal
                    * fitur untuk menampilkan dan merubah warna VierBox (masih sangat sederhana)
                    * fitur untuk merubah warna garis
                    * Bagian Title akan memberikan informasi jika ada parameter yang berubah sejak
                        disave dan memberi rekomendasi apa yang hrs dilakukan.
        ver 0.3.    * Warna box sesuai rules pak Vier. Maap pak Vier, baru kelar sekarang . Ternyata susah juga buatnya :D
                    * text di box untuk menandakan box uptrend, downtrend, stackingBox, etc.
                    * Warna Outline candlestick bisa dirubah di parameter
        ver 0.3.1   * text di box untuk ExplosiveBox


Cara penggunaan: Dari Menu Parameters, Klik "RESET ALL" untuk menampilkan parameter yang telah disave
                    atau
                    Dari Menu Parameters, Klik "SAVE" untuk menyimpan parameter yang telah dirubah

Catatan teknis: parameter setiap saham disimpan di directory C:\amibroker\vierbox\namasaham.txt

Saran, usul dan komentar dipersilahkan.

*/





function startWarna( y1, y2, range )
{

    result = colorWhite;

    if ( AlmostEqual( y1,  y2 + range* 1 / 2  ) )
        result = colorRose; //downTrend

    if ( AlmostEqual( y1,  y2 + range*3 / 2  ) )
        result = colorPink;//reversal

    if ( AlmostEqual( y1,  y2 + range*5 / 2  ) )
        result = colorRed;//Crash

    if ( AlmostEqual( y1,  y2 + range*7 / 2  ) )
        result = colorDarkRed;//Crash2

    if ( AlmostEqual( y1,  y2 + range*9 / 2  ) )
        result = colorDarkRed;//Crash3

    if ( AlmostEqual( y1,  y2 - range* 1 / 2  ) )
        result = colorAqua;//upTrend

    if ( AlmostEqual( y1,  y2 - range* 3 / 2  ) )
        result = colorTurquoise;//stackingBox

    if ( AlmostEqual( y1,  y2 - range* 5 / 2  ) )
        result = colorTeal;//ExplosiveBox

    if ( AlmostEqual( y1,  y2 - range* 7 / 2  ) )
        result = colorDarkGreen;//ExplosiveBox2

    if ( AlmostEqual( y1,  y2 - range* 9 / 2  ) )
        result = colorDarkOliveGreen;//ExplosiveBox3

    return result;
} //end function ColorWhite

function textTrend( y1, y2, range )
{

    result = "?";

    if ( AlmostEqual( y1,  y2 + range* 1 / 2  ) )
        result = "DownTrend";

    if ( AlmostEqual( y1,  y2 + range* 3 / 2  ) )
        result = "Reversal";

    if ( AlmostEqual( y1,  y2 + range* 5 / 2  ) )
        result = "Crash";

    if ( AlmostEqual( y1,  y2 + range* 7 / 2  ) )
        result = "Crash2";

    if ( AlmostEqual( y1,  y2 + range* 9 / 2  ) )
        result = "Crash3";

    if ( AlmostEqual( y1,  y2 - range* 1 / 2  ) )
        result = "UpTrend";

    if ( AlmostEqual( y1,  y2 - range* 3 / 2  ) )
        result = "StackingBox";

    if ( AlmostEqual( y1,  y2 - range* 5 / 2  ) )
        result = "ExplosiveBox";

    if ( AlmostEqual( y1,  y2 - range* 7 / 2  ) )
        result = "ExplosiveBox2";

    if ( AlmostEqual( y1,  y2 - range* 9 / 2  ) )
        result = "ExplosiveBox3";

    return result;
} //end function textTrend


_SECTION_BEGIN( "Save" );
save = ParamTrigger( "Save Parameter VierBox?", "KLIK DISINI UNTUK SAVE" ) ;
_SECTION_END(); //end section "Save"

_SECTION_BEGIN( "Price" );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "Close", ParamColor( "Warna Outline Candlestick", colorBlack ), styleNoTitle | GetPriceStyle() );
_SECTION_END(); //end section "Price"

_SECTION_BEGIN( "Tampilkan VierBox" );
DisplayVierBox = ParamToggle( "Tampilkan VierBox?", "Tidak|Ya", 1 );
_SECTION_END(); //end section "Tampilkan VierBox"

_SECTION_BEGIN( "VierBox by DF" );

//tampilanVierBox=ParamToggle("VierBox yang Ditampilkan","Permanen|Setup",0);
//jumlahBarDiBox = Param("Jumlah Bar di Box", 15, 5, 20);
//Offset= Param("Start mundur berapa bar",75,1,200);
//LineColor =   ParamColor("Warna Garis Outline", colorGreen);
//ColorStartBox = ParamColor("Warna Box Start",colorLightYellow);
//ColorBox2 = ParamColor("Warna Box 2",colorAqua);
//DisplayBoxColor=ParamToggle("Display Box Color","Yes|No",0);
//displayBoxLine=ParamToggle("Tampilkan Garis Outline Box","Ya|Tidak",0);

SetChartOptions( 0, chartShowArrows | chartShowDates );
SetBarsRequired( 100000, 100000 ); //use all bars
//EnableTextOutput( False );

if ( displayVierBox == 1 ) 
{

    //Buka file
    input_file = Name() + ".txt";
    input_folder = "vierbox";
    fh = fopen( input_folder + "\\" + input_file, "r" );

    if ( fh )
    {
        strFile = fgets( fh );
        fclose( fh );

        strStartBar = StrExtract( strfile, 0 );
        strJumlahBarDiBox = StrExtract( strFile, 1 );
        strdisplayBoxLine = StrExtract( strFile, 2 );
        strDisplayBoxColor = StrExtract( strFile, 3 );
        strlineColor = StrExtract( strFile, 4 );
        strColorStartBox = StrExtract( strFile, 5 );
        //strColorBox2 = StrExtract( strFile, 6 );

        startBar = StrToNum( strStartBar );
        lastBar = LastValue( BarIndex() ); //value jumlah total bar . Base 0
        Offset = Lastbar - startBar; //box mulai di sini. Nilai offset keluar cuma sekali di sini!
        Offset = Param( "Start Mundur Berapa Bar?", Offset, 1, 200 );
        startBar = LastBar - Offset;
        jumlahBarDiBox = StrToNum( strJumlahBarDiBox );

        jumlahBarDiBox = Param( "Jumlah Bar di Box", StrToNum( strJumlahBarDiBox ), 5, 20 );
        displayBoxLine = ParamToggle( "Tampilkan Garis Outline Box", "Tidak|Ya", StrToNum( strdisplayBoxLine ) );
        lineColor   =   ParamColor( "Warna Garis Outline Box", StrToNum( strLineColor ) );
        DisplayBoxColor = ParamToggle( "Tampilkan Warna Box", "Tidak|Ya", StrToNum( strDisplayBoxColor ) );
        ColorStartBox   = ParamColor( "Warna Start Box", StrToNum( strColorStartBox ) );
        //ColorBox2 = ParamColor( "Warna Box 2", StrToNum( strColorBox2 ) );

    }
    else //jika file yang mau dibuka gak ada, bikin jadi default dan save
    {
        printf( "error open file" );
        Offset = Param( "Start Mundur Berapa Bar?", 75, 1, 200 );
        lastBar = LastValue( BarIndex() );
        startBar = Lastbar - Offset;

        jumlahBarDiBox = Param( "Jumlah Bar di Box", 15, 5, 20 );
        displayBoxLine = ParamToggle( "Tampilkan Garis Outline Box", "Tidak|Ya", 0 );
        LineColor   =   ParamColor( "Warna Garis Outline", colorGreen );
        DisplayBoxColor = ParamToggle( "Display Box Color", "Tidak|Ya", 1 );
        ColorStartBox   = ParamColor( "Warna Start Box", colorLightYellow );
        //ColorBox2 = ParamColor( "Warna Box 2", colorAqua );


        //save
        Output_file = Name() + ".txt";
        Output_folder = "vierbox";
        fmkdir( Output_folder );
        fh = fopen( Output_folder + "\\" + Output_file, "w" );
        fputs( NumToStr( startBar, 1.0, False ) + "," + NumToStr( jumlahBarDiBox, 1.0, False ) + "," +
               NumToStr( displayBoxLine, 1.0, False ) + "," + NumToStr( displayBoxColor, 1.0, False ) + "," +
               NumToStr( Linecolor, 1.0, False ) + "," + NumToStr( ColorStartBox, 1.0, False ) + ",", fh );
        fclose( fh );
        fclose( fh );
    }

    ; //end if fh

    lastBar = LastValue( BarIndex() ); //value jumlah total bar

    lArray = LLV( Low, jumlahBardiBox ); //cari Low terendah di box awal

    hArray = HHV( High, jumlahBardiBox ); //cari High tertinggi di box awal

    VierL = Larray[startBar-1];//convert lArray jadi value

    VierH = Harray[startBar-1];//convert hArray jadi value

    range = VierH - VierL;//hitung tinggi box awal


    //cari baseline box, sedekat mungkin dengan harga 0
    //berguna agar box-box di depan selalu berada dekat bar-bar harga
    baselineBoxGenap = VierL - int( VierL / range ) * range;

    baselineBoxGanjil = VierL - int( VierL / range ) * range - range / 2;

    //start Buat box awal
    x0 = -jumlahBarDiBox + startBar;

    x1 = startBar ;

    garisBawah = LineArray( x0, VierL, x1 - 1, VierL, 0 );

    garisAtas = LineArray( x0, VierH, x1 - 1, VierH, 0 );

    if ( displayBoxLine == 1 )
    {
        garisKiriBawah = LineArray( x0 - 1, VierL  + range / 2, x0, VierL , 0 );
        garisKiriAtas = LineArray( x0 - 1, VierL  + range / 2, x0, VierL  + range, 0 );
        garisKananBawah = LineArray( x1 - 1, VierL  , x1, VierL  + range / 2, 0 );
        garisKananAtas = LineArray( x1 - 1, VierL  + range, x1, VierL  + range / 2, 0 );


        Plot( garisBawah, "", LineColor, styleNoLabel );
        Plot( garisatas, "", LineColor );
        Plot( garisKiriBawah, "", LineColor, styleNoLabel  );
        Plot( garisKiriAtas, "", LineColor, styleNoLabel  );
        Plot( garisKananBawah, "", LineColor, styleNoLabel  );
        Plot( garisKananAtas, "", LineColor, styleNoLabel  );
    }

    //selesai buat box awal

    //hitung harus buat berapa box ke depan(horisontal dan vertikal)
    jumlahBoxH = int( ( LastBar - startBar ) / jumlahBarDiBox ) + 1;//jumlah box ke depan yang akan dibuat


    /*unused. replaced with new code.

    //Plot Vertical Line
        if ( displayBoxLine == 1 )
        {
            for ( k = 0;k < jumlahBoxH + 1;k++ )
            {
                x0 = -jumlahBarDiBox + startBar + k * jumlahBarDiBox;
                Plot( BarIndex() == x0, "", LineColor,  styleOwnScale + styleHistogram + styleNoLabel );
            } //end for
        }

    */

    if ( displayBoxColor == 1 )
    {
        PlotOHLC( Null, garisAtas, garisBawah, Null, "", ColorStartBox, styleCloud + styleNoLabel );
        PlotText( "Start", startBar - jumlahBarDiBox*2 / 3, VierL + Range / 2 , colorBlack, colorYellow );
    } //end if displayBoxColor

    baselineBox[0] = VierL; //akan dipakai untuk nilai y1 di box horisontal ke 1

    //buat baselineBox[boxNo] dari box horisontal pertama sampai terakhir
    for ( boxNo = 1;boxNo <= jumlahBoxH;boxNo++ )
    {
        testGanjil = frac( boxNo / 2 ) != 0;//genap jika nilainya 0

        if ( testGanjil == 0 )
        {
            baselineBox[boxNo] = BaselineBoxGenap;
        }
        else
        {
            baselineBox[boxNo] = BaselineBoxGanjil;
        } //end testGanjil
    } //end for buat baselineBox[boxNo]

    for ( boxNo = 1;boxNo < jumlahBoxH;boxNo++ ) //mulai buat box didepan sebanyak jumlahBoxH
    {

        boxL = lArray[startBar-1 + boxNo*jumlahBarDiBox];//nilai minimum harga di box no k
        boxH = hArray[startBar-1 + boxNo*jumlahBarDiBox];//nilai maksimum harga di box no k

        //hitung jumlahBoxV

        for ( i = 0;baselineBox[boxNo] + i*Range <= boxH;i++ )
        {
            jumlahBoxV = i + 1;
        }

        ClosingPriceBoxSebelumnya = C[startBar-1 + ( boxNo-1 )*jumlahBarDiBox];//harga penutupan di ujung kanan box sebelumnya.

        //Cari garis bawah terdekat dengan ClosingPriceBoxSebelumnya

        for ( i = 0;( baselineBox[boxNo-1] + i*range ) <= ClosingPriceBoxSebelumnya ;i++ )
        {
            y1 = baselineBox[BoxNo-1] + i * range;
        }

        //hitung garis bawah terdekat dengan boxL
        for ( i = 0;( baselineBox[boxNo] + i*range ) <= boxL;i++ )
        {
            y2 = baselineBox[boxNo] + i * range;
        }


        //printf( "\nClosingPriceBoxSebelumnya=" + ClosingPriceBoxSebelumnya );

        //printf( "\ny1=" + y1 );

        //printf( "\ny2=" + y2 );

        //buat Plot di box-box di depan

        for ( j = 1;j <= jumlahBoxV;j++ )
        {
            if ( ( baselineBox[boxNo] + j*range ) > boxL ) //buat plot hanya kalau dekat dengan bar
            {
                x0 = -jumlahBarDiBox + startBar + boxNo * jumlahBarDiBox;
                x1 =                      startBar + boxNo * jumlahBarDiBox;
                y = baselineBox[boxNo] + ( j - 1 ) * range;

                garisBawah = LineArray( x0, y, x1 - 1 , y, 0 );
                garisAtas = LineArray( x0, y + range, x1 - 1 , y + range, 0 );

                if ( displayBoxLine == 1 )
                {
                    garisKiriBawah = LineArray( x0 - 1, y + range / 2, x0, y, 0 );
                    garisKiriAtas = LineArray( x0 - 1, y + range / 2, x0, y + range, 0 );
                    garisKananBawah = LineArray( x1 - 1, y , x1, y + range / 2, 0 );
                    garisKananAtas = LineArray( x1 - 1, y + range, x1, y + range / 2, 0 );


                    Plot( garisBawah, "", LineColor, styleNoLabel );
                    Plot( garisatas, "", LineColor );
                    Plot( garisKiriBawah, "", LineColor, styleNoLabel  );
                    Plot( garisKiriAtas, "", LineColor, styleNoLabel  );
                    Plot( garisKananBawah, "", LineColor, styleNoLabel  );
                    Plot( garisKananAtas, "", LineColor, styleNoLabel  );
                } //end if displayBoxLine


                if ( displayBoxColor == 1 )
                {
                    ColorBoxBawah = startWarna( y1, y2, range );
                    PlotOHLC( Null, garisAtas, garisBawah, Null, "", ColorBoxBawah, styleCloud );
                    PlotText( textTrend( y1, y2, range ), x0 + jumlahBarDiBox / 3, y + Range / 2, colorBlack, colorYellow );
                    y2 = y2 + range; //rubah warna untuk box diatas box sekarang
                } //end if displayBoxColor
            } //end if dekat dengan bar
        } //end for
    } //end for


    //sekarang plot khusus di box paling kanan. Like I said, messy :(
    boxNo = jumlahBoxH; //no box yang paling kanan

    //BoxL dan BoxH di box terakhir. Very-very messy. Try and error.
    boxL = LastValue( LLV( Low, ( 1 + Offset - jumlahBarDiBox * ( jumlahBoxH - 1 ) ) ) ); //nilai minimum harga di box terakhir

    boxH = LastValue( HHV( High, ( 1 + Offset - jumlahBarDiBox * ( jumlahBoxH - 1 ) ) ) );//nilai maksimum harga di box terakhir

    //printf( "\nmundur berapa periode di bar terakhir=" + ( 1 + ( Offset - jumlahBarDiBox*( ( jumlahBoxH - 1 ) ) ) ) );

    //printf( "\nboxL=" + boxL );

    //printf( "\nboxH=" + boxH );

    //hitung jumlahBoxV
    for ( i = 0;baselineBox[boxNo] + i*Range <= boxH;i++ )
    {
        jumlahBoxV = i + 1;
    }

    ClosingPriceBoxSebelumnya = C[startBar-1 + ( boxNo-1 )*jumlahBarDiBox];//harga penutupan di ujung kanan box sebelumnya.

    //Cari garis bawah terdekat dengan ClosingPriceBoxSebelumnya

    for ( i = 0;( baselineBox[boxNo-1] + i*range ) <= ClosingPriceBoxSebelumnya ;i++ )
    {
        y1 = baselineBox[BoxNo-1] + i * range;
    }

    //hitung garis bawah terdekat dengan boxL
    for ( i = 0;( baselineBox[boxNo] + i*range ) <= boxL;i++ )
    {
        y2 = baselineBox[boxNo] + i * range;
    }


    for ( j = 1;j <= jumlahBoxV;j++ )
    {
        if ( ( baselineBox[boxNo] + j*range ) > boxL ) //buat plot hanya kalau dekat dengan bar
        {
            x0 = -jumlahBarDiBox + startBar + boxNo * jumlahBarDiBox;
            x1 =                      startBar + boxNo * jumlahBarDiBox;
            y = baselineBox[boxNo] + ( j - 1 ) * range;
            garisBawah = LineArray( x0, y, x1 - 1, y, 0 );
            garisAtas = LineArray( x0, y + range, x1 - 1, y + range, 0 );
            displace = jumlahBarDiBox - ( LastBar - x0 ) - 1;
            //printf( "\ndisplace=" + displace );

            if ( displayBoxLine == 1 )
            {
                garisKiriBawah = LineArray( x0 - 1, y + range / 2, x0, y, 0 );
                garisKiriAtas = LineArray( x0 - 1, y + range / 2, x0, y + range, 0 );
                garisKananBawah = LineArray( x1 - 1 , y , x1, y + range / 2, 0 );
                garisKananAtas = LineArray( x1 - 1 , y + range, x1, y + range / 2, 0 );
                Plot( Ref( garisbawah, displace ), "", LineColor, styleLine + styleNoLabel ,  0, 0, displace );//garis bawah box
                Plot( Ref( garisAtas, displace ), "", LineColor, styleLine , 0, 0, displace );//garis atas box
                Plot( garisKiriBawah, "", LineColor, styleNoLabel );
                Plot( garisKiriAtas, "", LineColor, styleNoLabel );
                Plot( garisKananBawah, "", LineColor, styleNoLabel ,  0, 0, displace );
                Plot( garisKananAtas, "", LineColor, styleNoLabel ,  0, 0, displace  );
            }

            if ( displayBoxColor == 1 )
            {
                ColorBoxBawah = startWarna( y1, y2, range );//
                PlotOHLC( Null, Ref( garisbawah, displace ), Ref( garisAtas, displace ), Null, "", ColorBoxBawah, styleCloud + styleNoLabel, 0, 0, displace );
                PlotText( textTrend( y1, y2, range ), x0 + jumlahBarDiBox / 3, y + Range / 2, colorBlack, colorYellow );
                y2 = y2 + range; //rubah warna untuk box diatas box sekarang
            }
        }//end if
    }//end for


    stringTitleOri = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +
                                WriteVal( V, 1.0 ) + " {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) ;

    stringVierBox = WriteIf( jumlahBarDiBox != StrToNum( strJumlahBarDiBox ) OR
                             startBar != StrToNum( strStartBar ) OR
                             displayBoxLine != StrToNum( strdisplayBoxLine ) OR
                             displayBoxColor != StrToNum( strDisplayBoxColor ) OR
                             Linecolor != StrToNum( strLineColor ) OR
                             ColorStartBox != StrToNum( strColorStartBox ),
                             EncodeColor( colorRed ) + "VierBox " + Name() + " belum disimpan." +
                             "\nUntuk menyimpan VierBox ini, masuk ke menu Parameters dan klik SAVE." +
                             "\nUntuk memanggil VierBox yang telah disimpan, masuk ke menu Parameters dan klik RESET ALL.",
                             EncodeColor( colorBlue ) + "VierBox " + Name() + " sudah disimpan" );


    Title = stringVierBox + "\n\n" + EncodeColor( colorBlack ) + stringTitleOri;

    if ( save == 1 )
    {
        Output_file = Name() + ".txt";
        Output_folder = "vierbox";
        fmkdir( Output_folder );
        fh = fopen( Output_folder + "\\" + Output_file, "w" );
        fputs( NumToStr( startBar, 1.0, False ) + "," + NumToStr( jumlahBarDiBox, 1.0, False ) + "," +
               NumToStr( displayBoxLine, 1.0, False ) + "," + NumToStr( displayBoxColor, 1.0, False ) + "," +
               NumToStr( Linecolor, 1.0, False ) + "," + NumToStr( ColorStartBox, 1.0, False ) + "," , fh );
        fclose( fh );
    } //end if

} //end if displayVierBox
 

No comments:

Post a Comment

Thanks