Moving Average Cross over problem.

 

I'm writing a code for Moving average crossover. The most basic concept.

FastMA>SlowMA==BUY

SlowMA>FastMA==SELL.

But i'm having trouble in it. It's been few days since i'm having this trouble.

It misses opportunities to buy and sell, even though the loop for that trade is executed.

Please guide me about wht the problem is.

Thanks in advance.

extern double MPeriod1 = 5;
extern double MPeriod2 = 17;
extern double Mshift   =0;
string lasttrade;

//------- Moving Averages
double FMA,SMA,PFMA,PSMA;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  { checkforopen();
    checkforclose();
   return(0);
  }
//+------------------------------------------------------------------+




void checkforopen()
{

//------go trading only on first ticks of new Bars
if(Volume[0]>1) return;
//-----Get Moving Averages

   FMA=iMA(NULL,0,MPeriod1,Mshift,MODE_SMA,PRICE_CLOSE,0);
   SMA=iMA(NULL,0,MPeriod2,Mshift,MODE_SMA,PRICE_CLOSE,0);
   PFMA=iMA(NULL,0,MPeriod1,Mshift,MODE_SMA,PRICE_CLOSE,2);
   PSMA=iMA(NULL,0,MPeriod2,Mshift,MODE_SMA,PRICE_CLOSE,2);
   
//------Giving Conditions

   if(FMA>SMA && PFMA<=PSMA)
   {
   if(lasttrade=="sell"){
   OrderSend(Symbol(),OP_BUY,0.2,Ask,3,0,0,"",19862712,0,Red); // open new BUy Order 
   Alert("",GetLastError());
   }
   lasttrade="buy";
   Comment("BUY");
   }else{
   if(FMA<SMA && PFMA>=PSMA){
   if(lasttrade=="buy"){
   OrderSend(Symbol(),OP_SELL,0.2,Bid,3,0,0,"",19862712,0,Blue);    //open new Sell Order
   Alert("",GetLastError());
   }
   lasttrade="sell";
   Comment("SELL");
   }
        }
}



void checkforclose()
{
//---- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//----
   
 for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderMagicNumber()!=19862712 || OrderSymbol()!=Symbol()) continue;
      if(OrderType()==OP_BUY)
      {
      if(FMA<SMA)OrderClose(OrderTicket(),OrderLots(),Bid,3,Snow);
      Comment("SELL WILL NOT BE EXCECUTED");
      Alert("",GetLastError());
      break;
      }
      if(OrderType()==OP_SELL)
      {
      if(FMA>SMA)OrderClose(OrderTicket(),OrderLots(),Ask,3,Gold);
      Comment("BUY WILL NOT BE EXECUTED");
      Alert("",GetLastError());
      break;
      }
     }
}

P.S. :: The comments arefor checking whether the loop is visited. Also in strategy tester it doesn't give alerts!!!! :(

 

if lasttrade==NULL then what?

 
qjol:

if lasttrade==NULL then what?

That is not a problem we can replace that with:

bool buycondition=true; //initialization

bool sellcondition=true;

if(buycondition==true) {

//code

sellcondition=false;

buycondition=true;}

and same for sell also.

But my problem is that it is not executing close orders at all. And also misses to open positions!!!!

Please can u suggest some way to overcome this...

Thank you.

 

Please also tell me how can I open only one order at a time for each currency. i.e even if two currencies are being traded simultaneously, each should have only one trade on for each.(Total trades ON==2)

 

I checked it with the tester and it opens orders just fine

 
qjol:

I checked it with the tester and it opens orders just fine


Sir,

Thank you for checking it.

But it doesn't close orders when required!!!

It also leaves some trades. For a crossover it might not open a trade or may end a trade before any crossover.

Also when i add Period() in comment section of OrderSend(), it doesn't execute any trade. Same for numerical values of stop-loss and take-profit.

 
I've added some points to the code, but it has gone worse since. Though it is in profit,but misses critical crossovers!!!
Files:
 
ksrohit2712:


Sir,

Thank you for checking it.

But it doesn't close orders when required!!!


i'm not sure but could be if(Volume[0]>1) return; causing not to close (Sometimes there are two tics by bar opening)
 
qjol:

i'm not sure but could be if(Volume[0]>1) return; causing not to close (Sometimes there are two tics by bar opening)


well, yes it is. He is supposed to use Time[0] instead.

Good Luck,

SF

 
scarface:


well, yes it is. He is supposed to use Time[0] instead.

Good Luck,

SF


Time[0] is not what he meant he wants only the first tick (//---- go trading only for first tiks of new bar)
 

Thank you for your suggestions. I've used Time[0], but still it doesn't visit the checkforclose()!!!!

It only shuttles in the checkforopen()!!!

You can only see "BUY" and "SELL" comments.... even tough start() has both of them!!!

Reason: