Closing all positions when a profit is targeted or a % BUT....

 

I am searching something that would close all positions when a profit is targeted or a %,  but without removing my robot that open positions. It can also be a % in the balance account to be targeted).

Is it something possible on MT4?


I search the net but I found only another Expert Advisor. So I have to remove my robot that is auto trading also has an Expert Advisor to use this other expert advisor.


The idea is to get a certain profit then it close all the positions and the robot open new ones. 


Thank you in advance if you can show me the path to maybe a script that could work at the same time as my Expert Advisor.

 
Topdaryl: The idea is to get a certain profit then it close all the positions and the robot open new ones.

All that does is cost you a second spread. Change the EA or do partial close(s).

 
The idea is to close all my position for the day without to have to look at it. Then the next day the robot choose new positions.
 
Topdaryl:

I am searching something that would close all positions when a profit is targeted or a %,  but without removing my robot that open positions. It can also be a % in the balance account to be targeted).

Is it something possible on MT4?


I search the net but I found only another Expert Advisor. So I have to remove my robot that is auto trading also has an Expert Advisor to use this other expert advisor.


The idea is to get a certain profit then it close all the positions and the robot open new ones. 


Thank you in advance if you can show me the path to maybe a script that could work at the same time as my Expert Advisor.

@ Topdaryl

it is simply code attached. Add it to another gallery where the robot is not available. You can follow the robot's magic number if you want.

//+------------------------------------------------------------------+
//|                                       Haskayafxstubborn goat.mq4 |
//|                                 Copyright 2019, Haskaya Software |
//|                                   https://www.haskayayazilim.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, Haskaya Software"
#property link      "https://www.haskayayazilim.net"
#property version   "1.00"
#property strict
input  bool   UseRobotMagic=true;// using Robot Magic
input  int    KontrolMagicEA=0;// Robot Expert Magic Number
input  double KarTotalKapat=300;// example 300 USD Profit. All Close Orders
double    TP=0;


  
int OnInit()
  {
//---
   ;
//---
   return(INIT_SUCCEEDED);
  }
  
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }

void TakeProfitTakip(bool GlnMagicType)
  {
   for(int it=0; it<OrdersTotal(); it++)
     {
      if(OrderSelect(it,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(GlnMagicType  && OrderMagicNumber()==KontrolMagicEA && OrderSymbol()==Symbol() ) TP+=OrderProfit()+OrderSwap()+OrderCommission();
      if(!GlnMagicType && OrderSymbol()==Symbol() ) TP+=OrderProfit()+OrderSwap()+OrderCommission();
      }
     
     if(KarTotalKapat>0 && TP>=KarTotalKapat) TotalKapat(UseRobotMagic);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
//-
TakeProfitTakip(UseRobotMagic);

  }
  
  int TotalKapat(bool GlnMagicType )
{
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    bool sdsf=OrderSelect(i, SELECT_BY_POS);
    int type   = OrderType();

    bool result = false;
   if(GlnMagicType && OrderMagicNumber()==KontrolMagicEA && OrderSymbol()==Symbol() )
        {
    switch(type)
    {
      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE );
                          break;
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE );
                          break;
    case OP_BUYLIMIT  :
      case OP_BUYSTOP   :
      case OP_SELLLIMIT :
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
      }
    }
    if(!GlnMagicType && OrderSymbol()==Symbol() )
        {
    switch(type)
    {
      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE );
                          break;
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE );
                          break;
    case OP_BUYLIMIT  :
      case OP_BUYSTOP   :
      case OP_SELLLIMIT :
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
      }
    }
    
  }
  
  return(0);
}

Topdaryl
Topdaryl
  • 2021.02.05
  • www.mql5.com
Trader's profile
 

Mehmet hocam,

How can we write this in Mql5 ?

 
cuneytates #: How can we write this in Mql5 ?

You have only four choices:

  1. Search for it (CodeBase or Market). Do you expect us to do your research for you?

  2. Beg at:

  3. MT4: Learn to code it.
    MT5: Begin learning to code it.

    If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into your code.

    Also:
              Migrating from MQL4 to MQL5 - MQL5 Articles № 18 (2010)
              Transferring Indicators from MQL4 to MQL5
              MQL's OOP notes: Converting MetaTrader 4 indicators to MetaTrader 5
              Market Condition Evaluation based on standard indicators in Metatrader 5 - Trading Systems - MQL5 programming forum - Page 222 #2218
              Problem with drawing indicators in mql5 - General - MQL5 programming forum #2 (2021)

  4. Or pay (Freelance) someone to code it. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2019)

We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
          No free help (2017)

 

hello i have ea and i want to add 'close all position at price....'

please help me i cant do this

can someone help me add code to my ea?

 
Vusal Besirov #: please help me i cant do this
  1. Don't Hijack other threads for your off-topic post. Next time, make your own, new, thread.

  2. Why can't you?

    MT4: Learn to code it.
    MT5: Begin learning to code it.

    If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into your code.

  3. Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your difficulty.
              No free help (2017)

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2018)

    We're not going to code it for you (although it could happen if you are lucky or the problem is interesting).
              No free help (2017)

 
Cuneyt Ates #:

Mehmet hocam,

How can we write this in Mql5 ?


IF THERE ARE ANY BUGS JUST USE CHAT GPT TO DEBUG

//+------------------------------------------------------------------+
//|                                                    orders_close.mq5|
//|                                       Copyright 2021, Haskaya FX |
//|                                 https://www.haskayafx.com         |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, Haskaya FX"
#property link      "https://www.haskayafx.com"
#property version   "1.00"
#property strict

input bool   UseRobotMagic=true; // using Robot Magic
input int    KontrolMagicEA=0;   // Robot Expert Magic Number
input double KarTotalKapat=300;  // example 300 USD Profit. All Close Orders

double TP=0;

void TakeProfitTakip(bool GlnMagicType)
{
   for(int it=0; it<PositionsTotal(); it++)
   {
      if(PositionSelectByTicket(PositionGetTicket(it))==false) break;
      if(GlnMagicType && PositionGetInteger(POSITION_MAGIC)==KontrolMagicEA && PositionGetSymbol()==Symbol()) 
         TP+=PositionGetDouble(POSITION_PROFIT)+PositionGetDouble(POSITION_SWAP)+PositionGetDouble(POSITION_COMMISSION);
      if(!GlnMagicType && PositionGetSymbol()==Symbol()) 
         TP+=PositionGetDouble(POSITION_PROFIT)+PositionGetDouble(POSITION_SWAP)+PositionGetDouble(POSITION_COMMISSION);
   }
     
   if(KarTotalKapat>0 && TP>=KarTotalKapat) TotalKapat(UseRobotMagic);
}

int TotalKapat(bool GlnMagicType)
{
   int total = PositionsTotal();
   for(int i=total-1; i>=0; i--)
   {
      bool sdsf=PositionSelectByTicket(PositionGetTicket(i));
      ENUM_POSITION_TYPE type = PositionGetInteger(POSITION_TYPE);

      bool result = false;
      if(GlnMagicType && PositionGetInteger(POSITION_MAGIC)==KontrolMagicEA && PositionGetSymbol()==Symbol())
      {
         switch(type)
         {
            case POSITION_TYPE_BUY  : result = PositionClose( PositionGetTicket(), PositionGetDouble(POSITION_VOLUME), SymbolInfoDouble(Symbol(), SYMBOL_BID), 5, clrNONE );
                                     break;
            case POSITION_TYPE_SELL : result = PositionClose( PositionGetTicket(), PositionGetDouble(POSITION_VOLUME), SymbolInfoDouble(Symbol(), SYMBOL_ASK), 5, clrNONE );
                                     break;
            case POSITION_TYPE_BUY_LIMIT :
            case POSITION_TYPE_BUY_STOP  :
            case POSITION_TYPE_SELL_LIMIT:
            case POSITION_TYPE_SELL_STOP : result = PositionDelete( PositionGetTicket() );
         }
      }
      if(!GlnMagicType && PositionGetSymbol()==Symbol())
      {
         switch(type)
         {
            case POSITION_TYPE_BUY  : result = PositionClose( PositionGetTicket(), PositionGetDouble(POSITION_VOLUME), SymbolInfoDouble(Symbol(), SYMBOL_BID), 5, clrNONE );
                                     break;
            case POSITION_TYPE_SELL : result = PositionClose( PositionGetTicket(), PositionGetDouble(POSITION_VOLUME), SymbolInfoDouble(Symbol(), SYMBOL_ASK), 5, clrNONE );
                                     break;
            case POSITION_TYPE_BUY_LIMIT :
            case POSITION_TYPE_BUY_STOP  :
            case POSITION_TYPE_SELL_LIMIT:
            case POSITION_TYPE_SELL_STOP : result = PositionDelete( PositionGetTicket() );
         }
      }
   }
  
   return(0);
}

void OnTick()
{
   TakeProfitTakip(UseRobotMagic);
}

void OnDeinit(const int reason)
{
}