2011 Apr: Identifying Cup Formation Early

ICE Data Services -

SemiCup.efs, isSemiCup.efs  
EFSLibrary - Discussion Board  

File Name:

  • SemiCup.efs
  • isSemiCup.efs

Description:
Identifying Cup Formation Early and Identifying Cup Formation Early For Grid Window

Formula Parameters:

SemiCup.efs

  • Parameter: 1

isSemiCup.efs

  • Parameter: 1

Notes:
The related article is copyrighted material. If you are not a subscriber of Stocks & Commodities, please visit www.traders.com.

Download File:
SemiCup.efs
isSemiCup.efs

SemiCup.efs

isSemiCup.efs

EFS Code:

SemiCup.efs

/*********************************
Provided By:  
    Interactive Data Corporation (Copyright © 2010) 
    All rights reserved. This sample eSignal Formula Script (EFS)
    is for educational purposes only. Interactive Data Corporation
    reserves the right to modify and overwrite this EFS file with 
    each new release. 
	
Description:        
    Identifying Cup Formation Early
 
Version:            1.0  11/02/2011

Formula Parameters:                     Default:
    Parameter                              1
    
Notes:
    The related article is copyrighted material. If you are not
    a subscriber of Stocks & Commodities, please visit www.traders.com.
**********************************/

var fpArray = new Array();
var bVersion = null;
function preMain()
{
    setPriceStudy(true);
    setShowCursorLabel(false);
    
    
    var x=0;
    fpArray[x] = new FunctionParameter("gParam", FunctionParameter.NUMBER);
    with(fpArray[x++])
    {
	setName("Parameter");
	setLowerLimit(1);
        setUpperLimit(100);
        setDefault(1);
    }
    fpArray[x] = new FunctionParameter("gColorSC", FunctionParameter.COLOR);
    with(fpArray[x++])
    {
	setName("Semicup Color");
        setDefault(Color.lime);
    }
    fpArray[x] = new FunctionParameter("gColorNC", FunctionParameter.COLOR);
    with(fpArray[x++])
    {
	setName("No Cup Color");
        setDefault(Color.red);
    }
    fpArray[x] = new FunctionParameter("gGrid", FunctionParameter.BOOLEAN);
    with(fpArray[x++])
    {
	setName("Show Grid");
        setDefault(true);
    }

}

var bInit = false;
var xCls = null;
var xFilC = null;
var eps = 0.0000000001;
var vCls = 0;
var nCupPeriod = 0;

var pTop = 0;
var pBot = 0;
var b0 = 0; 
var b1 = 0; 
var b2 = 0; 
var b3 = 0; 
var b4 = 0; 
var b5 = 0; 
var L2 = 0; 
var L3 = 0; 

var L1 = 0; 
var L4 = 0;

var DSX1 = 0;
var DSX2 = 0;
var bIsSemiCup = false;

function main(gParam, gColorSC, gColorNC, gGrid)
{
    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;   
    if (gParam == null) gParam = 1;
    
    if (!bInit)
    {
            xCls = close();
            xFilC = efsInternal("calcFilC", xCls);
            bInit = true; 
    }
    
    if( getCurrentBarIndex() < 0 ) return;
    var nBarState = getBarState();
    if ( nBarState == BARSTATE_NEWBAR || isReplayMode())    
    {

        var nBarCount = getCurrentBarCount();

        //potential semicup period calculation 
        var vLast = gParam*xCls.getValue(0);
        var curBar = 1 ;
        var curMax = vLast;
        while (curBar < nBarCount)
        {
            vCloseCur = xCls.getValue(-curBar);
            if ( vCloseCur >= vLast && vCloseCur >= curMax )
            { 
                nCupPeriod = curBar;
                curMax = vCloseCur;
            }
            curBar++;
        }
    
        //calculation of grid parameters    
        pTop = xFilC.getValue(0);
        pBot = xFilC.getValue(0);
        iBot = 0;
        iTop = 0;
        for (i = -nCupPeriod; i<0; i++)
        {
            if ( pTop < xFilC.getValue(i) ) pTop = xFilC.getValue(i);  
            if ( pBot > xFilC.getValue(i) ) pBot = xFilC.getValue(i);
        }
        bHeight = Math.abs((pTop - pBot)/5);
        bLength = Math.max(Math.round(nCupPeriod/5),1);
        b0 = -nCupPeriod;
        b5 = 0;
        b1 = Math.min(b0 + bLength, b5);
        b2 = Math.min(b1 + bLength, b5);
        b3 = Math.min(b2 + bLength, b5);
        b4 = Math.min(b3 + bLength, b5);
        L2 = pBot + 2*bHeight;
        L3 = pBot + 3*bHeight;
      
        //calculation of directional strength 1
        var DSX1P = 0;
        var DSX1N = 0;
        for (i = b0; i < b2; i++ )
        {
            DSX1P += Math.max(xFilC.getValue(i+1)-xFilC.getValue(i),0); 
            DSX1N += Math.max(xFilC.getValue(i)-xFilC.getValue(i+1),0); 
        }
        DSX1 = 100 * Math.abs(DSX1P - DSX1N)/( eps + DSX1P + DSX1N );    
    
        //calculation  directional strength 2
        var DSX2P = 0;
        var DSX2N = 0;
        var bInside1 = false;
        var bInside2 = false;
        for (i = b2; i < b5; i++ )
        {
            DSX2P += Math.max(xFilC.getValue(i+1)-xFilC.getValue(i),0); 
            DSX2N += Math.max(xFilC.getValue(i)-xFilC.getValue(i+1),0); 
            if ( xFilC.getValue(i)>L3 ) bInside1=true;
            if ( i >= b3 && xFilC.getValue(i)>L2 ) bInside2=true;
        }
        DSX2 = 100 * Math.abs(DSX2P - DSX2N)/( eps + DSX2P + DSX2N );    
    
        //checking condition of cup existing
        bIsSemiCup = (nCupPeriod >=20 && DSX1 > 25 && DSX2 < 25 && !bInside1 && !bInside2 )  ; //добавить условие, что в соответствующих квадратах не появляются случайные бары

        //plotting grid
        if (gGrid)
        {
            drawLineRelative(b0, Math.exp(pTop),b0, Math.exp(pBot), PS_DASH, 1, Color.grey, "b0");  
            drawLineRelative(b1, Math.exp(pTop),b1, Math.exp(pBot), PS_DASH, 1, Color.grey, "b1");  
            drawLineRelative(b2, Math.exp(pTop),b2, Math.exp(pBot), PS_DASH, 1, Color.grey, "b2");  
            drawLineRelative(b3, Math.exp(pTop),b3, Math.exp(pBot), PS_DASH, 1, Color.grey, "b3");  
            drawLineRelative(b4, Math.exp(pTop),b4, Math.exp(pBot), PS_DASH, 1, Color.grey, "b4");  
            drawLineRelative(b5, Math.exp(pTop),b5, Math.exp(pBot), PS_DASH, 1, Color.grey, "b5");  
            drawLineRelative(b0, Math.exp(pTop),b5, Math.exp(pTop), PS_DASH, 1, Color.grey, "l0");  
            drawLineRelative(b0, Math.exp(pBot),b5, Math.exp(pBot), PS_DASH, 1, Color.grey, "l5");  
            drawLineRelative(b2, Math.exp(L3),b5, Math.exp(L3), PS_DASH, 1, Color.grey, "l3");  
            drawLineRelative(b3, Math.exp(L2),b5, Math.exp(L2), PS_DASH, 1, Color.grey, "l2");  

        }
    }// if (nBarState == BARSTATE_NEWBAR)....
    
    //correction of conditions on real time ticks
    var bRTCond = true;
    if (nBarState == BARSTATE_CURRENTBAR) bRTCond = ( xFilC.getValue(0) < L2 );    

    //options of result output
    var strSemiCup = "No Cup"; 
    var retCol = gColorNC;
    if ( bIsSemiCup && bRTCond ) 
    {
            strSemiCup = "Semicup";
            retCol = gColorSC;
    }

    //marking the position of potentional starting of semicup
    drawTextRelative(-nCupPeriod, AboveBar3, "Start", retCol, null, Text.PRESET|Text.CENTER, "Arial", 12, "beg1");     
    drawTextRelative(-nCupPeriod, AboveBar2, "semicup", retCol, null, Text.PRESET|Text.CENTER, "Arial", 12, "beg2");     
    drawShapeRelative(-nCupPeriod, AboveBar1, Shape.DOWNARROW, null, retCol, Shape.PRESET|Shape.CENTER, "beg3" );     

    //plotting graph
    P1 = (Math.exp(pTop)-Math.exp(pBot))/Math.pow(nCupPeriod,10);
    P2 = 0.98 * Math.exp(pBot);
    prevY = close(-nCupPeriod);
    for (i=0; i<nCupPeriod; i++)
    {
        curY = P1 * Math.pow(nCupPeriod-i,10)+P2;
        drawLineRelative(i-nCupPeriod, prevY, i-nCupPeriod+1, curY, PS_SOLID, 2, retCol, i);
        prevY = curY;
    }

    drawTextAbsolute(0, 15, strSemiCup, Color.white, retCol, Text.BOLD|Text.RELATIVETOBOTTOM|Text.RELATIVETOLEFT, "Arial", 11, "txt");  
    return null;    
}

