Maximum length of a symbol name on FOREX - page 4

 

Now, you can also use it on FOREX (takes any "crooked" characters, even such as _BTCUSD_i )

//+------------------------------------------------------------------+
//|                                                    AutoMagic.mqh |
//|                                 Copyright 2017-2018 prostotrader |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
//version   "1.01
//#define FORTS
//---
struct SYMBOL_MAGIC
{
  long ch_id;
  ulong magic;
};
SYMBOL_MAGIC symb_magic;
//-------------------------------------------------------------------+
// Split string function                                             |
//+------------------------------------------------------------------+
string SplitString(const string a_str,ulong &a_month,ulong &a_year)
  {
   int str_size=StringLen(a_str);
   int str_tire=StringFind(a_str, "-");
   int str_tochka=StringFind(a_str, ".", str_tire);
   if((str_tire>0) && (str_tochka>0) &&(str_size > 0))
     {
      a_month= ulong(StringToInteger(StringSubstr(a_str,str_tire+1,str_tochka-str_tire-1)));
      a_year = ulong(StringToInteger(StringSubstr(a_str,str_tochka+1,str_size-str_tochka-1)));
      if((a_month > 0) && (a_year > 0)) return(StringSubstr(a_str, 0, str_tire));
     }
   return("");
  }
//-------------------------------------------------------------------+
// Get FOREX symbol function                                       |
//+------------------------------------------------------------------+
string GetForexName(const string a_str)
{
  string s_base = SymbolInfoString(Symbol(), SYMBOL_CURRENCY_BASE);
  if(StringLen(s_base) == 3)
  {
    int base_pos = StringFind(a_str, s_base, 0);
    if(base_pos > -1)
    {
      switch(base_pos)
      {
        case 0:
        case 1:
        case 2:
          return(StringSubstr(a_str, base_pos, 6));
        break;
        default:
          {
            string left_str = StringSubstr(a_str, base_pos - 3, 3);
            string right_str = StringSubstr(a_str, base_pos + 3, 3);
            if(right_str == "")
            {
              return(StringSubstr(a_str, base_pos - 3, 6));
            }
            else
            if(StringLen(right_str) < 3)
            {
              return(StringSubstr(a_str, base_pos - 3, 6));
            }
            else
            {
              bool is_match = true;
              uchar uch_array[];
              int result = StringToCharArray(right_str, uch_array, 0, WHOLE_ARRAY, CP_ACP);
              if(result == 4)
              {
                for(int i=0; i < result - 1; i++)
                {
                  if((uch_array[i] < 65) || (uch_array[i] > 90))
                  {
                    is_match = false;
                    break;
                  } 
                }
                if(is_match == true)
                {
                  return(StringSubstr(a_str, base_pos, 6));  
                }
                else
                {
                  is_match = true;
                  result = StringToCharArray(left_str, uch_array, 0, WHOLE_ARRAY, CP_ACP);
                  if(result == 4)
                  {
                    for(int i=0; i < result - 1; i++)
                    {
                      if((uch_array[i] < 65) || (uch_array[i] > 90))
                      {
                        is_match = false;
                        break;
                      } 
                    }
                    if(is_match == true)
                    {
                      return(StringSubstr(a_str, base_pos - 3, 6));  
                    }  
                  } 
                }
              }  
            }
          }
        break;  
      }
    }
  }
  return("");
}  
//-------------------------------------------------------------------+
// Get Magic function                                                |
//+------------------------------------------------------------------+
ulong GetMagic(const string a_symbol)
{
//--- Get ChartID
  symb_magic.ch_id = ChartID();
//---
  if(SymbolSelect(Symbol(), true) == false)
  {
    Print(__FUNCTION__, ": Нет такого символа!");
    return(0);
  }  
#ifdef  FORTS
//--- Test symdol
  if(StringLen(a_symbol)>10)
  {
    Print(__FUNCTION__, ": Не правильный символ!");
    return(0);
  }
  if(symb_magic.ch_id != 0)
  {
    ulong month = 0;
    ulong year = 0;
    string new_str=SplitString(a_symbol,month,year);
    if(StringLen(new_str)>0)
    {
      symb_magic.magic = 0;
      uchar char_array[];
      int result=StringToCharArray(new_str,char_array,0,WHOLE_ARRAY,CP_ACP);
      if(result>0)
      {
        ulong value;
        for(int i = 0; i < result - 1; i++)
        {
          value=ulong(char_array[i]);
          value<<=(56 -(i*8));
          symb_magic.magic += value;
        }
        month<<=24;
        symb_magic.magic += month;
        year<<=16;
        symb_magic.magic += year;
        ulong a_chid = ulong(symb_magic.ch_id);
        a_chid<<=16;
        return(symb_magic.magic&=symb_magic.ch_id);
      }
    }
  }
#else
  string in_str = GetForexName(a_symbol);
  if(in_str != "")
  { 
    symb_magic.magic = 0;
    uchar char_array[];
    int result=StringToCharArray(a_symbol,char_array,0,WHOLE_ARRAY,CP_ACP);
    if(result>0)
    {
      ulong value;
      for(int i = 0; i < 6; i++)
      {
        value=ulong(char_array[i]);
        value<<=(56 -(i*8));
        symb_magic.magic += value;
      }  
      ulong a_chid = ulong(symb_magic.ch_id);
      a_chid<<=16;
      return(symb_magic.magic&=symb_magic.ch_id);
    }
  }
#endif    
  return(0); 
}
//-------------------------------------------------------------------+
// Is my magic function                                              |
//+------------------------------------------------------------------+
bool IsMyMagic(const ulong m_magic, const long chart_id)
{
  if(symb_magic.ch_id == chart_id)
  {
    ulong stored_magic=symb_magic.magic;
    stored_magic>>=16;
    ulong in_magic = m_magic;
    in_magic>>=16;
    if(in_magic == stored_magic) return(true);
  }
  return(false);
}
//+------------------------------------------------------------------+
 
