Calculations going wrong when zoom level changes

I don't know how to debug this problem. The calculations are wrong when i scroll to left. I am posting some screenshots because the code related to this particular functions is too long.
openpositioncalculationerror
openpositioncalculationerror1

I will post the code as you advice. It is too long I don't know which part to post. Looks like somewhere i am looking into future.

@AlgoEnthusiast - make sure that you are getting all of the bars that you think you are when you are zoomed in - it possibly has something to do with QuickAFL.

Setbarsrequired is set to sbrall so no quickafl.

Unfortunately your question isn't clear enough and does not provide all necessary details to give you an answer. Please follow this advice: How to ask a good question

In short - want help with the code - POST THE CODE!

1 Like
VarSet( "profitTargetHitDistance_1", IIf( VarGet( "profitTarget1iHitConditionHasBeenTrueInThisSetup" ), ValueWhen( VarGet( "profitTarget1iHitOnThisBar" ), VarGet( "profitTarget1iHitDistanceFromPatternFoundi" ) ), 10000 ) ) ;
    VarSet( "profitTargetHitDistance_2", IIf( VarGet( "profitTarget2iHitConditionHasBeenTrueInThisSetup" ), ValueWhen( VarGet( "profitTarget2iHitOnThisBar" ), VarGet( "profitTarget2iHitDistanceFromPatternFoundi" ) ), 10000 ) ) ;
    VarSet( "profitTargetHitDistance_3", IIf( VarGet( "profitTarget3iHitConditionHasBeenTrueInThisSetup" ), ValueWhen( VarGet( "profitTarget3iHitOnThisBar" ), VarGet( "profitTarget3iHitDistanceFromPatternFoundi" ) ), 10000 ) ) ;
    VarSet( "profitTargetHitDistance_4", IIf( minRetracementWave5XConditionHasBeenTrueInThisSetupWithOpenPosition,
            ValueWhen( minRetracementWave5XConditionMetOnThisBarWithOpenPosition, minRetracementWave5XConditionMetWithOpenPositionDistanceFromPatternFoundi ), 10000 ) ) ;
            
    VarSetText("profitTargetHitDistanceAllString", NumToStr(VarGet( "profitTargetHitDistance_1"), 5.0)+NumToStr(VarGet( "profitTargetHitDistance_2"), 5.0)+NumToStr(VarGet( "profitTargetHitDistance_3"), 5.0)+NumToStr(VarGet( "profitTargetHitDistance_4"), 5.0));
	StaticVarSetText( Name() + chartid + "profitTargetHitDistanceAllString", VarGetText("profitTargetHitDistanceAllString"));
	
    /*
        profitTargetHitDistance_1= IIf( VarGet( "profitTarget1iHitConditionHasBeenTrueInThisSetup" ), ValueWhen( VarGet( "profitTarget1iHitOnThisBar" ), VarGet( "profitTarget1iHitDistanceFromPatternFoundi" ) ), 10000 ) ;
        profitTargetHitDistance_2= IIf( VarGet( "profitTarget2iHitConditionHasBeenTrueInThisSetup" ), ValueWhen( VarGet( "profitTarget2iHitOnThisBar" ), VarGet( "profitTarget2iHitDistanceFromPatternFoundi" ) ), 10000 ) ;
        profitTargetHitDistance_3= IIf( VarGet( "profitTarget3iHitConditionHasBeenTrueInThisSetup" ), ValueWhen( VarGet( "profitTarget3iHitOnThisBar" ), VarGet( "profitTarget3iHitDistanceFromPatternFoundi" ) ), 10000 ) ;
        profitTargetHitDistance_4= IIf( minRetracementWave5XConditionHasBeenTrueInThisSetupWithOpenPosition,
        ValueWhen( minRetracementWave5XConditionMetOnThisBarWithOpenPosition, minRetracementWave5XConditionMetWithOpenPositionDistanceFromPatternFoundi ), 10000 ) ;*/

    profitTargetHitDistance_1WithId = VarGet( "profitTargetHitDistance_1" ) + 0.1;
    profitTargetHitDistance_2WithId = VarGet( "profitTargetHitDistance_2" ) + 0.2;
    profitTargetHitDistance_3WithId = VarGet( "profitTargetHitDistance_3" ) + 0.3;
    profitTargetHitDistance_4WithId = VarGet( "profitTargetHitDistance_4" ) + 0.4;
    VarSet( "profitHitOccurance1", 0 );
    VarSet( "profitHitOccurance2", 0 );
    VarSet( "profitHitOccurance3", 0 );
    VarSet( "profitHitOccurance4", 0 );

    //_TRACE("profitTargetHitDistance_1WithId="+profitTargetHitDistance_1WithId+", profitTargetHitDistance_2WithId="+profitTargetHitDistance_2WithId+", profitTargetHitDistance_3WithId="+profitTargetHitDistance_3WithId+", profitTargetHitDistance_4WithId="+profitTargetHitDistance_4WithId);


    VarSet( "profitHitOccurance1", round( frac( Min( Min( profitTargetHitDistance_1WithId, profitTargetHitDistance_2WithId ), Min( profitTargetHitDistance_3WithId, profitTargetHitDistance_4WithId ) ) ) * 10 ) );
    VarSet( "profitHitOccurance4", round( frac( Max( Max( profitTargetHitDistance_1WithId, profitTargetHitDistance_2WithId ), Max( profitTargetHitDistance_3WithId, profitTargetHitDistance_4WithId ) ) ) * 10 ) );

    //_TRACE("profitHitOccurance1="+VarGet("profitHitOccurance1")+", profitHitOccurance4="+VarGet("profitHitOccurance4"));

    VarSet( "profitHitOccurance2", IIf( VarGet( "profitHitOccurance1" ) == 1 && VarGet( "profitHitOccurance4" ) == 2 || VarGet( "profitHitOccurance1" ) == 2 && VarGet( "profitHitOccurance4" ) == 1, 3,
                                        IIf( VarGet( "profitHitOccurance1" ) == 1 && VarGet( "profitHitOccurance4" ) == 3 || VarGet( "profitHitOccurance1" ) == 3 && VarGet( "profitHitOccurance4" ) == 1, 2,
                                                IIf( VarGet( "profitHitOccurance1" ) == 1 && VarGet( "profitHitOccurance4" ) == 4 || VarGet( "profitHitOccurance1" ) == 4 && VarGet( "profitHitOccurance4" ) == 1, 2,
                                                        IIf( VarGet( "profitHitOccurance1" ) == 2 && VarGet( "profitHitOccurance4" ) == 3 || VarGet( "profitHitOccurance1" ) == 3 && VarGet( "profitHitOccurance4" ) == 2, 1,
                                                                IIf( VarGet( "profitHitOccurance1" ) == 3 && VarGet( "profitHitOccurance4" ) == 4 || VarGet( "profitHitOccurance1" ) == 4 && VarGet( "profitHitOccurance4" ) == 3, 2,
                                                                        IIf( VarGet( "profitHitOccurance1" ) == 2 && VarGet( "profitHitOccurance4" ) == 4 || VarGet( "profitHitOccurance1" ) == 4 && VarGet( "profitHitOccurance4" ) == 2, 3, 0 ) ) ) ) ) ) );

    //_TRACE("profitHitOccurance2="+VarGet("profitHitOccurance2"));

    VarSet( "profitHitOccurance3", 10 - ( VarGet( "profitHitOccurance1" ) + VarGet( "profitHitOccurance2" ) + VarGet( "profitHitOccurance4" ) ) );

    //_TRACE("profitHitOccurance3="+VarGet("profitHitOccurance3"));

    VarSet( "profitHitOccurance2", IIf( VarGet( "profitTargetHitDistance_" + VarGet( "profitHitOccurance2" ) ) < VarGet( "profitTargetHitDistance_" + VarGet( "profitHitOccurance3" ) ), VarGet( "profitHitOccurance2" ), VarGet( "profitHitOccurance3" ) ) );

    VarSet( "profitHitOccurance3", 10 - ( VarGet( "profitHitOccurance1" ) + VarGet( "profitHitOccurance2" ) + VarGet( "profitHitOccurance4" ) ) );
	VarSet( "profitTargetHitSequence", VarGet( "profitHitOccurance1")*1000 + VarGet( "profitHitOccurance2")*100 + VarGet( "profitHitOccurance3")*10 + VarGet( "profitHitOccurance4"));
	StaticVarSet( Name() + chartid + "profitTargetHitSequence",  VarGet( "profitTargetHitSequence"), 1, cmNever );

    VarSet( "profitTarget4iHitOnThisBar", minRetracementWave5XConditionMetOnThisBarWithOpenPosition );
    VarSet( "profitTarget4iPrice", Wave5XMinRetracementPrice );
    

    if( globalLoopCounter > 0 )
    {
        m = 0;

        for( i = 1; i <= 4; i++ )
        {
            for( j = 1; j <= 4; j++ )
            {
                m = m + 1;
                profitTargHitOnThisBar = IIf( j == VarGet( "profitHitOccurance" + i ), VarGet( "profitTarget" + j + "iHitOnThisBar" ), 0 );
                scaleOutQTYPercentOfTotalOpen = IIf( profitTargHitOnThisBar, IIf( j == 1, 0.75, IIf( j == 2 || j == 3, 0.10, IIf( j == 4, 0.75, 0 ) ) ), 0 );
                scaleOutQTY = IIf( profitTargHitOnThisBar, totalOpenPositionsInThisSetup * scaleOutQTYPercentOfTotalOpen, 0 );
                scaleOutPrice = IIf( profitTargHitOnThisBar, Min( O, IIf( j == 1, VarGet( "profitTarget1iPrice" ), IIf( j == 2, VarGet( "profitTarget2iPrice" ), IIf( j == 3, VarGet( "profitTarget3iPrice" ), IIf( j == 4, VarGet( "profitTarget4iPrice" ), 0 ) ) ) ) ), 0 );
                scaleOutNumber = IIf( profitTargHitOnThisBar, j, 0 );
                calculateTradeMetrics( profitTargHitOnThisBar, m );
                //_TRACE("profitTargHitOnThisBar="+profitTargHitOnThisBar+", i="+i+", j="+j+", m="+m);
            }

        }

        for( j = 17; j <= 21; j++ )
        {
            switch( j )
            {
                case 17:
                    profitTargHitOnThisBar = W4iHitOnThisBar;
                    scaleOutQTY = totalOpenPositionsInThisSetup * 0.80;
                    scaleOutPrice = Min( O, Wave4LowiForPosition );
                    scaleOutNumber = 17;
                    break;

                case 18:
                    profitTargHitOnThisBar = W2tpiHitOnThisBar;
                    scaleOutQTY = totalOpenPositionsInThisSetup * 0.60;
                    scaleOutPrice = Min( O, Wave2tpLowiForPosition );
                    scaleOutNumber = 18;
                    break;

                case 19:
                    profitTargHitOnThisBar = W2iHitOnThisBar;
                    scaleOutQTY = totalOpenPositionsInThisSetup * 0.60;
                    scaleOutPrice = Min( O, Wave2LowiForPosition );
                    scaleOutNumber = 19;
                    break;

                case 20:
                    profitTargHitOnThisBar = point0iHitOnThisBar;
                    scaleOutQTY = totalOpenPositionsInThisSetup * 0.60;
                    scaleOutPrice = Min( O, point0iForPosition );
                    scaleOutNumber = 20;
                    break;

                case 21: //profitTargetT
                    profitTargHitOnThisBar = profitTargetHitOnThisBar;
                    scaleOutQTY = totalOpenPositionsInThisSetup * 0.60;
                    scaleOutPrice = Max( profitTargetForPosition, L );//because of padding
                    scaleOutNumber = 21;
                    break;

                default:
                    profitTargHitOnThisBar = 0;
                    scaleOutQTY = 0;
                    scaleOutPrice = 0;
                    scaleOutNumber = 0;
                    break;
            }

            calculateTradeMetrics( profitTargHitOnThisBar, j );
        }

        //_TRACE("000moneyValueForAllScaleOutsOnThisBarSoFar="+moneyValueForAllScaleOutsOnThisBarSoFar +", moneyValueForThisKindofScaleOut="+moneyValueForThisKindofScaleOut);
    }

When chart pattern is in focus it works perfectly. But when out of focus (also in backtest) it fails. I don't know how to fix this.

First thing to do is to remove all those VarSet/VarGet. They are pointless if you are not creating dynamic variable name.

Instead of:

VarSet( "profitHitOccurance1", 0 );

Just write

profitHitOccurance1 = 0;
1 Like

I am removing them. Could the below line be the culprit?

    profitHitOccurance2= IIf( VarGet( "profitTargetHitDistance_" + VarGet( "profitHitOccurance2" ) ) < VarGet( "profitTargetHitDistance_" + VarGet( "profitHitOccurance3" ) ), VarGet( "profitHitOccurance2" ), VarGet( "profitHitOccurance3" ) );

here the VarGet( "profitTargetHitDistance_" + VarGet( "profitHitOccurance2" ) can't be array.

replace that with

    comparator1=comparator2=0;
    //_TRACE("profitHitOccurance3="+VarGet("profitHitOccurance3"));
    for (i=1;i<=4;i++)
    {
    comparator1=IIf(VarGet( "profitHitOccurance2" )==i, VarGet( "profitTargetHitDistance_"+i), comparator1);
    comparator2=IIf(VarGet( "profitHitOccurance3" )==i, VarGet( "profitTargetHitDistance_"+i), comparator2);
    }

    profitHitOccurance2= IIf( comparator1 < comparator2, VarGet( "profitHitOccurance2" ), VarGet( "profitHitOccurance3" ) );

it worked in backtest. :grinning:

moral of the story is
varGet("variableName"+ dynamicpart)

dynamicpart has to be static not array.

You are mixing up things. "Static" refers to lifetime. Array refers to type. So you should have said: Scalar (single number), not array.

yes scalar is the right term. It can be either a number(single) or a string But CANNOT be array. thankyou.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.