How to looking for gaps


#1

Hi everybody.

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.


#2

@Monday you need to make an effort to code and then post asking for improvements.

Ask you self what is a GAP? Then make an attempt to code it (AmiBroker has built in functions for GapUp and GapDown).

Here are a couple of examples to get you running (untested so test to make sure they work).

aGapUp = ( L > Ref(H, -1) ); // Identify GapUp bars
aGapDn = ( H < Ref(L, -1) ); // Identify GapDown bars

BigGapUp = ( L > 1.3*Ref(H, -1) ); // today's Low 30% above yesterday's high
BigGapDn = ( H < 0.7*Ref(L, -1) ); // today's High 30% below yesterday's Low

// you want to wait 5 days, so if using Daily Bars
FiveDaysLater = Ref(BigGapUp, -5);

Read up on Applystop and look for your Profit taking stop

That should get you started. Then read on, the next two Bandy books,


And for more on Gaps watch this fun introductory video.

Good luck!


#3

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.

But I'll try again with the tips you give me. :wink:


#4

@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).


#5

thanks a lot for the tip beppe, in fact I never though before about it.


#6

Hello everybody.

Here I'm again, but now with the homework done.

But, I think it doesn't work. :sweat_smile:

First of all there are too many arrows (sell and buy signals), quite rare if you consider that I use the comand "EXrem" to get rid of them...

And the gap down of 30% doesn't appear in the stocks Amibroker choose.

I launched the code in Nasdaq from 2000 to 2015.

If you have any tip it will be apreciated. :wink:

CAPITAL= 100000;// Capital inicial

SetOption("ExtraColumnsLocation",1);
SetOption("Commissionmode",2);
SetOption("Commissionamount",1); 
SetOption("InitialEquity", CAPITAL);
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 
FiveDaysLater = Ref(BigGapDown, -5);
Oneyear= Ref(BigGapDown, -252);
Profit= ApplyStop( 2, stopTypeTrailing, 30, 0, volatile = False, ReEntryDelay = 0, ValidFrom = 0, ValidTo = -1 );

Buy = FiveDaysLater AND BigGapDown; 
Sell = Oneyear OR Profit;

Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell, Buy); 

#7

@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!


#8

@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.


#9

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 :grinning:

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.


#10

Hello.

Hello Mradtke.

Yes. You right. I'm not looking for gaps today or 5 days ago.

I'm looking for gaps since the begining of the backtest to the end. I'm looking for gaps every day.

But, I don't know how to say this in AFL code.

When is going Google to make something usefull for mankind and provide us an AFL translator.??!? :rofl::rofl::rofl:


#11

Well, at last this is how my code looks like...

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); 

#12

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.


#13

Don't worry Mradtke, any help it's wellcome, this includes the non clear helps. The aim is the important.

So, about the question, I'm sure that I don't need a two gaps condition to enter. Just a simple gap and 5 days after that gap occurs enter.


#14

Buy when 5 days ago a 30% gap down happen AND today is also a Biggapdown AND today the volume is above 5000

Don't think this is what you want....


#15

If that's the case, then you should remove BigGapDown from your Buy statement.


#16

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); 

Any ideas?:sweat_smile:


#17

Oh oh oh.... I THINK IT WORKS!!!!!

Just I change the take profit order, the 10% for a 30%, just to have best view of the trades, and now looks fine to me.

What do you think?

How can I set up a condition in order to avoid the gaps of more than a 90%?


#18
Sell = 0; 

// or exit one year after entry (EOD bars)
ApplyStop( stopTypeNBar, stopModeBars, 252, priority = 1 );

amount = 10; // 10% profit
ApplyStop( stopTypeProfit, stopModePercent, amount, True );

#19

wow, that's very interesting. It is possible to place to different Applystop orders in the same code?


#20

Yes, that is correct.