prostotrader:

Now, you can use it on FOREX (accepts any "crooked" characters, even such as _BTCUSD_i)

To the kodobase

 
prostotrader:

A long time ago I wrote a plug-in file to automatically retrieve Magic

What was the task?

Please check if message "FOREX done." appears on "crooked" symbols

It does not appear on "EURUSD".

Is the base symbol always the first in the pair?

Custom symbols can have anything. Probably the most convenient way to run such checks on custom ones.


SZ

Forum on trading, automated trading systems and strategy testing

Libraries: AutoMagic

Dmitry Fedoseev, 2017.02.03 13:31

Using a chart handle to form a magik means that if the chart is closed accidentally, the orders will be lost.

 
fxsaber:

Doesn't appear on EURUSD.

SZY

Corrected

Added to ZY

Write something better

 
prostotrader:

Corrected

It's working now. Now would like to hear a few words about the practical side of things.


ZS "AUDNZD"...

 
fxsaber:

It's working now. Now would like to hear a few words about the practical side of things.


ZS "AUDNZD"...

Automatically creates a unique Magik for the EA on the chart with the ability to use 65535 additional Magiks

Added

I do not have FOREX.

I wrote theGetForexName functionwithout checking

 
prostotrader:

Automatically creates a unique Magik for the EA on the chart with the ability to use 65535 additional Magiks

So what if the EA is cut off by closing the chart at a time when there are its positions/orders?


Not quite sure why something like this didn't work for Forex

bool SymbolIsExist( const string Symb = NULL )
{
  ResetLastError();

  SymbolInfoInteger((Symb == NULL) ? _Symbol : Symb, SYMBOL_CUSTOM);

  return(::GetLastError() != ERR_MARKET_UNKNOWN_SYMBOL);
}

string GetForexName( const string Symb )
{
  return(SymbolIsExist(Symb) ? SymbolInfoString(Symb, SYMBOL_CURRENCY_BASE) + SymbolInfoString(Symb, SYMBOL_CURRENCY_PROFIT) : NULL);
}

void OnStart()
{
  Print(GetForexName(_Symbol));
}
 
prostotrader:

I don't have FOREX.

Do - MQ-Demo.

 
fxsaber:

So what if the EA is cut off by closing the chart at a time when there is a position/order?


If there is a position, then all data can be retrieved when the chart is closed, and the pending orders on this symbol can simply be "nailed".

No one has come up with a better idea yet (for opening several EAs on the same symbol)

 
fxsaber:

ZS "AUDNZD"...

AUDNZD works on Roboforex

Reason: