# Stochastic / MA240 strategy

13

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
if( M_1 > S_1 && M_0 <= S_0 )  // Green line crosses red downwards

if( M_1 > S_1 && M_0 > S_0 )   // Green line higher than red
if( M_1 < S_1 && M_0 < S_0 )   // Green line lower than red
//--------------------------------------------------------------------
return;                         // Exit start()
}
//--------------------------------------------------------------------```

Zalezny

13

Here I modified my code a little bit.

EA needs to open:

- only if price is bottom MA240

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

- only if stochastic reverse

- only if price is over MA240

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

- 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();

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

}
// 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
}

}
}
}

// If price movement continue
// then...
if( M_1 > S_1 && M_0 > S_0 )   // Green line higher than red

if( M_1 < S_1 && M_0 < S_0 )   // Green line lower than red
```               OrderSend(Symbol(),OP_BUY,0.1,Ask,3,Bid-15*Point,Bid+15*Point);