Need EA to have a Take Profit at the current candle close

To add comments, please log in or register
Jaybee66
33
Jaybee66  

Hello people. I am just starting out to learn coding my EA and its been a week of thick studying how best i can code it.

This EA trades 1M candle open and does not have a Stop loss, and i am well with that.

Can anyone help me with the code or help me complete the script. I would be grateful

1. I need Take profit to be the price at the close of the current candle (only if trade is in profit). if its in loss don't close trade.

2. Help with the right coding for lot/position size. the EA is only using minimum lot size even if i change it on the input tab after attaching EA to chart


//---
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
//---
CTrade         jbtrade;                      // object of CTrade class
CSymbolInfo    jbsymbol;                     // object of CSymbolInfo class
//--- input parameters
input ulong              InpDeviation              = 10;          // Deviation, in points (1.00045-1.00055=10 points)
input ulong              InpMagic                  = 201;         // Magic number
//---
datetime m_prev_bars                               = 0;           // "0" -> D'1970.01.01 00:00';
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
input double Lots       =0.25;   // Position size 
extern double TP        =110;   // Maximum Profit target in pips
int OnInit()
  {
     if(!jbsymbol.Name(Symbol())) // sets symbol name
     {
      Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");
      return(INIT_FAILED);
     }
//---
   jbtrade.SetExpertMagicNumber(InpMagic);
   jbtrade.SetMarginMode();
   jbtrade.SetTypeFillingBySymbol(jbsymbol.Name());
   jbtrade.SetDeviationInPoints(InpDeviation);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  //--- we work only at the time of the birth of new bar
   datetime time_0=iTime(jbsymbol.Name(),Period(),0);
   if(time_0==m_prev_bars)
      return;
      m_prev_bars=time_0;
//---
   jbtrade.Buy(jbsymbol.LotsMin());
//---
   
  }
//+------------------------------------------------------------------+
Basic Principles - Trading Operations - MetaTrader 5 Help
Basic Principles - Trading Operations - MetaTrader 5 Help
  • www.metatrader5.com
is an instruction given to a broker to buy or sell a financial instrument. There are two main types of orders: Market and Pending. In addition, there are special Take Profit and Stop Loss levels. is the commercial exchange (buying or selling) of a financial security. Buying is executed at the demand price (Ask), and Sell is performed at the...
Vladimir Karputov
Moderator
215844
Vladimir Karputov  

There is no take profit. So far, just the correct minimal code:

//+------------------------------------------------------------------+
//|                                                            1.mq5 |
//|                              Copyright © 2020, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2020, Vladimir Karputov"
#property version   "1.000"
//---
#include  <Trade\Trade.mqh>
#include  <Trade\SymbolInfo.mqh>
//---
CTrade         m_trade;                      // object of CTrade class
CSymbolInfo    m_symbol;                     // object of CSymbolInfo class
//--- input parameters
input group             "Position size management (lot calculation)"
input double   InpLots              = 0.25;        // Lots
input group             "Additional features"
input ulong    InpDeviation         = 10;          // Deviation
input ulong    InpMagic             = 201;         // Magic number
//---
datetime m_prev_bars                = 0 ;           // "0" -> D'1970.01.01 00:00';
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(!m_symbol.Name(Symbol())) // sets symbol name
     {
      Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");
      return(INIT_FAILED);
     }
//---
   m_trade.SetExpertMagicNumber(InpMagic);
   m_trade.SetMarginMode();
   m_trade.SetTypeFillingBySymbol(m_symbol.Name());
   m_trade.SetDeviationInPoints(InpDeviation);
//---
   return (INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- we work only at the time of the birth of new bar
   datetime time_0=iTime(m_symbol.Name(),Period(),0);
   if(time_0==m_prev_bars)
      return;
   m_prev_bars=time_0;
//--- check the number of 'BUY' positions for the current symbol and with the specified 'Magic number'
   ulong tickets_buy[];
   int position_exist_buy=PositionExist(m_symbol.Name(),POSITION_TYPE_BUY,InpMagic,tickets_buy);
   if(position_exist_buy>0)
      return;
//--- check the number of 'SELL' positions for the current symbol and with the specified 'Magic number'
   ulong tickets_sell[];
   int position_exist_sell=PositionExist(m_symbol.Name(),POSITION_TYPE_SELL,InpMagic,tickets_sell);
   if(position_exist_sell>0)
      return;
//---
   m_trade.Buy(InpLots,m_symbol.Name());
//---
  }
//+------------------------------------------------------------------+
Files:
1.mq5 6 kb
Jaybee66
33
Jaybee66  
Vladimir Karputov:

There is no take profit. So far, just the correct minimal code:

Vladimir Karputov thank you very much for your response and corrections. So is it not possible to input profit target (TP) as the close of the candle only if the trade is in profit. ?Whereas if candle closes in a loss TP, will have to set a fixed TP of say 10 pips? 
Vladimir Karputov
Moderator
215844
Vladimir Karputov  
Jaybee66 :
Vladimir Karputov  thank you very much for your response and corrections. So is it not possible to input profit target (TP) as the close of the candle only if the trade is in profit. ?Whereas if candle closes in a loss TP, will have to set a fixed TP of say 10 pips? 

The bar's closing price cannot be a Take Profit target - since the bar's closing price is the current price (plus or minus the spread). You can use either a fixed Take Profit (N points) or the second option: close the position (close by the market) if the position has reached profit (profit either in Points or in the money of the deposit).

Jaybee66
33
Jaybee66  
Vladimir Karputov:

The bar's closing price cannot be a Take Profit target - since the bar's closing price is the current price (plus or minus the spread). You can use either a fixed Take Profit (N points) or the second option: close the position (close by the market) if the position has reached profit (profit either in Points or in the money of the deposit).

Vladimir Karputov:

The bar's closing price cannot be a Take Profit target - since the bar's closing price is the current price (plus or minus the spread). You can use either a fixed Take Profit (N points) or the second option: close the position (close by the market) if the position has reached profit (profit either in Points or in the money of the deposit).

Hooo alright i see now...so the second option would really do good. Now i i have to code for the second option. Can you help me with coding the fixed profit target in points since its mt5. I will then be able to customize a favourable profit target in when i do strategy testing and optimization. 
Jaybee66
33
Jaybee66  
Vladimir Karputov:

There is no take profit. So far, just the correct minimal code:

So may you help me adding tp to the script 110 points
Vladimir Karputov
Moderator
215844
Vladimir Karputov  
Jaybee66 :
So may you help me adding tp to the script 110 points

Specify:

   pips=0,     // Pips (1.00045-1.00055=1 pips)
   points=1,   // Points (1.00045-1.00055=10 points)

?

Jaybee66
33
Jaybee66  
Vladimir Karputov:

Specify:

?

That means i will only specify pip/ point conversion like that and write the code under onTick right 
Vladimir Karputov
Moderator
215844
Vladimir Karputov  
Jaybee66:
That means i will only specify pip/ point conversion like that and write the code under onTick right 

Forum on trading, automated trading systems and testing trading strategies

Need EA to have a Take Profit at the current candle close

Vladimir Karputov, 2020.09.03 13:42

Specify:

   pips=0,     // Pips (1.00045-1.00055=1 pips)
   points=1,   // Points (1.00045-1.00055=10 points)

?


Jaybee66
33
Jaybee66  
//+------------------------------------------------------------------+
//|                                                            1.mq5 |
//|                              Copyright © 2020, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2020, Vladimir Karputov"
#property version   "1.000"
//---
#include  <Trade\Trade.mqh>
#include  <Trade\SymbolInfo.mqh>
//---
CTrade         m_trade;                      // object of CTrade class
CSymbolInfo    m_symbol;                     // object of CSymbolInfo class
//--- input parameters
input group             "Position size management (lot calculation)"
input double   InpLots              = 0.25;        // Lots
input group             "Additional features"
input ulong    InpDeviation         = 10;          // Deviation
input ulong    InpMagic             = 201;         // Magic number
//---
datetime m_prev_bars                = 0 ;           // "0" -> D'1970.01.01 00:00';
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(!m_symbol.Name(Symbol())) // sets symbol name
     {
      Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");
      return(INIT_FAILED);
     }
//---
   m_trade.SetExpertMagicNumber(InpMagic);
   m_trade.SetMarginMode();
   m_trade.SetTypeFillingBySymbol(m_symbol.Name());
   m_trade.SetDeviationInPoints(InpDeviation);
//---
   return (INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- we work only at the time of the birth of new bar
   datetime time_0=iTime(m_symbol.Name(),Period(),0);
   if(time_0==m_prev_bars)
      return;
   m_prev_bars=time_0;
//--- check the number of 'BUY' positions for the current symbol and with the specified 'Magic number'
   ulong tickets_buy[];
   int position_exist_buy=PositionExist(m_symbol.Name(),POSITION_TYPE_BUY,InpMagic,tickets_buy);
   if(position_exist_buy>0)
      return;
//--- check the number of 'SELL' positions for the current symbol and with the specified 'Magic number'
   ulong tickets_sell[];
   int position_exist_sell=PositionExist(m_symbol.Name(),POSITION_TYPE_SELL,InpMagic,tickets_sell);
   if(position_exist_sell>0)
      return;
//---
   m_trade.Buy(InpLots,m_symbol.Name());
//---
  }
   pips=0,     // Pips (1.00045-1.00055=1 pips)
   points=1,   // Points (1.00045-1.00055=10 points)
   
 if(PositionSelectByTicket(PositionExist)){
  double posPrice = PositionGetDouble(POSITION_PRICE_OPEN);
  double posTp    = PositionGetDouble(POSITION_TP);
 
  if(posTp == 0){
  double Tp = posPrice + 0.0010
  trade.PositonModify(PositionExist, Tp);
I have been researching and trying to figure out how best i can fuse Take profit together with the rest of the script that you coded Vlad. I am lost on this one. There are 2 errors. Also from my research i have noticed i have to clarify how TP has to be done depending on whether broker is a 3 or 4 digit. But for now i need some help with the EA to take profit. You can set any TP target for now, otherwise i will change later when backtesting.
Vladimir Karputov
Moderator
215844
Vladimir Karputov  
How to start with MQL5
How to start with MQL5
  • 2020.03.05
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
12
To add comments, please log in or register