How to find the highest price between two Stochastic crosses to set stoploss

 

Hi, Very new to coding and am writing up EA that requires a stop loss to be placed at the highest price between the lastest stochastic cross (buy signal) and the previous stochastic cross.


Below is the code for reference. 

Any help or push in the right direction would be greatly appreciated.


Cheers,


Dale


//SuperTren Stochastic Strategy
#property copyright "Dale Cowie"
#property link      ""
#property version   "1.00"

#include <Trade/Trade.mqh>


//User input Variables
input ENUM_TIMEFRAMES Timeframe = PERIOD_CURRENT;

input double Lots = 0.1;

input int SuperTrend_ATR_Legnth = 10;
input int SuperTrend_Factor = 3;
input int Stochastic_K_Legnth = 8;
input int Stochastic_K_Smoothing = 3;
input int Stochastic_D_Legnth = 3;
input int EMA_Legnth = 200;


//EA Handles
int ATR_SuperTrend_Handle;
int Stochastic_Handle;
int EMA_Handle;
int TotalBars;

//Trading external code
CTrade trade;
ulong positionTicket;

int OnInit(){

//Definition to only trade once per bar
   TotalBars = iBars(_Symbol, Timeframe);

//Indicator Handle definitions
   ATR_SuperTrend_Handle = iCustom(_Symbol, Timeframe, "ATR SuperTrend", SuperTrend_ATR_Legnth, SuperTrend_Factor);
   Stochastic_Handle = iStochastic(_Symbol, Timeframe, Stochastic_K_Legnth, Stochastic_D_Legnth, Stochastic_K_Smoothing,MODE_SMA,STO_LOWHIGH);
   EMA_Handle = iMA(_Symbol, Timeframe, EMA_Legnth, 0, MODE_EMA, PRICE_CLOSE);
  
   return(INIT_SUCCEEDED); 
}

void OnDeinit(const int reason){
   



}

void OnTick(){

//Get number of Bars
   int bars = iBars(_Symbol, Timeframe);
   if(TotalBars != bars) {
      TotalBars = bars;
      
//Data Collection Arrays      
   double ATR_SuperTrend_Array [];
   double K_Array [];
   double D_Array [];
   double EMA_Array[];
   double Candle_Close_Price_Array[];
   
//Sort the array from the current candle backwards
   ArraySetAsSeries(K_Array, true);
   ArraySetAsSeries(D_Array, true);
   ArraySetAsSeries(EMA_Array, true);
   ArraySetAsSeries(Candle_Close_Price_Array, true);   

//Fill the array with data  
   CopyBuffer(ATR_SuperTrend_Handle, 0, 0, 3, ATR_SuperTrend_Array);
   CopyBuffer(Stochastic_Handle, 0, 0, 3, K_Array);
   CopyBuffer(Stochastic_Handle, 1, 0, 3, D_Array);
   CopyBuffer(EMA_Handle, 0, 0, 3, EMA_Array);
   
//Get Close Price of Current candle
   CopyClose(_Symbol, Timeframe, 0, 3, Candle_Close_Price_Array);
   
//Calculate the candle close for the current and previous candles (SuperTrend)
   double close1 = iClose(_Symbol, Timeframe, 1);
   double close2 = iClose(_Symbol, Timeframe, 2);


//Calculate the Stochastic value for the current candle
   double K_Value0 = K_Array[0];
   double D_Value0 = D_Array[0];

//Calculate the Stochastic value for the last candle
   double K_Value1 = K_Array[1];
   double D_Value1 = D_Array[1];

   double K_Value2 = K_Array[2];
   double D_Value2 = D_Array[2];
   
//Calculate the EMA Value for the current and previous candle
   double EMA_Value0 = EMA_Array[0];
   double EMA_Value1 = EMA_Array[1];

//Calculate the candle close for the current and previous candle
   double CandleClosePrice0 = Candle_Close_Price_Array [0];
   double CandleClosePrice1 = Candle_Close_Price_Array [1];
   double CandleClosePrice2 = Candle_Close_Price_Array [2]; 

//Buy Condition 

   bool Condition1 = close1 > ATR_SuperTrend_Array [1] && close2 > ATR_SuperTrend_Array [0],
        Condition2 = K_Value2 > D_Value2 && K_Value1 < D_Value1,
        Condition3 = D_Value2 > K_Value2 && D_Value1 < K_Value1,
        Condition4 = CandleClosePrice1 > EMA_Value1,
        Condition5 = close1 < ATR_SuperTrend_Array [1] && close2 < ATR_SuperTrend_Array [0],
        Condition6 = CandleClosePrice1 < EMA_Value1;
      
   if (Condition1 && Condition4) {        
   
   if (Condition2 || Condition3){ 
    
      Print(__FUNCTION__, " >Buy Signal...");
      
   
} 

}

//Sell condition
    else if(Condition5 && Condition6) {
   
         if (Condition2 || Condition3){


      Print(__FUNCTION__, " >Sell Signal...");

}
}
}
}



   



 
Without going into details of your code, you need the period count between the two crosses and the datetime of the last cross happend.

You use iHighest and BarShift to get your values.

Read the doc and you will be able to solve it.
 
Dominik Egert #:
Without going into details of your code, you need the period count between the two crosses and the datetime of the last cross happend.

You use iHighest and BarShift to get your values.

Read the doc and you will be able to solve it.

Hey mate, Thanks for your time in replying. I have a handle on how to use the iHighest and BarShift to get the values, I am just unsure of how to go about getting  the period count between the two crosses and the datetime of when the last cross happend. 

 

Ahh, today some community coding... Hmm...

Someone should pay me for this.


double stoch_arr_R[];
double stoch_arr_D[];



const bool R_above_D = stoch_arr_R[index] > stoch_arr_D[index];
int cnt = index - 1;

while( (cnt > NULL)
    && ((stoch_arr_R[cnt] > stoch_arr_D[cnt]) == R_above_D)
    && (!_StopFlag) )
{ cnt--; }

const int index_of_first_cross = cnt;

cnt--;
while( (cnt > NULL)
    && ((stoch_arr_R[cnt] > stoch_arr_D[cnt]) != R_above_D)
    && (!_StopFlag) )
{ cnt--; }

const int index_of_second_cross = cnt;


const int distance_between_crosses = index_of_first_cross - index_of_second_cross;

datetime first_cross = iTime(Symbol(), Period(), index - index_of_first_cross);
datetime second_cross = iTime(Symbol(), Period(), index - index_of_second_cross);



index is equivalent to prev_calculated.

The index is taking into account to not analyze the current running period.

This is untested... should give an idea on how to do the job, shouldnt it.

 
Dominik Egert #:

Ahh, today some community coding... Hmm...

Someone should pay me for this.



index is equivalent to prev_calculated.


This is untested... should give an idea on how to do the job, shouldnt it.

Thanks so much Dominik, Ill have a play and try and make sense of it. Input and guidance from guys like you when learning to code is invaluable and cant thank you enough for your time.


Cheers,


Dale