I will write an advisor free of charge - page 168

 
Aesen #:
Hey Anton can you help me make my On Balance Volume Divergence EA more consistently profitable? Maybe change some things in the code or add some features and make it better please. the code is fully working but i am not satisfied with the results i am getting from it
#include <trade/trade.mqh>

input double Lots = 0.01;
input int VerificationCandles = 20;
input int TimeGapCandles = 5;

input int TpPoints = 1000;
input int SlPoints = 1000;

int totalBars;
int handleOBV;

datetime timeLow1, timeLow2, timeHigh1, timeHigh2;
double low1, low2, high1, high2;
datetime timeLowOBV1, timeLowOBV2, timeHighOBV1, timeHighOBV2;
double lowObv1, lowObv2, highObv1, highObv2;

int OnInit(){
   totalBars = iBars(_Symbol,PERIOD_CURRENT);
   
   handleOBV = iOBV(_Symbol,PERIOD_CURRENT,VOLUME_TICK);

   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason){

}

void OnTick(){
   int bars = iBars(_Symbol,PERIOD_CURRENT);
   if(totalBars != bars){
      totalBars = bars;
      
      datetime newTime = 0;
      double newlow = 0, newhigh = 0;
      findHighLow(newlow,newhigh,newTime);
      
      datetime newTimeObv = 0;
      double newlowOBV = 0, newhighOBV = 0;      
      findHighLowOBV(newlowOBV,newhighOBV,newTimeObv); 
      
      if(newlow != 0 || newlowOBV != 0){
         if(newlow != 0){
            low2 = low1;
            timeLow2 = timeLow1;
            low1 = newlow;
            timeLow1 = newTime;            
         }   
         if(newlowOBV != 0){
            lowObv2 = lowObv1;
            timeLowOBV2 = timeLowOBV1;
            lowObv1 = newlowOBV;
            timeLowOBV1=newTime;
         }
         
         ulong timeGap = TimeGapCandles * PeriodSeconds(PERIOD_CURRENT);
         if(low1 < low2 && lowObv1 > lowObv2 && (ulong)MathAbs(timeLow1-timeLowOBV1) < timeGap && (ulong)MathAbs(timeLow2-timeLowOBV2) < timeGap){
            Print(__FUNCTION__," > New Buy Signal...");
            
            double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
            ask = NormalizeDouble(ask,_Digits);
            
            double tp = ask + TpPoints * _Point;
            tp = NormalizeDouble(tp,_Digits);
            
            double sl = ask - SlPoints * _Point;
            sl = NormalizeDouble(sl,_Digits);
            
            CTrade trade;
            trade.Buy(Lots,_Symbol,ask,sl,tp);                 
         }
      } 
         
         if(newhigh != 0 || newhighOBV != 0){
            if(newhigh != 0){
               high2 = high1;
               timeHigh2 = timeHigh1;
               high1 = newhigh;
               timeHigh1 = newTime;
            }
            if(newhighOBV != 0){
               highObv2 = highObv1;
               timeHighOBV2 = timeHighOBV1;
               highObv1 = newhighOBV;
               timeHighOBV1 = newTimeObv;
            }
            
           ulong timeGap = TimeGapCandles * PeriodSeconds(PERIOD_CURRENT);
           if(high1 > high2 && highObv1 < highObv2 && (ulong)MathAbs(timeHigh1-timeHighOBV1) < timeGap && (ulong)MathAbs(timeHigh2-timeHighOBV2) < timeGap){
            Print(__FUNCTION__," > New Sell Signal...");
            
            double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
            bid = NormalizeDouble(bid,_Digits);
            
            double tp = bid - TpPoints * _Point;
            tp = NormalizeDouble(tp,_Digits);
            
            double sl = bid + SlPoints * _Point;
            sl = NormalizeDouble(sl,_Digits);
            
            CTrade trade;
            trade.Sell(Lots,_Symbol,bid,sl,tp);                  
         }   
      }
   }             
} 
     
