FVE Strategy

ICE Data Services -

FVEStrategy.efs  
EFSLibrary - Discussion Board  

File Name: FVEStrategy.efs

Description:
FVE Strategy

Formula Parameters:

  • Samples - number of bars to calculate FVE: 50
  • FVEenterl - lower entry level of FVE: -20
  • FVEenteru - upper entry level of FVE: 10
  • MA - number of bars to calculate MA: 40
  • LRPeriod - number of bars to calculate Linear regression: 20
  • Bangle - the angle to buy: 30
  • Sangle - the angle to sell: -30
  • LRC - number of bars to calculate Linear regression slope: 30
  • UB - limit for Linear regression rising: 0.1
  • LB - limit for Linear regression falling: -0.5
  • BarToExitOn - number of bars to exit position: 70

Notes:
This is a strategy based on FVE (Finite Volume Elements) indicator.

Download File:
FVEStrategy.efs


EFS Code:

/*********************************
Provided By:  
    eSignal (Copyright c eSignal), a division of Interactive Data 
    Corporation. 2008. All rights reserved. This sample eSignal 
    Formula Script (EFS) is for educational purposes only and may be 
    modified and saved under a new file name.  eSignal is not responsible
    for the functionality once modified.  eSignal reserves the right 
    to modify and overwrite this EFS file with each new release.


Description:        
    FVE Strategy 

Version:            1.0  10/14/2008

Notes:
    This is a strategy based on FVE (Finite Volume Elements) indicator. 

Formula Parameters:                              		Default:
    Samples - number of bars to calculate FVE        		50  
    FVEenterl - lower entry level of FVE                        -20
    FVEenteru - upper entry level of FVE                        10
    MA - number of bars to calculate MA                         40
    LRPeriod - number of bars to calculate Linear regression    20
    Bangle - the angle to buy                                   30
    Sangle - the angle to sell                                  -30
    LRC - number of bars to calculate Linear regression slope   30
    UB - limit for Linear regression rising                     0.1
    LB - limit for Linear regression falling                    -0.5
    BarToExitOn - number of bars to exit position               70
**********************************/

var fpArray = new Array();
var bInit = false;

function preMain(){
    setPriceStudy(false);
    setShowCursorLabel(false);
    setShowTitleParameters( false );
    
    setStudyTitle("FVE Strategy");
    setColorPriceBars(true);

    var x=0;
    fpArray[x] = new FunctionParameter("Samples", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setName("Number Of Bars To Calculate FVE");        
        setLowerLimit(1);		
        setDefault(50);
    }
    fpArray[x] = new FunctionParameter("FVEenterl", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setName("Lower Entry Level Pf FVE");
        setLowerLimit(-100);		
        setDefault(-20);
    }
    fpArray[x] = new FunctionParameter("FVEenteru", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setName("Upper Entry Level Of FVE");
        setLowerLimit(1);		
        setDefault(10);
    }
    fpArray[x] = new FunctionParameter("MA", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setName("Length To Calculate MA");
        setLowerLimit(1);		
        setDefault(40);
    }
    fpArray[x] = new FunctionParameter("LRPeriod", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setName("Length To Calculate Linear Regression");
        setLowerLimit(1);		
        setDefault(20);
    }
    fpArray[x] = new FunctionParameter("Bangle", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setName("The Angle To Buy");
        setLowerLimit(0);		
        setDefault(30);
    }
    fpArray[x] = new FunctionParameter("Sangle", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setName("The Angle To Sell");
        setLowerLimit(-100);		
        setDefault(-30);
    }
    fpArray[x] = new FunctionParameter("LRC", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setName("Length Linear Regression Slope");
        setLowerLimit(1);		
        setDefault(30);
    }
    fpArray[x] = new FunctionParameter("UB", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setName("Limit Ror Linear Regression Rising");
        setLowerLimit(0);		
        setDefault(0.1);
    }
    fpArray[x] = new FunctionParameter("LB", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setName("Limit For Linear Regression Falling");
        setLowerLimit(-100);		
        setDefault(-0.5);
    }
    fpArray[x] = new FunctionParameter("BarToExitOn", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setName("Length To Exit Position");
        setLowerLimit(1);		
        setDefault(70);
    }
}

