Timing of trade orders not coinciding with CI timings

 

I am using iCustom calls to a custom indicator I created that manually calculates the macd using EMA. My CI works great, I see buy and sell signals marked with green and red crosses. The problem is that the timing of the buys/sells in my EA does not correspond with the indicator crosses. Some of them are missed completely and there are trades when no crosses are identified in my CI. What am I doing wrong here?

Here is my logic for calling the CI using iCustom:

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

double M_c = iCustom(NULL,0,"MACD_rc",0,0); //get the current macd
double M_p = iCustom(NULL,1,"MACD_rc",0,0); //get the previous macd
double S_c = iCustom(NULL,0,"MACD_rc",1,0); // get the current signal
double S_p = iCustom(NULL,1,"MACD_rc",1,0); // get the previous signal
if(M_c < S_c && M_p > S_p)
{
Opn_B=true; //open buy order
Cls_S=true; // close sell order
}
if(M_c > S_c && M_p < S_p)
{
Opn_S=true; //open sell order
Cls_B=true; // close buy order
}

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

Here is a result of a back test, the trades are happening very late according to my signals and some are missed completely.


Result

 

double M_c = iCustom(NULL,0,"MACD_rc",0,0); //get the current macd
double M_p = iCustom(NULL,1,"MACD_rc",0,0); //get the previous macd
double S_c = iCustom(NULL,0,"MACD_rc",1,0); // get the current signal
double S_p = iCustom(NULL,1,"MACD_rc",1,0); // get the previous signal


These 0 and 1 numbers refer to timeframes.

 
ggekko wrote >>

double M_c = iCustom(NULL,0,"MACD_rc",0,0); //get the current macd
double M_p = iCustom(NULL,1,"MACD_rc",0,0); //get the previous macd
double S_c = iCustom(NULL,0,"MACD_rc",1,0); // get the current signal
double S_p = iCustom(NULL,1,"MACD_rc",1,0); // get the previous signal

These 0 and 1 numbers refer to timeframes.

As in periods (M5, H1) or time offsets from current?

 
bigcheez:

As in periods (M5, H1) or time offsets from current?

As periods. The offset is determined by the shift.

double iCustom( string symbol, int timeframe, string name, ..., int mode, int shift)


Try like this:

double M_c = iCustom(NULL,0,"MACD_rc",0,0); //get the current macd
double M_p = iCustom(NULL,0,"MACD_rc",0,1); //get the previous macd

 
bigcheez wrote >>

As in periods (M5, H1) or time offsets from current?

Ok, this seems to work better, the trades are dead onto my indicators, but there are some areas where I don't identify a cross, but a trade is occuring.

Here is my CI code

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

for(int i=0; i<limit; i++)
MacdBuffer[i]=iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,26,0,MODE_EMA,PRICE_CLOSE,i);
for(i=0; i<limit; i++)
SignalBuffer[i]=iMAOnArray(MacdBuffer,0,9,0,MODE_EMA,i);
//---- Cross identified in the 3-rd buffer
for(i=0; i<limit; i++)
{
if(MacdBuffer[i] < SignalBuffer[i] && MacdBuffer[i+1] > SignalBuffer[i+1])
CrossDownBuffer[i]=SignalBuffer[i];
if(MacdBuffer[i] > SignalBuffer[i] && MacdBuffer[i+1] < SignalBuffer[i+1])
CrossUpBuffer[i]=SignalBuffer[i];
}

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

Here is the new EA code

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

double M_c = iCustom(NULL,0,"MACD_rc",0,0,0);
double M_p = iCustom(NULL,0,"MACD_rc",0,0,1);
double S_c = iCustom(NULL,0,"MACD_rc",0,1,0);
double S_p = iCustom(NULL,0,"MACD_rc",0,1,1);
if(M_c > S_c && M_p < S_p)
{
Opn_B=true; //open buy order
Cls_S=true; // close sell order
}
if(M_c < S_c && M_p > S_p)
{
Opn_S=true; //open sell order
Cls_B=true; // close buy order
}

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

Here is the result with the false trades occuring (the red short trade left-middle) CI doesn't signal cross, but EA triggered it.

 
I think this occurs because you use PRICE_CLOSE. Its value is not steady. In "every tick" backtest mode (and on a real account as well) inside in the current candle a tick will trigger the trade for the sake of crossing, but this crossing will disappear if the price is moving backwards.
 
ggekko wrote >>
I think this occurs because you use PRICE_CLOSE. Its value is not steady. In "every tick" backtest mode (and on a real account as well) inside in the current candle a tick will trigger the trade for the sake of crossing, but this crossing will disappear if the price is moving backwards.

So, would there be a better indicator to use then? PRICE_OPEN? Or is this just something I have to account for in the logic?

Thanks for the help btw, this has been frustrating me for days.

 
bigcheez:

So, would there be a better indicator to use then? PRICE_OPEN? Or is this just something I have to account for in the logic?

Thanks for the help btw, this has been frustrating me for days.

Too bad nobody knows the future. All of these indicators show the past. You can use PRICE_CLOSE, but not on the current candle, because it may change. Wait for the confirmation (closing the current bar). You can use PRICE_OPEN, but its precision is doubtful.

 
ggekko wrote >>

Too bad nobody knows the future. All of these indicators show the past. You can use PRICE_CLOSE, but not on the current candle, because it may change. Wait for the confirmation (closing the current bar). You can use PRICE_OPEN, but its precision doubtful.

That makes a lot of sense, my reading on MACD states it is only useful the day (or period) after it has passed, which would be consistent with these results. Is there a way to check for confirmation (last closed bar) when performing my calculation?

 
bigcheez:

That makes a lot of sense, my reading on MACD states it is only useful the day (or period) after it has passed, which would be consistent with these results. Is there a way to check for confirmation (last closed bar) when performing my calculation?

Perhaps with using OPEN_PRICE, but as I said, its precision is doubtful. There is no 100% sureness.

 
ggekko wrote >>

Perhaps with using OPEN_PRICE, but as I said, its precision is doubtful. There is no 100% sureness.

That worked great, no more false trades. Thanks so much for your help!

Reason: