Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1394

 
Dear Watchmen, who knows.
is it possible to get data from mt5 for example to a website or some system for analysis https://www.mql5.com/ru/docs/integration/python_metatrader5
is there a similar api to get data from mt4 without using EA?
Документация по MQL5: Интеграция / MetaTrader для Python
Документация по MQL5: Интеграция / MetaTrader для Python
  • www.mql5.com
MetaTrader для Python - Интеграция - Справочник MQL5 - Справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
If you have a Goko, I'd like to see the code of how to write it.
 
Tenimagalon:
If you have one, I'd like to see the code of how to do it.

Catch

double MyProfit=1000; // уровень профита
//+--------------------------------------------------------------------------------------------------------------------+
//| Expert tick function                                                                                               |
//+--------------------------------------------------------------------------------------------------------------------+
void OnTick()
  {
//---
   if(Open_Pr()>MyProfit)DelOrders();
//---
  }
//+--------------------------------------------------------------------------------------------------------------------+
//|  Суммарный профит в валюте депозита открытых позиций                                                               |
//+--------------------------------------------------------------------------------------------------------------------+
double Open_Pr(string sy="")
  { double p = 0;
   if (sy == "0") sy = Symbol();
   for(int pos=OrdersTotal()-1;pos>=0;pos--)
     {
      if(OrderSelect(pos,SELECT_BY_POS)==true)
        {
         if(OrderSymbol() == sy || sy == ""){p+=OrderProfit()+OrderSwap()+OrderCommission();}
        }
     }
   return(p);
  }
//+--------------------------------------------------------------------------------------------------------------------+
//| Функция удаления и закрытия ордеров                                                                                |
//+--------------------------------------------------------------------------------------------------------------------+
void DelOrders()
  {
   while(true)
     {
      bool find_order=false;
      //----
      for(int pos=OrdersTotal()-1;pos>=0;pos--)
      if(OrderSelect(pos,SELECT_BY_POS)==true)
      if(OrderSymbol()==_Symbol)
        {
         find_order=true;
         //----
         if(OrderType()==OP_BUY)
           {
            RefreshRates(); int slip=(int)(((Ask-Bid)/Point)*2);
            if(OrderClose(OrderTicket(),OrderLots(),Bid,slip,clrBlue)==false){}
           }
         //----
         if(OrderType()==OP_SELL)
           {
            RefreshRates(); int slip=(int)(((Ask-Bid)/Point)*2);
            if(OrderClose(OrderTicket(),OrderLots(),Ask,slip,clrRed)==false){}
           }
         //----
         if(OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT)
         if(OrderDelete(OrderTicket(),clrRed)==false){}
         //----
         if(OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT)
         if(OrderDelete(OrderTicket(),clrBlue)==false){}
         Alert("Все ордера закрыты!");
        } 
      if(find_order==false) Alert("Нет ордеров!");break;
     } 
  }
//+--------------------------------------------------------------------------------------------------------------------+
 
MakarFX:

Catch

Hi! I tried the code - it won't close for some reason

Picture 5674

 
SanAlex:

Hi! I tried the code - it won't close for some reason


On total profit, not separately....
 
SanAlex:

Hi! I tried the code - it won't close for some reason.


I've done it like this. It's working!

//+------------------------------------------------------------------+
//|                                             MakarFX_MyProfit.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
input string   t2="------------ Exchange TP SL --------"; //
input double   InpTProfit       = 10;            // Exchange TP уровень профита
input double   InpStopLoss      = 1000000;       // Exchange SL
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   ProfitOnTick();
  }
//+------------------------------------------------------------------+
//| Суммарный профит в валюте депозита открытых позиций              |
//+------------------------------------------------------------------+
bool ProfitOnTick(void)
  {
   bool res=false;
   double PROFIT_BUY=0.00;
   double PROFIT_SELL=0.00;
   for(int i=OrdersTotal()-1; i>=0; i--) // returns the number of open positions
     {
      if(OrderSelect(i,SELECT_BY_POS) && OrderSymbol()==Symbol())
        {
         if(OrderSymbol()==Symbol() && OrderType()==OP_BUY)
           {
            PROFIT_BUY=PROFIT_BUY+NormalizeDouble(OrderProfit(),2);
           }
         if(OrderSymbol()==Symbol() && OrderType()==OP_SELL)
           {
            PROFIT_SELL=PROFIT_SELL+NormalizeDouble(OrderProfit(),2);
           }
        }
     }
   int Close_ticketb=0;
   int totalb=OrdersTotal();
   int b = 0;
   for(b = totalb; b >=0; b--)
     {
      if(OrderSelect(b,SELECT_BY_POS) && OrderSymbol()==Symbol())
        {
         //OrderSelect(i,SELECT_BY_POS);
         if(OrderSymbol()==Symbol() && OrderType()==OP_BUY)
           {
            if(PROFIT_BUY<-InpStopLoss || PROFIT_BUY>=InpTProfit)
              {
               Close_ticketb = OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symbol(),MODE_BID),5);
               PlaySound("ok.wav");
              }
           }
        }
      res=true;
     }
   int Close_tickets=0;
   int totals=OrdersTotal();
   int s = 0;
   for(s = totals; s >=0; s--)
     {
      if(OrderSelect(s,SELECT_BY_POS) && OrderSymbol()==Symbol())
        {
         if(OrderSymbol()==Symbol() && OrderType()==OP_SELL)
           {
            if(PROFIT_SELL<-InpStopLoss || PROFIT_SELL>=InpTProfit)
              {
               Close_tickets = OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symbol(),MODE_ASK),5);
               PlaySound("ok.wav");
              }
           }
        }
      res=true;
     }
//--- result
   return(res);
  }
//+------------------------------------------------------------------+
 
Vladislav Andruschenko:
Total profit, not separate....

Well, there you go! Then I apologise!

That's how I messed up.

//+------------------------------------------------------------------+
//| Check  closing                                                   |
//+------------------------------------------------------------------+
bool ProfitTarget(void)
  {
   bool res=false;
   if(AccountInfoDouble(ACCOUNT_EQUITY)>=TargetProfit)
     {
      CloseAllOrders();
      Sleep(SLEEPTIME*1000);
      CloseAllOrders();
      ExpertRemove();
      DeleteChart();
      PlaySound("expert.wav");
      res=true;
     }
//--- result
   return(res);
  }
//+------------------------------------------------------------------+
 
SanAlex:

Well, there you go! Then I apologise!

That's how I messed up.

Here it is, ready for battle!!!

//+------------------------------------------------------------------+
//|                                             MakarFX_MyProfit.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#include <stdlib.mqh>
//--- Inputs
input string   t="------------- Balans Parameters -----"; //
input string   Template         = "ADX";         // Имя шаблона(without '.tpl')
input double   TargetProfit     = 1000000;       // Баланс + Прибыль(прибавить к балансу)
input string   t2="------------ Exchange TP SL --------"; //
input double   InpTProfit       = 10;            // Exchange TP уровень профита
input double   InpStopLoss      = 1000000;       // Exchange SL
//---
uint   SLEEPTIME        = 1;
bool   CloseOpenOrders  = true;
double Price[2];
ENUM_TIMEFRAMES TimeFrame; // Change TimeFrame - Current = dont changed
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   ProfitOnTick();
   ProfitTarget();
  }
//+------------------------------------------------------------------+
//| Суммарный профит в валюте депозита открытых позиций              |
//+------------------------------------------------------------------+
bool ProfitOnTick(void)
  {
   bool res=false;
   double PROFIT_BUY=0.00;
   double PROFIT_SELL=0.00;
   for(int i=OrdersTotal()-1; i>=0; i--) // returns the number of open positions
     {
      if(OrderSelect(i,SELECT_BY_POS) && OrderSymbol()==Symbol())
        {
         if(OrderSymbol()==Symbol() && OrderType()==OP_BUY)
           {
            PROFIT_BUY=PROFIT_BUY+NormalizeDouble(OrderProfit(),2);
           }
         if(OrderSymbol()==Symbol() && OrderType()==OP_SELL)
           {
            PROFIT_SELL=PROFIT_SELL+NormalizeDouble(OrderProfit(),2);
           }
        }
     }
   int Close_ticketb=0;
   int totalb=OrdersTotal();
   int b = 0;
   for(b = totalb; b >=0; b--)
     {
      if(OrderSelect(b,SELECT_BY_POS) && OrderSymbol()==Symbol())
        {
         //OrderSelect(i,SELECT_BY_POS);
         if(OrderSymbol()==Symbol() && OrderType()==OP_BUY)
           {
            if(PROFIT_BUY<-InpStopLoss || PROFIT_BUY>=InpTProfit)
              {
               Close_ticketb = OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symbol(),MODE_BID),5);
               PlaySound("ok.wav");
              }
           }
        }
      res=true;
     }
   int Close_tickets=0;
   int totals=OrdersTotal();
   int s = 0;
   for(s = totals; s >=0; s--)
     {
      if(OrderSelect(s,SELECT_BY_POS) && OrderSymbol()==Symbol())
        {
         if(OrderSymbol()==Symbol() && OrderType()==OP_SELL)
           {
            if(PROFIT_SELL<-InpStopLoss || PROFIT_SELL>=InpTProfit)
              {
               Close_tickets = OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symbol(),MODE_ASK),5);
               PlaySound("ok.wav");
              }
           }
        }
      res=true;
     }
//--- result
   return(res);
  }
//+------------------------------------------------------------------+
//| Check  closing                                                   |
//+------------------------------------------------------------------+
bool ProfitTarget(void)
  {
   bool res=false;
   if(AccountInfoDouble(ACCOUNT_EQUITY)>=TargetProfit)
     {
      CloseAllOrders();
      Sleep(SLEEPTIME*1000);
      CloseAllOrders();
      ExpertRemove();
      DeleteChart();
      PlaySound("expert.wav");
      res=true;
     }
//--- result
   return(res);
  }
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
void CloseAllOrders(void)
  {
   int iOrders=OrdersTotal()-1, i;
   if(CloseOpenOrders)
     {
      for(i=iOrders; i>=0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderType()<=OP_SELL) && GetMarketInfo() &&
            !OrderClose(OrderTicket(),OrderLots(),Price[1-OrderType()],0))
            Print(OrderError());
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
           {
            if(OrderDelete(OrderTicket()))
               Print(OrderError());
           }
        }
     }
  }
//+------------------------------------------------------------------+
//| Function..: OrderError                                           |
//+------------------------------------------------------------------+
string OrderError(void)
  {
   int iError=GetLastError();
   return(StringConcatenate("Order:",OrderTicket()," GetLastError()=",iError," ",ErrorDescription(iError)));
  }
//+------------------------------------------------------------------+
//| Function..: GetMarketInfo                                        |
//+------------------------------------------------------------------+
bool GetMarketInfo(void)
  {
   RefreshRates();
   Price[0]=MarketInfo(OrderSymbol(),MODE_ASK);
   Price[1]=MarketInfo(OrderSymbol(),MODE_BID);
   double dPoint=MarketInfo(OrderSymbol(),MODE_POINT);
   if(dPoint==0)
      return(false);
   return(Price[0]>0.0 && Price[1]>0.0);
  }
//+------------------------------------------------------------------+
//| start function                                                   |
//+------------------------------------------------------------------+
void DeleteChart(void)
  {
   long currChart,prevChart=ChartFirst();
   int i=0,limit=100;
   bool errTemplate;
   while(i<limit)
     {
      currChart=ChartNext(prevChart);
      if(TimeFrame!=PERIOD_CURRENT)
        {
         ChartSetSymbolPeriod(prevChart,ChartSymbol(prevChart),TimeFrame);
        }
      errTemplate=ChartApplyTemplate(prevChart,Template+".tpl");
      if(!errTemplate)
        {
         Print("Error ",ChartSymbol(prevChart),"-> ",GetLastError());
        }
      if(currChart<0)
         break;
      Print(i,ChartSymbol(currChart)," ID =",currChart);
      prevChart=currChart;
      i++;
     }
  }
//+------------------------------------------------------------------+
 
SanAlex:

Here it is, ready for battle!!!

Why cheat?

Put the right symbol in here

Open_Pr(Symbol())

and everything will be fine.

P.S. Sasha, you didn't count swap and commission.

 

MakarFX:

Why cheat?

Put the right symbol here.

and everything will be fine.

P.S. Sasha, you didn't take into account swap and commission

i got used to such settings - and about commission and swaps - it's all nonsense, i care about profit - i set 100, let it close for me at 90 and that's good too - but sometimes it also closes at 110

i don't know what kind of action and how the broker closes it but the function works based on the set (amount) in the settings.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

for example i put 5 in the settings - it closed 5.20

Picture 567469

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

or another example - I had specified in the settings that on all pairs where Expert Advisor is(they all have the same settings) for a total profit of 70 000 I closed everything - I ended up with 15 losses

Picture 777469

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

I have a suspicion that not everyone understands what we are talking about. - As for the overall profit, it is one thing - but the profit on each pair is different. You have to put an Expert Advisor on every pair.

like this !!!

Picture 777469 ex.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

When the function works on the total profit, it will change the pattern on all open charts at once.

You can create your own pattern, give it a name - use it as an Expert Advisor with a new price or whatever you want.