What's wrong with my iStochastics??

 

Hi, I made an EA mainly based on Stochastics and have set a range for the signal (as shown below). 

However when i run it on the Strategy Tester, it trades outside of my range. 

How should i resolve this issue?

Thank you!!

if (kValue0>85)
if (kValue0<97){
if (dValue0>85){
if (dValue0<97){
if (dValue0_5<97 && kValue0_5<97){
if (dif>=2){
if (dif_5>=2){
if ((kValue0<dValue0) && (kValue1>dValue1)){
stoch_signal = "Sell";}}}}}}}
The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
The idea of ​​automated trading is appealing by the fact that the trading robot can work non-stop for 24 hours a day, seven days a week. The robot does not get tired, doubtful or scared, it's is totally free from any psychological problems. It is sufficient enough to clearly formalize the trading rules and implement them in the algorithms, and...
Files:
Capture.JPG  604 kb
 

Here are the standard parameters for creating an iStochastic indicator:

 int    iStochastic ( 
   string            symbol,           // symbol name  
   ENUM_TIMEFRAMES   period,           // period  
   int               Kperiod,         // K-period (number of bars for calculations)  
   int               Dperiod,         // D-period (period of first smoothing)  
   int               slowing,         // final smoothing  
   ENUM_MA_METHOD    ma_method,       // type of smoothing  
   ENUM_STO_PRICE    price_field       // stochastic calculation method  
   );

and here are two indicator indicator buffers:

The buffer numbers: 0 - MAIN_LINE , 1 - SIGNAL_LINE .

And now rewrite your condition using generally accepted notation - it will be easier to find an error.

 
Vladimir Karputov:

Here are the standard parameters for creating an iStochastic indicator:

and here are two indicator indicator buffers:

And now rewrite your condition using generally accepted notation - it will be easier to find an error.

Thank you for helping!

I actually left out that part. I defined it above the previous lines of codes. This is what the full Stochastics part look like.

But it still trades when the main line is at 100 when i restricted it to max 97. Did i do something wrong here??



double dArray[];
double kArray[];
double dArray5[];
double kArray5[];

ArraySetAsSeries(kArray,true);
ArraySetAsSeries(dArray,true);
ArraySetAsSeries(dArray5,true);
ArraySetAsSeries(kArray5,true);

int stochastic_definition = iStochastic(_Symbol,PERIOD_M1,5,3,3,MODE_SMA,STO_CLOSECLOSE);
int stochastic_definition5 = iStochastic(_Symbol,PERIOD_M5,5,3,3,MODE_SMA,STO_CLOSECLOSE);

CopyBuffer(stochastic_definition,0,0,3,kArray);
CopyBuffer(stochastic_definition,1,0,3,dArray);
CopyBuffer(stochastic_definition5,0,0,3,kArray5);
CopyBuffer(stochastic_definition5,1,0,3,dArray5);

double kValue0= kArray[0] ;
double dValue0= dArray[0] ;
double kValue1= kArray[1] ; 
double dValue1= dArray[1] ;
double kValue0_5 = kArray5[0];
double dValue0_5 = dArray5[0];


double dif_5 = dValue0_5 - kValue0_5;
double dif = dValue0 - kValue0;
//SELL Signal
if (kValue0>85)
if (kValue0<97){
if (dValue0>85){
if (dValue0<97){
if (dValue0_5<97 && kValue0_5<97){
if (dif>=2){
if (dif_5>=2){
if ((kValue0<dValue0) && (kValue1>dValue1)){
stoch_signal = "Sell";}}}}}}}
 
alim060: Did i do something wrong here??
  1. stoch_signal = "Sell";
    Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?

  2. int stochastic_definition = iStochastic(_Symbol,PERIOD_M1,5,3,3,MODE_SMA,STO_CLOSECLOSE);
    int stochastic_definition5 = iStochastic(_Symbol,PERIOD_M5,5,3,3,MODE_SMA,STO_CLOSECLOSE);
    

    Perhaps you should read the manual, especially the examples. They all (including iCustom) return a handle (an int.) You get that in OnInit. In OnTick you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
              How to call indicators in MQL5 - MQL5 Articles 12 March 2010

    Also see my example for encapsulating calls
              Detailed explanation of iCustom - MQL4 programming forum 2017.05.23

 
William Roeder:
  1. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?

  2. Perhaps you should read the manual, especially the examples. They all (including iCustom) return a handle (an int.) You get that in OnInit. In OnTick you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
              How to call indicators in MQL5 - MQL5 Articles 12 March 2010

    Also see my example for encapsulating calls
              Detailed explanation of iCustom - MQL4 programming forum 2017.05.23

Thanks for your response!

I'm not really asking for you guys to completely debug my code here. 

I am simply seeing discrepancy between my code and the resultant EA. I am asking for your expert advice since I don't have as much experience.


Perhaps you could point out where I went wrong? Because the code still runs currently. Thanks!

Reason: