Hi friends,
I am just trying to write a few simple Amibroker formulas to get a feel of how AFL works.
I was trying to scan signals for the following logic:
2 higher highs and 2 higher lows
one inside bar
close > 20 ma > 50 ma
//Exploration
Lowlogic = Ref(L,-1) > Ref(L,-2);
Highlogic = Ref(H,-1) > Ref(H,-2);
Logic1 = C > EMA(C,20) & C > EMA(C,50);
Iblow = L > Ref(L,-1);
Ibhigh = H < Ref(H,-1);
Buy = Lowlogic & Highlogic & Logic1 & Iblow & Ibhigh;
Filter = Buy;
AddColumn(IIf(Buy, C, 0),"Buy", 1.3);
Hi friends,
I am just trying to write a few simple Amibroker formulas to get a feel of how AFL works.
I was trying to scan signals for the following logic:
2 higher highs and 2 higher lows
one inside bar
close > 20 ma > 50 ma
for some reason, i am getting no buy signals.
when i replaced 'close > 20 ma > 50 ma' with ma crossover, I was getting a few signals.
I think it has got something to do with 'close > 20 ma > 50 ma' , but I can't figure out what it is.
Can someone please help me with this???
Thank You.
Code:
lowlogic = Ref(Low,-1)>Ref(Low,-2);
highlogic = Ref(High,-1)>Ref(High,-2);
logic1 = Close> lastValue(EMA(C,20))>lastValue(EMA(C,50));
iblow = Low>Ref(Low,-1);
ibhigh = High<Ref(High,-1);
Buy = lowlogic AND highlogic AND logic1 AND iblow AND ibhigh;
logic1 = Close > EMA(C,20) AND Close > EMA(C,50);
//Exploration
Lowlogic = Ref(L,-1) > Ref(L,-2);
Highlogic = Ref(H,-1) > Ref(H,-2);
Logic1 = C > EMA(C,20) & C > EMA(C,50);
Iblow = L > Ref(L,-1);
Ibhigh = H < Ref(H,-1);
Buy = Lowlogic & Highlogic & Logic1 & Iblow & Ibhigh;
Filter = Buy AND Status( "lastbarinrange" );
AddColumn(C,"Close@Buy", 1.3);
If you wanna do conditions like this one
Code:Lowlogic = L > Ref(L, -1) AND Ref(L,-1) > Ref(L,-2) AND Ref(L,-2) > Ref(L,-3);
Then it is shorter to use
Code:Lowlogic = Sum(L > Ref( L, -1 ), 3 ) == 3;
Plot( C, "Price", colorDefault, styleCandle );
Lowlogic = L > Ref( L, -1 ) AND Ref(L,-1) > Ref(L,-2) AND Ref(L,-2) > Ref(L,-3);
PlotShapes( Lowlogic * shapeSmallCircle, colorRed, layer = 0, y = L, -10 );
Lowlogic = Sum(L > Ref( L, -1 ), 3 ) == 3;
PlotShapes(Lowlogic * shapeSmallCircle, colorYellow, layer, y, -20 );
Hi friends,
Still struggling to understand how AFL works. (Sorry if this is a silly question)
--------------------------------------------------------
myhigh = HHV(Close,20);
mymacd = MACD(12,26) > 0;
myrsi = RSI(14) > 60;
Buy = myhigh AND mymacd AND myrsi;
--------------------------------------------------------
After writing the AFL, i scanned for buy signals for a given stock. But, when I scanned, I noticed that I was getting false signals.
View attachment 63495
As you can notice, one of my conditions is highest value of close of 20 days. So, if close of 25th June was higher than close of 26th June, why did it give me the signal on 26th June??? I am not sure where I am going wrong.
Thanks
Hi,
I think I understood my mistake. When I tried
myhigh = Close> Ref(HHV(Close,20),-1);
instead of
myhigh = HHV(Close,20);
it started working.
As per what I understand, we need a condition for buy. If I just say hhv(close,20) it is not a condition, hence Amibroker won't execute it.
period = 20; // number of averaging periods
m = MA( Close, period ); // simple moving average
Buy = Cross( Close, m ); // buy when close crosses ABOVE moving average
Sell = Cross( m, Close ); // sell when closes crosses BELOW moving average
Short = Cover = 0;
dt = DateTime();
for( i = 0; i < BarCount; i++ )
_TRACE( "Buy sig:" + Buy[i] + ", " + DateTimeToStr( dt[i] ) + " --- Sell sig:" + Sell[i] + ", " + DateTimeToStr( dt[i] ));
printf( typeof( Buy ) );
If you are not sure you may use _Trace or new one in 6.00 _TraceF to see what is output of a variable.
https://technet.microsoft.com/en-us/Library/bb896647.aspx
http://www.amibroker.org/userkb/2007/01/21/debugview/
http://www.amibroker.org/userkb/category/real-time-afl-applications/debugging-afl/
There is also an internal Trace log viewer, Window -> Log.
Also see Tools - Preferences - AFL - Trace output.
I wish to trade a watchlist which varies in time.
Is there a AFL function which would allow me to exclude a symbol from the defined watchlist from a certain date and on the other hand to only recognise a symbol after a certain date.
For instance, Watchlist1 contains the following members:
AAA
BBB
CCC
DDD
EEE
FFF
GGG
HHH
I aim to back test this trading system from 1 January 2012 to 30 June 2015
The first scenario
How to exclude symbol CCC from trading AFTER 1 February 2014
The second scenario
How to exclude symbol EEE from trading BEFORE 1 November 2014
switch( Name() ) {
case "CCC": datecond = dn <= 1140201; break;
case "EEE": datecond = dn >= 1141101; break;
default: datecond = true; break;
}
period = 20;
m = MA( Close, period );
Buy = Cross( Close, m ) AND datecond;
Sell = Cross( m, Close ) AND datecond;
Short = Cover = 0;
Code:switch( Name() ) { case "CCC": datecond = dn <= 1140201; break; case "EEE": datecond = dn >= 1141101; break; default: datecond = true; break; } period = 20; m = MA( Close, period ); Buy = Cross( Close, m ) AND datecond; Sell = Cross( m, Close ) AND datecond; Short = Cover = 0;
dn = DateNum();
SetTradeDelays( 1, 1, 1, 1 );
Buy = Close > MA( Close, 50 );
Sell = (MA(Close,50)>C);
Buy = Close > MA( Close, 50 );
Sell = Cross(MA(Close,50), C);
Sorry, I know this is probably very straight forward but for some reason I am having a problem.
With the following code:
Code:SetTradeDelays( 1, 1, 1, 1 ); Buy = Close > MA( Close, 50 ); Sell = (MA(Close,50)>C);
If I am exploring a date range, say from 14/01/15, and the close on 14/01/15 is already above MA(Close,50) then I am expecting that I will buy at the opening on 15/01/15.
This is not what I am getting. The buy is only being generated in the future after a sequence which firstly has a close below MA(Close,50) say on 15/02/15. The buy is generated the next time the close goes above MA(Close,50), say 16/03/15 - buy taking place on 17/03/15 at the open price.
As I said I am hoping to achieve an immediate buy from the date of my backtest, as my condition is already met and not wait until a future date.
I presume the answer is extremely obvious, but ......
I used this code and got 4 buys next day after test begin date. I assume you meant sell at crossing of price close and MA50.?
Code:Buy = Close > MA( Close, 50 ); Sell = Cross(MA(Close,50), C);
buy = ExRem( buy, sell );
So what is with your problem few posts ago?
Is this supposed to be a code factory where you just have to grab your stuff??
Hi Trash - yes thanks for your assistance before - your code worked.
As to code factory - I always try to get an answer from searching forums - but in absence of getting an outcome I do post for assistance. The last post was in some sense pretty obvious, except it wasn't when I posted.
Your input into these forums is always well informed and appreciated. Forgive someone who is challenged, but tries his hardest.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?