Script to Close Orders based on selection of Symbol(s)

 
Hi Everyone,

I'm new to fxtrading. Started a little over 3 months and have got quite a good understanding of how it works. Also been looking at a few scripts and EAs.

Ok, now I have a good understanding about scripting since I've done C, C++ and Visual Basics, but about 6-7yrs back. I can understand what the code does if I look at the script but if I had to write it, would take ages.

Anyways, my problem is what the subject says exactly. Is it even possible to do it as a script. I downloaded a script from forexfactory, and modified it to work as a script to close only 1 symbol from any chart. But I trade in about 12 pairs, and I can't make an individual script for each pair and execute them seperately.

I was thinking if it was possible to have an array and store the symbols in it and the do a case() to get a selection which pairs to close.

Please guys if anyone could help me.. Really appreciate it.

I'm attaching two scripts;

1. This is the original one I downloaded...
//+------------------------------------------------------------------+
//|                                          close_all_positions.mq4 |
//|                                                         Zen Leow |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Zen Leow"
#property link      ""
#property show_confirm
#include <stdlib.mqh>
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
{
//----
   int total = OrdersTotal()-1;
   int positionType;
   if (total > 0)
   {
      for(int cnt=total;cnt>=0;cnt--)
      {
         if(OrderSelect(cnt,SELECT_BY_POS))
         {
            positionType = OrderType();
            if (positionType == OP_BUY)
            {
               if (!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3))
               {
                  OrderError();
               }
            }
            if (positionType == OP_SELL)
            {
               if (!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3))
               {
                  OrderError();
               }
            }
            if (positionType != OP_SELL && positionType != OP_BUY) // pending orders
            {
               if (!OrderDelete(OrderTicket()))
               {
                  OrderError();
               }
            }
         }
      }
   }
//----
   return(0);
}
void OrderError() {
  int iError=GetLastError();
  Print("Order:",OrderTicket()," GetLastError()=",iError," ",ErrorDescription(iError));
}
//+------------------------------------------------------------------+

2. The same script but modified to close one single pair....

//+------------------------------------------------------------------+
//|                                          close_all_positions.mq4 |
//|                                                         Zen Leow |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Zen Leow"
#property link      ""
#property show_confirm
#include <stdlib.mqh>
extern double AUDUSD;
string Symbols = "AUDUSD";
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+

int start()
{
//----
   int total = OrdersTotal()-1;
   int positionType;
   if (total > 0)
   {
      for(int cnt=total;cnt>=0;cnt--)
      {
         if(OrderSelect(cnt,SELECT_BY_POS))
         {
            positionType = OrderType();
            if (positionType == OP_BUY)
            {
               if (!OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symbols,MODE_BID),3))
               {
                  OrderError();
               }
            }
            if (positionType == OP_SELL)
            {
               if (!OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symbols,MODE_ASK),3))
               {
                  OrderError();
               }
            }
            if (positionType != OP_SELL && positionType != OP_BUY) // pending orders
            {
               if (!OrderDelete(OrderTicket()))
               {
                  OrderError();
               }
            }
         }
      }
   }
//----
   return(0);
}
void OrderError() {
  int iError=GetLastError();
  Print("Order:",OrderTicket()," GetLastError()=",iError," ",ErrorDescription(iError));
}
//+------------------------------------------------------------------+

Thanks & regards
Ecronic
 
You need to add something like this in your loop:
      for(int cnt=total;cnt>=0;cnt--)
      {
         if(OrderSelect(cnt,SELECT_BY_POS))
         {
            if(OrderSymbol()!=symbol) continue;   // symbol is what we want to close/delete

            // ...
Regarding user input -> u can have them enter a comma separated list of pairs to close (via an extern of type string). Use a split function to break the string and verify the pairs are proper...
 

And replace
if (total > 0)
to
if (total >= 0)

Reason: