NexusFi: Find Your Edge


Home Menu

 





How to use if() function to check value returned from cross() function


Discussion in Platforms and Indicators

Updated
      Top Posters
    1. looks_one occasionallurker with 2 posts (0 thanks)
    2. looks_two vampirelord with 1 posts (0 thanks)
    3. looks_3 bob303 with 1 posts (0 thanks)
    4. looks_4 Cippo with 1 posts (0 thanks)
    1. trending_up 3,025 views
    2. thumb_up 0 thanks given
    3. group 4 followers
    1. forum 4 posts
    2. attach_file 0 attachments




 
Search this Thread

How to use if() function to check value returned from cross() function

  #1 (permalink)
vampirelord
Hong Kong
 
Posts: 1 since Feb 2014
Thanks Given: 0
Thanks Received: 0

Dear all, I am a beginner to Amibroker & AFL.
Actually, I want to implement the following logic:
I want to generate the buy signal when both EMA and MACD shows crossover but they need not to happen on the same day (e.g. EMA shows crossover today, and 3 days later, MACD shows crossover, I still buy the stock)
The following gives more details:
When 10 days EMA crossovers 20 days EMA, remember this as part of the buying signal by assigning TRUE to a variable.
Then, look at MACD, if the MACD line crossovers signal line, remember this buying signal by assigning TRUE to another variable.
After that, check both buying signal. If both are TRUE, buy the stock.

I would like to use if() function to check value returned from cross() function. However, an error occurs. I know cross() function returns an array with the value of true/flase or 1/0 and may be that's why causing the error.
Can anyone tell me how to extra the "true" or "false" value from cross() function and can be checked by if() function?
Thank you very much!

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
ZombieSqueeze
Platforms and Indicators
NexusFi Journal Challenge - April 2024
Feedback and Announcements
Request for MACD with option to use different MAs for fa …
NinjaTrader
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Retail Trading As An Industry
67 thanks
Battlestations: Show us your trading desks!
48 thanks
NexusFi site changelog and issues/problem reporting
47 thanks
GFIs1 1 DAX trade per day journal
32 thanks
What percentage per day is possible? [Poll]
31 thanks

  #2 (permalink)
Cippo
Rome/Italy
 
Posts: 2 since Feb 2014
Thanks Given: 0
Thanks Received: 0

I think you have to use "IIF" instruction instead of "if"
"IIf" function deals array, "if" deals variables.
See the manual for IIF function or post your TS.

Reply With Quote
  #3 (permalink)
 occasionallurker 
London UK
 
Posts: 18 since Mar 2014


you don't need if or IIf for this.

 
Code
dd = Day();
ema1 = EMA(C, 10);
ema2 = EMA(C, 20);
cross1 = Cross( ema1, ema2);
cross2 = Cross( MACD(), Signal() );
Buy = ema1 > ema2 AND cross2 AND ValueWhen( cross1, dd ) != ValueWhen( cross2, dd );

Reply With Quote
  #4 (permalink)
bob303
paris France
 
Posts: 5 since Mar 2014
Thanks Given: 1
Thanks Received: 1

You can use the function BarsSince for choose the max delai for Cross2 :

delai = Param("max delai since cross2",3,1,14,1);
ema1 = EMA(C, 10);
ema2 = EMA(C, 20);
cross1 = Cross( ema1, ema2);
cross2 = Cross( MACD(), Signal() );
Buy = ema1 > ema2 AND BarsSince(cross1) <= delai AND BarsSince(cross1) >= 1 AND Cross2 ;

// like says occasionallurker, you don't need if or iif

Reply With Quote
  #5 (permalink)
 occasionallurker 
London UK
 
Posts: 18 since Mar 2014


bob303 View Post
You can use the function BarsSince for choose the max delai for Cross2 :

delai = Param("max delai since cross2",3,1,14,1);
ema1 = EMA(C, 10);
ema2 = EMA(C, 20);
cross1 = Cross( ema1, ema2);
cross2 = Cross( MACD(), Signal() );
Buy = ema1 > ema2 AND BarsSince(cross1) <= delai AND BarsSince(cross1) >= 1 AND Cross2 ;

// like says occasionallurker, you don't need if or iif


Your code is using barindex. So it will work in EOD time frame only. If he wants to use intraday with day delay then this code should work. Not tested.

 
Code
dd = DaysSince1900();

ema1 = EMA( C, 10 );
ema2 = EMA( C, 20 );

cross1 = Cross( ema1, ema2 );
cross2 = Cross( MACD(), Signal() );

// delay of x days
delay = ValueWhen( cross2, dd ) - ValueWhen( cross1, dd ) >= 3;

Buy = ema1 > ema2 AND cross2 AND delay;

Reply With Quote





Last Updated on March 28, 2014


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts