Stock Portfolio Organizer

The ultimate porfolio management solution.

Shares, Margin, CFD's, Futures and Forex
EOD and Realtime
Dividends and Trust Distributions
And Much More ....
For Portfolio Manager Click Here

WiseTrader Toolbox

#1 Selling Amibroker Plugin featuring:

Advanced Adaptive Indicators
Advanced Pattern Exploration
Neural Networks
And Much More ....
Find Out More Here

Pebslib.afl for Amibroker (AFL)

Copy & Paste Friendly
//Pebslib.afl

//3 Jan 2011

function XXX_ST()
{

    e1 = EMA( C, 3 );
    e2 = EMA( C, 5 );
    e3 = EMA( C, 8 );
    e4 = EMA( C, 10 );
    e5 = EMA( C, 12 );
    e6 = EMA( C, 15 );


    rrr = ( e1 - e2 ) ^ 2 + ( e2 - e3 ) ^ 2 + ( e3 - e4 ) ^ 2 + ( e4 - e5 ) ^ 2 + ( e5 - e6 ) ^ 2;
    rrr = rrr / ( ATR( 100 ) / 100 ) ^ 2;
    rrr = rrr * IIf( e3 > e4, 1, -1 );

    return rrr;

}

function XXX_LT()
{

    e1 = EMA( C, 30 );
    e2 = EMA( C, 35 );
    e3 = EMA( C, 40 );
    e4 = EMA( C, 45 );
    e5 = EMA( C, 50 );
    e6 = EMA( C, 60 );


    sss = ( e1 - e2 ) ^ 2 + ( e2 - e3 ) ^ 2 + ( e3 - e4 ) ^ 2 + ( e4 - e5 ) ^ 2 + ( e5 - e6 ) ^ 2;
    sss = sss / ( ATR( 100 ) / 100 ) ^ 2;
    sss = sss * IIf( e3 > e4, 1, -1 );

    return sss;

}

function XXX_ULT()
{

    e1 = EMA( C, 60 );
    e2 = EMA( C, 70 );
    e3 = EMA( C, 80 );
    e4 = EMA( C, 90 );
    e5 = EMA( C, 100 );
    e6 = EMA( C, 120 );


    ttt = ( e1 - e2 ) ^ 2 + ( e2 - e3 ) ^ 2 + ( e3 - e4 ) ^ 2 + ( e4 - e5 ) ^ 2 + ( e5 - e6 ) ^ 2;
    ttt = ttt / ( ATR( 100 ) / 100 ) ^ 2;
    ttt = ttt * IIf( e3 > e4, 1, -1 );

    return ttt;

}

function XXX_ULT60()
{

    TimeFrameSet( inHourly );
    e1 = EMA( C, 60 );
    e2 = EMA( C, 70 );
    e3 = EMA( C, 80 );
    e4 = EMA( C, 90 );
    e5 = EMA( C, 100 );
    e6 = EMA( C, 120 );
    aaa = ATR( 100 );
    TimeFrameRestore();
    e1 = TimeFrameExpand( e1, inHourly );
    e2 = TimeFrameExpand( e2, inHourly );
    e3 = TimeFrameExpand( e3, inHourly );
    e4 = TimeFrameExpand( e4, inHourly );
    e5 = TimeFrameExpand( e5, inHourly );
    e6 = TimeFrameExpand( e6, inHourly );
    aaa = TimeFrameExpand( aaa, inHourly );


    e1 = e1 * 59 / 61 + C * 2 / 61;
    e2 = e2 * 69 / 71 + C * 2 / 71;
    e3 = e3 * 79 / 81 + C * 2 / 81;
    e4 = e4 * 89 / 91 + C * 2 / 91;
    e5 = e5 * 99 / 101 + C * 2 / 101;
    e6 = e6 * 119 / 121 + C * 2 / 121;

    ttt = ( e1 - e2 ) ^ 2 + ( e2 - e3 ) ^ 2 + ( e3 - e4 ) ^ 2 + ( e4 - e5 ) ^ 2 + ( e5 - e6 ) ^ 2;
    ttt = ttt / ( aaa / 100 ) ^ 2;
    ttt = ttt * IIf( e3 > e4, 1, -1 );

    return ttt;

}

function XXX_LT60()
{

    TimeFrameSet( inHourly );
    e1 = EMA( C, 30 );
    e2 = EMA( C, 35 );
    e3 = EMA( C, 40 );
    e4 = EMA( C, 45 );
    e5 = EMA( C, 50 );
    e6 = EMA( C, 60 );
    aaa = ATR( 100 );
    TimeFrameRestore();
    e1 = TimeFrameExpand( e1, inHourly );
    e2 = TimeFrameExpand( e2, inHourly );
    e3 = TimeFrameExpand( e3, inHourly );
    e4 = TimeFrameExpand( e4, inHourly );
    e5 = TimeFrameExpand( e5, inHourly );
    e6 = TimeFrameExpand( e6, inHourly );
    aaa = TimeFrameExpand( aaa, inHourly );


    e1 = e1 * 29 / 31 + C * 2 / 31;
    e2 = e2 * 34 / 36 + C * 2 / 36;
    e3 = e3 * 39 / 41 + C * 2 / 41;
    e4 = e4 * 44 / 46 + C * 2 / 46;
    e5 = e5 * 49 / 51 + C * 2 / 51;
    e6 = e6 * 59 / 61 + C * 2 / 61;

    ttt = ( e1 - e2 ) ^ 2 + ( e2 - e3 ) ^ 2 + ( e3 - e4 ) ^ 2 + ( e4 - e5 ) ^ 2 + ( e5 - e6 ) ^ 2;
    ttt = ttt / ( aaa / 100 ) ^ 2;
    ttt = ttt * IIf( e3 > e4, 1, -1 );

    return ttt;

}


function EMAdaily( n )
{
    TimeFrameSet( inDaily );
    eee = EMA( C, n );
    TimeFrameRestore();
    eee = TimeFrameExpand( eee, inDaily );

    eee = ( n - 1 ) / ( n + 1 ) * eee + 2 / ( n + 1 ) * C;

    return eee;
}

function EMA30( n )
{

    TimeFrameSet( 2*in15Minute );
    e1 = EMA( C, n );
    TimeFrameRestore();
    e1 = TimeFrameExpand( e1, 2 * in15Minute );

    e1 = e1 * ( n - 1 ) / ( n + 1 ) + C * 2 / ( n + 1 );

    return e1;


}

function EMA60( n )
{

    TimeFrameSet( inHourly );
    ttt = C; //ParamField("Field");
    e1 = EMA( ttt, n );
    TimeFrameRestore();
    e1 = TimeFrameExpand( e1, inHourly );

    e1 = e1 * ( n - 1 ) / ( n + 1 ) + ttt * 2 / ( n + 1 );

    return e1;


}

function Signal60()
{

    TimeFrameSet( inHourly );
    eee12 = EMA( C, 12 );
    eee26 = EMA( C, 26 );
    sss9 = EMA( eee12 - eee26, 9 );
    TimeFrameRestore();

    eee12 = TimeFrameExpand( eee12, inHourly );
    eee26 = TimeFrameExpand( eee26, inHourly );
    sss9 = TimeFrameExpand( sss9, inHourly );

    eee12 = ( 11 / 13 ) * eee12 + ( 2 / 13 ) * C;
    eee26 = ( 25 / 27 ) * eee26 + ( 2 / 27 ) * C;
    sss9 = ( 8 / 10 ) * sss9 + ( 2 / 10 ) * ( eee12 - eee26 );

    return sss9;



}


function MACD30()
{
    e1 = EMA30( 12 );
    e2 = EMA30( 26 );


    xxx = e1 - e2;
    return xxx;

}

function MACD60()
{
    TimeFrameSet( inHourly );
    eee12 = EMA( C, 12 );
    eee26 = EMA( C, 26 );
//sss9 = EMA(eee12-eee26,9);
    TimeFrameRestore();

    eee12 = TimeFrameExpand( eee12, inHourly );
    eee26 = TimeFrameExpand( eee26, inHourly );
//sss9 = TimeFrameExpand(sss9,inHourly);

    eee12 = ( 11 / 13 ) * eee12 + ( 2 / 13 ) * C;
    eee26 = ( 25 / 27 ) * eee26 + ( 2 / 27 ) * C;
//sss9 = (8/10)*sss9+(2/10)*(eee12-eee26);

    return ( eee12 -eee26 );
}





function StochRSI( n, Stochn )
{

    sss = ( Sum( RSI( n ) - LLV( RSI( n ), Stochn ), 3 ) /
            Sum( HHV( RSI( n ), Stochn ) - LLV( RSI( n ), Stochn ), 3 ) ) * 100;

    return sss;

}

function ARNup( n )
{


    HHVBarsSince = HHVBars( H, n ) + 1;
    Aroon_Up = 100 * ( n - HHVBarsSince ) / ( n - 1 );

    return Aroon_Up;

}

function xxxARNup( n, S1 )
{


    HHVBarsSince = HHVBars( S1, n ) + 1;
    Aroon_Up = 100 * ( n - HHVBarsSince ) / ( n - 1 );

    return Aroon_Up;

}


function ARNdn( n )
{

    LLVBarsSince = LLVBars( L, n ) + 1;
    Aroon_Down = 100 * ( n - LLVBarsSince ) / ( n - 1 );

    return Aroon_Down;
}

function xxxARNdn( n, s1 )
{

    LLVBarsSince = LLVBars( S1, n ) + 1;
    Aroon_Down = 100 * ( n - LLVBarsSince ) / ( n - 1 );

    return Aroon_Down;
}


function ZeroLagTEMA( array, period )
{
    TMA1 = TEMA( array, period );
    TMA2 = TEMA( TMA1, period );
    Diff = TMA1 - TMA2;
    return TMA1 + Diff ;
}


function kama( Periods, fastn, slown )
{

    P = Close;

    if ( periods <= 0 )
        periods = 10;

    Direction = P - Ref( P, -periods );

    Volatility = Sum( abs( P - Ref( P, -1 ) ), periods );

    Volatility = IIf( Volatility > 0, Volatility, 0.00001 );

    ER = abs( Direction / Volatility );

    FastSC = 2 / ( fastn + 1 );

    SlowSC = 2 / ( slown + 1 );

    SSC = ER * ( FastSC - SlowSC ) + SlowSC;

    Constant = SSC ^ 2;




    return AMA( p, Constant );


}

function HullMa( P, Periods, Delay )
{
    X = 2 * WMA( P, round( Periods / 2 ) ) - WMA( P, Periods );
    HullMov = WMA( X, round( sqrt( Periods ) ) );
    HullMov = Ref( HullMov, -Delay );
    return HullMov;
}

function BRKEMA( n )
{

    buffer = Param( "buffer", 2, 1, 30, 1 );

    aup = ARNup( n );
    adown = ARNdn( n );

    aaa = 100 + buffer - Max( aup, adown );

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

    return xxx;

}

function xxxBRKEMA( n, S1 )
{

    buffer = Param( "buffer", 2, 1, 30, 1 );

    aup = xxxARNup( n, s1 );
    adown = xxxARNdn( n, s1 );


    aaa = 100 + buffer - Max( aup, adown );

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

    return xxx;

}


//attempt to construct more robust brkema
function BRKEMA2( n )
{

    buffer = 2; //Param("buffer",2,1,30,1);

    aup = ARNup( n ) + ARNup( 2 * n );
    adown = ARNdn( n ) + ARNdn( 2 * n );

    aaa = 2 * ( 100 + buffer ) - Max( aup, adown );

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

    return xxx;

}

//based on ARNgap
function BRKEMA3( n )
{

    buffer = 2; //Param("buffer",2,1,30,1);

    aup = ARNup( n );
    adown = ARNdn( n );

    aaa = 100 + buffer - abs( aup - adown );

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

    return xxx;

}

//based on ARNgap, attempt a more robust version
function BRKEMA4( n )
{

    buffer = 2; //Param("buffer",2,1,30,1);

    aup = ARNup( n ) + 0.3 * ARNup( 2 * n );
    adown = ARNdn( n ) + 0.3 * ARNdn( 2 * n );

    aaa = ( 2 * ( 100 + buffer ) - abs( aup - adown ) ) / 1.3;

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

    return xxx;

}



function FRAMA( n )
{
//fractal ama

    Price = ( H + L ) / 2;


    N3 = ( HHV( High, N ) - LLV( Low, N ) ) / N;

    HH = HHV( High, N / 2 ) ;
    LL = LLV( Low, N / 2 );

    N1 = ( HH - LL ) / ( N / 2 );

    HH = HHV( Ref( High, - N / 2 ), N / 2 );
    LL = LLV( Ref( Low, - N / 2 ), N / 2 );

    N2 = ( HH - LL ) / ( N / 2 );

    Dimen = IIf( N1 > 0 AND N2 > 0 AND N3 > 0, ( log( N1 + N2 ) - log( N3 ) ) / log( 2 ), Null );

    alpha = exp( -4.6 * ( Dimen - 1 ) );
    alpha = Min( Max( alpha, 0.01 ), 1 ); // bound to 0.01...1 range

    return AMA( Price, alpha );

}

function FRAMA_SMB( n, symbol )
{
//fractal ama
    Hhh = Foreign( symbol, "High" );
    Lll = Foreign( symbol, "Low" );

    Price = ( Hhh + Lll ) / 2;


    N3 = ( HHV( Hhh, N ) - LLV( Lll, N ) ) / N;

    HH = HHV( Hhh, N / 2 ) ;
    LL = LLV( Lll, N / 2 );

    N1 = ( HH - LL ) / ( N / 2 );

    HH = HHV( Ref( Hhh, - N / 2 ), N / 2 );
    LL = LLV( Ref( Lll, - N / 2 ), N / 2 );

    N2 = ( HH - LL ) / ( N / 2 );

    Dimen = IIf( N1 > 0 AND N2 > 0 AND N3 > 0, ( log( N1 + N2 ) - log( N3 ) ) / log( 2 ), Null );

    alpha = exp( -4.6 * ( Dimen - 1 ) );
    alpha = Min( Max( alpha, 0.01 ), 1 ); // bound to 0.01...1 range

    return AMA( Price, alpha );

}


function LRSI( array, gamma )
//Laguerre RSI from John Ehlers' work
{
    L0 = array; // Initialize as array
    L1 = array;
    L2 = array;
    L3 = array;
    LRSIValue = array;

    for ( i = 1; i < BarCount; i++ )
    {
        L0[i] = ( 1 - gamma ) * array[i] + gamma * L0[i-1];
        L1[i] = - gamma * L0[i] + L0[i-1] + gamma * L1[i-1];
        L2[i] = - gamma * L1[i] + L1[i-1] + gamma * L2[i-1];
        L3[i] = - gamma * L2[i] + L2[i-1] + gamma * L3[i-1];

        CU = 0;
        CD = 0;

        if ( L0[i] >= L1[i] )
            CU = L0[i] - L1[i];
        else
            ( CD = L1[i] - L0[i] );

        if ( L1[i] >= L2[i] )
            CU = CU + L1[i] - L2[i];
        else
            CD = CD + L2[i] -
                 L1[i];

        if ( L2[i] >= L3[i] )
            CU = CU + L2[i] - L3[i];
        else
            CD = CD + L3[i] -
                 L2[i];

        if ( CU + CD != 0 )
            LRSIValue[i] = CU / ( CU + CD );
    }

    return LRSIValue;
}


function ULTEMA()
{

//buffer = Param("buffer",2,1,100,1);
    buffer = 2;

    ult = xxx_ult();
    aaa = 20 + buffer - Min( abs( ult ) / 1000, 20 );

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

    return xxx;



}

function ULT60EMA()
{

    buffer60 = Param( "buffer60", 43, 1, 100, 1 );

    ult = xxx_ult60();


    aaa = buffer60 + Min( abs( ult ) / 120, 13 );

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

    return xxx;



}


function S_EMA_XY( fastn, slown, Closingtime )
{

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

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

    aaa = EMA( ttt, fastn );
    bbb = EMA( 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_ARNORDER( ARNn, Closingtime )
{

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


    Buysignal = ( ARNup( ARNn ) > ARNdn( ARNn ) );
    Sellsignal = ( ARNup( ARNn ) < ARNdn( ARNn ) );


    Shortsignal = ( ARNup( ARNn ) < ARNdn( ARNn ) );
    Coversignal = ( ARNup( ARNn ) > ARNdn( ARNn ) );

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

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

    return Ref( totalstate, -xxxdelay );


}

function S_EMA_XY_CONF( fastn, slown, Closingtime )
{

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

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

    aaa = EMA( ttt, fastn );
    bbb = EMA( 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 ) );


    Buysignal2 = Buysignal;
    Sellsignal2 = Sellsignal;

    Shortsignal2 = Shortsignal;
    Coversignal2 = Coversignal;

    Longstate = Flip( Buysignal2, ( Sellsignal2 OR Shortsignal2 ) );
    Shortstate = Flip( Shortsignal2, ( Coversignal2 OR BuySignal2 ) );

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

    return Ref( totalstate, -xxxdelay );

}


function S_EMA_XY_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 = EMA( ttt, fastn );
    bbb = EMA( ttt, slown );

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


    Buysignal2 = Buysignal;
    Sellsignal2 = Sellsignal;

    Shortsignal2 = Shortsignal;
    Coversignal2 = Coversignal;

    Longstate = Flip( Buysignal2, ( Sellsignal2 OR Shortsignal2 ) );
    Shortstate = Flip( Shortsignal2, ( Coversignal2 OR BuySignal2 ) );

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

    return Ref( totalstate, -xxxdelay );

}



function S_EMA_BRK( fastn, slown, n, Closingtime )
{

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

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

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

    Buysignal = ( aaa >= bbb ) AND ( Close > Ref( HHV( H, n ), -1 ) );
    Sellsignal = ( aaa < bbb ) AND ( Close < Ref( LLV( L, n ), -1 ) );

    Shortsignal = ( aaa < bbb ) AND ( Close < Ref( LLV( L, n ), -1 ) );
    Coversignal = ( aaa >= bbb ) AND ( Close > Ref( HHV( H, n ), -1 ) );



    Buysignal2 = Buysignal;
    Sellsignal2 = Sellsignal;

    Shortsignal2 = Shortsignal;
    Coversignal2 = Coversignal;

    Longstate = Flip( Buysignal2, ( Sellsignal2 OR Shortsignal2 ) );
    Shortstate = Flip( Shortsignal2, ( Coversignal2 OR BuySignal2 ) );

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

    return Ref( totalstate, -xxxdelay );

}


function S_EMA_XY_CONF_SLSP( fastn, slown, Closingtime, SL, SP )
{

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

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

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

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



    LastBuy = ValueWhen( Ref( Buysignal, -xxxdelay ), O, 1 );
//LastBuy = ValueWhen(Buysignal AND NOT Ref(Buysignal,-1),O,1);
    LastShort = ValueWhen( Ref( shortsignal, -xxxdelay ), O, 1 );
//LastShort = ValueWhen(shortsignal AND NOT Ref(Shortsignal,-1),O,1);

    Sellsignal = ( ( aaa < bbb ) AND ( Close < ValueWhen( Cross( bbb, aaa ), Min( Open, Close ), 1 ) ) ) OR ( C < LastBuy - SL ) OR ( C > LastBuy + SP );
    Coversignal = ( aaa >= bbb ) AND ( Close > ValueWhen( Cross( aaa, bbb ), Max( Open, Close ), 1 ) ) OR ( C > LastShort + SL ) OR ( C < LastShort - SP );


    Coversignal = ExRem( Coversignal, Shortsignal );
    Sellsignal = ExRem( Sellsignal, Buysignal );

//Buysignal2 = Buysignal AND (Ref(BarsSince(Buysignal),-1)>Ref(BarsSince(Shortsignal),-1));
    Buysignal2 = Buysignal;// AND (BarsSince(Ref(Buysignal,-1))>BarsSince(Ref(Shortsignal,-1)));
    Sellsignal2 = Sellsignal;

//Shortsignal2 = Shortsignal AND (Ref(BarsSince(Buysignal),-1)<Ref(BarsSince(Shortsignal),-1));
    Shortsignal2 = Shortsignal;// AND (BarsSince(Ref(Buysignal,-1))<BarsSince(Ref(Shortsignal,-1)));
    Coversignal2 = Coversignal;

    Longstate = Flip( Buysignal2, ( Sellsignal2 OR Shortsignal2 ) );
    Shortstate = Flip( Shortsignal2, ( Coversignal2 OR BuySignal2 ) );

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

    return Ref( totalstate, -xxxdelay );

}


