CustomSymbolCreate

指定されたグループで指定された名前を持つ新しいカスタム銘柄を作成します。

bool  CustomSymbolCreate(
  const string     symbol_name,         // カスタム銘柄名
  const string    symbol_path="",     // 銘柄が作成されるグループ名
  const string    symbol_origin=NULL  // カスタム銘柄を作成するの基盤として使用される銘柄の名前
  );

パラメータ

symbol_name

[in]  カスタム銘柄名。銘柄が作成されたグループまたはサブグループを含んではいけない。

symbol_path=""

[in]  銘柄を持つグループの名前。

symbol_origin=NULL

[in]  作成するカスタム銘柄のプロパティのコピー元となる銘柄の名前。カスタム銘柄を作成した後は、適切な関数を使用して任意のプロパティ値を必要なものに変更できます。 <分節146763>

戻り値

成功の場合はtrue、それ以外の場合はfalse。エラー情報を取得するには、GetLastError() 関数が呼ばれます。

注意事項

すべてのカスタム銘柄は特別なCustomセクションで作成されます。グループ名が指定されていない場合(CustomSymbolCreate関数のsymbol_path パラメータが空の文字列またはNULL)、カスタム銘柄はCustomセクションのルートに生成されます。ここでは、グループとサブグループをフォルダーとサブフォルダーとして表示できるファイルシステムとの類似性を引き出すことができます。

銘柄名とグループ名には、句読点、スペース、特殊文字を含まないラテン文字のみを含めることができます("."、 "_"、 "&"、"#"のみを含めることができます)。"<"、">"、":"、" ""、"/"、"|"、"?"、"*"の使用は推奨されません。

カスタム銘柄名は、それが作成されたグループの名前に関係なく一意である必要があります。同名の銘柄が既に存在する場合、CustomSymbolCreate()関数は'falseを返し、それ以降の GetLastError()の呼び出しはエラー5300(ERR_NOT_CUSTOM_SYMBOL)または5304(ERR_CUSTOM_SYMBOL_EXIST)を返します。

銘柄名は 31字を超えられません。さもないと、CustomSymbolCreate()は'false'を返し、エラー5302 – ERR_CUSTOM_SYMBOL_NAME_LONGがアクティブになります。

symbol_pathパラメータを設定する方法は2つあります。

  • カスタム銘柄名を含まないグループ名(例:"CFD\\Metals")。エラーを避けるにはこのオプションが最適です。
  • または<グループ>名 + グループ区切り文字"\\"+<カスタム銘柄名>(例: "CFD\\Metals\\Platinum")。この場合、グループ名は正確にカスタム銘柄名で終わる必要があります。不一致の場合、カスタム銘柄は作成されますが、意図したグループ内には作成されません。例として、symbol_path="CFD\\Metals\\Platinum"でsymbol_name="platinum" (register error)の場合、カスタム銘柄"platinum"は"Custom\CFD\Metals\Platinum"グループに作成されます。SymbolInfoGetString("platinum",SYMBOL_PATH)関数は値"Custom\CFD\Metals\Platinum\platinum"を返します。

 

SYMBOL_PATHプロパティは銘柄名で終わるパスを返すことにご注意ください。したがって、同じグループ内にカスタム銘柄を作成する場合は、変更を加えずにコピーすることはできません。この場合、上記の結果が得られないように、銘柄名を切り捨てる必要があります。

存在しない銘柄がsymbol_originパラメータとして設定されると、カスタム銘柄はsymbol_originパラメータが設定されていない場合と同じく、空で作成されます。この場合、エラー4301 – ERR_MARKET_UNKNOWN_SYMBOLがアクティブになります。

"Custom\\"、"\\"グループ区切り文字と銘柄名が最後に指定されている場合、symbol_pathパラメータは127文字を超えることはできません。

 

Example:

//+------------------------------------------------------------------+
//|                                           CustomSymbolCreate.mq5 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link     "https://www.mql5.com"
#property version   "1.00"
 
#define   CUSTOM_SYMBOL_NAME     Symbol()+".C" // custom symbol name
#define   CUSTOM_SYMBOL_PATH     "Forex"       // name of the group, in which a symbol is to be created
#define   CUSTOM_SYMBOL_ORIGIN   Symbol()       // name of a symbol a custom one is to be based on
 
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
 {
//--- if failed to create a custom symbol, inform of that in the journal
  if(!CustomSymbolCreate(CUSTOM_SYMBOL_NAME, CUSTOM_SYMBOL_PATH, CUSTOM_SYMBOL_ORIGIN))
    {
    Print("CustomSymbolCreate() failed. Error ", GetLastError());
    return;
    }
 
//--- check the existence of the created symbol and get the group it was created in
  bool   custom= false;
  bool   exist = SymbolExist(CUSTOM_SYMBOL_NAME, custom);
  string path  = SymbolInfoString(CUSTOM_SYMBOL_NAME, SYMBOL_PATH);
 
//--- print the result of creating the symbol and the name of the group (specified and obtained from the symbol properties) in the journal
  PrintFormat("Custom symbol '%s' created\n"+
              "Symbol '%s' is exist: %s\n"+
              "Symbol '%s' is custom: %s\n"+
              "Path specified in the settings: '%s'\n"+
              "Path returned from the 'SYMBOL_PATH' property: '%s'",
              CUSTOM_SYMBOL_NAME,
              CUSTOM_SYMBOL_NAME, (string)exist,
              CUSTOM_SYMBOL_NAME, (string)custom,
              CUSTOM_SYMBOL_PATH,
              path);
 
//--- wait two seconds and delete the created symbol with the resulting message in the journal
  Sleep(2000);
  ResetLastError();
  bool deleted = CustomSymbolDelete(CUSTOM_SYMBOL_NAME);
  Print(deleted ? StringFormat("Custom symbol '%s' removed", CUSTOM_SYMBOL_NAME) : StringFormat("CustomSymbolDelete() failed. Error ",GetLastError()));
  /*
  result:
  Custom symbol 'EURUSD.C' created
  Symbol 'EURUSD.C' is exist: true
  Symbol 'EURUSD.C' is custom: true
  Path specified in the settings: 'Forex'
  Path returned from the 'SYMBOL_PATH' property: 'Custom\Forex\EURUSD.C'
  Custom symbol 'EURUSD.C' removed
  */
 }

 

参照

SymbolNameSymbolSelectCustomSymbolDelete