All, I am a newbie on AFL although I am an experienced trader. I am trying to get used to this language. In fact I wrote the following script and when I run it, I don't get the intended results. Can you please help me with this? The idea of the code is to give Buy signal for all the Stocks / Futures whose EMA follows: Buy - If latest value of 50 EMA > 50 EMA > 100 EMA Sell - If latest value of 50 EMA < 50 EMA < 100 EMA Code: _SECTION_BEGIN("AFL Example"); LongMA = EMA( C, 100 ); MidMA = EMA( C, 50 ); ShortMA = EMA( C, 20 ); Buy = Cross( ShortMA, MidMA ) AND Cross(MidMA,LongMA); Sell = Cross( LongMA, MidMA ) AND Cross(MidMA,ShortMA); Filter=1; AddTextColumn( FullName(), "Company Name" ); AddColumn( Buy, "Buy", 1 ); AddColumn( Sell, "Sell", 1 ); AddColumn( C, "Close", 1.3 ); AddColumn( H, "High", 1.3 ); AddColumn( ShortMA, "Short MA", 1,3 ); AddColumn( MidMA, "Mid MA", 1,3 ); AddColumn( LongMA, "Long MA", 1,3 ); _SECTION_END(); Please help. Thanks, Prash
See code below. Not tested Code: _SECTION_BEGIN("AFL Example"); LongMA = EMA( C, 100 ); MidMA = EMA( C, 50 ); ShortMA = EMA( C, 20 ); Buy = Cross( ShortMA, MidMA ) AND Cross(MidMA,LongMA) || ShortMA > MidMA and MidMA > LongMA; Sell = Cross( LongMA, MidMA ) AND Cross(MidMA,ShortMA) || ShortMA < MidMA and MidMA < LongMA; Filter=Buy || Sell; AddTextColumn( FullName(), "Company Name" ); AddColumn( Buy, "Buy", 1 ); AddColumn( Sell, "Sell", 1 ); AddColumn( C, "Close", 1.3 ); AddColumn( H, "High", 1.3 ); AddColumn( ShortMA, "Short MA", 1,3 ); AddColumn( MidMA, "Mid MA", 1,3 ); AddColumn( LongMA, "Long MA", 1,3 ); _SECTION_END(); Do you really wanna have a cross of shortma with midma and cross midma with longma at the same time? Do yo u know that this should occur very rare both crossing at the same bar. This would make more sense Cross( ShortMA, MidMA ) AND MidMA > LongMA for a buy signal
In my opinion this is what you were looking for Code: _SECTION_BEGIN("AFL Example"); LongMA = EMA( C, 100 ); MidMA = EMA( C, 50 ); ShortMA = EMA( C, 20 ); Buy = Cross( ShortMA, MidMA ) AND MidMA > LongMA ; Sell = Cross( LongMA, MidMA ) AND MidMA < LongMA; Filter=Buy || Sell; AddTextColumn( FullName(), "Company Name" ); AddColumn( Buy, "Buy", 1 ); AddColumn( Sell, "Sell", 1 ); AddColumn( C, "Close", 1.3 ); AddColumn( H, "High", 1.3 ); AddColumn( ShortMA, "Short MA", 1,3 ); AddColumn( MidMA, "Mid MA", 1,3 ); AddColumn( LongMA, "Long MA", 1,3 ); _SECTION_END(); (ShortMA > MidMA AND MidMA > LongMA) or (ShortMA< MidMA AND MidMA < LongMA) would produce too many signals.
Just saw an error in your code. Never use "," for setting a comma in English! So change all "1,3" to "1.3".
Thanks a ton for highlighting the mistake and the correct way to implement the logic that I had in mind. Much Appreciated. Thanks, Prash