function S_EMA_RSI_CONF( fastn, slown, Closingtime, n )
{

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

    ttt = RSI( n );

    aaa = EMA( ttt, fastn );
    bbb = EMA( 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_EMA_TSF_CONF( fastn, slown, Closingtime )
{


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


    n = Param( "n", 20, 10, 50, 5 );
    rrr = ( Open + High + Low + Close ) / 4;
    ttt = TSF( rrr, n );


    aaa = EMA( ttt, fastn );
    bbb = EMA( 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_EMA_TSF_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" );


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

    n = Param( "n", 20, 10, 50, 5 );

    ttt = TSF( rrr, n );


    aaa = EMA( ttt, fastn );
    bbb = EMA( 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_EMA_KAMA_CONF( fastn, slown, Closingtime )
{


    pebstime = Hour() * 60 + Minute();
    endendend = ( pebstime >= Closingtime ) OR ( Day() != Ref( Day(), 1 ) );
    xxxdelay = 1 - endendend * 1;

    P = Close;
    Periods = Param( "Time Periods", 10, 1, 1000, 1 );

    Direction = P - Ref( P, -periods );

    Volatility = Sum( abs( P - Ref( P, -1 ) ), periods );

    Volatility = IIf( Volatility > 0, Volatility, 0.00001 );

    ER = abs( Direction / Volatility );

    FastSC = 2 / ( 2 + 1 );

    SlowSC = 2 / ( 30 + 1 );

    SSC = ER * ( FastSC - SlowSC ) + SlowSC;

    Constant = SSC ^ 2;

    kkk = AMA( p, Constant );

//n1 = Param("fastn",15,1,15,1);
//n2 = Param("slown",24,16,50,1);


    e1 = EMA( kkk, fastn );
    e2 = EMA( kkk, slown );

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

    Shortsignal = ( e1 < e2 )AND ( Close < ValueWhen( Cross( e2, e1 ), Min( Open, Close ), 1 ) );
    Coversignal = ( e1 >= e2 )AND ( Close > ValueWhen( Cross( e1, e2 ), 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_EMA_WILDER_CONF( fastn, slown, Closingtime )
{

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


    ttt = Wilders( C, 2 );

    aaa = EMA( ttt, fastn );
    bbb = EMA( 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_EMA_HULL_CONF( fastn, slown, Closingtime )
{

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


    ttt = HULLMA( C, 15, 0 );

    aaa = EMA( ttt, fastn );
    bbb = EMA( 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_EMA_DEMA_CONF( fastn, slown, Closingtime )
{


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

    n = Param( "n", 6, 1, 20, 1 );
    rrr = ( Open + High + Low + Close ) / 4;
    ttt = DEMA( rrr, n );


    aaa = EMA( ttt, fastn );
    bbb = EMA( 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_EMA_EMA_CONF( fastn, slown, Closingtime )
{


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

    n = Param( "n", 3, 1, 20, 1 );
    rrr = ( Open + High + Low + Close ) / 4;
    ttt = EMA( rrr, n );


    aaa = EMA( ttt, fastn );
    bbb = EMA( 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_EMA_TEMA_CONF( fastn, slown, Closingtime )
{


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

    n = Param( "n", 29, 1, 50, 1 );
    rrr = ( Open + High + Low + Close ) / 4;
    ttt = TEMA( rrr, n );


    aaa = EMA( ttt, fastn );
    bbb = EMA( 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_DEMA_CONF( fastn, slown, Closingtime )
{


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


    ttt = ( Open + High + Low + Close ) / 4;
//fastn = Optimize("fastn",12,1,15,1);
//slown = Optimize("slown",34,25,50,1);

    aaa = DEMA( ttt, fastn );
    bbb = DEMA( 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_HHH_EMA( n1, n2, Closingtime )
{

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

//n1 = Optimize("n1",6,1,15,1);
//n2 = Optimize("n2",25,16,50,1);


    aaa = EMA( C, n1 );
    bbb = HHV( aaa, n2 );
    Ccc = LLV( aaa, n2 );


    Buysignal = aaa > Ref( bbb, -1 ) AND ( pebstime >= 9 * 60 + 30 );
    Sellsignal = aaa < Ref( Ccc, -1 )AND ( pebstime >= 9 * 60 + 30 );

    Shortsignal = aaa < Ref( Ccc, -1 )AND ( pebstime >= 9 * 60 + 30 );
    Coversignal = aaa > Ref( bbb, -1 )AND ( pebstime >= 9 * 60 + 30 );



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

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

    return Ref( totalstate, -xxxdelay );

}

function S_HHH2_EMA( n1, n2, Closingtime )
{
//this version requires ema to break channel of price not of ema like hhh
    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Closingtime ) * 1;
    Opentime = Param( "opentime", 570, 0, 9999 );

//n1 = Optimize("n1",6,1,15,1);
//n2 = Optimize("n2",25,16,50,1);


    aaa = EMA( C, n1 );
    bbb = HHV( H, n2 );
    Ccc = LLV( L, n2 );


    Buysignal = aaa > Ref( bbb, -1 ) AND ( pebstime >= Opentime );
    Sellsignal = aaa < Ref( Ccc, -1 )AND ( pebstime >= Opentime );

    Shortsignal = aaa < Ref( Ccc, -1 )AND ( pebstime >= Opentime );
    Coversignal = aaa > Ref( bbb, -1 )AND ( pebstime >= Opentime );



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

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

    return Ref( totalstate, -xxxdelay );

}

function S_HHH2_EMA_SMB( n1, n2, Closingtime, symbol )
{
//this version requires ema to break channel of price not of ema like hhh
    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Closingtime ) * 1;

//n1 = Optimize("n1",6,1,15,1);
//n2 = Optimize("n2",25,16,50,1);

    Ccc = Foreign( symbol, "C" );
    HHH = Foreign( symbol, "H" );
    LLL = Foreign( symbol, "L" );

    aaa = EMA( Ccc, n1 );
    bbb = HHV( Hhh, n2 );
    Ccc = LLV( Lll, n2 );


    Buysignal = aaa > Ref( bbb, -1 ) AND ( pebstime >= 9 * 60 + 30 );
    Sellsignal = aaa < Ref( Ccc, -1 )AND ( pebstime >= 9 * 60 + 30 );

    Shortsignal = aaa < Ref( Ccc, -1 )AND ( pebstime >= 9 * 60 + 30 );
    Coversignal = aaa > Ref( bbb, -1 )AND ( pebstime >= 9 * 60 + 30 );

    Buysignal = Ref( Buysignal, -xxxdelay ) AND ( pebstime >= 9 * 60 + 30 );
    Sellsignal = Ref( Sellsignal , -xxxdelay ) AND ( pebstime >= 9 * 60 + 30 );

    Shortsignal = Ref( Shortsignal , -xxxdelay ) AND ( pebstime >= 9 * 60 + 30 );
    Coversignal = Ref( Coversignal , -xxxdelay ) AND ( pebstime >= 9 * 60 + 30 );


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

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

    return Ref( totalstate, -xxxdelay );

}



function S_HHH_TSF( n2, TSFn, Closingtime )
{

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

//n1 = Optimize("n1",6,1,15,1);
//n2 = Optimize("n2",25,16,50,1);


    aaa = TSF( C, TSFn );
    bbb = HHV( aaa, n2 );
    Ccc = LLV( aaa, n2 );


    Buysignal = aaa > Ref( bbb, -1 ) AND ( pebstime >= 9 * 60 + 30 );
    Sellsignal = aaa < Ref( Ccc, -1 )AND ( pebstime >= 9 * 60 + 30 );

    Shortsignal = aaa < Ref( Ccc, -1 )AND ( pebstime >= 9 * 60 + 30 );
    Coversignal = aaa > Ref( bbb, -1 )AND ( pebstime >= 9 * 60 + 30 );



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

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

    return Ref( totalstate, -xxxdelay );

}

function S_HHH_EMA_TSF( n1, n2, TSFn, Closingtime )
{

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

//n1 = Optimize("n1",6,1,15,1);
//n2 = Optimize("n2",25,16,50,1);


    www = TSF( C, TSFn );
    aaa = EMA( www, n1 );
    bbb = HHV( aaa, n2 );
    Ccc = LLV( aaa, n2 );


    Buysignal = aaa > Ref( bbb, -1 ) AND ( pebstime >= 9 * 60 + 30 );
    Sellsignal = aaa < Ref( Ccc, -1 )AND ( pebstime >= 9 * 60 + 30 );

    Shortsignal = aaa < Ref( Ccc, -1 )AND ( pebstime >= 9 * 60 + 30 );
    Coversignal = aaa > Ref( bbb, -1 )AND ( pebstime >= 9 * 60 + 30 );



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

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

    return Ref( totalstate, -xxxdelay );

}


function S_EMA_ATRBRK( fastn, slown, ATRn, Closingtime )
{

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

    /*
    n = Optimize("ATRn",10,2,50,1);
    n2 = Optimize("ema1",3,2,10,1);
    n3 = Optimize("ema2",20,15,50,5);
    */

//n=6;
    n2 = 4;
    n3 = 20;

//n4 = Optimize("ema1",7,2,14,1);
//n5=Optimize("ema2",21,15,50,1);
    n4 = fastn;
    n5 = slown;


    aaa = ATR( ATRn );
    bbb = EMA( aaa, n2 );
    Ccc = EMA( aaa, n3 );

    e1 = EMA( C, 3 );
    e2 = EMA( C, 5 );
    e3 = EMA( C, 8 );
    e4 = EMA( C, 10 );
    e5 = EMA( C, 12 );
    e6 = EMA( C, 15 );


    rrr = ( e1 - e2 ) ^ 2 + ( e2 - e3 ) ^ 2 + ( e3 - e4 ) ^ 2 + ( e4 - e5 ) ^ 2 + ( e5 - e6 ) ^ 2;
    rrr = rrr / ( ATR( 100 ) / 100 ) ^ 2;
    rrr = rrr * IIf( e3 > e4, 1, -1 );

    Buysignal = ( bbb >= Ccc ) AND ( rrr < 10000 ) AND ( EMA( C, n4 ) > EMA( C, n5 ) );
    Sellsignal = ( EMA( C, n4 ) < EMA( C, n5 ) );

    Shortsignal = ( bbb >= Ccc ) AND ( rrr > -10000 ) AND ( EMA( C, n4 ) < EMA( C, n5 ) );
    Coversignal = ( EMA( C, n4 ) >= EMA( C, n5 ) );


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

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

    return Ref( totalstate, -xxxdelay );

}

function S_HEIKINASHI( n, Closingtime )
{


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

/////////////////////
// Heikin-Ashi code
    HaClose = ( O + H + L + C ) / 4;
    HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );

//avp = Optimize("Up TEMA avg", n, 1, 100,1 );
//avpdn = Optimize("Dn TEMA avg", n, 1, 100,1 );
    avp = n;
    avpdn = n;

// Velvoort is using not original, but modified Heikin-Ashi close
    HaClose = ( HaClose + HaOpen + Max( H, HaOpen ) + Min( L, HaOpen ) ) / 4;

// up average
    ZlHa = ZeroLagTEMA( HaClose, avp );
    ZlCl = ZeroLagTEMA( ( H + L ) / 2, avp );
    ZlDif = ZlCl - ZlHa;

    keep1 = Hold( HaClose >= HaOpen, 2 );
    keep2 = ZlDif >= 0;
    keeping = keep1 OR keep2;
    keepall = keeping OR ( Ref( keeping, -1 ) AND ( C > O ) OR C >= Ref( C, -1 ) );
    keep3 = abs( C - O ) < ( H - L ) * 0.35 AND H >= Ref( L, -1 );
    utr = keepall OR ( Ref( keepall, -1 ) AND keep3 );

// dn average
    ZlHa = ZeroLagTEMA( HaClose, avpdn );
    ZlCl = ZeroLagTEMA( ( H + L ) / 2, avpdn );
    ZlDif = ZlCl - ZlHa;

    keep1 = Hold( HaClose < HaOpen, 2 );
    keep2 = ZlDif < 0;
    keeping = keep1 OR keep2;
    keepall = keeping OR ( Ref( keeping, -1 ) AND ( C < O ) OR C < Ref( C, -1 ) );
    keep3 = abs( C - O ) < ( H - L ) * 0.35 AND L <= Ref( H, -1 );
    dtr = keepall OR ( Ref( keepall, -1 ) AND keep3 );

    upw = dtr == 0 AND Ref( dtr, -1 ) AND utr;
    dnw = utr == 0 AND Ref( utr, -1 ) AND dtr;

    Haco = Flip( upw, dnw );

    Haco = Haco * 2 - 1;

    xxx = Ref( Haco, -xxxdelay );

    return xxx;

}

function S_RSI( n, Closingtime )
{
//conventional rsi reversion trading
    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Closingtime ) * 1;

    rrr = RSI( n );

    Buysignal = Cross( rrr, 30 );
    Sellsignal = Cross( 70, rrr );

    Shortsignal = Cross( 70, rrr );
    Coversignal = Cross( rrr, 30 );


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

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

    return Ref( totalstate, -xxxdelay );


}

function S_RSIpebs( n, Closingtime )
{
//rsi trend trading
    pebstime = Hour() * 60 + Minute();
    xxxdelay = 1 - ( pebstime >= Closingtime ) * 1;

    rrr = RSI( n );

    Buysignal = Cross( rrr, 50 );
    Sellsignal = Cross( 50, rrr );

    Shortsignal = Cross( 50, rrr );
    Coversignal = Cross( rrr, 50 );


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

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

    return Ref( totalstate, -xxxdelay );


}


function S_STOCHRSI( n, stochn, Closingtime )
{

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

    sss = stochrsi( n, Stochn );
    sss2 = EMA( sss, 3 );

    Buysignal = Cross( sss, sss2 );
    Sellsignal = Cross( sss2, sss );

    Shortsignal = Cross( sss2, sss );
    Coversignal = Cross( sss, sss2 );


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

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

    return Ref( totalstate, -xxxdelay );


}



function ContextZone()
{
    Conzone = 0;
    TimeFrameSet( inDaily );

    rrr1 = Param( "fastday", 7, 1, 50, 1 );
    rrr2 = Param( "slowday", 21, 1, 50, 1 );

    e1_day = Ref( EMA( C, rrr1 ), -1 );
    e2_day = Ref( EMA( C, rrr2 ), -1 );
    ATR_day = Ref( ATR( 20 ), -1 );

    TimeFrameRestore();

    e1_day = TimeFrameExpand( e1_day, inDaily );
    e2_day = TimeFrameExpand( e2_day, inDaily );
    ATR_day = TimeFrameExpand( ATR_day, inDaily );

    e1_day = IIf( Day() == Ref( Day(), -1 ), Ref( e1_day, -1 ), e1_day );
    e2_day = IIf( Day() == Ref( Day(), -1 ), Ref( e2_day, -1 ), e2_day );
    ATR_day = IIf( Day() == Ref( Day(), -1 ), Ref( ATR_day, -1 ), ATR_day );

    e1 = e1_day * ( rrr1 - 1 ) / ( rrr1 + 1 ) + Ref( C, -1 ) * 2 / ( rrr1 + 1 );
    e2 = e2_day * ( rrr2 - 1 ) / ( rrr2 + 1 ) + Ref( C, -1 ) * 2 / ( rrr2 + 1 );



    band1 = Param( "band1", 1.2, 0, 2, 0.05 );
    band2 = Param( "band2", 0.2, 0, 2, 0.05 );
    band3 = Param( "band3", 0.45, 0, 2, 0.05 );

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

        if ( ( C[i-1] > e1[i] + band1*ATR_day[i] )*( e1[i] > e2[i] ) )
            Conzone[i] = 1;
        else
            if ( ( C[i-1] > e1[i] + band2*ATR_day[i] )*( e1[i] > e2[i] ) )
                Conzone[i] = 2;
            else
                if ( ( C[i-1] > e1[i] )*( e1[i] > e2[i] ) )
                    Conzone[i] = 3;
                else
                    if ( ( C[i-1] > e2[i] )*( e1[i] > e2[i] ) )
                        Conzone[i] = 4;
                    else
                        if ( ( C[i-1] > e2[i] - band3*ATR_day[i] )*( e1[i] > e2[i] ) )
                            Conzone[i] = 5;
                        else
                            if ( e1[i] > e2[i] )
                                Conzone[i] = 6;
                            else
                                if ( ( C[i-1] < e1[i] - band1*ATR_day[i] )*( e1[i] < e2[i] ) )
                                    Conzone[i] = -1;
                                else
                                    if ( ( C[i-1] < e1[i] - band2*ATR_day[i] )*( e1[i] < e2[i] ) )
                                        Conzone[i] = -2;
                                    else
                                        if ( ( C[i-1] < e1[i] )*( e1[i] < e2[i] ) )
                                            Conzone[i] = -3;
                                        else
                                            if ( ( C[i-1] < e2[i] )*( e1[i] < e2[i] ) )
                                                Conzone[i] = -4;
                                            else
                                                if ( ( C[i-1] < e2[i] + band3*ATR_day[i] )*( e1[i] < e2[i] ) )
                                                    Conzone[i] = -5;
                                                else
                                                    if ( e1[i] < e2[i] )
                                                        Conzone[i] = -6;
    }



    return Conzone;
}

function ContextZone_SMB( symbol )
{
//still need to adjust for symbol info
    SetBarsRequired( 9999 );

    rrr1 = Param( "fastday", 7, 1, 50, 1 );
    rrr2 = Param( "slowday", 21, 1, 50, 1 );

    Conzone = 0;

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

    Hhh_D = TimeFrameCompress( Hhh, inDaily );
    Lll_D = TimeFrameCompress( Lll, inDaily );
    Ccc_d = TimeFrameCompress( Ccc, inDaily );
    Ooo_d = TimeFrameCompress( Ooo, inDaily );

    TrueRange = Max( Hhh_D - Lll_D, Max( abs( Hhh_D - Ref( Ccc_d, -1 ) ), abs( Lll_d - Ref( Ccc_d, -1 ) ) ) );


    TimeFrameSet( inDaily );


    Hhh_D = Foreign( symbol, "High" );
    Lll_D = Foreign( symbol, "Low" );
    Ccc_D = Foreign( symbol, "Close" );
    Ooo_D = Foreign( symbol, "Open" );

    TrueRange = Max( Hhh - Lll, Max( abs( Hhh - Ref( Ccc, -1 ) ), abs( Lll - Ref( Ccc, -1 ) ) ) );
    myATR = Wilders( TrueRange, 20 );

    e1_day = Ref( EMA( Ccc, rrr1 ), -1 );
    e2_day = Ref( EMA( Ccc, rrr2 ), -1 );
    ATR_day = Ref( myATR, -1 );



    TimeFrameRestore();

    e1_day = TimeFrameExpand( e1_day, inDaily );
    e2_day = TimeFrameExpand( e2_day, inDaily );
    ATR_day = TimeFrameExpand( ATR_day, inDaily );

    e1_day = IIf( Day() == Ref( Day(), -1 ), Ref( e1_day, -1 ), e1_day );
    e2_day = IIf( Day() == Ref( Day(), -1 ), Ref( e2_day, -1 ), e2_day );
    ATR_day = IIf( Day() == Ref( Day(), -1 ), Ref( ATR_day, -1 ), ATR_day );


    e1 = e1_day * ( rrr1 - 1 ) / ( rrr1 + 1 ) + Ref( Ccc, -1 ) * 2 / ( rrr1 + 1 );
    e2 = e2_day * ( rrr2 - 1 ) / ( rrr2 + 1 ) + Ref( Ccc, -1 ) * 2 / ( rrr2 + 1 );



    band1 = Param( "band1", 1.2, 0, 2, 0.05 );
    band2 = Param( "band2", 0.2, 0, 2, 0.05 );
    band3 = Param( "band3", 0.45, 0, 2, 0.05 );

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

        if ( ( Ccc[i-1] > e1[i] + band1*ATR_day[i] )*( e1[i] > e2[i] ) )
            Conzone[i] = 1;
        else
            if ( ( Ccc[i-1] > e1[i] + band2*ATR_day[i] )*( e1[i] > e2[i] ) )
                Conzone[i] = 2;
            else
                if ( ( Ccc[i-1] > e1[i] )*( e1[i] > e2[i] ) )
                    Conzone[i] = 3;
                else
                    if ( ( Ccc[i-1] > e2[i] )*( e1[i] > e2[i] ) )
                        Conzone[i] = 4;
                    else
                        if ( ( Ccc[i-1] > e2[i] - band3*ATR_day[i] )*( e1[i] > e2[i] ) )
                            Conzone[i] = 5;
                        else
                            if ( e1[i] > e2[i] )
                                Conzone[i] = 6;
                            else
                                if ( ( Ccc[i-1] < e1[i] - band1*ATR_day[i] )*( e1[i] < e2[i] ) )
                                    Conzone[i] = -1;
                                else
                                    if ( ( Ccc[i-1] < e1[i] - band2*ATR_day[i] )*( e1[i] < e2[i] ) )
                                        Conzone[i] = -2;
                                    else
                                        if ( ( Ccc[i-1] < e1[i] )*( e1[i] < e2[i] ) )
                                            Conzone[i] = -3;
                                        else
                                            if ( ( Ccc[i-1] < e2[i] )*( e1[i] < e2[i] ) )
                                                Conzone[i] = -4;
                                            else
                                                if ( ( Ccc[i-1] < e2[i] + band3*ATR_day[i] )*( e1[i] < e2[i] ) )
                                                    Conzone[i] = -5;
                                                else
                                                    if ( e1[i] < e2[i] )
                                                        Conzone[i] = -6;
    }



    return Conzone;
}


function XXXContextZone()
{
    Conzone = 0;
    TimeFrameSet( inDaily );

    rrr1 = Param( "fastday", 7, 1, 50, 1 );
    rrr2 = Param( "slowday", 21, 1, 50, 1 );

    e1_day = Ref( EMA( C, rrr1 ), -1 );
    e2_day = Ref( EMA( C, rrr2 ), -1 );
    ATR_day = Ref( ATR( 20 ), -1 );

    TimeFrameRestore();

    e1_day = TimeFrameExpand( e1_day, inDaily );
    e2_day = TimeFrameExpand( e2_day, inDaily );
    ATR_day = TimeFrameExpand( ATR_day, inDaily );

    e1 = e1_day * ( rrr1 - 1 ) / ( rrr1 + 1 ) + Ref( C, -1 ) * 2 / ( rrr1 + 1 );
    e2 = e2_day * ( rrr2 - 1 ) / ( rrr2 + 1 ) + Ref( C, -1 ) * 2 / ( rrr2 + 1 );



    band1 = Param( "band1", 1.2, 0, 2, 0.05 );
    band2 = Param( "band2", 0.45, 0, 2, 0.05 );
    band3 = Param( "band3", 0.2, 0, 2, 0.05 );
    band4 = Param( "band4", 0.45, 0, 2, 0.05 );

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

        if ( ( C[i-1] > e1[i] + band1*ATR_day[i] )*( e1[i] > e2[i] ) )
            Conzone[i] = 1;
        else
            if ( ( C[i-1] > e1[i] + band2*ATR_day[i] )*( e1[i] > e2[i] ) )
                Conzone[i] = 2;
            else
                if ( ( C[i-1] > e1[i] + band3*ATR_day[i] )*( e1[i] > e2[i] ) )
                    Conzone[i] = 3;
                else
                    if ( ( C[i-1] > e1[i] )*( e1[i] > e2[i] ) )
                        Conzone[i] = 4;
                    else
                        if ( ( C[i-1] > e2[i] )*( e1[i] > e2[i] ) )
                            Conzone[i] = 5;
                        else
                            if ( ( C[i-1] > e2[i] - band3*ATR_day[i] )*( e1[i] > e2[i] ) )
                                Conzone[i] = 6;
                            else
                                if ( e1[i] > e2[i] )
                                    Conzone[i] = 7;
                                else
                                    if ( ( C[i-1] < e1[i] - band1*ATR_day[i] )*( e1[i] < e2[i] ) )
                                        Conzone[i] = -1;
                                    else
                                        if ( ( C[i-1] < e1[i] - band2*ATR_day[i] )*( e1[i] < e2[i] ) )
                                            Conzone[i] = -2;
                                        else
                                            if ( ( C[i-1] < e1[i] - band3*ATR_day[i] )*( e1[i] < e2[i] ) )
                                                Conzone[i] = -3;
                                            else
                                                if ( ( C[i-1] < e1[i] )*( e1[i] < e2[i] ) )
                                                    Conzone[i] = -4;
                                                else
                                                    if ( ( C[i-1] < e2[i] )*( e1[i] < e2[i] ) )
                                                        Conzone[i] = -5;
                                                    else
                                                        if ( ( C[i-1] < e2[i] + band3*ATR_day[i] )*( e1[i] < e2[i] ) )
                                                            Conzone[i] = -6;
                                                        else
                                                            if ( e1[i] < e2[i] )
                                                                Conzone[i] = -7;
    }



    return Conzone;
}


function RVI( n1, n2 )
{
//usual of hhh = 10,30 for n1,n2
    SD = StDev( C, n1 );

    xxx = RSIa( SD, n2 );

    return xxx;

}

function ContextPower()
{

    TimeFrameSet( inDaily );

    e1_day = Ref( EMA( C, 7 ), -1 );
    e2_day = Ref( EMA( C, 21 ), -1 );
    ATR_day = Ref( ATR( 20 ), -1 );

    TimeFrameRestore();

    e1_day = TimeFrameExpand( e1_day, inDaily );
    e2_day = TimeFrameExpand( e2_day, inDaily );
    ATR_day = TimeFrameExpand( ATR_day, inDaily );

    e1 = e1_day * 6 / 8 + Ref( C, -1 ) * 2 / 8;
    e2 = e2_day * 20 / 22 + Ref( C, -1 ) * 2 / 22;

    xxx = ( e1_day - e2_day ) / e2_day;

    return xxx;

}


function Contextfilter( s1 )
{

    Ccc = Contextzone();

    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] == 5 )
        {
            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] == 6 )
        {
            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] == -5 )
        {
            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] == -6 )
        {
            if ( s1[i] > 0 )
                s1[i] = 0;
        }

    }

    return s1;
}

//new contextfilter not keen on shorting in bigpicture strength or buying bigpicture weakness too soon
function Contextfilter2( s1 )
{

    Ccc = Contextzone();

    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] == 5 )
        {
            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] == 6 )
        {
            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] == -5 )
        {
            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] == -6 )
        {
            if ( s1[i] > 0 )
                s1[i] = 0;
        }

    }

    return s1;
}

function Contextfilter2_SMB( s1, symbol )
{

    Ccc = Contextzone_SMB( symbol );

    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] == 5 )
        {
            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] == 6 )
        {
            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] == -5 )
        {
            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] == -6 )
        {
            if ( s1[i] > 0 )
                s1[i] = 0;
        }

    }

    return s1;
}


function Contextfilter2plus( s1 )
{

    Ccc = Contextzone();
    uuu = xxx_ult60();




    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] == 5 )
        {
            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] == 6 )
        {
            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] == -5 )
        {
            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] == -6 )
        {
            if ( s1[i] > 0 )
                s1[i] = 0;
        }

//now the ult scenarios
        if ( i > 0 )
        {
            if ( ( uuu[i-1] > 10000 ) AND ( s1[i] < s1[i-1] ) )
                s1[i] = s1[i-1];

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

    return s1;
}


function Contextfilter3( s1 )
{

    Ccc = Contextzone();

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

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

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

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

            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] == 5 )
        {
            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] == 6 )
        {
            if ( s1[i] < 0 )
                s1[i] = 0;
        }

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

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

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

            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] == -5 )
        {
            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] == -6 )
        {
            if ( s1[i] > 0 )
                s1[i] = 0;
        }

    }

    return s1;
}

//this allows longs to be taken/increased in cz=2, and same for shorts
function Contextfilter4( s1 )
{

    Ccc = Contextzone();

    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] == 5 )
        {
            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] == 6 )
        {
            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] == -5 )
        {
            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] == -6 )
        {
            if ( s1[i] > 0 )
                s1[i] = 0;
        }

    }

    return s1;
}

function XXXContextfilter2( s1 )
{

    Ccc = XXXContextzone();

    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] == 3 )
        {
            if ( s1[i] < s1[i-1] )
                s1[i] = s1[i-1];
        }


        if ( Ccc[i] == 6 )
        {
            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] == 7 )
        {
            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] == -3 )
        {
            if ( s1[i] > s1[i-1] )
                s1[i] = s1[i-1];
        }

        if ( Ccc[i] == -6 )
        {
            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] == -7 )
        {
            if ( s1[i] > 0 )
                s1[i] = 0;
        }

    }

    return s1;
}


function MiniContextfilter( s1, smallgap, biggap, smallST, bigST )
{

    ttt = ( O + H + L + C ) / 4;
//just use 9,27 arbitrarily
    xxx = Ref( EMA( ttt, 9 ), -1 ) - Ref( EMA( ttt, 27 ), -1 );
    sss = Ref( xxx_st(), -1 );

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

        if ( xxx[i] > biggap )
            Ccc[i] = 3; //here we sell all longs
        else
            if ( xxx[i] > smallgap )
                Ccc[i] = 2; //here we do not buy extra if we are long
            else
                if ( xxx[i] >= 0 )
                    Ccc[i] = 1; //here we can go long or buy extra
                else
                    if ( xxx[i] > -smallgap )
                        Ccc[i] = -1; //here we can go short or sell extra
                    else
                        if ( xxx[i] > -biggap )
                            Ccc[i] = -2; //here we do not sell extra if we are short
                        else
                            Ccc[i] = -3; //here we close all shorts

    }

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

        if ( Ccc[i] == 3 )
        {
            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] == -2 )
        {
            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] == -3 )
        {
            if ( s1[i] < 0 )
                s1[i] = 0;
        }
    }

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


        if ( sss[i] > bigST )
            Ccc[i] = 3; //here we sell all longs
        else
            if ( sss[i] > smallST )
                Ccc[i] = 2; //here we do not buy extra if we are long
            else
                if ( xxx[i] >= 0 )
                    Ccc[i] = 1; //here we can go long or buy extra
                else
                    if ( sss[i] > -smallST )
                        Ccc[i] = -1; //here we can go short or sell extra
                    else
                        if ( sss[i] > -bigST )
                            Ccc[i] = -2; //here we do not sell extra if we are short
                        else
                            Ccc[i] = -3; //here we close all shorts


    }

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

        if ( Ccc[i] == 3 )
        {
            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] == -2 )
        {
            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] == -3 )
        {
            if ( s1[i] < 0 )
                s1[i] = 0;
        }
    }



    return s1;
}

function Contextfilter_MACD60( s1 )
{

    mmm = Ref( MACD60(), -1 );





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

        if ( mmm[i] > 120 )
        {
            if ( s1[i] < s1[i-1] )
                s1[i] = s1[i-1];
        }

        if ( mmm[i] < -120 )
        {
            if ( s1[i] > s1[i-1] )
                s1[i] = s1[i-1];
        }





    }

    return s1;


}

function Contextfilter_HIST60( s1 )
{

    mmm = Ref( MACD60(), -1 );
    sss = Ref( Signal60(), -1 );


    Hurdle = Param( "macd60 hurdle", 100, 1, 200, 1 );


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

        if ( ( mmm[i] > Hurdle ) AND ( mmm[i] > sss[i] ) )
        {
            if ( s1[i] < s1[i-1] )
                s1[i] = s1[i-1];
        }

        if ( ( mmm[i] < -Hurdle ) AND ( mmm[i] < sss[i] ) )
        {
            if ( s1[i] > s1[i-1] )
                s1[i] = s1[i-1];
        }





    }

    return s1;


}


function Contextfilter_ULT60( s1 )
{

    mmm = Ref( xxx_ult60(), -1 );





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

        if ( mmm[i] > 12000 )
        {
            if ( s1[i] < s1[i-1] )
                s1[i] = s1[i-1];
        }

        if ( mmm[i] < -12000 )
        {
            if ( s1[i] > s1[i-1] )
                s1[i] = s1[i-1];
        }





    }

    return s1;


}


function S_EMA_KAMA_CONF_VALZONE( fastn, slown, Closingtime, zonetrigger )
{

    pebstime = Hour() * 60 + Minute();
    endendend = ( pebstime >= Closingtime ) OR ( Day() != Ref( Day(), 1 ) );
    xxxdelay = 1 - endendend * 1;


    s = S_EMA_KAMA_CONF( fastn, slown, Closingtime );
    Cz = Contextzone();

    Buysignal = ( s == 1 ) AND ( Ref( Cz, -xxxdelay ) >= zonetrigger );
    Sellsignal = ( s < 1 );

    Shortsignal = ( s == -1 ) AND ( Ref( Cz, -xxxdelay ) <= -zonetrigger );
    Coversignal = ( s > -1 );


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

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

    return totalstate;
//return Ref(totalstate,-xxxdelay);

}

function GfxConvertBarToPixelX( Bar )
{
    lvb = Status( "lastvisiblebar" );
    fvb = Status( "firstvisiblebar" );
    pxchartleft = Status( "pxchartleft" );
    pxchartwidth = Status( "pxchartwidth" );
    return pxchartleft + Bar * pxchartwidth / ( Lvb - fvb + 1 );
}

procedure MultiRibbon( RibbonThickness, RibbonColor, Position, Label, Font )
{

//RibbonThickness = Param("Ribbon Thickness", 1, 1, 5, 0.1);
//Font = ParamList("Font:","Arial|Calibri|Futura|Tahoma|Times New Roman");


    LineColor = colorLightGrey;
    Position = RibbonThickness * Position;
    x2 = Status( "pxchartright" );
    y2 = Status( "pxchartbottom" );

    RibbonColor =
        IIf( GfxConvertBarToPixelX( BarIndex() - Status( "firstvisiblebarindex" ) ) > y2 / 1.5 *
             ( RibbonThickness / 100 ) * 18 , RibbonColor, colorYellow );

    Plot( 0, "", LineColor, styleOwnScale | styleNoLabel, 0, 100 );
    Plot( Position, "", LineColor, styleOwnScale | styleNoLabel, 0, 100 );
    Plot( Position, "", RibbonColor, styleArea | styleOwnScale | styleNoLabel, 0,
          100 );

    GfxSetTextColor( colorBlack );
    GfxSelectFont( Font, y2 / 1.5 * ( RibbonThickness / 100 ), 400 );
    GfxDrawText( Label, 8, y2 * 1.001 - ( y2 * Position / 100 ) , y2 / 1.5 *
                 ( RibbonThickness / 100 ) * 17, y2, 2 + 32 + 256 );

}

function SendToTwitter( MSG )
{


    AlertIf( ( 1 == 1 ), "EXEC C:\\Webtyphoon.exe", "\"" + msg + "\"", 9 );

}





function S_EMA_BRKEMA_CONF( fastn, slown, Closingtime, n )
{


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



    ttt = BRKEMA( n );


    aaa = EMA( ttt, fastn );
    bbb = EMA( 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 );


}


//use the new brkema looking at longer arn as well
function S_EMA_BRKEMA2_CONF( fastn, slown, Closingtime, n )
{


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



    ttt = BRKEMA2( n );


    aaa = EMA( ttt, fastn );
    bbb = EMA( 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_EMA_BRKEMA3_CONF( fastn, slown, Closingtime, n )
{


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



    ttt = BRKEMA3( n );


    aaa = EMA( ttt, fastn );
    bbb = EMA( 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_EMA_BRKEMA4_CONF( fastn, slown, Closingtime, n )
{


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



    ttt = BRKEMA4( n );


    aaa = EMA( ttt, fastn );
    bbb = EMA( 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_EMA_ULTEMA_CONF( fastn, slown, Closingtime )
{


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



    ttt = ULTEMA();


    aaa = EMA( ttt, fastn );
    bbb = EMA( 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_EMA_FRAMA_CONF( fastn, slown, Closingtime )
{


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

    n = Param( "n", 16, 1, 50, 1 );

    ttt = FRAMA( n );


    aaa = EMA( ttt, fastn );
    bbb = EMA( 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_EMA_FRAMA_CONF_SMB( fastn, slown, Closingtime, symbol )
{


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

    n = Param( "n", 16, 1, 50, 1 );

    Ooo = Foreign( symbol, "Open" );
    Ccc = Foreign( symbol, "Close" );
    ttt = FRAMA_SMB( n, symbol );


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

    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_SVE( Closingtime )
{

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

    ATRfact = Param( "ATR multiplication", 2.8, 1, 10, 0.1 );
    period = Param( "ATR Period", 10, 1, 100 );

    HiLo = IIf( ( H - L ) < 1.5 * MA( H - L, period ), H - L, 1.5 * MA( H - L, period ) ); //limit HILO to 1.5 * usual size

    Href = IIf( L <= Ref( H, -1 ), H - Ref( C, -1 ), ( H - Ref( C, -1 ) ) - ( L - Ref( H, -1 ) ) / 2 );
    Lref = IIf( H >= Ref( L, -1 ), Ref( C, -1 ) - L, ( Ref( C, -1 ) - L ) - ( Ref( L, -1 ) - H ) / 2 );
    diff1 = Max( HiLo, Href );
    diff2 = Max( diff1, Lref );
    ATRmod = Wilders( diff2, period );

    loss = atrfact * ATRmod;
    resistance = C + loss;

    support = L;
    trends = H;


    for ( i = 4; i < BarCount; i++ )
    {
        if ( L[ i ] >= L[ i-2 ] AND
                L[ i-1 ] >= L[ i-2 ] AND
                L[ i-3 ] >= L[ i-2 ] AND
                L[ i-4 ] >= L[ i-2 ] )
        {
            support[ i ] = L[ i - 2];
        }
        else
            if ( L[ i ] > H[ i-1 ] * 1.0013 )
            {
                support[ i ] = H[ i-1 ] * 0.9945;
            }
            else
                if ( L[ i ] > support[ i-1 ] * 1.1 )
                {
                    support[ i ] = support[ i-1 ] * 1.05;
                }
                else
                {
                    support[ i ] = support[ i-1 ];
                }

        if ( H[ i ] > trends[ i-1 ] AND
                H[ i-1 ] > trends[ i-1 ] )
        {
            trends[ i ] = Max( trends[ i-1 ], support[ i ] );
        }
        else
            if ( H[ i ] < trends[ i-1 ] AND
                    H[ i-1 ] < trends[ i-1 ] )
            {
                trends[ i ] = Min( trends[ i-1 ], resistance[ i ] );
            }
            else
                if ( H[ i ] >= trends[ i-1 ] )
                    trends[ i ] = support[ i ];
                else
                    trends[ i ] = resistance[ i ];

    }


    Buysignal = Cross( C, trends );

    Sellsignal = Cross( trends, H );

    Shortsignal = Sellsignal;
    Coversignal = Buysignal;



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

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

    return Ref( totalstate, -xxxdelay );

}

function xxx_factor()
{

    TimeFrameSet( inHourly );

    ult60 = xxx_ult();

    TimeFrameRestore();

    ult60 = TimeFrameExpand( ult60, inHourly, expandFirst );

    TimeFrameSet( 2*in15Minute );

    ult30 = xxx_ult();

    TimeFrameRestore();

    ult30 = TimeFrameExpand( ult30, 2 * in15Minute, expandFirst );

    TimeFrameSet( in15Minute );

    ult15 = xxx_ult();

    TimeFrameRestore();

    ult15 = TimeFrameExpand( ult15, in15Minute, expandFirst );


    ult5 = xxx_ult();


    xxx = ult60 + ult30 / 1.4 + ult15 / 2 + ult5 / 3.5;

    return xxx;

}

function MAgic()
{

//only works in 5min timeframe

    aaa5 = Ref( EMA( C, 85 ), -1 );

    TimeFrameSet( in15Minute );

    aaa15 = Ref( EMA( C, 85 ), -1 );

    TimeFrameRestore();

    aaa15 = TimeFrameExpand( aaa15, in15Minute, expandFirst );

    TimeFrameSet( 2*in15Minute );

    aaa30 = Ref( EMA( C, 85 ), -1 );

    TimeFrameRestore();

    aaa30 = TimeFrameExpand( aaa30, 2 * in15Minute, expandFirst );

    TimeFrameSet( inHourly );

    aaa60 = Ref( EMA( C, 85 ), -1 );

    TimeFrameRestore();

    aaa60 = TimeFrameExpand( aaa60, inHourly, expandFirst );


//come up with some bullshit trendmeasure
    mmm = Ref( C, -1 ) * 4 - aaa5 - aaa15 - aaa30 - aaa60;

    mmm = mmm / ( Ref( C, -1 ) / 10000 );

    return mmm;


}

function BIGMAgic()
{


    aaa30 = Ref( EMA( C, 85 ), -1 );

    TimeFrameRestore();

    aaa30 = TimeFrameExpand( aaa30, 2 * in15Minute, expandFirst );

    TimeFrameSet( inHourly );

    aaa60 = Ref( EMA( C, 85 ), -1 );

    TimeFrameRestore();

    aaa60 = TimeFrameExpand( aaa60, inHourly, expandFirst );


//come up with some bullshit trendmeasure
    mmm = Ref( C, -1 ) * 2 - aaa30 - aaa60;

    mmm = mmm / ( Ref( C, -1 ) / 10000 );

    return mmm;


}


function MAgic2()
{

//only works in 5min timeframe

    aaa5 = Ref( EMA( C, 85 ), -1 );

    TimeFrameSet( in15Minute );

    aaa15 = Ref( EMA( C, 85 ), -1 );

    TimeFrameRestore();

    aaa15 = TimeFrameExpand( aaa15, in15Minute, expandFirst );

    TimeFrameSet( 2*in15Minute );

    aaa30 = Ref( EMA( C, 85 ), -1 );

    TimeFrameRestore();

    aaa30 = TimeFrameExpand( aaa30, 2 * in15Minute, expandFirst );

    TimeFrameSet( inHourly );

    aaa60 = Ref( EMA( C, 85 ), -1 );

    TimeFrameRestore();

    aaa60 = TimeFrameExpand( aaa60, inHourly, expandFirst );

    TimeFrameSet( inDaily );

    aaaD = Ref( EMA( C, 85 ), -1 );

    TimeFrameRestore();

    aaaD = TimeFrameExpand( aaaD, inDaily, expandFirst );



//come up with some bullshit trendmeasure
    mmm = Ref( C, -1 ) * 5 - aaa5 - aaa15 - aaa30 - aaa60 - aaaD;

    mmm = mmm / ( Ref( C, -1 ) / 10000 );

    return mmm;


}


function MAGICema( n )
{


    mmm = MAgic();
    ttt = ( Open + High + Low + Close ) / 4;
    rrr = Optimize( "rrr", 200, 50, 300, 50 );

    nnn = n * ( 1 + int( abs( mmm ) / rrr ) );
    xxx = AMA( ttt, 2 / ( nnn + 1 ) );

    return xxx;


}

function S_EMA_ULT60EMA_CONF( fastn, slown, Closingtime )
{


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

    n = Param( "n", 3, 1, 20, 1 );
    ttt = ult60ema();


    aaa = EMA( ttt, fastn );
    bbb = EMA( 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_EMA_BRKEMA_CONF_REVERSE( fastn, slown, Closingtime, n )
{

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


    Maxloss = Param( "Maxloss", 9999, 0, 9999, 1 );

    s = S_EMA_BRKEMA_CONF( fastn, slown, Closingtime, n );
    Hhh = brkema2( n );
    H1 = EMA( Hhh, fastn );
    H2 = EMA( Hhh, slown );

    bottomstopline = ValueWhen( Cross( H2, H1 ), Min( Open, Close ), 1 );
    topstopline = ValueWhen( Cross( H1, H2 ), Max( Open, Close ), 1 );




// trade on next bar open
    SetTradeDelays( 0, 0, 0, 0 );
    BuyPrice = SellPrice = Open;

//Points only
    PositionSize = MarginDeposit = 1;



    Buysignal = ( s > 0 ) AND ( Ref( C, -1 ) > bottomstopline ) AND ( Ref( C, -1 ) - bottomstopline < Maxloss );
    Sellsignal = ( s <= 0 );

    Shortsignal = ( s < 0 ) AND ( Ref( C, -1 ) < topstopline ) AND ( topstopline - Ref( C, -1 ) < Maxloss );
    Coversignal = ( s >= 0 );


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

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

    return Ref( totalstate, -xxxdelay );

}

function S_EMA_BRKEMA2_CONF_REVERSE( fastn, slown, Closingtime, n )
{

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


    Maxloss = Param( "Maxloss", 9999, 0, 9999, 1 );

    s = S_EMA_BRKEMA2_CONF( fastn, slown, Closingtime, n );
    Hhh = brkema2( n );
    H1 = EMA( Hhh, fastn );
    H2 = EMA( Hhh, slown );

    bottomstopline = ValueWhen( Cross( H2, H1 ), Min( Open, Close ), 1 );
    topstopline = ValueWhen( Cross( H1, H2 ), Max( Open, Close ), 1 );




// trade on next bar open
    SetTradeDelays( 0, 0, 0, 0 );
    BuyPrice = SellPrice = Open;

//Points only
    PositionSize = MarginDeposit = 1;



    Buysignal = ( s > 0 ) AND ( Ref( C, -1 ) > bottomstopline ) AND ( Ref( C, -1 ) - bottomstopline < Maxloss );
    Sellsignal = ( s <= 0 );

    Shortsignal = ( s < 0 ) AND ( Ref( C, -1 ) < topstopline ) AND ( topstopline - Ref( C, -1 ) < Maxloss );
    Coversignal = ( s >= 0 );


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

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

    return Ref( totalstate, -xxxdelay );

}

function S_EMA_BRKEMA3_CONF_REVERSE( fastn, slown, Closingtime, n )
{

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


    Maxloss = Param( "Maxloss", 9999, 0, 9999, 1 );

    s = S_EMA_BRKEMA3_CONF( fastn, slown, Closingtime, n );
    Hhh = brkema2( n );
    H1 = EMA( Hhh, fastn );
    H2 = EMA( Hhh, slown );

    bottomstopline = ValueWhen( Cross( H2, H1 ), Min( Open, Close ), 1 );
    topstopline = ValueWhen( Cross( H1, H2 ), Max( Open, Close ), 1 );




// trade on next bar open
    SetTradeDelays( 0, 0, 0, 0 );
    BuyPrice = SellPrice = Open;

//Points only
    PositionSize = MarginDeposit = 1;



    Buysignal = ( s > 0 ) AND ( Ref( C, -1 ) > bottomstopline ) AND ( Ref( C, -1 ) - bottomstopline < Maxloss );
    Sellsignal = ( s <= 0 );

    Shortsignal = ( s < 0 ) AND ( Ref( C, -1 ) < topstopline ) AND ( topstopline - Ref( C, -1 ) < Maxloss );
    Coversignal = ( s >= 0 );


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

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

    return Ref( totalstate, -xxxdelay );

}

function S_EMA_BRKEMA4_CONF_REVERSE( fastn, slown, Closingtime, n )
{

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


    Maxloss = Param( "Maxloss", 9999, 0, 9999, 1 );

    s = S_EMA_BRKEMA4_CONF( fastn, slown, Closingtime, n );
    Hhh = brkema2( n );
    H1 = EMA( Hhh, fastn );
    H2 = EMA( Hhh, slown );

    bottomstopline = ValueWhen( Cross( H2, H1 ), Min( Open, Close ), 1 );
    topstopline = ValueWhen( Cross( H1, H2 ), Max( Open, Close ), 1 );




// trade on next bar open
    SetTradeDelays( 0, 0, 0, 0 );
    BuyPrice = SellPrice = Open;

//Points only
    PositionSize = MarginDeposit = 1;



    Buysignal = ( s > 0 ) AND ( Ref( C, -1 ) > bottomstopline ) AND ( Ref( C, -1 ) - bottomstopline < Maxloss );
    Sellsignal = ( s <= 0 );

    Shortsignal = ( s < 0 ) AND ( Ref( C, -1 ) < topstopline ) AND ( topstopline - Ref( C, -1 ) < Maxloss );
    Coversignal = ( s >= 0 );


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

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

    return Ref( totalstate, -xxxdelay );

}


//switch long short around certain chosen value
function S_flippivot( pivot, Closingtime )
{

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



    Buysignal = ( C > pivot );
    Sellsignal = ( C < pivot );

    Shortsignal = ( C < pivot );
    Coversignal = ( C > pivot );


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

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

    return Ref( totalstate, -xxxdelay );


}

function S_longpivot( pivot, Closingtime )
{

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



    Buysignal = ( C > pivot );
    Sellsignal = ( C < pivot );

    Shortsignal = 0;
    Coversignal = 0;


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

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

    return Ref( totalstate, -xxxdelay );


}

function S_shortpivot( pivot, Closingtime )
{

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



    Buysignal = 0;
    Sellsignal = 0;

    Shortsignal = ( C < pivot );
    Coversignal = ( C > pivot );


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

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

    return Ref( totalstate, -xxxdelay );


}

function PivotPoint()
{

    TimeFrameSet( inDaily );
    Hhh = H;
    Lll = L;
    Ccc = C;
    TimeFrameRestore();

    Hhh = TimeFrameExpand( Hhh, inDaily );
    Lll = TimeFrameExpand( Lll, inDaily );
    Ccc = TimeFrameExpand( Ccc, inDaily );

    ppp = ( Hhh + Lll + Ccc ) / 3;

    return ppp;


}

function MAgicCount()
{

    TimeFrameSet( in5Minute );

    e1 = EMA( C, 85 );

    TimeFrameRestore();

    e1 = TimeFrameExpand( e1, in5Minute, expandLast );



    TimeFrameSet( in15Minute );

    e2 = EMA( C, 85 );

    TimeFrameRestore();

    e2 = TimeFrameExpand( e2, in15Minute, expandLast );



    TimeFrameSet( 2*in15Minute );

    e3 = EMA( C, 85 );

    TimeFrameRestore();

    e3 = TimeFrameExpand( e3, 2 * in15Minute, expandLast );




    TimeFrameSet( inHourly );

    e4 = EMA( C, 85 );

    TimeFrameRestore();

    e4 = TimeFrameExpand( e4, inHourly, expandLast );

    rrr = Ref( C, -1 );

    xxx = ( rrr > e1 ) + ( rrr > e2 ) + ( rrr > e3 ) + ( rrr > e4 );

    return xxx;

}
Back