# Stochastic cross form over bought or oversold regions

4648

I have been learning the MQL4 and so far I have made some progress however, I have this challenge I encountered while trying to code the below:

• "Generally, the area above 80 indicates an overbought region, while the area below 20 is considered an oversold region. A sell signal is given when the oscillator is above the 80 level and then crosses back below 80. Conversely, a buy signal is given when the oscillator is below 20 and then crossed back above 20. 80 and 20 are the most common levels used but can be adjusted as needed".

I tried this code (below), but it does not take all the signals:

```  bool buy_condition_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, 1) < 20 ;

bool buy_condition_2 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, 0) > 20 ;```

```bool sell_condition_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, 1) > 80 ;

bool sell_condition_2 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, 0) < 80 ;

```

This does not work exactly as the above quote.

Documentation on MQL5: Standard Constants, Enumerations and Structures / Objects Constants / Object Types
• www.mql5.com
When a graphical object is created using the ObjectCreate() function, it's necessary to specify the type of object being created, which can be one of the values of the ENUM_OBJECT enumeration. Further specifications of object properties are possible using functions for working with graphical objects.
757

Chinedu Onuoha :

bool buy_condition_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, 1) < 20 ;

bool buy_condition_2 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, 0) > 20 ;

This does not work exactly as the above quote.

Have you misunderstood something? I think that there is no problem in particular.

```//+------------------------------------------------------------------+
//|                                                TestIndicator.mq4 |
//|                                                             None |
//+------------------------------------------------------------------+
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   2
#property indicator_level1 20
#property indicator_level2 80
//--- plot Line
#property indicator_label1  "Line"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot Arrow
#property indicator_label2  "Arrow"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrAqua
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- input parameters

//--- indicator buffers
double         Line_Buffer[];
double         Arrow_Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0, Line_Buffer);
SetIndexBuffer(1, Arrow_Buffer);
SetIndexArrow(1, 159);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
{
//---
int i, limit;

if (prev_calculated == 0)
limit = rates_total - 11;
else
limit = rates_total - prev_calculated;

for (i = limit; i >= 0; i--)
{
Line_Buffer[i] = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, i);

bool buy_condition_1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, i + 1) < 20 ;
bool buy_condition_2 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, i) > 20 ;

Arrow_Buffer[i] = 20;
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
```
4648

Have you misunderstood something? I think that there is no problem in particular.

Thanks I will try this now

4648

```// Init function

int init(){

open = 0;

RealPoint = RealPipPoint(Symbol());

}

// Start function

int start(){

if (open  == Open[0]) return 0;

open = Open[0];

//long

OrderSelect(LongTicket,SELECT_BY_TICKET);

if(OrderCloseTime() != 0 || LongTicket == 0) {

bool buy_condition_1 = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, 1)  >  0  ;

bool buy_condition_2 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, 1) < 20 ;

bool buy_condition_3 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, 0) > 20 ;

OrderSelect(ShortTicket,SELECT_BY_TICKET);

```
:

Have you misunderstood something? I think that there is no problem in particular.

Sorry if I sound stupid but please I am still learning, this got me more confused especially the i+1.... below is where I got stuck initially

/

882

Hello,

I am a beginner with programming completely, but myself I would use more than two indicators to place orders. I plan on having 5 to 7 indicators to tell me about the securities I place positions on.

757

Chinedu Onuoha :

Sorry if I sound stupid but please I am still learning, this got me more confused especially the i+1.... below is where I got stuck initially

I changed 1 to i + 1 and 0 to i to output consecutive data, but it's the same for output one point of data. You have no mistake.

4648

I changed 1 to i + 1 and 0 to i to output consecutive data, but it's the same for output one point of data. You have no mistake.

Ok, but this does not work exactly like this quote:

• "Generally, the area above 80 indicates an overbought region, while the area below 20 is considered an oversold region. A sell signal is given when the oscillator is above the 80 level and then crosses back below 80. Conversely, a buy signal is given when the oscillator is below 20 and then crossed back above 20. 80 and 20 are the most common levels used but can be adjusted as needed".
It picks some but not all
757

Chinedu Onuoha :

Ok, but this does not work exactly like this quote:

• "Generally, the area above 80 indicates an overbought region, while the area below 20 is considered an oversold region. A  sell signal  is given when the oscillator is above the 80 level and then crosses back below 80. Conversely, a buy signal is given when the oscillator is below 20 and then crossed back above 20. 80 and 20 are the most common levels used but can be adjusted as needed".
It picks some but not all
It works exactly like that as I proved in my program. If it does not work, there will be other problems. But I do not know.
24193

Chinedu Onuoha: below is where I got stuck initially
1. ### Don't double post!

General rules and best pratices of the Forum. - General - MQL5 programming forum
General rules and best pratices of the Forum. - General - MQL5 programming forum

3. ```// Init function

int init(){...}

// Start function

int start(){...```
Start using the new Event Handling Functions.
Event Handling Functions - Functions - Language Basics - MQL4 Reference

4. `OrderSelect(LongTicket,SELECT_BY_TICKET);`
Check your return codes for errors and report them.
What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

5. ```if(OrderCloseTime() != 0 || LongTicket == 0) {

bool buy_condition_1 = iMACD(NULL, 0, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, 1)  >  0  ;

bool buy_condition_2 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, 1) < 20 ;

bool buy_condition_3 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 1, MODE_MAIN, 0) > 20 ;```
Again, all you are doing is setting variables. Unless you write code that opens orders, n o t h i n g  w i l l   e v e r   h a p p e n.

Moderator
120172

============

### Stochastic

The beginning

1. Stochastic Oscillator, the beginning

After