# Need help in running the AFL faster

I have an AFL that calculates composite score based on indicator values.
Help needed in below aspects:

1. There must be a better way to do than the way i am doing. The code is taking about 75 seconds for 200 symbols.
2. Looking for guidance on running this AFL as a function that can be called from another AFL.
``````// AFL to calculate composite score
// PPO part is working as expected.
// CCI part is working as expected.
// ADX part is working as expected.

for( i = 1; i < BarCount; i++ )
{
CompScore = 0;
// Below values to be commented when not testing
//PPOScore = 0;
//CCIScore = 0;
_SECTION_BEGIN("PPO Calculation");
// PPO Status
// Current variables match the PPO settings
PPOScore = 0;
PPOImportance = 5;
PPOMulFac = 0;
PPOShort = 8;
PPOLong = 17;
PPOsignal = 9;
PPO = ( EMA( C, PPOShort ) - EMA( C, PPOLong ) ) / EMA( C, PPOLong );
if(PPO[I] <= -0.08)
{
PPOMulFac = -4;
} else if(PPO[I] <= -0.02){
PPOMulFac = -2;
} else if(PPO[I] <= 0.001){
PPOMulFac = 0;
} else if(PPO[I] <= 0.01){
PPOMulFac = 2;
}
ELSE{
PPOMulFac = 4;
}
PPOScore = PPOImportance * PPOMulFac;
_SECTION_END();
_SECTION_BEGIN("CCI Calculation");
CCIScore = 0;
CCIImportance = 3;
CCIMulFac = 0;
CCIPeriods = 5; // 5 periods
CCIValue = CCI(CCIPeriods);
if(CCIValue[I] <= -150)
{
CCIMulFac = -4;
} else if(CCIValue[I] <= -30){
CCIMulFac = -2;
} else if(CCIValue[I] <= 30){
CCIMulFac = 0;
} else if(CCIValue[I] <= 150){
CCIMulFac = 2;
}
ELSE{
CCIMulFac = 4;
}
CCIScore = CCIImportance * CCIMulFac;
_SECTION_END();
ADXPeriods = 2; // 2 periods
if(ADXValue[I] > 25 AND PDIValue[i] > MDIValue[i]){
{
}
}if(ADXValue[I] > 25 AND MDIValue[i] > PDIValue[i]){
{
} else
}
_SECTION_END();
CompScore = CompScore + PPOScore + CCIScore + ADXScore;

}

Filter = 1;

``````

Creating Composites has been covered many times before - search forum before posting.

Loops (and Foreign) are not required and should not be used to create a composite.

Thank you @TrendSurfer, will take a look at Add to Composite and Static Var Add.

I went through Add to composite examples, i am not sure whether it provides me what i am looking for. I am not an expert programmer.

I am using for loop because i do not know of a better method to access last value and i need the last value of indicator as PPOMulFac is dependent on the last value and hence the indicator value(PPOScore). Posting a shorter (but standalone) afl below.

``````for( i = 1; i < BarCount; i++ )
{
CompScore = 0;
// Below values to be commented when not testing
//PPOScore = 0;
//CCIScore = 0;
_SECTION_BEGIN("PPO Calculation");
// PPO Status
// Current variables match the PPO settings
PPOScore = 0;
PPOImportance = 5;
PPOMulFac = 0;
PPOShort = 8;
PPOLong = 17;
PPOsignal = 9;
PPO = ( EMA( C, PPOShort ) - EMA( C, PPOLong ) ) / EMA( C, PPOLong );
if(PPO[I] <= -0.08)
{
PPOMulFac = -4;
} else if(PPO[I] <= -0.02){
PPOMulFac = -2;
} else if(PPO[I] <= 0.001){
PPOMulFac = 0;
} else if(PPO[I] <= 0.01){
PPOMulFac = 2;
}
ELSE{
PPOMulFac = 4;
}
PPOScore = PPOImportance * PPOMulFac;
_SECTION_END();
CompScore = CompScore + PPOScore;
}

Filter = 1;
``````

Your loop is incorrect because you are iterating entire array ( `PPO` ) inside that `BarCount` loop (besides other wrong doings...).

You do not need loop.

``````PPOScore = 0;
PPOImportance = 5;
PPOShort = 8;
PPOLong = 17;
PPOsignal = 9;

// THIS IS AN  ARRAY
PPO = ( EMA( C, PPOShort ) - EMA( C, PPOLong ) ) / EMA( C, PPOLong );

// THIS IS AN ARRAY
PPOMulFac = IIf(PPO<= -0.08, -4,
IIf(PPO <= -0.02, -2,
IIf(PPO <= 0.001, 0,
IIf(PPO <= 0.01, 2, 4))));

// THIS IS AN ELEMENT OF ARRAY
last_value_PPOMulFac = LastValue(PPOMulFac);
``````