How to start with MQL5 - page 26

 

Reverse the last position

Code: 'Reverse the last position.mq5'

//+------------------------------------------------------------------+
//|                                    Reverse the last position.mq5 |
//|                              Copyright © 2021, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2021, Vladimir Karputov"
#property version   "1.000"
//---
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
//---
CPositionInfo  m_position;                   // object of CPositionInfo class
CTrade         m_trade;                      // object of CTrade class
CSymbolInfo    m_symbol;                     // object of CSymbolInfo class
//--- input parameters
input uint     InpStopLoss       = 150;      // Stop Loss
input uint     InpTakeProfit     = 300;      // Take Profit
//---
double               m_stop_loss       = 0.0;         // Stop Loss         -> double
double               m_take_profit     = 0.0;         // Take Profit       -> double
ENUM_POSITION_TYPE   m_last_positions  = WRONG_VALUE; // last position type
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- forced initialization of variables
   m_stop_loss       = 0.0;         // Stop Loss         -> double
   m_take_profit     = 0.0;         // Take Profit       -> double
   m_last_positions  = WRONG_VALUE; // last position type
//---
   ResetLastError();
   if(!m_symbol.Name(Symbol())) // sets symbol name
     {
      Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");
      return(INIT_FAILED);
     }
   RefreshRates();
//---
   m_stop_loss       = InpStopLoss     * m_symbol.Point();
   m_take_profit     = InpTakeProfit   * m_symbol.Point();
//--- initialize the generator of random numbers
   MathSrand(GetTickCount());
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- check if a position is present and display the time of its changing
   if(!PositionSelect(Symbol()))
     {
      if(m_last_positions==WRONG_VALUE)
        {
         if(!RefreshRates())
            return;
         int math_rand=MathRand();
         if(math_rand<32767/2)
           {
            //--- stop loss
            double sl=(m_stop_loss==0.0)?0.0:m_symbol.Ask()-m_stop_loss;
            if(m_stop_loss>0.0)
               sl=m_symbol.NormalizePrice(sl);
            //--- take profit
            double tp=(m_take_profit==0.0)?0.0:m_symbol.Ask()+m_take_profit;
            if(m_take_profit>0.0)
               tp=m_symbol.NormalizePrice(tp);
            //--- open buy
            Print("Start BUY ...");
            m_trade.Buy(1.0,m_symbol.Name(),m_symbol.Ask(),sl,tp);
           }
         else
           {
            //--- stop loss
            double sl=(m_stop_loss==0.0)?0.0:m_symbol.Bid()+m_stop_loss;
            if(m_stop_loss>0.0)
               sl=m_symbol.NormalizePrice(sl);
            //--- take profit
            double tp=(m_take_profit==0.0)?0.0:m_symbol.Bid()-m_take_profit;
            if(m_take_profit>0.0)
               tp=m_symbol.NormalizePrice(tp);
            //--- open sell
            Print("Start SELL ...");
            m_trade.Sell(1.0,m_symbol.Name(),m_symbol.Bid(),sl,tp);
           }
        }
      else
        {
         if(m_last_positions==POSITION_TYPE_BUY)
           {
            if(!RefreshRates())
               return;
            //--- stop loss
            double sl=(m_stop_loss==0.0)?0.0:m_symbol.Bid()+m_stop_loss;
            if(m_stop_loss>0.0)
               sl=m_symbol.NormalizePrice(sl);
            //--- take profit
            double tp=(m_take_profit==0.0)?0.0:m_symbol.Bid()-m_take_profit;
            if(m_take_profit>0.0)
               tp=m_symbol.NormalizePrice(tp);
            //--- open sell
            Print("Start SELL ...");
            m_trade.Sell(1.0,m_symbol.Name(),m_symbol.Bid(),sl,tp);
           }
         else
           {
            if(!RefreshRates())
               return;
            //--- stop loss
            double sl=(m_stop_loss==0.0)?0.0:m_symbol.Ask()-m_stop_loss;
            if(m_stop_loss>0.0)
               sl=m_symbol.NormalizePrice(sl);
            //--- take profit
            double tp=(m_take_profit==0.0)?0.0:m_symbol.Ask()+m_take_profit;
            if(m_take_profit>0.0)
               tp=m_symbol.NormalizePrice(tp);
            //--- open buy
            Print("Start BUY ...");
            m_trade.Buy(1.0,m_symbol.Name(),m_symbol.Ask(),sl,tp);
           }
        }
     }
   else
     {
      //--- receive position type
      m_last_positions=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
     }
  }
//+------------------------------------------------------------------+
//| Refreshes the symbol quotes data                                 |
//+------------------------------------------------------------------+
bool RefreshRates()
  {
//--- refresh rates
   if(!m_symbol.RefreshRates())
     {
      Print(__FILE__," ",__FUNCTION__,", ERROR: ","RefreshRates error");
      return(false);
     }
//--- protection against the return value of "zero"
   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
     {
      Print(__FILE__," ",__FUNCTION__,", ERROR: ","Ask == 0.0 OR Bid == 0.0");
      return(false);
     }
//---
   return(true);
  }
//+------------------------------------------------------------------+
 
I am having issues to subscribe in a trade  signal i want to copy
 
ifyboya:
I am having issues to subscribe in a trade  signal i want to copy
How to Subscribe to MT4/MT5 Signal -
https://www.mql5.com/en/forum/189731
  (MT4)
https://www.mql5.com/en/forum/336422
  (MT5)
How to Subscribe to a MT4 Signal (new instructions, after 1065 version upgrade)
How to Subscribe to a MT4 Signal (new instructions, after 1065 version upgrade)
  • 2017.04.10
  • www.mql5.com
Below are step by step instructions, of how to subscribe to a MΤ4 signal: 1. Register a MQL5 account: https://www.mql5.com/en/auth_register 2...
 
Hey Vladimir, do you have any idea how can I check to see if current price is at a support and resistance level ?
 
Ahmad861 :
Hey Vladimir, do you have any idea how can I check to see if current price is at a support and resistance level ?

It's easy. Just first decide what these "levels" are :)

 
Vladimir Karputov:

It's easy. Just first decide what these "levels" are :)

I wont know the levels, i need to manually check, how do i automate this procedure

 
Ahmad861 :

I wont know the levels, i need to manually check , how do i automate this procedure

Start with a small problem: describe the "level" algorithm. Start embodying your idea in MQL5 code. Then I will help.

 

I want to code the following picture for my EA

You can see it forms a V shape but the candles don't form a very clear V

 

Im able to code the candlesticks where they form a perfect V shape but can't understand how i would do this

 
Ahmad861 :

I want to code the following picture for my EA

You can see it forms a V shape but the candles don't form a very clear V

 

Im able to code the candlesticks where they form a perfect V shape but can't understand how i would do this

Possible idea: apply Fractals. Probably with Fractals the picture will become clearer ...

 
Vladimir Karputov:

Possible idea: apply Fractals. Probably with Fractals the picture will become clearer ...

Oh, I need to look into it.