Buy single scrip at every 100 days interval

Buy 	=  1;
Buy	= Buy AND BarsSince(Buy) > 100;
Sell 	=  0;

ApplyStop(stopTypeNBar, stopModeBars, 10);

I want to buy same scrip at every interval of 100 days and hold it for 10 days on daily timeframe.
But when I run the code, it does nothing. Any idea how to achieve this task.

BUY1

Hi Siraj,
your code doesn't produce any trade because you wrote wrong conditions. You have to consider that AFL interprete any variable as an array and the word "buy" and "sell" are reserved for long entry and exit signals. For example by writing:
Buy = 1;
you are telling AFL to buy for every day. Moreover adding the second condition is pointless because all the possible entry points are already set on TRUE.

For further information on how AFL works I can suggest to read carefully the manual.

Your task could be solved in different ways either by looping or using the array features of AFL.
I could suggest you this one that use AFL's array processing features:

// buy_every_100_days.afl

//determining the first trading day:
bi = BarIndex();
firstBI = ValueWhen(Status("firstbarinrange"),bi);
barsSinceFirstBuy = bi - firstBI;

//	signals every 100 bars:
Buy = barsSinceFirstBuy % 100 == 0;
Sell = 0;

//	stop after 10 bars
ApplyStop(stopTypeNBar, stopModeBars, 10);

5 Likes

@2DD thanks for the suggestion it worked!

1 Like

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