NonLagMA Crossover EA Help

 
I am trying to write an EA to place orders on the crosssing of two MAs. The MAs I am using is a custom indicator called NonLagMA_v7.1. The current code I have places an order (at the wrong time), and then fails to close it when the MAs cross the other way. The EA is supposed to be in the market all the time. I have writen a couple of EAs now and am building from my template so I am confident the error is in my entry/exit code. Please could someone have a look at show me where I am going wrong. Thanks.
//Program specific variables                    
int            CrossDirection;
int            PreviousDirection    = NODIRECTION;
int            CurrentDirection     = NODIRECTION;
datetime       CrossTime;
.
.
.
start()
{
.
.
.
//Check if we have the mininum number of bars available.
   if(Bars<iSlowMAPeriod)
   {
      Print("bars less than 100");
      return(0);  
   }

//Check if MAs have crossed, check performed here as various functions use this result
   CrossDirection = MACrossCheck(sSymbol, CurrentTimeFrame); 

}


//+------------------------------------------------------------------+
//Name:    EntrySignal
//Purpose: Checks for entry signal according to rules
//Returns: NOTRADE if not signal detected, BUY for a buy order,
//         SELL for a sell order. 
int EntrySignal(datetime CurrentBrokerTime, string sSymbol, int CurrentTimeFrame) 
{
   int TradesOpen  = CurrentOpenTrades(sSymbol, ExpertMagicNumber);
   
   //exit if TradeFridays is false 
   if(TradeFridays==false && DayOfWeek()==5) return(NOTRADE);

   if (!TradeEveryTick) // if we don't take trade signals at every tick
   {                 // and have no new bar at the timeframe of 'period' minutes
      if (!NewBar(sSymbol, CurrentTimeFrame) ) 
      {   //then exit with an empty signal
         return(NOTRADE);
      }   
   }
   
   if(TradesOpen>0) return(NOTRADE);
   
   if(CurrentDirection==UP)   return(BUY);
   if(CurrentDirection==DOWN) return(SELL);

}    

int MACrossCheck(string& sSymbol, int& CurrentTimeFrame)
{

   double dFastMA = iCustom(sSymbol,CurrentTimeFrame,sMAIndicatorName,0,iFastMAPeriod,0,0,1,1,0,0,0,0,0);
   double dSlowMA = iCustom(sSymbol,CurrentTimeFrame,sMAIndicatorName,0,iSlowMAPeriod,0,0,1,1,0,0,0,0,0);

   if(dFastMA>dSlowMA)      CurrentDirection = UP;
   else if(dFastMA<dSlowMA) CurrentDirection = DOWN;
   
   if(PreviousDirection==NODIRECTION)
   {
      PreviousDirection = CurrentDirection;
      return(NODIRECTION);
   }

   if(PreviousDirection!=CurrentDirection)
   {
      PreviousDirection = CurrentDirection;
      //Saves the open time of the bar the cross was detected in
      CrossTime         = iTime(NULL,CurrentTimeFrame,0);
      return(CurrentDirection);
   }
   else return(NODIRECTION);

}//end MACrossoverCheck

Extract from OrderManagement()...

if(OrderSelect(iTicket, SELECT_BY_TICKET)) {RefreshRates();} 
      
      if(iType==OP_BUY) 
      {
         if((CurrentDirection==DOWN) && (CurrentBrokerTime > OrderOpenTime() + CurrentTimeFrame*60))
         {
            CloseOrder(sOrderSymbol, iType, iTicket, dLots, dOpenPrice, 1, dMaxSlippage);
         }   
      }
      
      else if(iType==OP_SELL) 
      {  //
         if((CurrentDirection==UP) && (CurrentBrokerTime > OrderOpenTime() + CurrentTimeFrame*60))
         {
            CloseOrder(sOrderSymbol, iType, iTicket, dLots, dOpenPrice, 1, dMaxSlippage);
         }   
      }      
      
 
int MACrossCheck(string& sSymbol, int& CurrentTimeFrame)
Why are you using & ?
 
Mistake there, corrected now. It's still not working however.
 
whitebloodcell wrote >>
Mistake there, corrected now. It's still not working however.

Are these params correct for your custom indicator.

double dFastMA = iCustom(sSymbol,CurrentTimeFrame,sMAIndicatorName,0,iFastMAPeriod,0,0,1,1,0,0,0,0,0);
double dSlowMA = iCustom(sSymbol,CurrentTimeFrame,sMAIndicatorName,0,iSlowMAPeriod,0,0,1,1,0,0,0,0,0);

 
jyforex:

Are these params correct for your custom indicator.

double dFastMA = iCustom(sSymbol,CurrentTimeFrame,sMAIndicatorName,0,iFastMAPeriod,0,0,1,1,0,0,0,0,0);
double dSlowMA = iCustom(sSymbol,CurrentTimeFrame,sMAIndicatorName,0,iSlowMAPeriod,0,0,1,1,0,0,0,0,0);


jy has it - assuming params are as below, this is your problem - the tenth param is missing:

double dSlowMA = iCustom(sSymbol,CurrentTimeFrame,sMAIndicatorName,0,iSlowMAPeriod,0,0,1,1,0,0,0,0,0,0);

nonlagma_v711 takes following params:

  1. Price -- 0..6
  2. Length -- indicator's lookback history..
  3. Displace
  4. PctFilter
  5. Color -- 0 or 1
  6. ColorBarBack
  7. Deviation
  8. AlertMode
  9. WarningMode
  10. WarningTicks

and... you are happy with using index buffer #0?

just mentioning as assuming same, you have buffers 0,1,2 to play with... (Colors tab on inputs window shows 3bufrs available for polling)

 

Have think about below. It appears at quick glance that on first entry to (),

if(PreviousDirection==NODIRECTION)
   {
      PreviousDirection = CurrentDirection;
      return(NODIRECTION);
   }

even tho you detect a cross on 1st entry to () it is ignored and above block entered.
this would give a delay until next cross detected, at which time below block entered.
iow, first call to () and it sees cross -> rets(nodir) cuz prevDir=NoDir
on second+ () call it [eventually] sees second cross -> sets time, rets(up/dn)
  
 if(PreviousDirection!=CurrentDirection)
   {
      PreviousDirection = CurrentDirection;
      //Saves the open time of the bar the cross was detected in
      CrossTime         = iTime(NULL,CurrentTimeFrame,0);
      return(CurrentDirection);
   }

suggest, if happy with above observation, revisit your design flow...


also, what is purpose of refreshing rates? as not see code following next line make refs to series data...:
if(OrderSelect(iTicket, SELECT_BY_TICKET)) {RefreshRates();}

 

The version of NonLagMA I have has only 9 parameters, I don't have the 'WarningTicks' option available.


'and... you are happy with using index buffer #0?' - Could you elaborate what you mean please? I was using buffer #0 as in my version of NonLagMA, the absolute value whatever the MA is currently at is stored there, buffers #1,#2, store UpBuffer and DnBuffer respectively, if I made comparisons on them I would be comparing zero sometimes - at least that was my understanding of the situation anyway.


As to the purpose of RefreshRates - none really, in my previous EA I was getting a lot of 'wrong price' errors and so I stuck RefreshRates in anywhere I thought might help, its just remaining from that.


It was my intention to ignore the first 'cross' as the EA could be loaded onto the chart at any point I thought it would be better to wait for a fresh signal and enter there. Is there a better way of going about this?

 

I have NonLogMA_v6.1. I have attached it along with this message. Can you put pics comparing the lag of both the indicvators from your charts.

The problem of using the 0 candle is that there will be multiple conflicting conditions during the formation of the candle. If you are checking for MA crosses it

is better to check it at the open of the next bar/candle. I sometimes put a param saying 90% completion of the candle by the following method.

bool

candle_ready(double percentage_completion)

{

datetime open_time = Time[0];

datetime curr_time = TimeCurrent();

double delta = curr_time - open_time;

double total = Period() * 60;

if ( ( ( delta * 100 ) / total ) >= percentage_completion )

return(true);

return(false);

}

You can call this function as

if (candle_ready(90)) {

*******

Do your processing here.

}

Files:
 

I just notice NonLagMA_v711 is available in the forum at

'Using Information of an Custom Indicator'

This one has 9 params and 3 line indicators

extern int Price = 0; //Apply to Price(0-Close;1-Open;2-High;3-Low;4-Median price;5-Typical price;6-Weighted Close)
extern int Length = 15; //Period of NonLagMA
extern int Displace = 0; //DispLace or Shift
extern double PctFilter = 0; //Dynamic filter in decimal
extern int Color = 1; //Switch of Color mode (1-color)
extern int ColorBarBack = 1; //Bar back for color mode
extern double Deviation = 0; //Up/down deviation
extern int AlertMode = 0; //Sound Alert switch (0-off,1-on)
extern int WarningMode = 0; //Sound Warning switch(0-off,1-on)

How many lines does you indicator have?

You many need to read different indicators depending on the condition. Otherwise the indicator value will be 0 in some cases.

double dFastMA = iCustom(sSymbol,CurrentTimeFrame,sMAIndicatorName,0,iFastMAPeriod,0,0,1,1,0,0,0,0,0);
double dSlowMA = iCustom(sSymbol,CurrentTimeFrame,sMAIndicatorName,0,iSlowMAPeriod,0,0,1,1,0,0,0,0,0);

Does your indicator have only one line?

 
whitebloodcell:

The version of NonLagMA I have has only 9 parameters, I don't have the 'WarningTicks' option available.

understood


'and... you are happy with using index buffer #0?' - Could you elaborate what you mean please? I was using buffer #0 as in my version of NonLagMA, the absolute value whatever the MA is currently at is stored there, buffers #1,#2, store UpBuffer and DnBuffer respectively, if I made comparisons on them I would be comparing zero sometimes - at least that was my understanding of the situation anyway.

It can happen that buffer choice is not given due thought and I asked to ensure you were happy with what buffer using - nothing more


As to the purpose of RefreshRates - none really, in my previous EA I was getting a lot of 'wrong price' errors and so I stuck RefreshRates in anywhere I thought might help, its just remaining from that.


It was my intention to ignore the first 'cross' as the EA could be loaded onto the chart at any point I thought it would be better to wait for a fresh signal and enter there. Is there a better way of going about this?

guess the question is what is a good point? since a cross can happen at anytime. Do you employ some confirming filters? eg, can you guarantee that UP,DOWN mirrors price direction?

all thots - is your code/design...


 

I've attached a screenshot of what the EA does currently, once this order is opened (since it has been opened incorrectly I assume) it is not closed by the ea and no more order's can be placed due to the only one order restriction within EntrySignal(), The tester later says the order was 'closed at stop', which is also confusing since no stop is placed...


I was not going to employ any sort of confirmation for the moment, what I had envisioned was an order being placed on the first cross, and then subsequent crosses back and forth during this candle period are ignored and the order left to stand.


'Does your indicator have only one line?' - Yes only one line is drawn. When the line is increasing/decreasing the current value is stored in Buffer1/2 respectively. Buffer0 holds the value regardless of the current direction and it is this I wanted to be reading.