(MQL5) Close when position reaches a specified profit

 

Hi, i am trying to create a script which would close a position when a specified profit is met. so far i have had zero luck at all.


Currently i am using the below code to test out. I can compile it fine with 1 warning, but it doesn't work.... ( i understand that if successful this would close all open positions which is fine for now)

Can anyone please point me in the right direction??

i am very new to this, and am slowly going through an online c++ tutorial to try and learn things, and have been through the help reference but to my clumsy way  of thinking it is very obtuse and difficult to use.

the code below is basically cobbled together with parts from ( *** ) tutorials, and what little i have understood from the c++ i have gone through so far.

#include<Trade\Trade.mqh>
// create instance of ctrade
CTrade trade;
// create variable for ask,bid, profit, goal
double Ask,Bid,PositionProfit;
double Goal=2.00;
void OnTick()
  {
// calc ask price
   Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
// calc bid price
   Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
// calc positionprofit price
   PositionProfit=NormalizeDouble(PositionGetDouble(POSITION_PROFIT),_Digits);
   if(PositionProfit>Goal)
     {
      // count down until there are no open positions
      for(int i=PositionsTotal()-1; i>=0; i--) // goes thru all positions
        {
         //get the ticket number for current position
         int ticket=PositionGetTicket(i);
         // close the current position
         trade.PositionClose(ticket);
        }
     }
  }


Many thanks to anyone that can help!

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Position ticket. Unique number assigned to each newly opened position. It usually matches the ticket of an order used to open the position except when the ticket is changed as a result of service operations on the server, for example, when charging swaps with position re-opening. To find an order used to open a position, apply the...
 

Please insert the code correctly (use the button Code ) - the first time I corrected your text and applied the button  Code .

An example of an Expert Advisor (this is an Expert, not a Script !!!). The EA checks the positions for all symbols and for all Magic Numbers.

//+------------------------------------------------------------------+
//|                                    Close Profitable Position.mq5 |
//|                              Copyright © 2020, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2020, Vladimir Karputov"
#property version   "1.000"
/*
   barabashkakvn Trading engine 3.143
*/
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
//---
CPositionInfo  m_position;             // object of CPositionInfo class
CTrade         m_trade;                // object of CTrade class
//--- input parameters
input double   InpProfitInMoney  = 9;  // Profit in money
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
        {
         double profit=m_position.Commission()+m_position.Swap()+m_position.Profit();
         if(profit>=InpProfitInMoney)
            m_trade.PositionClose(m_position.Ticket());
        }
  }
//+------------------------------------------------------------------+
 
Vladimir Karputov:

Please insert the code correctly (use the button  ) - the first time I corrected your text and applied the button   .

An example of an Expert Advisor (this is an Expert, not a Script !!!). The EA checks the positions for all symbols and for all Magic Numbers.


i had no idea that button was even there! many thanks for your help, much appreciated!