Problem in loop?

Hello,
If condition 4 is satisfied, then I want the next condition to be satisfied should be 1 i.e. if condition 2 or 3 occurs after 4 then avoid them and take only 1 when it comes. I have written this loop for this but it is not plotting the desired buy signals. Please help.
Also kindly tell me how to use trace function so that I can debug my codes in the future.
Thanks

Condition1 = Close > MA(Close,10);
Condition2 = Close > MA(Close,15);
Condition3 = Close > MA(Close,20);
Condition4 = Close > MA(Close,25);

Buy = 0;

Plot(MA(Close,10),"ma10",colorWhite);
Plot(MA(Close,15),"ma15",colorYellow);
Plot(MA(Close,20),"ma20",colorRed);
Plot(MA(Close,25),"ma25",colorPink);


for ( i = 0 ; i < BarCount - 1 ; i++)
{ 

  if (Condition4[i])
   {
     for ( j = i + 1   ; j < BarCount - 1 ; j ++)
      
         {
            if (Condition2[j])
            {}; 
            if (Condition3[j])
            {}; 
            if (Condition1[j])
            (Buy[j]);
         }
    }   
}
//PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorGreen, 0, L, Offset = -30 );

Try to use BarSince() instead of loop , Something like the below example should work fine and give you the desired result

Condition5 = BarsSince(Condition4) > BarsSince(Condition1);
Condition5 = Condition5 AND ( Condition1 OR Condition3 )
2 Likes

Thank you so much. That was a crisp solution.

Also when i am plotting shapes, the shape is repeating on every bar when it is getting satisfied.
So lets say if condition 4 is satisfied on any bar, how do I remove the excessive signals until (any of the condition 1,2 or 3) is met ?

As exrem works on only 2 variables, I am a bit confused

I am not sure that i understand what you want but the solution is likely to be a combination of Flip() and Exrem()

You should assign computations like these ma10 = MA(Close,10); to a variable and use that variable subsequently in your code for optimal performance.

If Condition2 and 3 aren't required, then you can skip them altogether instead of defining them unless you are using them to negate/cancel condition 1 or 4.

Sorry Travick but I did not understand what you said ? How can I modify if (Condition2[j]) in a better way ?

Also I can't remove condition2 and 3 since I also want them if they are in order i.e condition 1 then 2 then 3 and then 4 and then again 1

Can you please let me know what is wrong with the loop I wrote above ?
Thanks for your help

Can you explain what you are trying to do, I'm not really sure what you mean by the order of the conditions.
Use a chart to show a True and False scenario. that would be better.

Lets say at 10 am my condition 4 triggers.

10:15 am condition 2 triggers ( I want to ignore it )
10:30 am condition 3 triggers ( I want to ignore it )
10:45 am condition 1 triggers (I want make trade here )
So in a nutshell, I want to avoid condition 2 and condition 3 if they come and directly want to trade condition 1 when it comes.
Also above is solved my Sebastian by use of different function however i want to know whats wrong in the loop I shared above ?

But that was the whole point of my post. If you are ignoring Conditions 2 & 3, why define them in the first place? You are not using them anyway to negate the succeeding events.

AFL is designed on array processing, not conventional scalar general purpose programming languages.
Technically, you can loop everything, but by convention using a loop is the last resort and used only when certain things have to be in a loop only.

As far as your loop is concerned, you have nested for reason I cant get because its still unclear what you mean by avoid 2 and 3.
Those are just empty if() one after the other, not even if-else which would make more sense.

And from @Sebastian's post, I don't get condition 3's purpose.
Condition5 = Condition5 AND ( Condition1 OR Condition3 )
was it supposed to be 4? because now condition2 seems to be left out.

Tracvick, well, If i did not incorporate condition 2 and condition 3 in the loop, then don't you think that it could have been in action after condition 4 too, which I don't want.

Also if condition 2 occurs after condition 1, then i would take it since its after condition 1 and not after condition 4
Explanation of the loop - First the loop will check the condition 4 . If it is not satisfied, it will check it again when i ++. If condition 4 is satisfied it will go inside the loop and test 2 and 3. If they occur, it will ignore them and then test condition 1 . If it takes place then loop will signal buy.

Please let me know if the above concept of loop is right or not ? I am still new to loops

            if (Condition2[j])
            {}; 
            if (Condition3[j])
            {}; 
            if (Condition1[j])
            (Buy[j]);

In your own words, can you explain what you are trying to do ?
Condition 2/3 aren't doing anything, nor altering the flow of the loop in any way and will therefore just be redundant.

if (Condition2[j])
{};

