Stochastic + MA240 strategy - problem with opening and closing orders

 

Dear Colleagues,


since few months I`m succesfully trading with Stochastic and MA on 1h/4h charts.

Few days ago I decided to automate my simple strategy, but I have some problems with opening and closing trades.


The rules of my strategy are very simple:

Open Buy trades:

- if price is above Moving Avarenge 240

- If Stochastic green line crossing upwards red line

Close Buy trade:

- If Stochastic green line crossing downwards red line (or if MA240 is crossed downwards)


Open Sell trades :

- if prices is under Moving Averange 240

- if Stochastic green line crossing downwards red line

Close Sell trade:

- if Stochastic green line crossing upwards red line (or if MA240 is crossed upwards)


EA needs to open maximum one trade for current symbol if market conditions are fit to my strategy. If Stochastic is going to be reverse, all current positions needs to be closed.

EA properly sending alerts if market conditions are fit to my requirements, but I dont know how to properly open and close (if stochastic reversing) trades.


I would like to open maximum one trade per symbol, and if Stochastic reverse, simply close it and wait for another setup.


Maybe somebody will be so kind and support me here ?



Thanks in advance


Zalezny

//--------------------------------------------------------------------
// callstohastic.mq4
// Stoch + MA Strategy @Zalezny
//--------------------------------------------------------------------

extern int Period_MA = 240;            // Calculated MA period
bool Fact_Up = true;                  // Fact of report that price..
bool Fact_Dn = true;                  //..is above or below MA

int start()                       // Special function start()
  {
   double M_0, M_1,               // Value MAIN on 0 and 1st bars
          S_0, S_1;               // Value SIGNAL on 0 and 1st bars
   
   double MA;                         // MA value on 0 bar    
 //--------------------------------------------------------------------
                                      // Tech. ind. function call
   MA=iMA(NULL,0,Period_MA,0,MODE_SMA,PRICE_CLOSE,0); 
//--------------------------------------------------------------------

          
//--------------------------------------------------------------------
   //double stoch;
                                  // Tech. ind. function call
   M_0 = iStochastic(NULL,0,7,2,2,MODE_SMA,0,MODE_MAIN,  0);// 0 bar
   M_1 = iStochastic(NULL,0,7,2,2,MODE_SMA,0,MODE_MAIN,  1);// 1st bar
   S_0 = iStochastic(NULL,0,7,2,2,MODE_SMA,0,MODE_SIGNAL,0);// 0 bar
   S_1 = iStochastic(NULL,0,7,2,2,MODE_SMA,0,MODE_SIGNAL,1);// 1st bar
//--------------------------------------------------------------------

//-------------------------------------------
   int total;                     // zmienne do otwarcia pozycji
   string Symb = Symbol();
   total = OrdersTotal();
   
//-------------------------------------------
 
   if( M_1 < S_1 && M_0 >= S_0 && Bid > MA && Fact_Up == true)  //Checking if stochastic crossing upwards and price above MA240
     {
        Fact_Dn = true;                 // Report about price above MA
        Fact_Up = false; 
        //MA = 1;                  // MA = 1 - price above MA
        Alert("Crossing upwards. BUY.", MA, _Symbol); // Alert 
        Sleep(10000);
        
        
// OPENING BUY TRADES
       
          int OrdersBuyForMyPair = 0;              
    
    for (int i = 0; i < OrdersTotal(); i++)    
      {   
      if (OrderSelect(i, SELECT_BY_POS) == true)
         {
         if (OrderSymbol() == Symbol())
            {
            if(OrderType() == OP_SELL)
               {
               // if trader SELL already open 
               // do nothing
               }
            else
               {
               
               //close previous BUY order if exist
               // open NEW SELL ORDER
               //OrderClose(Symbol(),OP_BUY);
               OrdersBuyForMyPair--;             //  increment the count
               OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
               return(0); // exit
               Sleep(10000);
               OrderSend(Symbol(),OP_BUY,0.01,Bid,3,0,0);
               OrdersBuyForMyPair++;             //  increment the count
               }
             }
         }

      }  //  end of for loop  
  
        
     }
 
 
  if( M_1 > S_1 && M_0 <= S_0 && Bid < MA && Fact_Dn == true)   // Green line crosses red downwards
     {
     Fact_Up = true;                 // Report about price below MA
     Fact_Dn = false;                // Don't report about price above MA
   //            MA = 0;                         // Price below MA
     Alert("Crossing downwards. SELL.", MA, _Symbol); // Alert 
     Sleep(10000);
     
    // OrderSend(Symbol(),OP_SELL,0.01,Bid,3,0,0);
    // OPEN TRADE
    
    int OrdersSellForMyPair = 0;
    
    for (int j = 0; j < OrdersTotal(); j++)    
      {   
      if (OrderSelect(j, SELECT_BY_POS) == true)
         {
         if (OrderSymbol() == Symbol())
            {
            if(OrderType() == OP_SELL)
               {
               // if trader SELL already open 
               // do nothing
               }
            else
               {
               
               //close previous BUY order
               // open NEW SELL ORDER
               //OrderClose(Symbol(),OP_BUY);
               OrdersSellForMyPair--;             //  increment the count
               OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
               return(0); // exit
               Sleep(10000);
               OrderSend(Symbol(),OP_SELL,0.01,Bid,3,0,0);
               OrdersSellForMyPair++;             //  increment the count
               }
             }
         }

      }  //  end of for loop  
      
      
                 
 
     }
   
   
      
   if( M_1 > S_1 && M_0 > S_0 && Bid > MA && Fact_Up == true )   // Green line higher than red
   {
       Fact_Dn = true;                 // Report about price above MA
       Fact_Up = false; 
       Alert("Do nothing. Continue buy!", _Symbol);       // Alert
       Sleep(10000);
   }
   
    
   if( M_1 < S_1 && M_0 < S_0 && Bid < MA && Fact_Dn == true )   // Green line lower than red
   {
       Fact_Up = true;                 // Report about price below MA
       Fact_Dn = false;                // Don't report about price above MA
       Alert("Do nothing. Continue sell!", _Symbol);       // Alert
       Sleep(10000);
   }
   
   // -------------------------------------------------------------
   // Open trade
   // -------------------------------------------------------------
   
  
   
     // Sleep(10000); 
//--------------------------------------------------------------------
   return;                         // Exit start()
  }
//--------------------------------------------------------------------
Reason: