I'm a newbie in Amibroker, in fact this is my second post in this forum. I'm trying to learn, but that's not easy if you are not computer scientist or something like that...
So, now I'm triying to place myself into little tasks. For instance right now I looking for a system that looks for gaps down, with more or equal to 30%, and then past five days of that gap, buy.
Then hold for one year, or when you have a profit around 20%, just sell.
In "easy" coding will be:
If gap >= 30% AND day=5
BUY
If day= 365 or price >= 20%
SELL
Somebody could help me to place this into AFL language?
And if I would like to change the second condition (price>=20%) for a trailing take profit, how it will be?
Thanks a lot, and sorry for such an easy question. I promise I will improve.
Thanks a lot for your answer Portfoliobuilder. I read the Bandy's book, Introduction to Amibroker, and also I watched the video of gaps. I place the other books you say to my "wishes list".
I think I need to go back to school and looking for the AFL course I missed and do it for one year at list.
@Monday, one small additional tip: one trading year in USA markets is usually estimated in 252 days (in general you need to take care of the trading days and not of the 365 calendar days).
@Monday based on your previous description, as far as I understand it, you probably do not need to provide any condition to Sell, just use the different ApplyStop modes for a Profit Taking, to close the trade after a certain number of bars and optionally for a Trailing Stop.
Note that the way you are using the ApplyStop function in the above code is WRONG since by definition it returns NOTHING.
See the ApplyStop documentation.
By the way, the formula code editor will provide you all the correct required code if you use the "Insert Snippet" feature!
Moreover, when you have the correct basic code in place, it will be probably useful to add some additional conditions. I suggest to set a minimum price to exclude penny stocks and define both a minimum and a maximum gap value to limit trades to a specific gap range (to avoid for example a -90% gap down).
Unfortunately, I cannot help more since, my expertise in developing trading systems is still very limited, but I'm sure some more advanced users will step in to help if you persist in your coding/learning effort!
One final tip: IMO this is the kind of strategy to test ONLY if you have a very reliable data source, with prices that includes all the possible prices splits and reverse splits, special dividends etc. otherwise your results will be greatly misleading!
@Monday: one other thing to note is that the Buy logic that you are using requires a 30% gap down today, as well as a 30% gap down 5 days ago. I'm not sure that's what you intended.
If you think you're getting too many or too few signals, you should probably use an Exploration to check the values of your variables. Explorations are very easy to create: You just need to set the Filter variable and use the AddColumn() function for each variable that you want to output.
Yes!! I have Norgate data, then I think it's a good source, I'm sure it works, far better than my code I guess
The Snippets are GREAT!!! Thanks Beppe. They will help me a lot.
Now I think I corrected the Apply stop function, and I added a line in order to avoid 90% gaps as you suggested. I think it's a good contribution, also I added another line to skip iliquid stocks by volume,
Let me work a little bit more on the code and I post it here.
I think I fixed the part of the apply stop, now works good with a 10% profit. Also I fixed a filter for volume.
The part who try to avoid stocks with a 90% gap down sends an error and I still don't know how to tell Amibroker he must looking for gaps since the begining of the backtest to the end.
SetOption("ExtraColumnsLocation",1);
SetOption("Commissionmode",2);
SetOption("Commissionamount",1);
SetOption("InitialEquity", 10000);
SetOption( "AllowSameBarExit", True );
SetOption( "ReverseSignalForcesExit", False );
SetOption( "HoldMinBars", 1 );
SetOption( "AccountMargin", 100 );
SetOption("maxopenpositions", 50);
SetPositionSize(5, spsPercentOfEquity);
BigGapDown = ( H < 0.7*Ref(L, -1) ); // today's High 30% below yesterday's Low
//BigGapDownMax= (H < 0.1*Ref(L, -1)); This line try to avoid stocks with a gap of 90% but do not work
Filter= Volume > 5000;
FiveDaysLater = Ref(BigGapDown, -5);
Oneyear= Ref(BigGapDown, -252);
Buy = FiveDaysLater AND BigGapDown AND Filter;
Sell = Oneyear OR true;
amount = 10; // 10% profit
ApplyStop( stopTypeProfit, stopModePercent, amount, True );
Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell, Buy);
Sorry @Monday, I used shorthand that most people who use AFL will understand. When I said "today" and "five days ago", I really meant that on each bar of the analysis, AmiBroker will evaluate whether the BigGapDown condition is true on the current bar and was also true 5 bars prior to the current bar. That's the beauty of AmiBroker's array processing: all elements of the array are processed in a single operation so that you don't have to iterate through them.
The question for you to consider is whether you require two gaps to enter a trade, or if you simply want to enter 5 days after a BigGapDown condition occurs.
Hello everyone. Thanks to all of you for your help. The recomendation about remove "BigGapDown" do the job.
I keep fighting against Amibroker and the coding issue.
It looks like the "buy" works, more or less, as I want to. But the "sell" part is underwater.... just I want to sell the stock after one year holding OR if we have a 10% gains.
I think my code it's not right.
This is the code:
SetOption("ExtraColumnsLocation",1)
SetOption("Commissionmode",2);
SetOption("Commissionamount",1);
SetOption("InitialEquity", 10000);
SetOption( "AllowSameBarExit", True );
SetOption( "ReverseSignalForcesExit", False );
SetOption( "HoldMinBars", 1 );
SetOption( "AccountMargin", 100 );
SetOption("maxopenpositions", 50);
SetPositionSize(5, spsPercentOfEquity);
BigGapDown = ( H < 0.7*Ref(L, -1) ); // today's High 30% below yesterday's Low
//BigGapDownMax= (H < 0.1*Ref(L, -1)); This line try to avoid stocks with a gap of 90% but do not work
Filter= Volume > 5000;
FiveDaysLater = Ref(BigGapDown, -4);
Oneyear= Ref(BigGapDown, -252);
Buy = FiveDaysLater AND Filter;
Sell = oneyear;
amount = 10; // 10% profit
ApplyStop( stopTypeProfit, stopModePercent, amount, True );
Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell, Buy);