var bFilCInit = false;
function calcFilC(xSrc)
{
        var vSrc = xSrc.getValue(0);
        vSrc = Math.log(vSrc);
        return vSrc;
}

function verify() {
    var b = false;
    if (getBuildNumber() < 779) {
        drawTextAbsolute(5, 35, "This study requires version 8.0 or later.", 
            Color.white, Color.blue, Text.RELATIVETOBOTTOM|Text.RELATIVETOLEFT|Text.BOLD|Text.LEFT,
            null, 13, "error");
        drawTextAbsolute(5, 20, "Click HERE to upgrade.@URL=http://www.esignal.com/download/default.asp", 
            Color.white, Color.blue, Text.RELATIVETOBOTTOM|Text.RELATIVETOLEFT|Text.BOLD|Text.LEFT,
            null, 13, "upgrade");
        return b;
    } else {
        b = true;
    }
    return b;
}

EFS Code:

isSemiCup.efs

/*********************************
Provided By:  
    Interactive Data Corporation (Copyright © 2010) 
    All rights reserved. This sample eSignal Formula Script (EFS)
    is for educational purposes only. Interactive Data Corporation
    reserves the right to modify and overwrite this EFS file with 
    each new release. 
	
Description:        
    Identifying Cup Formation Early For Grid Window
 
Version:            1.0  11/02/2011

Formula Parameters:                     Default:
    Parameter                              1
    
Notes:
    The related article is copyrighted material. If you are not
    a subscriber of Stocks & Commodities, please visit www.traders.com.

**********************************/
var fpArray = new Array();
var bVersion = null;
function preMain()
{
    setPriceStudy(true);
    setCursorLabelName("isSemiCup",0);
    
    var x=0;
    fpArray[x] = new FunctionParameter("gParam", FunctionParameter.INTEGER);
    with(fpArray[x++])
    {
	setName("Parameter");
	setLowerLimit(1);
        setUpperLimit(100);
        setDefault(1);
    }
    fpArray[x] = new FunctionParameter("gColorSC", FunctionParameter.COLOR);
    with(fpArray[x++])
    {
	setName("Semicup Color");
        setDefault(Color.lime);
    }
    fpArray[x] = new FunctionParameter("gColorNC", FunctionParameter.COLOR);
    with(fpArray[x++])
    {
	setName("No Cup Color");
        setDefault(Color.red);
    }
}

var bInit = false;
var xCls = null;
var xFilC = null;
var eps = 0.0000000001;
var vCls = 0;
var nCupPeriod = 0;

var pTop = 0;
var pBot = 0;
var b0 = 0; 
var b1 = 0; 
var b2 = 0; 
var b3 = 0; 
var b4 = 0; 
var b5 = 0; 
var L2 = 0; 
var L3 = 0; 

var DSX1 = 0;
var DSX2 = 0;
var bIsSemiCup = false;

function main(gParam, gColorSC, gColorNC)
{
    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;   
    if (gParam == null) gParam = 1;
   
    if (!bInit)
    {
            xCls = close();
            xFilC = efsInternal("calcFilC", xCls);
            bInit = true; 
    }
    
    if( getCurrentBarIndex() < 0 ) return;

    var nBarState = getBarState();
    if ( nBarState == BARSTATE_NEWBAR )    
    {
        var nBarCount = getCurrentBarCount();

        //potential semicup period calculation 
        var vLast = gParam*xCls.getValue(0);
        var curBar = 1 ;
        var curMax = vLast;
        while (curBar < nBarCount)
        {
            var vCloseCur = xCls.getValue(-curBar);
            if ( vCloseCur >= vLast && vCloseCur >= curMax )
            { 
                nCupPeriod = curBar;
                curMax = vCloseCur;
            }
            curBar++;
        }

        //calculation of grid parameters
        pTop = xFilC.getValue(0);
        pBot = xFilC.getValue(0);
        for (i = -nCupPeriod; i<0; i++)
        {
            if ( pTop < xFilC.getValue(i) ) pTop = xFilC.getValue(i);  
            if ( pBot > xFilC.getValue(i) ) pBot = xFilC.getValue(i);
        }
        var bHeight = Math.abs((pTop - pBot)/5);
        var bLength = Math.max(Math.round(nCupPeriod/5),1);
        b0 = -nCupPeriod;
        b5 = 0;
        b1 = Math.min(b0 + bLength, b5);
        b2 = Math.min(b1 + bLength, b5);
        b3 = Math.min(b2 + bLength, b5);
        b4 = Math.min(b3 + bLength, b5);
        L2 = pBot + 2*bHeight;
        L3 = pBot + 3*bHeight;
        
        //calculation of directional strength 1
        var DSX1P = 0;
        var DSX1N = 0;
        for (i = b0; i < b2; i++ )
        {
            DSX1P += Math.max(xFilC.getValue(i+1)-xFilC.getValue(i),0); 
            DSX1N += Math.max(xFilC.getValue(i)-xFilC.getValue(i+1),0); 
        }
        DSX1 = 100 * Math.abs(DSX1P - DSX1N)/( eps + DSX1P + DSX1N );    

        //calculation of define directional strength 2
        var DSX2P = 0;
        var DSX2N = 0;
        var bInside1 = false;
        var bInside2 = false;
        for (i = b2; i < b5; i++ )
        {
            DSX2P += Math.max(xFilC.getValue(i+1)-xFilC.getValue(i),0); 
            DSX2N += Math.max(xFilC.getValue(i)-xFilC.getValue(i+1),0); 
            if ( xFilC.getValue(i)>L3 ) bInside1=true;
            if ( i >= b3 && xFilC.getValue(i)>L2 ) bInside2=true;
        }
        DSX2 = 100 * Math.abs(DSX2P - DSX2N)/( eps + DSX2P + DSX2N );    
        
        //checking conditions of cup existing
        bIsSemiCup = (nCupPeriod >=20 && DSX1 > 25 && DSX2 < 25 && !bInside1 && !bInside2 )  ; //добавить условие, что в соответствующих квадратах не появляются случайные бары
    }
    
    if (nBarState == BARSTATE_CURRENTBAR) bIsSemiCup = ( bIsSemiCup && xFilC.getValue(0) < L3 ); 
    
    //output options
    var strSemiCup = "NO CUP"; 
    var retColor = gColorNC;
    if ( bIsSemiCup ) 
    {
            strSemiCup = "SEMICUP";
            retColor = gColorSC;
    }

    setBarBgColor(retColor,0);
    setBarFgColor(Color.white,0);
    
    return strSemiCup;    
    
}

var bFilCInit = false;
function calcFilC(xSrc)
{
        var vSrc = xSrc.getValue(0);
        vSrc = Math.log(vSrc);
        return vSrc;
}

function verify() {
    var b = false;
    if (getBuildNumber() < 779) {
        drawTextAbsolute(5, 35, "This study requires version 8.0 or later.", 
            Color.white, Color.blue, Text.RELATIVETOBOTTOM|Text.RELATIVETOLEFT|Text.BOLD|Text.LEFT,
            null, 13, "error");
        drawTextAbsolute(5, 20, "Click HERE to upgrade.@URL=http://www.esignal.com/download/default.asp", 
            Color.white, Color.blue, Text.RELATIVETOBOTTOM|Text.RELATIVETOLEFT|Text.BOLD|Text.LEFT,
            null, 13, "upgrade");
        return b;
    } else {
        b = true;
    }
    return b;
}