when the code sees this, shouldn't the loop avoid condition 2 which I want. Lets say if i did not have condition 2 in this loop, then condition 2 could have been triggered.
By leaving the {} empty, I am asking the code to do nothing if condition 2 occurs

Is my thought process right, travick ?

Well, in this context, if you don't want to do anything for condition 2 or 3, then you just don't need those lines.
in this case, code sees condition2 == True, doesn't do anything, so what the point. same for 3.

You will only change or alter the Flow if you used Else

            if (Condition3[j])
            {
            }
            else if (Condition1[j])
            {
                Buy[j] = .....;
             }

only in this case, if 3 were to be true, then the entire check for 1 would be skipped thereby achieving a logic.

Just read any tutorial or language that you are familiar with, although AB/AFL is based on C++, if that's too hard try javascript and can familiar with programming constructs etc.
The AFL manual also has a wealth of info but just if want to get familiar with scalars and then move to Arrays which AFL is all about.

{};
the semi-colon was erroneously copied from previous code, should not be there.
Certain closing brackets can terminated by ; but not mandatory. I personally am not in a habit of using them as most by convention.

@travick
The above formula has a small typo but i think it 's clear now for OP how he can use BarsSince() Function to sort the occurrence of conditions as he like
Cheers

1 Like

well thanks for helping me out. I am still confused in loops. I have one more loop to identifty supply and try is sell signals comes but still the loop is not giving me the desired output. There is no syntax error in amibroker however no sell is getting triggered. Please let me know what is wrong in this loop

bigcandle = abs(Close- Open) > 0.05 * Close AND Open > Close;  // big candle is when candle is 5% big and open > close
highofbigcandle = ValueWhen(bigcandle,High);  // recording the high of big candle
Sell = 0;
j = 0;
for ( i = 0 ; i < BarCount -1 ; i++)

{  
   if (bigcandle[i])  
    {  
    for ( j = i+1 ; j < BarCount -1 ; j ++) 
           if(High[j] > highofbigcandle[j]) 
           Sell[j];
           
      }     
}

Plot(Sell,"Sell",colorWhite);

That code is not correct and in addition you just need single loop.
And, what sense does a Sell without Buy (e.g. at "big candle") make?

percent = 1;
bigcandle = abs(C-O) / C * 100 > percent AND Open > Close;  // big candle is when candle is 5% big and open > close

BuyPrice = Open;
Buy = Ref(bigcandle,-1); 
Sell = 0;

/// @link https://forum.amibroker.com/t/problem-in-loop/11199/17
/// based on trail stop KB example
/// @link www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/
high_at_buy = Null;
hh_bigCandle = 0;
for ( i = 1; i < BarCount; i++ )
{
	// store bar High at Buy signale
	if ( hh_bigCandle == 0 AND Buy[ i ] )
		hh_bigCandle = High[ i-1 ];
	else Buy[ i ] = 0; // remove excess buy signals

	// Sell if Buy bar's high is broken
	if ( hh_bigCandle > 0 AND High[ i ] > hh_bigCandle )
	{
		Sell[ i ] = 1;
		SellPrice[ i ] = Max( O[ i ], hh_bigCandle);
		hh_bigCandle = 0;
	}

	// create array of Buy bar's high
	if ( hh_bigCandle > 0 )
		high_at_buy[ i ] = hh_bigCandle;
}

PlotShapes(ExRem(bigcandle,Sell)*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Buy*shapeSmallCircle,colorGreen,0,BuyPrice, 0);
PlotShapes(Sell*shapeDownArrow,colorRed,0,High);
PlotShapes(Sell*shapeSmallCircle,colorRed,0,SellPrice, 0);

Plot( Close,"Price",colorDefault,styleBar);
Plot( high_at_buy, "high_at_buy", colorGreen );

53


Are you and @thetrader one and same user?
Why do you confuse people with two IDs?

Thank you so much fxshrat. You have helped me great deal. Actually it was my mistake but instead of sell, I should have written short. Because of which you have coded something else but still it was immensely helpful. It would be great if you could help me out with the following below.
So all I want is the moment when prices touch the high of the big candle (for shorting purpose)
In the above code, only one big candle is recorded until its high is breached. Till then no other big candle is taken into consideration.
How can i modify the codes in such a way that all the supplies are recorded and as soon as the retest is made, that supply is forgotten as its no more fresh
so lets on 1st jan a big supply is formed.
2nd jan - no retest to 1st jan supply
3rd jan - new supply formed
6th jan - retest to 3rd jan supply ( I want sell here) ( 1st jan should be there since its fresh)
and so on
Thank you very much fxshrat

Yes thetrader is me only. It was a funny incident. P button on my keyboard has stopped working which was in the password for that account. So had to make new.

Please reply fxshrat

fxshrat where are you ?