var EMA_1 = 0;
var EMA = 0; 
var VolumePlusMinusArray = new Array();
var FVEArray = new Array();
var BarsSinceEntry = 0;

var xTP = null;

function main(Samples, FVEenterl, FVEenteru, MA, LRPeriod, Bangle, Sangle, LRC, UB, LB, BarToExitOn){
var nState = getBarState();
var TP = 0,
    TP1 = 0,
    MF = 0,
    Cintra = .1,
    Cinter = .1,
    CutOff = 0,
    VolumePlusMinus = 0,
    FVE = 0,
    Fvesum = 0,
    FveFactor = 0,
    Intra = 0, 
    Inter = 0, 
    Vintra = 0, 
    Vinter = 0,
    i = 0, 
    VolSum = 0; 
    K = 2 / (MA + 1), 
    vHL = high(0) - low(0), 
    vVar = vHL * 0.25,
	vAddVar = vVar * 0.35, 
    Condition1 = false, 
    Condition2 = false, 
    Condition3 = false, 
    Condition4 = false, 
    Condition5 = false;

var	IntraArray = new Array();
var	InterArray = new Array();

    if (nState == BARSTATE_ALLBARS) {
        if(Samples == null) Samples = 50;
        if(FVEenterl == null) FVEenterl = -20;
        if(FVEenteru == null) FVEenteru = 10;
        if(MA == null) MA = 40;
        if(LRPeriod == null) LRPeriod = 20;
        if(Bangle == null) Bangle = 30;
        if(Sangle == null) Sangle = -30;
        if(LRC == null)	LRC = 30;
        if(UB == null) UB = .1;
        if(LB == null) LB = -.5;
        if(BarToExitOn == null) BarToExitOn = 70;
    }

    if (bInit == false) {
        xTP = hlc3();
        bInit = true;
    }

    if(getCurrentBarIndex() == 0) return;

    setPriceBarColor(Color.black);

	for(i = 0; i < Samples; i++){
		IntraArray[i] = Math.log(high(-i)) - Math.log(low(-i));
		InterArray[i] = Math.log(xTP.getValue(-i)) - Math.log(xTP.getValue(-i-1));
	}
	
	Intra = Math.log(high(0)) - Math.log(low(0));
	Vintra = StandardDev(IntraArray, Samples);
	Inter = Math.log(xTP.getValue(0)) - Math.log(xTP.getValue(-1));
	Vinter = StandardDev(InterArray, Samples);
	CutOff = Cintra * Vintra + Cinter * Vinter;	
	MF = (close(0) - (high(0) + low(0))/2)+ xTP.getValue(0) - xTP.getValue(-1);
	
	if(MF > CutOff * close(0))
		FveFactor = 1;
	else if(MF < -1 * CutOff * close(0))
		FveFactor = -1;
	else
		FveFactor=0;
		
	VolumePlusMinus = volume(0) * FveFactor;

	for(i = Samples - 1; i > 0; i--)
		VolumePlusMinusArray[i] = VolumePlusMinusArray[i - 1];
		
	VolumePlusMinusArray[0] = VolumePlusMinus;
	
	for(i = 0; i < Samples; i++){
		Fvesum += VolumePlusMinusArray[i];
		VolSum += volume(-i);
	}
	
	if(VolumePlusMinusArray[Samples - 1] != null){
		FVE = (Fvesum / VolSum) * 100;
		for(i = LRPeriod - 1; i > 0; i--)
			FVEArray[i] = FVEArray[i - 1];
		FVEArray[0] = FVE;
		if (getBarState() == BARSTATE_NEWBAR)
			EMA_1 = EMA;
		EMA = K * FVE  + (1 - K) * EMA_1;
	}
	else
		return;
	
	
    if(FVEArray[LRPeriod - 1] != null){
        BarsSinceEntry++;
        if(FVE > FVEenterl && FVE < FVEenteru){
            Condition1 = true;
        }
        if(LinearReg(FVEArray,LRPeriod,"AngleFC",LRC-1) > Bangle){
            Condition2 = true;
        }
        if(FVE > EMA){
            Condition3 = true;
        }
        if(LinearReg(close(0,-LRC),LRC,"Slope",LRC-1) < UB * LinearReg(close(0,-LRC),LRC,"Value",LRC-1) / 100 &&  LinearReg(close(0,-LRC),LRC,"Slope",LRC-1) > LB * LinearReg(close(0,-LRC),LRC,"Value",LRC-1) / 100){
            Condition4 = true;
        }
        if(LinearReg(FVEArray,LRPeriod,"AngleFC",LRC-1) < Sangle){
            Condition5 = true;
        }
        if(!Strategy.isLong()  && Condition1 && Condition2 && Condition3 && Condition4){
            Strategy.doLong("BUY", Strategy.CLOSE, Strategy.THISBAR);
            setPriceBarColor(Color.lime);
            drawTextRelative(-1, 0, "Long", Color.black, Color.lime, Text.VCENTER  | Text.BOLD |  Text.PRESET, null, null, "buyTxt" + getValue("time"));
            BarsSinceEntry = 0;
        }
        if(Condition5 && Strategy.isLong()){
            Strategy.doSell("FVE EXIT", Strategy.CLOSE, Strategy.THISBAR);
            setPriceBarColor(Color.red);
            drawTextRelative(-1, 0, "Exit Long", Color.black, Color.red, Text.VCENTER | Text.BOLD |  Text.PRESET, null, null, "buyTxt" + getValue("time"));
        }
        if(BarsSinceEntry == BarToExitOn && Strategy.isLong()){
            Strategy.doSell("TimeBarsLX", Strategy.CLOSE, Strategy.THISBAR);
            setPriceBarColor(Color.red);
            drawTextRelative(-1, 0, "Exit Long", Color.black, Color.red, Text.VCENTER | Text.BOLD |  Text.PRESET, null, null, "buyTxt" + getValue("time"));
        }
        return LinearReg(close(0,-LRC),LRC,"Slope",LRC-1);
    }
	else
		return;
}
		
				
function StandardDev(Array, Length, Type){
	var i;
	var vSum = 0;
	var SumSqr = 0;
	var StdDev = 0;
	
	for(i = 0; i < Length; i++)
        	vSum += Array[i];

	if(Length != 0)
		for(i = 0; i < Length; i++)
			SumSqr += (Array[i] - vSum / Length) * (Array[i] - vSum / Length);

	StdDev = Math.sqrt(SumSqr / Length);
	return StdDev;
}

function LinearReg(Array, Length, Type, TargetB){
	var i = 0, num1 = 0, num2 = 0, SumBars = 0, SumSqrBars = 0, SumY = 0, Sum1 = 0, Sum2 = 0, Slope = 0, Intercept = 0;

	if(Length == 0)
		return 0;
	
	SumBars = Length * (Length - 1) * .5;
	SumSqrBars = (Length - 1) * Length * (2 * Length - 1) / 6;
	
	Sum1 = 0;
	for(i = 0; i < Length; i++){
		Sum1 += i * Array[i];
		SumY += Array[i];
	}
	Sum2 = SumBars * SumY;
	Num1 = Length * Sum1 - Sum2;
	Num2 = SumBars * SumBars - Length * SumSqrBars;

	if(Num2 != 0)
		Slope = Num1 / Num2;
	else 
		Slope = 0;
	if(Type == "AngleFC")
		return Math.atan(Slope); 
	else if(Type == "Slope")
		return Slope; 
	else if(Type == "Value"){
		Intercept = (SumY - Slope * SumBars) / Length;
		return Intercept + Slope * (Length - 1 - TargetB);
	}
}