Australian (ASX) Stock Market Forum

Amibroker FAQ

Yes, I have made functions for all kinds of US and UK holidays (incl. Good Friday). But they are part of a plugin.
 
Yeah. I was hoping not to have to do that, but I will concede defeat.



It's mainly the good Friday that seems to be the problem. As any other can be screened out with a code that works for each year.

But if anyone has already done that leg work, that'd be awesome, and save me some time.

I found this in a search this morning, not sure if it's applicable to your code but may be worth a look.

I have found that using a specific day of the week can cause
unexpected results when the market isn't open that day. I use the
following approach to evaluate daily data on the last day of the week
(typically but not always a Friday) with trades occurring the next
market day (typically but not always a Monday).

To determine if a given day is in fact the last market day of the
week use:

LastDayofWeek = IIf(DayOfWeek() > Ref(DayOfWeek(),1),1,0);

You can then use ScoreNoRotate if it is not the last day.
You can use SetTradeDelays(1,1,1,1) to execute on next market day.

Using this approach saves you from having to mess with Time Frames
and takes into account that Friday is not always the last trading day
of the week (Good Friday for example in US), and that Monday is not
always the first trading day.

Note that this method "looks ahead" one day to determine if it is the
end of the week. The "Check AFL code" function will give you a
warning. Also because it needs to know if the next trading day is in
this week or next, it may not work very well in real time, but is
good for backtesting purposes.

http://osdir.com/ml/finance.amibroker.ts/2007-04/msg00016.html
 
LastDayofWeek = IIf(DayOfWeek() > Ref(DayOfWeek(),1),1,0);

The code looks ahead to the following day and returns true on the last day. If it's a Friday it evaluates as 5 (Friday) greater than 1 (Monday). If it's a Thursday (ie. no trade on Friday) then it evaluates as 4 (Thursday) greater than 1 (Monday) as there's no 5 (Friday). Hope that makes sense :)
 
Apologies for my disjointed posts, trying to do too much at once atm!

To sell on the last day of the week would be:

Sell = LastDayofWeek;

This would either be the Friday, or if there's no trade on the Friday, would be the Thursday.
 
The code looks ahead to the following day and returns true on the last day. If it's a Friday it evaluates as 5 (Friday) greater than 1 (Monday). If it's a Thursday (ie. no trade on Friday) then it evaluates as 4 (Thursday) greater than 1 (Monday) as there's no 5 (Friday). Hope that makes sense :)

It is not a proper way looking into the future. You can not look into the future of price data in realtime.

A proper code is not dependent on looking into the future of DB data. And it can be done so.
 
It is not a proper way looking into the future. You can not look into the future of price data in realtime.

A proper code is not dependent on looking into the future of DB data. And it can be done so.

Correct.

But we aren't attempting to look ahead into future price data. Only available trading days. Exactly the same as if we know Christmas is on a Friday etc.

I'll give this code a crack and see if it works. :)
 
But we aren't attempting to look ahead into future price data. Only available trading days. Exactly the same as if we know Christmas is on a Friday etc.

Yes you are since Day() is dependent on price data.
On the other hand with proper formula being independent from price data you know the holiday date whenever you want to.

Look I already know the date of Goodfriday of 2016 and following years without having price data yet. Still exploration outputs them. Why? Because formulas are not dependent on arrays.

OBvgB2c.png

The functions output datenum values so you can know the day of end of week of Good Friday like this for example:

Code:
EndOfWeekGF = datenum() == GoodFriday() -1;

You don't look into future price data. So such functions work for backtest and realtime.
 

Attachments

  • OBvgB2c.png
    OBvgB2c.png
    11.4 KB · Views: 0
If not then there's always the option of collating the Friday holiday dates and/or using a plugin like Trash has illustrated in the post above.

There is not a plug-in required to create such holiday functions. It can be coded using AFL also. Multiple roads lead to Rome.
 
With conditionals, if there is a sum, or calculation, is it easier to do this outside of the if function, and then refer to it within the conditional?

For example a = x + y
b = y + z

If( a > b) then

Rather than If (x+y > y+z) then etc.

I ask this because I'm getting problems with this. Especially as I'm trying to optimize some conditions. Will the conditional then accept an optimized value? Or would I have to do that manually?

Cheers.
Chops.
 
Hi Chops --

The compiler for the afl language is pretty good about optimizing expressions. For the example you posted, I doubt that there will be any detectable difference in execution time.

My recommendation is to write code in such a way that code maintenance some time in the future will be as easy as possible for the programmer. Given that, write the expressions, assignments, and functions so that they make the most sense to the person reading the code.

Best,
Howard
 
Thanks Howard.

This is a general question to all. Is there an easy way to analyse percentage movements to optimize buying breakouts and gathering other statistics etc?

For example:

//breakout criteria

pchng = ((O - Ref(C, -1)) / Ref(C,-1)) *100;
PositiveChg = pchng >=0;
PositiveChg = Optimize("PositiveChg",0,0,5,0.2);
Buy = PositiveChg;

I can't seem to get it to optimize PositiveChg before or in the Buy line. Will I have to use another function given I want it to return a numerical value and not just a true or false?

Cheers,
Chops.
 
Thanks Howard.


I can't seem to get it to optimize PositiveChg before or in the Buy line. Will I have to use another function given I want it to return a numerical value and not just a true or false?

Cheers,
Chops.

Buy = .... cannot be a number. The buyline requires a condition.

Try instead:

Buy = ((O - Ref(C, -1)) / Ref(C,-1)) *100 > Optimize("PositiveChg",0,0,5,0.2);

...and lose all the other lines. They are unecessary.
 
I can't seem to get it to optimize PositiveChg before or in the Buy line. Will I have to use another function given I want it to return a numerical value and not just a true or false?

G'day Chops,

This line:
PositiveChg = pchng >=0;
returns either true or false so your currently optimising the wrong variable. I assume you're really wanting to optimise the "pchng" value?
 
Buy = .... cannot be a number. The buyline requires a condition.

Chops code returns true or false for the buy line, not a number.

Try instead:

Buy = ((O - Ref(C, -1)) / Ref(C,-1)) *100 > Optimize("PositiveChg",0,0,5,0.2);

...and lose all the other lines. They are unecessary.

Optimize should be assigned to a variable to make the code more readable.
 
Sweet!

Thanks. :)

How come we can use ">" in the buy line? I would have thought that would have brought up an error like that.

"Returns either true or false so your currently optimising the wrong variable. I assume you're really wanting to optimise the "pchng" value."

That's right. But I couldn't seem to get that. Slowly getting there again...

Cheers.
 
How come we can use ">" in the buy line? I would have thought that would have brought up an error like that.

">" is a comparison operator and always returns true or false:

https://www.amibroker.com/guide/a_language.html

(scroll down to "operators")

It's neater to assign the conditions and optimisation code to variables then call the variables in the buy line (makes it easier to read and adjust).

"Returns either true or false so your currently optimising the wrong variable. I assume you're really wanting to optimise the "pchng" value."

That's right. But I couldn't seem to get that. Slowly getting there again...

:)

I've been using AB for over 10 years and I'm still slowly getting there too... ;)
 
Top