void findHighLow(double &newlow, double &newhigh, datetime &newTime){
   int indexBar = VerificationCandles+1;
   double high = iHigh(_Symbol,PERIOD_CURRENT,indexBar);
   double low = iLow(_Symbol,PERIOD_CURRENT,indexBar);
   datetime time = iTime(_Symbol,PERIOD_CURRENT,indexBar);
      
   bool isHigh = true, isLow = true;
   for(int i = 1; i <= VerificationCandles; i++){
       double highLeft = iHigh(_Symbol,PERIOD_CURRENT,indexBar+i);
       double highRight = iHigh(_Symbol,PERIOD_CURRENT,indexBar-i);
       if(highLeft > high || highRight > high) isHigh = false;
         
       double lowLeft = iLow(_Symbol,PERIOD_CURRENT,indexBar+i);
       double lowRight = iLow(_Symbol,PERIOD_CURRENT,indexBar-i);
       if(lowLeft < low || highRight < low) isLow = false;
          
       if(!isHigh && !isLow) break;
       if(i == VerificationCandles){
         if(isHigh){
            Print(__FUNCTION__," > Found a new high (",DoubleToString(high,_Digits),") at ",time,"...");
            ObjectCreate(0,"High@"+TimeToString(time),OBJ_ARROW_SELL,0,time,high);
            newhigh = high;
            newTime = time;  
         }            
         if(isLow){
            Print(__FUNCTION__," > Found a new low (",DoubleToString(low,_Digits),") at ",time,"...");
            ObjectCreate(0,"Low@"+TimeToString(time),OBJ_ARROW_BUY,0,time,low); 
            newlow = low;
            newTime = time;               
         }
      }   
   }
}
   
void findHighLowOBV(double &newlow, double &newhigh, datetime &newTime){
   int indexBar = VerificationCandles;
   double OBV[];
   if(CopyBuffer(handleOBV,0,1,VerificationCandles*2+1,OBV) < VerificationCandles *2+1) return;
   
   double value = OBV[indexBar];
   datetime time = iTime(_Symbol,PERIOD_CURRENT,indexBar+1);
      
   bool isHigh = true, isLow = true;
   for(int i = 1; i <= VerificationCandles; i++){
       double valLeft = OBV[indexBar+i];
       double valRight = OBV[indexBar-i];
       if(valLeft > value || valRight > value) isHigh = false;      
       if(valLeft < value || valRight < value) isLow = false;
          
       if(!isHigh && !isLow) break;
       if(i == VerificationCandles){
         if(isHigh){
            Print(__FUNCTION__," > Found a new high (",DoubleToString(value,_Digits),") at ",time,"...");
            ObjectCreate(0,"High@"+TimeToString(time),OBJ_ARROW_SELL,1,time,value);
            newhigh = value;
            newTime = time;    
         }
         if(isLow){
            Print(__FUNCTION__," > Found a new low (",DoubleToString(value,_Digits),") at ",time,"...");
            ObjectCreate(0,"Low@"+TimeToString(time),OBJ_ARROW_BUY,1,time,value);
            newlow = value;
            newTime = time;    
         }
      }   
   }
} 
 
Aesen #:

Some features i had in mind that would be nice to have on the EA is trend lines for when hidden and regular divergence is formed on the price charts and the obv indicator, maybe a nice trailing stop, also inputs that look something like this:


Variable Value

Regular Divergence True/False

Hidden Divergence True/False

Indicator Trendlines True/False

Price Trendlines True/False


I am trying to get this OBV Divergence EA to follow this Divergence cheat sheet i got from babypips.com:


Bullish Divergence (Reversal Up):

Candlestick Price - Lower Low

On Balance Volume - Higher Low


Bearish Divergence (Reversal Down):

Candlestick Price- Higher High

On Balance Volume - Lower High


Bullish Hidden Divergence (Trend Continuation Up):

Candlestick Price- Higher Low

On Balance Volume - Lower Low


Bearish Hidden Divergence (Trend Continuation Down):

Candlestick Price- Lower High

On Balance Volume - Higher High

A New Approach to Interpreting Classic and Hidden Divergence. Part II
A New Approach to Interpreting Classic and Hidden Divergence. Part II
  • www.mql5.com
The article provides a critical examination of regular divergence and efficiency of various indicators. In addition, it contains filtering options for an increased analysis accuracy and features description of non-standard solutions. As a result, we will create a new tool for solving the technical task.
 
Aesen #:

Some features i had in mind that would be nice to have on the EA is trend lines for when hidden and regular divergence is formed on the price charts and the obv indicator, maybe a nice trailing stop, also inputs that look something like this:


Variable Value

Regular Divergence True/False

Hidden Divergence True/False

Indicator Trendlines True/False

Price Trendlines True/False


I am trying to get this OBV Divergence EA to follow this Divergence cheat sheet i got from babypips.com:


Bullish Divergence (Reversal Up):

Candlestick Price - Lower Low

On Balance Volume - Higher Low


Bearish Divergence (Reversal Down):

Candlestick Price- Higher High

On Balance Volume - Lower High


Bullish Hidden Divergence (Trend Continuation Up):

Candlestick Price- Higher Low

On Balance Volume - Lower Low


Bearish Hidden Divergence (Trend Continuation Down):

Candlestick Price- Lower High

On Balance Volume - Higher High

Another feature that i had in mind was that the EA will stack more trades if it goes in favor of the first trade that it places, if that makes sense....


for example, when the EA spots a bullish divergence and there is a buy signal and the first buy trade is placed, if the trade goes in favor of the first buy trade that the EA placed then the EA will stack more trades in the buy direction and vice versa if it were a sell trade.
 
Aesen #:

Another feature that i had in mind was that the EA will stack more trades if it goes in favor of the first trade that it places, if that makes sense....


for example, when the EA spots a bullish divergence and there is a buy signal and the first buy trade is placed, if the trade goes in favor of the first buy trade that the EA placed then the EA will stack more trades in the buy direction and vice versa if it were a sell trade.
Can you also include a  zone recovery hedging strategy where a first trade is initiated lets say a buy with lot size 0.01, and when it goes in the negative direction with some pips, say below by 30 pips, a counter sell is activated with a higher lot size, say 0.02, where if it continues to go down, the trades close at a breakeven or a profit and the loop is started again. however, if the sell does not work out well and price goes up, a another buy trade of a higher lot size, say 0.03 is activated at the same price the first buy was initiated. if it continues up and the net results are profit , the loop closes the trades at a profit and starts again. therefore, the 30 pips gap is fixed and referred to as a recovery zone, making it profitable for higher accounts by eliminating loses. If this is possible to program
 
the code script is for Mt5 not mt4
 

Hello. Please write an EA for MT4:

Closes all previously manually opened trades when the MA (with all available muwings settings) touches the current price. Nothing else (is it a semi-automatic EA?).

 
torrr the current price. Nothing else.

Hello,

I just made it and didn't get to test it. I hope it works for you.

Regards.

//+------------------------------------------------------------------+
//|                                                     Practica.mq4 |
//|                        Copyright 2022, Antonio Simón Del Vecchio |
//|                    https://www.mql5.com/es/users/simondelvecchio |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, Antonio Simón Del Vecchio"
#property link      "https://www.mql5.com/es/users/simondelvecchio"
#property version   "1.00"
#property strict


input int Periodo = 50;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(OrdersTotal() > 0 && CruceMediaPrecio())
     {
      Cerrar();
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Cerrar()
  {
   double Precio = 0;
   for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS))
        {
         if(OrderType() == OP_BUY)
            Precio = Bid;
         else
            Precio = Ask;
         if(!OrderClose(OrderTicket(), OrderLots(), Precio, 3, clrNONE))
            Print("Error al cerrar la órden: ", GetLastError());
        }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool CruceMediaPrecio()
  {
   double Media = iMA(Symbol(), PERIOD_CURRENT, Periodo, 0, MODE_SMA, PRICE_CLOSE, 0);
   double Max = iHigh(Symbol(), PERIOD_CURRENT, 0);
   double Min = iLow(Symbol(), PERIOD_CURRENT, 0);
   if(Max > Media && Min < Media)
     {
      return(true);
     }
   return(false);
  }
//+------------------------------------------------------------------+
 

To: Antonio Simon Del Vecchio

Thank you! I'm a dummy... I have tocreate an EA in MetaEditor, insert this code, compile it and that's it?

 
torrr an EA in MetaEditor, insert this code, compile it and that's it?
Correct. When you compile it, the EA will be generated with the name you have given to this file in the MetaEditor.

Now I ask in general for those who can read me: as I shared the code, is it forbidden to share the EA? I mean the .exe file?
 
"I just cooked it and didn't have time to test it". And how can you run an EA in the tester that only closes positions?
Reason: