// Downloaded From https://www.WiseStockTrader.com
//libA.afl

//3 jan 2011



#include_once <Pebslib.afl>;

function S_SMA( fastn, slown, Closingtime )
{

    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Closingtime ) * 1;

    ttt = ( Open + High + Low + Close ) / 4;

    aaa = MA( ttt, fastn );
    bbb = MA( ttt, slown );

    Buysignal = ( aaa >= bbb );
    Sellsignal = ( aaa < bbb );

    Shortsignal = ( aaa < bbb );
    Coversignal = ( aaa >= bbb );

    Longstate = Flip( Buysignal, ( Sellsignal OR Shortsignal ) );
    Shortstate = Flip( Shortsignal, ( Coversignal OR BuySignal ) );

    Totalstate = Longstate * 1 + Shortstate * ( -1 );

    return Ref( totalstate, -xxxdelay );

}

function S_TSF( fastn, slown, Closingtime )
{

    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Closingtime ) * 1;

    ttt = ( Open + High + Low + Close ) / 4;

    aaa = TSF( ttt, fastn );
    bbb = TSF( ttt, slown );

    Buysignal = ( aaa >= bbb );
    Sellsignal = ( aaa < bbb );

    Shortsignal = ( aaa < bbb );
    Coversignal = ( aaa >= bbb );

    Longstate = Flip( Buysignal, ( Sellsignal OR Shortsignal ) );
    Shortstate = Flip( Shortsignal, ( Coversignal OR BuySignal ) );

    Totalstate = Longstate * 1 + Shortstate * ( -1 );

    return Ref( totalstate, -xxxdelay );

}


function S_KAMA( fastn, slown, Closingtime )
{

    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Closingtime ) * 1;



    aaa = kaMA( 20, 2, fastn );
    bbb = kaMA( 20, 2, slown );

    Buysignal = ( aaa >= bbb );
    Sellsignal = ( aaa < bbb );

    Shortsignal = ( aaa < bbb );
    Coversignal = ( aaa >= bbb );

    Longstate = Flip( Buysignal, ( Sellsignal OR Shortsignal ) );
    Shortstate = Flip( Shortsignal, ( Coversignal OR BuySignal ) );

    Totalstate = Longstate * 1 + Shortstate * ( -1 );

    return Ref( totalstate, -xxxdelay );

}

function S_HULL( fastn, slown, Closingtime )
{

    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Closingtime ) * 1;

    ttt = ( Open + High + Low + Close ) / 4;

    aaa = Hullma( ttt, fastn, 0 );
    bbb = Hullma( ttt, slown, 0 );

    Buysignal = ( aaa >= bbb );
    Sellsignal = ( aaa < bbb );

    Shortsignal = ( aaa < bbb );
    Coversignal = ( aaa >= bbb );

    Longstate = Flip( Buysignal, ( Sellsignal OR Shortsignal ) );
    Shortstate = Flip( Shortsignal, ( Coversignal OR BuySignal ) );

    Totalstate = Longstate * 1 + Shortstate * ( -1 );

    return Ref( totalstate, -xxxdelay );

}

function S_BRKEMA( fastn, slown, Closingtime )
{

    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Closingtime ) * 1;



    aaa = BRKEMA( fastn );
    bbb = BRKEMA( slown );

    Buysignal = ( aaa >= bbb );
    Sellsignal = ( aaa < bbb );

    Shortsignal = ( aaa < bbb );
    Coversignal = ( aaa >= bbb );

    Longstate = Flip( Buysignal, ( Sellsignal OR Shortsignal ) );
    Shortstate = Flip( Shortsignal, ( Coversignal OR BuySignal ) );

    Totalstate = Longstate * 1 + Shortstate * ( -1 );

    return Ref( totalstate, -xxxdelay );

}

function S_BRKEMA2( fastn, slown, Closingtime )
{

    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Closingtime ) * 1;



    aaa = BRKEMA2( fastn );
    bbb = BRKEMA2( slown );

    Buysignal = ( aaa >= bbb );
    Sellsignal = ( aaa < bbb );

    Shortsignal = ( aaa < bbb );
    Coversignal = ( aaa >= bbb );

    Longstate = Flip( Buysignal, ( Sellsignal OR Shortsignal ) );
    Shortstate = Flip( Shortsignal, ( Coversignal OR BuySignal ) );

    Totalstate = Longstate * 1 + Shortstate * ( -1 );

    return Ref( totalstate, -xxxdelay );

}

function S_BRKEMA3( fastn, slown, Closingtime )
{

    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Closingtime ) * 1;



    aaa = BRKEMA3( fastn );
    bbb = BRKEMA3( slown );

    Buysignal = ( aaa >= bbb );
    Sellsignal = ( aaa < bbb );

    Shortsignal = ( aaa < bbb );
    Coversignal = ( aaa >= bbb );

    Longstate = Flip( Buysignal, ( Sellsignal OR Shortsignal ) );
    Shortstate = Flip( Shortsignal, ( Coversignal OR BuySignal ) );

    Totalstate = Longstate * 1 + Shortstate * ( -1 );

    return Ref( totalstate, -xxxdelay );

}

function S_BRKEMA4( fastn, slown, Closingtime )
{

    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Closingtime ) * 1;



    aaa = BRKEMA4( fastn );
    bbb = BRKEMA4( slown );

    Buysignal = ( aaa >= bbb );
    Sellsignal = ( aaa < bbb );

    Shortsignal = ( aaa < bbb );
    Coversignal = ( aaa >= bbb );

    Longstate = Flip( Buysignal, ( Sellsignal OR Shortsignal ) );
    Shortstate = Flip( Shortsignal, ( Coversignal OR BuySignal ) );

    Totalstate = Longstate * 1 + Shortstate * ( -1 );

    return Ref( totalstate, -xxxdelay );

}

function S_FRAMA( fastn, slown, Closingtime )
{

    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Closingtime ) * 1;


    aaa = FRAMA( fastn );
    bbb = FRAMA( slown );

    Buysignal = ( aaa >= bbb );
    Sellsignal = ( aaa < bbb );

    Shortsignal = ( aaa < bbb );
    Coversignal = ( aaa >= bbb );

    Longstate = Flip( Buysignal, ( Sellsignal OR Shortsignal ) );
    Shortstate = Flip( Shortsignal, ( Coversignal OR BuySignal ) );

    Totalstate = Longstate * 1 + Shortstate * ( -1 );

    return Ref( totalstate, -xxxdelay );

}

function S_SMA_CONF( fastn, slown, Closingtime )
{

    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Closingtime ) * 1;

    ttt = ( Open + High + Low + Close ) / 4;

    aaa = MA( ttt, fastn );
    bbb = MA( ttt, slown );

    Buysignal = ( aaa >= bbb ) AND ( Close > ValueWhen( Cross( aaa, bbb ), Max( Open, Close ), 1 ) );
    Sellsignal = ( aaa < bbb ) AND ( Close < ValueWhen( Cross( bbb, aaa ), Min( Open, Close ), 1 ) );

    Shortsignal = ( aaa < bbb ) AND ( Close < ValueWhen( Cross( bbb, aaa ), Min( Open, Close ), 1 ) );
    Coversignal = ( aaa >= bbb ) AND ( Close > ValueWhen( Cross( aaa, bbb ), Max( Open, Close ), 1 ) );

    Longstate = Flip( Buysignal, ( Sellsignal OR Shortsignal ) );
    Shortstate = Flip( Shortsignal, ( Coversignal OR BuySignal ) );

    Totalstate = Longstate * 1 + Shortstate * ( -1 );

    return Ref( totalstate, -xxxdelay );

}

function S_TSF_CONF( fastn, slown, Closingtime )
{

    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Closingtime ) * 1;

    ttt = ( Open + High + Low + Close ) / 4;

    aaa = TSF( ttt, fastn );
    bbb = TSF( ttt, slown );

    Buysignal = ( aaa >= bbb ) AND ( Close > ValueWhen( Cross( aaa, bbb ), Max( Open, Close ), 1 ) );
    Sellsignal = ( aaa < bbb ) AND ( Close < ValueWhen( Cross( bbb, aaa ), Min( Open, Close ), 1 ) );

    Shortsignal = ( aaa < bbb ) AND ( Close < ValueWhen( Cross( bbb, aaa ), Min( Open, Close ), 1 ) );
    Coversignal = ( aaa >= bbb ) AND ( Close > ValueWhen( Cross( aaa, bbb ), Max( Open, Close ), 1 ) );

    Longstate = Flip( Buysignal, ( Sellsignal OR Shortsignal ) );
    Shortstate = Flip( Shortsignal, ( Coversignal OR BuySignal ) );

    Totalstate = Longstate * 1 + Shortstate * ( -1 );

    return Ref( totalstate, -xxxdelay );

}


function S_FRAMA_CONF( fastn, slown, Closingtime )
{

    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Closingtime ) * 1;

    ttt = ( Open + High + Low + Close ) / 4;

    aaa = FRAMA( fastn );
    bbb = FRAMA( slown );

    Buysignal = ( aaa >= bbb ) AND ( Close > ValueWhen( Cross( aaa, bbb ), Max( Open, Close ), 1 ) );
    Sellsignal = ( aaa < bbb ) AND ( Close < ValueWhen( Cross( bbb, aaa ), Min( Open, Close ), 1 ) );

    Shortsignal = ( aaa < bbb ) AND ( Close < ValueWhen( Cross( bbb, aaa ), Min( Open, Close ), 1 ) );
    Coversignal = ( aaa >= bbb ) AND ( Close > ValueWhen( Cross( aaa, bbb ), Max( Open, Close ), 1 ) );

    Longstate = Flip( Buysignal, ( Sellsignal OR Shortsignal ) );
    Shortstate = Flip( Shortsignal, ( Coversignal OR BuySignal ) );

    Totalstate = Longstate * 1 + Shortstate * ( -1 );

    return Ref( totalstate, -xxxdelay );

}

function S_FRAMA_CONF_SMB( fastn, slown, Closingtime, symbol )
{

    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Closingtime ) * 1;

    Ooo = Foreign( symbol, "Open" );
    Hhh = Foreign( symbol, "High" );
    Lll = Foreign( symbol, "Low" );
    Ccc = Foreign( symbol, "Close" );

    ttt = ( Ooo + Hhh + Lll + Ccc ) / 4;

    aaa = FRAMA_SMB( fastn, symbol );
    bbb = FRAMA_SMB( slown, symbol );

    Buysignal = ( aaa >= bbb ) AND ( Ccc > ValueWhen( Cross( aaa, bbb ), Max( Ooo, Ccc ), 1 ) );
    Sellsignal = ( aaa < bbb ) AND ( Ccc < ValueWhen( Cross( bbb, aaa ), Min( Ooo, Ccc ), 1 ) );

    Shortsignal = ( aaa < bbb ) AND ( Ccc < ValueWhen( Cross( bbb, aaa ), Min( Ooo, Ccc ), 1 ) );
    Coversignal = ( aaa >= bbb ) AND ( Ccc > ValueWhen( Cross( aaa, bbb ), Max( Ooo, Ccc ), 1 ) );

    Longstate = Flip( Buysignal, ( Sellsignal OR Shortsignal ) );
    Shortstate = Flip( Shortsignal, ( Coversignal OR BuySignal ) );

    Totalstate = Longstate * 1 + Shortstate * ( -1 );

    return Ref( totalstate, -xxxdelay );

}



function S_KAMA_CONF( fastn, slown, Closingtime )
{

    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Closingtime ) * 1;

    ttt = ( Open + High + Low + Close ) / 4;

    aaa = kaMA( 20, 2, fastn );
    bbb = kaMA( 20, 2, slown );

    Buysignal = ( aaa >= bbb ) AND ( Close > ValueWhen( Cross( aaa, bbb ), Max( Open, Close ), 1 ) );
    Sellsignal = ( aaa < bbb ) AND ( Close < ValueWhen( Cross( bbb, aaa ), Min( Open, Close ), 1 ) );

    Shortsignal = ( aaa < bbb ) AND ( Close < ValueWhen( Cross( bbb, aaa ), Min( Open, Close ), 1 ) );
    Coversignal = ( aaa >= bbb ) AND ( Close > ValueWhen( Cross( aaa, bbb ), Max( Open, Close ), 1 ) );

    Longstate = Flip( Buysignal, ( Sellsignal OR Shortsignal ) );
    Shortstate = Flip( Shortsignal, ( Coversignal OR BuySignal ) );

    Totalstate = Longstate * 1 + Shortstate * ( -1 );

    return Ref( totalstate, -xxxdelay );

}

function S_HULL_CONF( fastn, slown, Closingtime )
{

    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Closingtime ) * 1;

    ttt = ( Open + High + Low + Close ) / 4;

    aaa = Hullma( ttt, fastn, 0 );
    bbb = Hullma( ttt, slown, 0 );

    Buysignal = ( aaa >= bbb ) AND ( Close > ValueWhen( Cross( aaa, bbb ), Max( Open, Close ), 1 ) );
    Sellsignal = ( aaa < bbb ) AND ( Close < ValueWhen( Cross( bbb, aaa ), Min( Open, Close ), 1 ) );

    Shortsignal = ( aaa < bbb ) AND ( Close < ValueWhen( Cross( bbb, aaa ), Min( Open, Close ), 1 ) );
    Coversignal = ( aaa >= bbb ) AND ( Close > ValueWhen( Cross( aaa, bbb ), Max( Open, Close ), 1 ) );

    Longstate = Flip( Buysignal, ( Sellsignal OR Shortsignal ) );
    Shortstate = Flip( Shortsignal, ( Coversignal OR BuySignal ) );

    Totalstate = Longstate * 1 + Shortstate * ( -1 );

    return Ref( totalstate, -xxxdelay );

}

function S_HULL_CONF_MaxGAP( fastn, slown, Closingtime, gap )
{

    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Closingtime ) * 1;

    ttt = ( Open + High + Low + Close ) / 4;

    aaa = Hullma( ttt, fastn, 0 );
    bbb = Hullma( ttt, slown, 0 );

    Buysignal = ( aaa >= bbb ) AND ( Close > ValueWhen( Cross( aaa, bbb ), Max( Open, Close ), 1 ) ) AND ( aaa <= ( 1 + gap ) * bbb );
    Sellsignal = ( aaa < bbb ) AND ( Close < ValueWhen( Cross( bbb, aaa ), Min( Open, Close ), 1 ) );

    Shortsignal = ( aaa < bbb ) AND ( Close < ValueWhen( Cross( bbb, aaa ), Min( Open, Close ), 1 ) ) AND ( aaa >= ( 1 - gap ) * aaa );
    Coversignal = ( aaa >= bbb ) AND ( Close > ValueWhen( Cross( aaa, bbb ), Max( Open, Close ), 1 ) );

    Longstate = Flip( Buysignal, ( Sellsignal OR Shortsignal ) );
    Shortstate = Flip( Shortsignal, ( Coversignal OR BuySignal ) );

    Totalstate = Longstate * 1 + Shortstate * ( -1 );

    return Ref( totalstate, -xxxdelay );

}


function S_HULL_BRKEMA_CONF( fastn, slown, Closingtime, BRKn )
{

    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Closingtime ) * 1;

    ttt = brkema( BRKn );

    aaa = Hullma( ttt, fastn, 0 );
    bbb = Hullma( ttt, slown, 0 );

    Buysignal = ( aaa >= bbb ) AND ( Close > ValueWhen( Cross( aaa, bbb ), Max( Open, Close ), 1 ) );
    Sellsignal = ( aaa < bbb ) AND ( Close < ValueWhen( Cross( bbb, aaa ), Min( Open, Close ), 1 ) );

    Shortsignal = ( aaa < bbb ) AND ( Close < ValueWhen( Cross( bbb, aaa ), Min( Open, Close ), 1 ) );
    Coversignal = ( aaa >= bbb ) AND ( Close > ValueWhen( Cross( aaa, bbb ), Max( Open, Close ), 1 ) );

    Longstate = Flip( Buysignal, ( Sellsignal OR Shortsignal ) );
    Shortstate = Flip( Shortsignal, ( Coversignal OR BuySignal ) );

    Totalstate = Longstate * 1 + Shortstate * ( -1 );

    return Ref( totalstate, -xxxdelay );

}

function HIn()
{

    for ( Herhaling = 0;Herhaling < BarCount;Herhaling++ )
    {
        Herhaling2 = Herhaling;

        do
        {
            Herhaling2--;


        }
        while ( ( Herhaling2 > 0 ) AND ( H[Herhaling2] < H[Herhaling] ) );

        nnn[Herhaling] = Herhaling - Herhaling2;
    }


    return nnn;

}

function HIn_SMB( symbol )
{

    Hhh = Foreign( symbol, "High" );

    for ( Herhaling = 0;Herhaling < BarCount;Herhaling++ )
    {
        Herhaling2 = Herhaling;

        do
        {
            Herhaling2--;


        }
        while ( ( Herhaling2 > 0 ) AND ( Hhh[Herhaling2] < Hhh[Herhaling] ) );

        nnn[Herhaling] = Herhaling - Herhaling2;
    }


    return nnn;

}


function LOn()
{

    for ( Herhaling = 0;Herhaling < BarCount;Herhaling++ )
    {
        Herhaling2 = Herhaling;

        do
        {
            Herhaling2--;


        }
        while ( ( Herhaling2 > 0 ) AND ( L[Herhaling2] > L[Herhaling] ) );

        nnn[Herhaling] = Herhaling - Herhaling2;
    }


    return nnn;

}

function LOn_SMB( symbol )
{

    Lll = Foreign( symbol, "Low" );

    for ( Herhaling = 0;Herhaling < BarCount;Herhaling++ )
    {
        Herhaling2 = Herhaling;

        do
        {
            Herhaling2--;


        }
        while ( ( Herhaling2 > 0 ) AND ( Lll[Herhaling2] > Lll[Herhaling] ) );

        nnn[Herhaling] = Herhaling - Herhaling2;
    }


    return nnn;

}


function MAjorpeak( n )
{

    Hhh = HIn();
    xxx = ValueWhen( ( ( Hhh >= n ) AND ( Hhh >= Ref( Hhh, -1 ) ) AND ( Hhh >= Ref( Hhh, -1 ) ) ), H, 1 );

    return xxx;
}

function MAjorpeak_SMB( n, symbol )
{

    Hhh = HIn_SMB( symbol );
    xxx = ValueWhen( ( ( Hhh >= n ) AND ( Hhh >= Ref( Hhh, -1 ) ) AND ( Hhh >= Ref( Hhh, -1 ) ) ), H, 1 );

    return xxx;
}


function MAjorlow( n )
{
    Lll = Lon();
    xxx = ValueWhen( ( ( Lll >= n ) AND ( Lll >= Ref( Lll, -1 ) ) AND ( Lll >= Ref( Lll, -1 ) ) ), L, 1 );

    return xxx;
}

function MAjorlow_SMB( n, symbol )
{
    Lll = Lon_SMB( symbol );
    xxx = ValueWhen( ( ( Lll >= n ) AND ( Lll >= Ref( Lll, -1 ) ) AND ( Lll >= Ref( Lll, -1 ) ) ), L, 1 );

    return xxx;
}


function MAjorpeakAbove( n, X )
{

    Hhh = HIn();
    xxx = ValueWhen( ( ( H > X ) AND ( Hhh >= n ) AND ( Hhh >= Ref( Hhh, -1 ) ) AND ( Hhh >= Ref( Hhh, -1 ) ) ), H, 1 );

    return xxx;

}

function MAjorlowbelow( n, X )
{
    Lll = Lon();
    xxx = ValueWhen( ( ( L < X ) AND ( Lll >= n ) AND ( Lll >= Ref( Lll, -1 ) ) AND ( Lll >= Ref( Lll, -1 ) ) ), L, 1 );

    return xxx;
}

function MAjorpeakpotential( n )
{

    xxx = MAjorpeakabove( n, MAjorpeak( n ) ) - MAjorpeak( n );

    return xxx;

}

function MAjorlowpotential( n )
{

    xxx = MAjorlowbelow( n, MAjorlow( n ) ) - MAjorlow( n );

    return xxx;

}




function TradingATR()
{

    TimeFrameSet( inDaily );
    aaa = ATR( 10 );
    TimeFrameRestore();
    aaa = TimeFrameExpand( aaa, inDaily );

    return aaa;


}

function Varema( Varn )
{

    ttt = tradingatr();
    xxx = int( ( 1000 - ttt ) / Varn );

    xxx = IIf( xxx > 100, 100, IIf( xxx < 10, 10, xxx ) );

    eee = AMA( ( O + H + L + C ) / 4, 2 / xxx );

    return eee;

}


function Onesidedness()
{

    TimeFrameSet( inHourly );
    xxx = BarsSince( Cross( C, EMA( C, 85 ) ) OR Cross( EMA( C, 85 ), C ) );
    TimeFrameRestore();
    xxx = TimeFrameExpand( xxx, inHourly );

    return xxx;

}

function s_hilobrk( n, Closingtime )
{

    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Closingtime ) * 1;


    Hhh = MAjorpeak( n );
    LLL = MAjorlow( n );


    Buysignal = C > Ref( Hhh, -1 );
    Sellsignal = C < Ref( LLL, -1 );

    Shortsignal = C < Ref( LLL, -1 );
    Coversignal = C > Ref( Hhh, -1 );

//Buysignal = ExRem(Buysignal,Shortsignal);
//Shortsignal = ExRem(Shortsignal,Buysignal);

    Longstate = Flip( Buysignal, ( Sellsignal OR Shortsignal ) );
    Shortstate = Flip( Shortsignal, ( Coversignal OR BuySignal ) );

    xxx = Longstate * 1 + Shortstate * ( -1 );
    Totalstate = Ref( xxx, -xxxdelay );


    return totalstate;



}

function s_hilobrk_smb( n, Closingtime, symbol )
{

    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Closingtime ) * 1;


    Hhh = MAjorpeak_SMB( n, symbol );
    LLL = MAjorlow_SMB( n, symbol );


    Buysignal = C > Ref( Hhh, -1 );
    Sellsignal = C < Ref( LLL, -1 );

    Shortsignal = C < Ref( LLL, -1 );
    Coversignal = C > Ref( Hhh, -1 );

//Buysignal = ExRem(Buysignal,Shortsignal);
//Shortsignal = ExRem(Shortsignal,Buysignal);

    Longstate = Flip( Buysignal, ( Sellsignal OR Shortsignal ) );
    Shortstate = Flip( Shortsignal, ( Coversignal OR BuySignal ) );

    xxx = Longstate * 1 + Shortstate * ( -1 );
    Totalstate = Ref( xxx, -xxxdelay );


    return totalstate;



}


function ContextZone_60()
{
    Conzone = 0;
    TimeFrameSet( inHourly );

    ult = xxx_ult();

    TimeFrameRestore();

    ult = TimeFrameExpand( ult, inHourly );



    band1 = Param( "band1", 600, 0, 2000, 1 );
    band2 = Param( "band2", 250, 0, 2000, 1 );
    band3 = Param( "band3", 250, 0, 2000, 1 );

    eee = EMA60( 85 );

    for ( i = 1; i < BarCount; i++ )
    {

        if ( ( C[i-1] > eee[i-1] + band1 )*( ult[i] > 0 ) )
            Conzone[i] = 1;
        else
            if ( ( C[i-1] > eee[i-1] + band2 )*( ult[i] > 0 ) )
                Conzone[i] = 2;
            else
                if ( ( C[i-1] > eee[i-1] )*( ult[i] > 0 ) )
                    Conzone[i] = 3;
                else
                    if ( ( C[i-1] > eee[i-1] - band3 )*( ult[i] > 0 ) )
                        Conzone[i] = 4;
                    else
                        if ( ult[i] > 0 )
                            Conzone[i] = 5;
                        else
                            if ( ( C[i-1] < eee[i-1] - band1 )*( ult[i] < 0 ) )
                                Conzone[i] = -1;
                            else
                                if ( ( C[i-1] < eee[i-1] - band2 )*( ult[i] < 0 ) )
                                    Conzone[i] = -2;
                                else
                                    if ( ( C[i-1] < eee[i-1] )*( ult[i] < 0 ) )
                                        Conzone[i] = -3;
                                    else
                                        if ( ( C[i-1] < eee[i-1] + band3 )*( ult[i] < 0 ) )
                                            Conzone[i] = -4;
                                        else
                                            if ( ult[i] < 0 )
                                                Conzone[i] = -5;
    }

    return Conzone;
}

function Contextfilter_60( s1 )
{

    Ccc = Contextzone_60();

    for ( i = 0; i < BarCount; i++ )
    {

        if ( Ccc[i] == 1 )
        {
            if ( s1[i] > 0 )
                s1[i] = 0;
        }

        if ( Ccc[i] == 2 )
        {
            if ( ( s1[i] > 0 )AND( s1[i-1] < 0 ) )
                s1[i] = 0;

            if ( ( s1[i-1] >= 0 ) AND ( s1[i] > s1[i-1] ) )
                s1[i] = s1[i-1];
        }

        if ( Ccc[i] == 4 )
        {
            if ( ( s1[i-1] <= 0 ) AND ( s1[i] < s1[i-1] ) )
                s1[i] = s1[i-1];

            if ( ( s1[i] < 0 ) AND ( s1[i-1] > 0 ) )
                s1[i] = 0;
        }

        if ( Ccc[i] == 5 )
        {
            if ( s1[i] < 0 )
                s1[i] = 0;
        }

//now bearish scenario
        if ( Ccc[i] == -1 )
        {
            if ( s1[i] < 0 )
                s1[i] = 0;
        }

        if ( Ccc[i] == -2 )
        {
            if ( ( s1[i] < 0 )AND( s1[i-1] > 0 ) )
                s1[i] = 0;

            if ( ( s1[i-1] <= 0 ) AND ( s1[i] < s1[i-1] ) )
                s1[i] = s1[i-1];
        }

        if ( Ccc[i] == -4 )
        {
            if ( ( s1[i-1] >= 0 ) AND ( s1[i] > s1[i-1] ) )
                s1[i] = s1[i-1];

            if ( ( s1[i] > 0 ) AND ( s1[i-1] < 0 ) )
                s1[i] = 0;
        }

        if ( Ccc[i] == -5 )
        {
            if ( s1[i] > 0 )
                s1[i] = 0;
        }

    }

    return s1;
}

function Contextfilter2_60( s1 )
{

    Ccc = Contextzone_60();

    for ( i = 0; i < BarCount; i++ )
    {
//now in cz=1 or 2 do not allow number of contracts to be reduced or shorts to be increasedd
        if ( Ccc[i] == 1 )
        {
            if ( s1[i] < s1[i-1] )
                s1[i] = s1[i-1];

            if ( s1[i] > 0 )
                s1[i] = 0;

        }

        if ( Ccc[i] == 2 )
        {
            if ( s1[i] < s1[i-1] )
                s1[i] = s1[i-1];

            if ( ( s1[i] > 0 )AND( s1[i-1] < 0 ) )
                s1[i] = 0;

            if ( ( s1[i-1] >= 0 ) AND ( s1[i] > s1[i-1] ) )
                s1[i] = s1[i-1];
        }

        if ( Ccc[i] == 4 )
        {
            if ( ( s1[i-1] <= 0 ) AND ( s1[i] < s1[i-1] ) )
                s1[i] = s1[i-1];

            if ( ( s1[i] < 0 ) AND ( s1[i-1] > 0 ) )
                s1[i] = 0;
        }

        if ( Ccc[i] == 5 )
        {
            if ( s1[i] < 0 )
                s1[i] = 0;
        }

//now bearish scenario
//now in cz=-1 or -2 do not allow number of contracts to be increased or shorts to be reduced

        if ( Ccc[i] == -1 )
        {
            if ( s1[i] > s1[i-1] )
                s1[i] = s1[i-1];

            if ( s1[i] < 0 )
                s1[i] = 0;
        }

        if ( Ccc[i] == -2 )
        {
            if ( s1[i] > s1[i-1] )
                s1[i] = s1[i-1];

            if ( ( s1[i] < 0 )AND( s1[i-1] > 0 ) )
                s1[i] = 0;

            if ( ( s1[i-1] <= 0 ) AND ( s1[i] < s1[i-1] ) )
                s1[i] = s1[i-1];
        }

        if ( Ccc[i] == -4 )
        {
            if ( ( s1[i-1] >= 0 ) AND ( s1[i] > s1[i-1] ) )
                s1[i] = s1[i-1];

            if ( ( s1[i] > 0 ) AND ( s1[i-1] < 0 ) )
                s1[i] = 0;
        }

        if ( Ccc[i] == -5 )
        {
            if ( s1[i] > 0 )
                s1[i] = 0;
        }

    }

    return s1;
}

function CreateEquityCurve( S1, PointValue )
{


    Cashflow = IIf( IsNull( Ref( s1, -1 ) ), -s1 * Open, ( Ref( s1, -1 ) - s1 ) * Open );
    Cumcashflow = Cum( Cashflow );
    Valuecontracts = s1 * Close;
    PortEquity = ( cumCashflow + Valuecontracts ) * PointValue;
//YestEquity= ValueWhen(Day()!=Ref(Day(),-1),Ref(PortEquity,-1),1);
//MonthstartEquity = ValueWhen(Month()!=Ref(Month(),-1),Ref(PortEquity,-1),1);
//TradeEquity = portequity-ValueWhen( sign(s1)!=sign(Ref(s1,-1)),Ref(s1,-1),1);



    s2 = portequity;


    return s2;
}

function BRKContextZone()
{

    z1 = Param( "z1", 0.05, 0.01, 0.4, 0.05 );
    z2 = Param( "z2", 0.05, 0.01, 0.4, 0.05 );

    Conzone = 0;
    TimeFrameSet( inHourly );
    rrrtop = MAjorpeak( 200 );
    rrrbottom = MAjorlow( 200 );
    TimeFrameRestore();

    rrrtop = TimeFrameExpand( rrrtop, inHourly );
    rrrbottom = TimeFrameExpand( rrrbottom, inHourly );


    band1 = rrrbottom + ( 1 - z1 ) * ( rrrtop - rrrbottom );
    band2 = rrrbottom + ( 1 - z2 ) * ( rrrtop - rrrbottom );
    band3 = rrrbottom + z2 * ( rrrtop - rrrbottom );
    band4 = rrrbottom + z1 * ( rrrtop - rrrbottom );


    for ( i = 1; i < BarCount; i++ )
    {

        if ( C[i-1] > band1[i-1] )
            Conzone[i] = 1;
        else
            if ( C[i-1] > band2[i-1] )
                Conzone[i] = 2;
            else
                if ( C[i-1] < band4[i-1] )
                    Conzone[i] = 5;
                else
                    if ( C[i-1] < band3[i-1] )
                        Conzone[i] = 4;
                    else
                        Conzone[i] = 3;
    }



    return Conzone;
}

function BRKContextfilter( s1 )
{

    Ccc = BRKContextzone();

    for ( i = 0; i < BarCount; i++ )
    {

        if ( Ccc[i] == 1 )
        {
            if ( s1[i] < s1[i-1] )
                s1[i] = s1[i-1];
        }

        if ( Ccc[i] == 2 )
        {
            if ( s1[i] < 0 )
                s1[i] = 0;
        }

        if ( Ccc[i] == 5 )
        {
            if ( s1[i] > s1[i-1] )
                s1[i] = s1[i-1];

        }

        if ( Ccc[i] == 4 )
        {
            if ( s1[i] > 0 )
                s1[i] = 0;
        }

    }

    return s1;
}

function PebsRVI()
{
//own modified "RVI" type indicator

    periods = Param( "RVI Periods", 14, 1, 200, 1 );
    SdevPer = Param( "STD Deviation periods", 10, 1, 200, 1 );

    StH = StDev( H, SdevPer );
    StL = StDev( L, SdevPer );

    RVIDownH[0] = 0;
    RVIUpH[0] = 0;

    RVIDownL[0] = 0;
    RVIUpL[0] = 0;


    for ( i = 1;i < BarCount;i++ )
    {

        RVIDownH[i] = ( ( RVIDownH[i-1] * ( periods - 1 ) ) + IIf( H[i] < H[i-1], Nz( StH[i] ), 0 ) ) / Periods;
        RVIUpH[i] = ( ( RVIUpH[i-1] * ( periods - 1 ) ) + IIf( H[i] > H[i-1], Nz( StH[i] ), 0 ) ) / Periods;

        RVIDownL[i] = ( ( RVIDownL[i-1] * ( periods - 1 ) ) + IIf( L[i] < L[i-1], Nz( StL[i] ), 0 ) ) / Periods;
        RVIUpL[i] = ( ( RVIUpL[i-1] * ( periods - 1 ) ) + IIf( L[i] > L[i-1], Nz( StL[i] ), 0 ) ) / Periods;

    }


    RVIH = ( 100 * RVIUpH ) / ( RVIUpH + RVIDownH );

    RVIL = ( 100 * RVIUpL ) / ( RVIUpL + RVIDownL );

    RVIall = ( RVIL + RVIH ) / 2;

    return RVIall;

}

function HenkRVI()
{
    Periods = Param( "RVI Periods", 30, -50, 200 );
    StdevPeriod = Param( "Stdev Periods", 10, -50, 200 );

    ArrayRocHigh = ROC( H, 1 );
    ArrayRocLow = ROC( L, 1 );
    ArrayStdevHigh = StDev( H, StdevPeriod );
    ArrayStdevLow = StDev( L, StdevPeriod );
    ArrayRVI[StDevPeriod-1] = 50;


    for ( i = StdevPeriod;i < BarCount;i++ )
    {
        Prev = ArrayRVI[i-1];
        factor = PREV * ( Periods - 1 );

        DownH = IIf( ArrayRocHigh[i] < 0, ArrayStdevHigh[i], 0 );
        UpH = IIf( ArrayRocHigh[i] > 0, ArrayStdevHigh[i], 0 );

        RVIDownH = ( factor + DownH ) / Periods;
        RVIUpH = ( factor + UpH ) / Periods;

        RVIH = ( 100 * RVIUpH ) / ( RVIUpH + RVIDownH );


        DownL = IIf( ArrayRocLow[i] < 0, ArrayStdevHigh[i], 0 );
        UpL = IIf( ArrayRocLow[i] > 0, ArrayStdevHigh[i], 0 );

        RVIDownL = ( factor + DownL ) / Periods;
        RVIUpL = ( factor + UpL ) / Periods;

        RVIL = ( 100 * RVIUpL ) / ( RVIUpL + RVIDownL );
        RVIall = ( RVIL + RVIH ) / 2;
        ArrayRVI[i] = RVIH;
    }

    return ArrayRVI;
}

function TradeEquityCurve( s1, pvalue )
{

    e1 = CreateEquitycurve( s1, pvalue );

//YestEquity= ValueWhen(Day()!=Ref(Day(),-1),Ref(PortEquity,-1),1);
//MonthstartEquity = ValueWhen(Month()!=Ref(Month(),-1),Ref(PortEquity,-1),1);
    TradeEquity = e1 - ValueWhen( sign( s1 ) != sign( Ref( s1, -1 ) ), Ref( e1, -1 ), 1 );

    return TradeEquity;


}

function DayEquityCurve( s1, pvalue )
{

    e1 = CreateEquitycurve( s1, pvalue );

//YestEquity= ValueWhen(Day()!=Ref(Day(),-1),Ref(PortEquity,-1),1);
//MonthstartEquity = ValueWhen(Month()!=Ref(Month(),-1),Ref(PortEquity,-1),1);
    DayEquity = e1 - ValueWhen( Day() != Ref( Day(), -1 ), Ref( e1, -1 ), 1 );

    return DayEquity;


}

function MonthEquityCurve( s1, pvalue )
{

    e1 = CreateEquitycurve( s1, pvalue );

//YestEquity= ValueWhen(Day()!=Ref(Day(),-1),Ref(PortEquity,-1),1);
//MonthstartEquity = ValueWhen(Month()!=Ref(Month(),-1),Ref(PortEquity,-1),1);
    MonthEquity = e1 - ValueWhen( Month() != Ref( Month(), -1 ), Ref( e1, -1 ), 1 );

    return MonthEquity;


}



function TradeSLSP( s1, Pvalue, SL, SP, firststoptime, Laststoptime, Lastticktime )
{
//THis takes series s1 of pos sizes and applies a Stoploss and stopprofit per trade, pvalue is the pointvalue, stops only applied within the time limits
    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Lastticktime );
    TradeEquity = TradeEquityCurve( s1, pvalue );


    newtrade = IIf( ( abs( s1 ) != 0 ) AND ( s1 * Ref( s1, -1 ) <= 0 ), True, False );

    profitstop = IIf( ( Ref( TradeEquity, -xxxdelay ) >= SP ) AND ( pebstime >= firststoptime ) AND ( pebstime <= Laststoptime ), True, False );
    Losstop = IIf( ( Ref( TradeEquity, -xxxdelay ) <= -SL ) AND ( pebstime >= firststoptime ) AND ( pebstime <= Laststoptime ), True, False );

    mult = Flip( newtrade, ( profitstop OR Losstop ) AND NOT newtrade );

//s2=s1;
    s2 = s1 * mult;


    return s2;

}

function DaySLSP( s1, Pvalue, SL, SP, firststoptime, Laststoptime, Lastticktime )
{
//THis takes series s1 of pos sizes and applies a Stoploss and stopprofit per day, pvalue is the pointvalue, stops only applied within the time limits
    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Lastticktime );

    DayEquity = DayEquityCurve( s1, pvalue );


    newDay = IIf( Day() != Ref( Day(), -1 ), True, False );
    newDay[0] = True;
    profitstop = IIf( ( Ref( DayEquity, -xxxdelay ) >= SP ) AND ( pebstime >= firststoptime ) AND ( pebstime <= Laststoptime ), True, False );
    Losstop = IIf( ( Ref( DayEquity, -xxxdelay ) <= -SL ) AND ( pebstime >= firststoptime ) AND ( pebstime <= Laststoptime ), True, False );

    mult = Flip( newDay, ( profitstop OR Losstop ) AND NOT newDay );

//s2=s1;
    s2 = s1 * mult;


    return s2;

}

function MonthSLSP( s1, Pvalue, SL, SP, firststoptime, Laststoptime, Lastticktime )
{
//THis takes series s1 of pos sizes and applies a Stoploss and stopprofit per day, pvalue is the pointvalue, stops only applied within the time limits
    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Lastticktime );

    MonthEquity = MonthEquityCurve( s1, pvalue );


    newMonth = IIf( Month() != Ref( Month(), -1 ), True, False );
    newMonth[0] = True;
    profitstop = IIf( ( Ref( MonthEquity, -xxxdelay ) >= SP ) AND ( pebstime >= firststoptime ) AND ( pebstime <= Laststoptime ), True, False );
    Losstop = IIf( ( Ref( MonthEquity, -xxxdelay ) <= -SL ) AND ( pebstime >= firststoptime ) AND ( pebstime <= Laststoptime ), True, False );

    mult = Flip( newMonth, ( profitstop OR Losstop ) AND NOT newMonth );

//s2=s1;
    s2 = s1 * mult;


    return s2;

}