What's the difference between

 

Hello I have 2 codes, that look almost the same but the results on backtest are 100% different. Can somebody tell me what is wrong with 2nd code because that is the one that is broken.

1st code:

int start()
{               
double MA_1_t=iMA(NULL,0,FastMAPeriod,0,1,0,0);
double MA_2_t=iMA(NULL,0,SlowMAPeriod,0,1,0,0); 

   if (MA_1_t < MA_2_t - Distance*UsePoint)  
                        
   {Sell();}  //here is the block for sell function
}

2nd code:

int start()
        {
         if (signal()==sell_ma)
   {Sell();}
        }

//-------------------------------------------------------------------- 
int signal()
{
      double MA_1_t=iMA(NULL,0,FastMAPeriod,0,1,0,0);
      double MA_2_t=iMA(NULL,0,SlowMAPeriod,0,1,0,0); 
      
  if(use_ma)
  {
          if (MA_1_t > MA_2_t + Distance*UsePoint) return(buy_ma);
          if (MA_1_t < MA_2_t - Distance*UsePoint) return(sell_ma);
  }

} 

On the backtest 2nd code still open sell while there is buy signal... for ex: I take daily with 10 and 20 ema and from 6 June to 19 June which is buy signal only, my 2nd code open 10 sell. While the 1st code work properly and do not open any sell order on that period.

 

The signal() function returns zero if no condition is met. Is it expected result?

A good practice is to assign the return value to a variable and return the variable at the end of the function.

 
darchii:

Hello I have 2 codes, that look almost the same but the results on backtest are 100% different. Can somebody tell me what is wrong with 2nd code because that is the one that is broken.

1st code:

2nd code:

On the backtest 2nd code still open sell while there is buy signal... for ex: I take daily with 10 and 20 ema and from 6 June to 19 June which is buy signal only, my 2nd code open 10 sell. While the 1st code work properly and do not open any sell order on that period.

What is the value of use_ma ?
 
RaptorUK:
What is the value of use_ma ?


it's bool, true


Ovo:

The signal() function returns zero if no condition is met. Is it expected result?

A good practice is to assign the return value to a variable and return the variable at the end of the function.

Nothing has been changed even with adding "return(0);"


----------------------

And I would like to use the function signal(); because later i am thinking to add more signal, and if i have 3-4 indicators the start() function will be hard to be modify on of the indicator.

 

darchii:On the backtest 2nd code still open sell while there is buy signal... for ex: I take daily with 10 and 20 ema and from 6 June to 19 June which is buy signal only, my 2nd code open 10 sell. While the 1st code work properly and do not open any sell order on that period.

you use MA value of shift #0, which probably changes on almost every tick - this may result (correct) signals which are not the same as the signal you see at the end of the bar.

retry the test with MAs of bar shift #1.

 
szgy74:

you use MA value of shift #0, which probably changes on almost every tick - this may result (correct) signals which are not the same as the signal you see at the end of the bar.

retry the test with MAs of bar shift #1.




Still open short position on that buy period (or long on sell period).
 
what is the value of buy_ma and sell_ma? is it the same in the function and where you check the return value?
 
szgy74:
what is the value of buy_ma and sell_ma? is it the same in the function and where you check the return value?

This one was the problem, thank you for you time. I just tried to take one block from one EA, and forget to define those, i will have to figure out how to, now.

 
darchii:

This one was the problem, thank you for you time. I just tried to take one block from one EA, and forget to define those, i will have to figure out how to, now.



you can define constants at the beginning of your program, like this:


#define SIGNAL_NONE 0
#define SIGNAL_BUY   1
#define SIGNAL_SELL  2
#define SIGNAL_CLOSEBUY 3
#define SIGNAL_CLOSESELL 4
 
szgy74:

you can define constants at the beginning of your program, like this:



It was define like this in the EA from where i took that block, but it still open buy on sell signal(true that its less but still open and should not), so that's why i should figure something else based on boolean. Because i am thinking to have more than one indicator. Something like if buy_indicator1 is true than check for buy_indicator2 to be true and so on and only if both of them are true the signal should start the buy/sell function.
 
darchii:


Nothing has been changed even with adding "return(0);"

That would surprise me if return(0) changed anything. The question was not about the missing return, but whether the default value 0 was expected, because you did not publish definition of sell_ma and buy_ma.
Reason: