I am trying to calculate longest distance that can reached High to High and Low to Low. My code (complete, runnable) below does the job well. But due to the nested loop it is slow. Is there a way to speed this up?
I keep hearing of faster running c++ code that can written and converted to dll. Please let me know if that is relevant here.
I have also attached a data file of 10 minutes interval.
_TRACE( "!CLEAR!" );
SetBarsRequired( sbrAll, sbrAll );
_SECTION_BEGIN( "Price" );
SetChartOptions( 0, chartShowArrows | chartShowDates | chartWrapTitle );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}} ", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
PlotOHLC( O, H, L, C, "Close", colorRed, styleBar , Null, Null, 0, 1, 1 );
_SECTION_END();
constScoreTypeLL=1;constScoreTypeHH=2;
intersectionPrice = intersectionBar = 0;
function findIntersection( priceA, barIndxA, priceB, barIndxB, priceC, barIndxC, priceD, barIndxD )
{
a0 = b0 = c0 = a1 = b1 = c1 = det = Null;
// Line AB represented as a1x + b1y = c1
a0 = priceB - priceA;
b0 = barIndxA - barIndxB;
c0 = a0 * barIndxA + b0 * priceA;
// Line CD represented as a2x + b2y = c2
a1 = priceD - priceC;
b1 = barIndxC - barIndxD;
c1 = a1 * ( barIndxC ) + b1 * ( priceC );
det = a0 * b1 - a1 * b0;
intersectionBar = iif( det == 0, null, ( b1 * c0 - b0 * c1 ) / ( det + 1e-9 ) ) ;
intersectionPrice = iif( det == 0, null, ( ( a0 * c1 - a1 * c0 ) / ( det + 1e-9 ) ) );
linesAreIntersecting = ( ( intersectionPrice > priceD && intersectionPrice < priceC || intersectionPrice < priceD && intersectionPrice > priceC )
&& ( intersectionPrice > priceA && intersectionPrice < priceB || intersectionPrice < priceA && intersectionPrice > priceB ) )
&& ( ( intersectionBar < barIndxD || intersectionBar > barIndxC ) || ( intersectionBar > barIndxA || intersectionBar < barIndxB ) );
intersectionPrice = IIf(linesAreIntersecting, intersectionPrice, Null);
intersectionBar = IIf(linesAreIntersecting, int( intersectionBar ), Null);
}
function getBarScore(forwardSearchDistanceArray, type)
{
//max search Distance is 100
barScore = 0;
for( i = 2; i < 102; i++ )
{
intresectionReached = 0;
for( j = 1; j < i; j++ ) //is there a way to get rid of this nested loop?
{
ReachedLastBar=Ref(BarIndex(), j+1)==Ref(BarIndex(), j);
if(type==constScoreTypeHH){
findIntersection( H, BarIndex(), IIf( Ref( H, i ) == H, Ref( H, i ) + 1e-3, Ref( H, i ) ), Ref( BarIndex(), i ), Ref( H, j ), Ref( BarIndex(), j ), Ref( L, j ) * 0.50, Ref( BarIndex(), j ));
intresectionReached = IIf( intresectionReached, 1, !IsNull( intersectionBar ) );
barScore = IIf( IsNull( intersectionBar ) && !intresectionReached && !ReachedLastBar && i<forwardSearchDistanceArray+2, IIf( j > barScore, j, barScore ), barScore );
}
else
{
findIntersection( L, BarIndex(), IIf( Ref( L, i ) == L, Ref( L, i ) - 1e-3, Ref( L, i ) ), Ref( BarIndex(), i ), Ref( H, j )*1.5, Ref( BarIndex(), j ), Ref( L, j ) , Ref( BarIndex(), j ));
intresectionReached = IIf( intresectionReached, 1, !IsNull( intersectionBar ) );
barScore = IIf( IsNull( intersectionBar ) && !intresectionReached && !ReachedLastBar && i<forwardSearchDistanceArray+2, IIf( j > barScore, j, barScore ), barScore );
}
//if(i==12)
//_TRACE("i="+i+", j="+j+", isnull(intersectionBar)="+isnull(intersectionBar)+", intersectionBar="+intersectionBar+", isnull(intersectionPrice)="+isnull(intersectionPrice)+", intersectionPrice="+intersectionPrice);
}
}
return(barScore);
}
scoreLL=getBarScore(50, constScoreTypeLL);
scoreHH=getBarScore(40, constScoreTypeHH);
Atr5 = ATR( 5 );
for( i = 0; i < BarCount; i++ ){
PlotTextSetFont( "" + scoreHH[i], "windings", 7, i , H[i] + Atr5[i] / 3, colorYellow );
PlotTextSetFont( "" + scoreLL[i], "windings", 7, i , L[i] - Atr5[i] / 3, colorYellow );
}