Close order by sequence

İş tamamlandı

Tamamlanma süresi: 3 gün
Müşteri tarafından geri bildirim
Super genius and super fast
Geliştirici tarafından geri bildirim
5 star client! Happy to have worked with Naresh.

İş Gereklilikleri

I need to modify my script to create a loop to check the buy and sell orders profit separately. close when 1st buy order and last buy order's average is profit. 

Currently, my code closes all the buy/sell order's  average is in profit. I would like to modify that for buy and sell orders. 

//+------------------------------------------------------------------+
//|                                               CloseOnAverage.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

extern int Choose_stragegy=1; // 1-hedging 2-trend
input int             InpMaxTrades = 10;         // Max number of trades
input double          InpTradeGap = 0.005;       // Distance between trades
//input ENUM_ORDER_TYPE InpType = ORDER_TYPE_BUY;  // Order type;//
input double          InpMinProfit = 1.00;       // Profit point
input int             InpMagicNumber = 1111;     // Magic number
input double          Multiplier = 2;       
input string          InpTradeComment = __FILE__; // Trade comment
input double          InpVolume = 0.01;          // Volume per order
double pips;

//double multi= (InpVolume*Multiplier);

struct STradeSum {
   int buycount;
   int sellcount;
   double buyprofit;
   double sellprofit;
   double buytrailPrice;
   double selltrailPrice;
};

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+

void OnTick() 
{
 
   STradeSum  sum;
   GetSum(sum);
    
   if (sum.buyprofit>InpMinProfit) 
   { // target reached
      CloseAll(ORDER_TYPE_BUY);
   } 
   if (OrdersTotal()>=3)
   {
      drawdownreduction();
   }
   
   else if (sum.sellprofit>InpMinProfit) 
   { // target reached
      CloseAll(ORDER_TYPE_SELL);
   }
   
   else if (sum.buycount==0) 
   {  // no buy trades
      OpenTrade(ORDER_TYPE_BUY);
   } 
   
   else if (sum.sellcount==0)
   {  // no sell trades
      OpenTrade(ORDER_TYPE_SELL);
   } 
   
   else if ((sum.buycount<InpMaxTrades))
   {
      
      if ( OrderType()==ORDER_TYPE_BUY && SymbolInfoDouble(Symbol(), SYMBOL_ASK)<=(sum.buytrailPrice-InpTradeGap)) 
      {   // Far enough below
         OpenmultiTrade(ORDER_TYPE_BUY);
      } 
      else if ((sum.sellcount<InpMaxTrades))
      {
         if ( OrderType() ==ORDER_TYPE_SELL && SymbolInfoDouble(Symbol(), SYMBOL_BID)>=(sum.selltrailPrice+InpTradeGap)) 
            {   // Far enough above
              OpenmultiTrade(ORDER_TYPE_SELL);
             }
      }
    }
    
   //else  if (sum.sellcount<InpMaxTrades)     
}

//+------------------------------------------------------------------+

void  OpenTrade(ENUM_ORDER_TYPE InpType) {
 
   double   price =  (InpType==ORDER_TYPE_BUY) ?
                        SymbolInfoDouble(Symbol(), SYMBOL_ASK) :
                        SymbolInfoDouble(Symbol(), SYMBOL_BID);
   OrderSend(Symbol(), InpType, InpVolume, price, 0, 0, 0, InpTradeComment, InpMagicNumber);
    
}

//+------------------------------------------------------------------+

void  OpenmultiTrade(ENUM_ORDER_TYPE InppType) {
 
   double   price =  (InppType==ORDER_TYPE_BUY) ?
                        SymbolInfoDouble(Symbol(), SYMBOL_ASK) :
                        SymbolInfoDouble(Symbol(), SYMBOL_BID);
   double multi = (InpVolume*Multiplier);
   OrderSend(Symbol(), InppType, multi, price, 0, 0, 0, InpTradeComment, InpMagicNumber);
}

//+------------------------------------------------------------------+

void  CloseAll(ENUM_ORDER_TYPE orderType) {
 
   int   count    =  OrdersTotal();
 
   for (int i = count-1; i>=0; i--) {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
         if (  OrderSymbol()==Symbol()
               && OrderMagicNumber()==InpMagicNumber
               && OrderType()==orderType
               ) {
            OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0);
         }
      }
   }
 
}
//+------------------------------------------------------------------+

void  GetSum(STradeSum &sum) {
 
   sum.buycount      =  0;
   sum.buyprofit     =  0.0;
   sum.buytrailPrice =  0.0;
   sum.sellcount      =  0;
   sum.sellprofit     =  0.0;
   sum.selltrailPrice =  0.0;
    
   int   count    =  OrdersTotal();
 
   for (int i = count-1; i>=0; i--) 
   {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) 
      {
      //Buy orders
         if (OrderSymbol()==Symbol() && OrderMagicNumber()==InpMagicNumber && OrderType()== ORDER_TYPE_BUY)
                {
                  sum.buycount++;
                  sum.buyprofit  += OrderProfit()+OrderSwap()+OrderCommission();
                  if (sum.buytrailPrice==0 || OrderOpenPrice()<sum.buytrailPrice)
                     {
                        sum.buytrailPrice =  OrderOpenPrice();
                     }
                 }
          //sellorders
          if (OrderSymbol()==Symbol()&& OrderMagicNumber()==InpMagicNumber && OrderType()==ORDER_TYPE_SELL)
                { 
                  sum.sellcount++;
                  sum.sellprofit  += OrderProfit()+OrderSwap()+OrderCommission();
                  if (sum.selltrailPrice==0 || OrderOpenPrice()>sum.selltrailPrice)
                     {
                        sum.selltrailPrice =  OrderOpenPrice();
                     }
                }
            
        } 
     }
          
   return;
    
}

//+------------------------------------------------------------------+
//found this code online. The below code compares and closes the last two orders
void drawdownreduction()
{
  int slippage=10;
  double minimum_profit=InpMinProfit;
  int x;
  double trades[][4];
  int total=OrdersTotal();
  if(total>1)
  {
  ArrayResize(trades,total);
  for(x=total-1;x>=0;x--)
     {
     if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES))
        {
        trades[x][0]=OrderTicket();
        trades[x][1]=OrderProfit()+OrderCommission()+OrderSwap();
        trades[x][2]=OrderLots();
        trades[x][3]=OrderType();
        }
     }
  
  ArraySort(trades,WHOLE_ARRAY,0,MODE_ASCEND);
  x=0;
  
  while(x<total-1)
     {
     double profit=trades[x][1]+trades[total-1][1];
     if(profit>=minimum_profit)
        {
        RefreshRates();
        double close_price=Ask;
        if(trades[x][3]==OP_BUY)
           close_price=Bid;
        if(!OrderClose((int)trades[x][0],trades[x][2],close_price,slippage,clrNONE))
           Print("Error closing #",DoubleToStr(trades[x][0],0)," Error code ",GetLastError());
        RefreshRates();
        close_price=Ask;
        if(trades[x+1][3]==OP_BUY)
           close_price=Bid;
        if(!OrderClose((int)trades[x+1][0],trades[x+1][2],close_price,slippage,clrNONE))
           Print("Error closing #",DoubleToStr(trades[x][0],0)," Error code ",GetLastError());
        }
        x+=2;
     }
     }
 }   
  


Yanıtlandı

1
Geliştirici 1
Derecelendirme
(21)
Projeler
23
0%
Arabuluculuk
6
17% / 83%
Süresi dolmuş
2
9%
Serbest
2
Geliştirici 2
Derecelendirme
(49)
Projeler
62
40%
Arabuluculuk
1
0% / 100%
Süresi dolmuş
7
11%
Çalışıyor
3
Geliştirici 3
Derecelendirme
(181)
Projeler
242
45%
Arabuluculuk
18
78% / 17%
Süresi dolmuş
35
14%
Çalışıyor
4
Geliştirici 4
Derecelendirme
(139)
Projeler
181
24%
Arabuluculuk
23
22% / 39%
Süresi dolmuş
13
7%
Serbest
5
Geliştirici 5
Derecelendirme
(93)
Projeler
133
35%
Arabuluculuk
13
38% / 31%
Süresi dolmuş
32
24%
Serbest
6
Geliştirici 6
Derecelendirme
(1)
Projeler
1
0%
Arabuluculuk
2
0% / 100%
Süresi dolmuş
0
Serbest
7
Geliştirici 7
Derecelendirme
(564)
Projeler
777
46%
Arabuluculuk
23
39% / 13%
Süresi dolmuş
63
8%
Çalışıyor
8
Geliştirici 8
Derecelendirme
(24)
Projeler
28
32%
Arabuluculuk
1
0% / 0%
Süresi dolmuş
0
Serbest
9
Geliştirici 9
Derecelendirme
(155)
Projeler
239
70%
Arabuluculuk
3
67% / 33%
Süresi dolmuş
20
8%
Serbest
Benzer siparişler
I want grate robot for making profits that know when to start a good trade and close a trade and must be active all time to avoid lost of money
I have developed a very strong TradingView strategy in Pine Script but unfortunately, a third-party connector is requiired and in my opinion, I want a more direct connection. I am not brilliant at coding, but I have coded the majority of the MT5 code and I would like you to make sure that the MT5 code matches my TradingView script and executes the same way as the TradingView script that I will provide if you are
Mbeje fx 50+ USD
I like to own my robot that why I want to build my own.i like to be a best to every robot ever in the life to be have more money
I need an MT5 EA that can do the following: I have to give the EA a price in advance, when the price is reached the EA has to automatically place a buy stop or sell stop order 0.5 pips below or above the price. Is this possible
Dr Pattern 30+ USD
good day i need the service of the seaso coder to help me fix my ea The Job required 1 knowledge of Mt4 and Mt5 indicator coding 2. Telegram code 3. ability to code indicator to work on multiple Time frame combine to trade 4 Ability to Join two or three indicator on same ir different time frame if you have these skill please let chart i will discuss the details of the Job inside to you The required day including
Good day, I want someone to help me create a universal news filter with on/off switch, with start and end settings, and drawdown control with magic number of EAs, etc. Thanks
Hello, I am looking for a professional programmer to optimize my existing EA integrating it with ChatGPT to analyze currencies using various methods to make the right trading decisions. i want it to be an EA that can be trusted to carry trade with the help of chat gpt and also have a very low drawdown
Hello, I am looking for a professional programmer to create a trading expert on the MT4 platform, integrating it with ChatGPT to analyze currencies using various methods to make the right trading decisions. Further details will be provided to the applicants later
I have developed a very strong TradingView strategy in Pine Script but unfortunately, a third-party connector is requiired and in my opinion, I want a more direct connection. I am not brilliant at coding, but I have coded the majority of the MT5 code and I would like you to make sure that the MT5 code matches my TradingView script and executes the same way as the TradingView script that I will provide if you are
EA based on RSI and MA 100 - 400 USD
Program is based on RSI and MA indicators dynamic as triggers, for Open/Close criteria. Runs automatically but inputs can be updated manually. It uses a GUI to manage it. Multi TF analysis. Log register of every operation for analysis (db) Open Source deliver. Kindly apply IF you have previous experience with trading and mql/python/c bot/algo developing. And if you have a good track record . ps: Better if you have a

Proje bilgisi

Bütçe
30+ USD
Geliştirici için
27 USD
Son teslim tarihi
to 2 gün