Stochastic / MA240 strategy

 

Dear Colleagues,


I just started learning mql4 and searching for a help.

How to put "open/close trade" function to this example ? I would like to open a BUY trade once stochastic red line(signal) crossing stochastic (main) green line upwards.

And close if stochastic green (main) line crossing signal line.

And  SELL trader in Stochastic reverse.


https://book.mql4.com/samples/indicators

//--------------------------------------------------------------------
// callstohastic.mq4
// The code should be used for educational purpose only.
//--------------------------------------------------------------------
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
//--------------------------------------------------------------------
                                  // Tech. ind. function call
   M_0 = iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_MAIN,  0);// 0 bar
   M_1 = iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_MAIN,  1);// 1st bar
   S_0 = iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_SIGNAL,0);// 0 bar
   S_1 = iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_SIGNAL,1);// 1st bar
//--------------------------------------------------------------------
                                  // Analysis of the situation
   if( M_1 < S_1 && M_0 >= S_0 )  // Green line crosses red upwards
      Alert("Crossing upwards. BUY."); // Alert 
   if( M_1 > S_1 && M_0 <= S_0 )  // Green line crosses red downwards
      Alert("Crossing downwards. SELL."); // Alert 
      
   if( M_1 > S_1 && M_0 > S_0 )   // Green line higher than red
      Alert("Continue holding Buy position.");       // Alert 
   if( M_1 < S_1 && M_0 < S_0 )   // Green line lower than red
      Alert("Continue holding Buy position.");       // Alert 
//--------------------------------------------------------------------
   return;                         // Exit start()
  }
//--------------------------------------------------------------------

Thanks in advance for Your support.



Zalezny

 

Here I modified my code a little bit.

EA needs to open:

1) open SELL trades

- only if price is bottom MA240

- if Stoch red line (signal) crossing Stoch green line (main) downwards

Close SELL trades

- only if stochastic reverse


2) open BUY trades

- only if price is over MA240

- if Stoch green line (main) crossing Stoch red line (signal) upwards

close BUY trades

- only if stochastic reverse



But for some reason this code is not able to open any trades. What could be a reason ?


//--------------------------------------------------------------------
// callstohastic.mq4
// The code should be used for educational purpose only.
//--------------------------------------------------------------------

//MA Settings
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 pos;


int start()                       // Special function start()
  {
// Stochastic settings
   double M_0, M_1,               // Value MAIN on 0 and 1st bars
          S_0, S_1;               // Value SIGNAL on 0 and 1st bars

// Moving averange settings
   double MA;
   
//--------------------------------------------------------------------
   MA=iMA(NULL,0,Period_MA,0,MODE_SMA,PRICE_CLOSE,0);
//--------------------------------------------------------------------
   
   
//--------------------------------------------------------------------
                                  // Tech. ind. function call
   M_0 = iStochastic(NULL,0,8,3,3,MODE_SMA,0,MODE_MAIN,  0);// 0 bar
   M_1 = iStochastic(NULL,0,8,3,3,MODE_SMA,0,MODE_MAIN,  1);// 1st bar
   S_0 = iStochastic(NULL,0,8,3,3,MODE_SMA,0,MODE_SIGNAL,0);// 0 bar
   S_1 = iStochastic(NULL,0,8,3,3,MODE_SMA,0,MODE_SIGNAL,1);// 1st bar
//--------------------------------------------------------------------


int Total_Order = OrdersTotal();


// Check if trade is already open, if yes, do nothing
for (pos = 0; pos <= Total_Order; pos ++)
   {
   if (OrderSelect (pos, SELECT_BY_POS) == true)
        {
        if (OrderSymbol () == Symbol ())
          {
          Print (":( Sorry already have that symbol opened ");
          // get outta here do something else
          break ;
          }
          else
          {
             Print ("No order opened for this symbol");
                 // If STOCH green line crossing STOCH red line upwords
                 // and if price above MA240 - BUY          
             if(M_1 < S_1 && M_0 >= S_0)     // Green line crosses red upwards
                {
                   Fact_Dn = true;                 // Report about price above MA
                   Fact_Up = false;                // Don't report about price below MA
                   Alert("Stochastic going up, price above MA240 - BUY. "); // Alert 
                   OrderSend(Symbol(),OP_BUY,0.1,Ask,3,Bid-15*Point,Bid+15*Point);
                   
               }
                 // If STOCH green line crosses STOCH red line downwards
                 // and if price bottom MA240 - SELL
            if( M_1 > S_1 && M_0 <= S_0)  // Green line crosses red downwards
            {
               Fact_Up = true;                 // Report about price below MA
               Fact_Dn = false;                // Don't report about price above MA
               Alert("Stochastic going down, price bottom MA240 - SELL."); // Alert 
               OrderSend(Symbol(),OP_BUY,0.1,Ask,3,Bid-15*Point,Bid+15*Point);
            }
               
               
          }
        }
     }
     
      
      
// If price movement continue
// then...
   if( M_1 > S_1 && M_0 > S_0 )   // Green line higher than red
      //Alert("Continue holding Buy position.");       // Alert 
      
   if( M_1 < S_1 && M_0 < S_0 )   // Green line lower than red
      //Alert("Continue holding Buy position.");       // Alert 
      
      
//--------------------------------------------------------------------
   return;                         // Exit start()
  }
//--------------------------------------------------------------------
 
zalezny: But for some reason this code is not able to open any trades. What could be a reason ?
               OrderSend(Symbol(),OP_BUY,0.1,Ask,3,Bid-15*Point,Bid+15*Point);
So find out why! Check your return codes (OrderSend) What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
Reason: