CustomSymbolSetMarginRate
CustomSymbolSetMarginRate
Sets the margin rates depending on the order type and direction for a custom symbol.
bool CustomSymbolSetMarginRate(
const string symbol_name, // symbol name
ENUM_ORDER_TYPE order_type, // order type
double initial_margin_rate, // initial margin rate
double maintenance_margin_rate // maintenance margin rate
);Parameters
- symbol_name
[in] Custom symbol name.
- order_type
[in] Order type.
- initial_margin_rate
[in] A double type variable with an initial margin rate. Initial margin is a security deposit for 1 lot deal in the appropriate direction. Multiplying the rate by the initial margin, we receive the amount of funds to be reserved on the account when placing an order of the specified type.
- maintenance_margin_rate
[in] A double type variable with a maintenance margin rate. Maintenance margin is a minimum amount for maintaining an open position of 1 lot in the appropriate direction. Multiplying the rate by the maintenance margin, we receive the amount of funds to be reserved on the account after an order of the specified type is activated.
Return Value
true – success, otherwise – false. To get information about the error, call the GetLastError() function.
Example:
//+------------------------------------------------------------------+
//| CustomSymbolSetMarginRate.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
#define INITIAL_MARGIN_RATE 1.5 // initial margin rate
#define MAINTENANCE_MARGIN_RATE 1.5 // maintenance margin rate
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//--- get the error code when creating a custom symbol
int create=CreateCustomSymbol(CUSTOM_SYMBOL_NAME, CUSTOM_SYMBOL_PATH, CUSTOM_SYMBOL_ORIGIN);
//--- if the error code is not 0 (successful symbol creation) and not 5304 (symbol has already been created) - leave
if(create!=0 && create!=5304)
return;
//--- get and print in the journal the properties of the symbol the custom one is based on
//--- (initial and maintenance margin rates for Buy and Sell orders)
double initial_margin_rate_buy=0;
double maintenance_margin_rate_buy=0;
double initial_margin_rate_sell=0;
double maintenance_margin_rate_sell=0;
if(!GetSymbolMarginRate(CUSTOM_SYMBOL_ORIGIN, ORDER_TYPE_BUY, initial_margin_rate_buy, maintenance_margin_rate_buy))
return;
if(!GetSymbolMarginRate(CUSTOM_SYMBOL_ORIGIN, ORDER_TYPE_SELL,initial_margin_rate_sell,maintenance_margin_rate_sell))
return;
PrintFormat("The '%s' symbol from which the custom '%s' was created:\n"+
" Buy order initial margin rate: %f\n Buy order maintenance margin rate: %f\n"+
" Sell order initial margin rate: %f\n Sell order maintenance margin rate: %f",
CUSTOM_SYMBOL_ORIGIN, CUSTOM_SYMBOL_NAME,
initial_margin_rate_buy, maintenance_margin_rate_buy, initial_margin_rate_sell, maintenance_margin_rate_sell);
//--- set other values for the custom symbol properties
ResetLastError();
bool res=true;
res &=CustomSymbolSetMarginRate(CUSTOM_SYMBOL_NAME, ORDER_TYPE_BUY, INITIAL_MARGIN_RATE, MAINTENANCE_MARGIN_RATE);
res &=CustomSymbolSetMarginRate(CUSTOM_SYMBOL_NAME, ORDER_TYPE_SELL,INITIAL_MARGIN_RATE, MAINTENANCE_MARGIN_RATE);
//--- if there was an error when setting any of the properties, display an appropriate message in the journal
if(!res)
Print("CustomSymbolSetMarginRate() failed. Error ", GetLastError());
//--- get and print in the journal the modified custom symbol properties
//--- (initial and maintenance margin rates for Buy and Sell orders)
if(!GetSymbolMarginRate(CUSTOM_SYMBOL_NAME, ORDER_TYPE_BUY, initial_margin_rate_buy, maintenance_margin_rate_buy))
return;
if(!GetSymbolMarginRate(CUSTOM_SYMBOL_NAME, ORDER_TYPE_SELL,initial_margin_rate_sell,maintenance_margin_rate_sell))
return;
PrintFormat("Custom symbol '%s' based on '%s':\n"+
" Buy order initial margin rate: %f\n Buy order maintenance margin rate: %f\n"+
" Sell order initial margin rate: %f\n Sell order maintenance margin rate: %f",
CUSTOM_SYMBOL_NAME, CUSTOM_SYMBOL_ORIGIN,
initial_margin_rate_buy, maintenance_margin_rate_buy, initial_margin_rate_sell, maintenance_margin_rate_sell);
//--- display a hint about the script termination keys on the chart comment
Comment(StringFormat("Press 'Esc' to exit or 'Del' to delete the '%s' symbol and exit", CUSTOM_SYMBOL_NAME));
//--- wait for pressing the Esc or Del keys to exit in an endless loop
while(!IsStopped() && TerminalInfoInteger(TERMINAL_KEYSTATE_ESCAPE)==0)
{
Sleep(16);
//--- when pressing Del, delete the created custom symbol
if(TerminalInfoInteger(TERMINAL_KEYSTATE_DELETE)<0)
{
if(DeleteCustomSymbol(CUSTOM_SYMBOL_NAME))
PrintFormat("Custom symbol '%s' deleted successfully", CUSTOM_SYMBOL_NAME);
break;
}
}
//--- clear the chart before exiting
Comment("");
/*
result:
The 'EURUSD' symbol from which the custom 'EURUSD.C' was created:
Buy order initial margin rate: 1.000000
Buy order maintenance margin rate: 0.000000
Sell order initial margin rate: 1.000000
Sell order maintenance margin rate: 0.000000
Custom symbol 'EURUSD.C' based on 'EURUSD':
Buy order initial margin rate: 1.500000
Buy order maintenance margin rate: 1.500000
Sell order initial margin rate: 1.500000
Sell order maintenance margin rate: 1.500000
*/
}
//+------------------------------------------------------------------+
//| Create a custom symbol, return an error code |
//+------------------------------------------------------------------+
int CreateCustomSymbol(const string symbol_name, const string symbol_path, const string symbol_origin=NULL)
{
//--- define the name of a symbol a custom one is to be based on
string origin=(symbol_origin==NULL ? Symbol() : symbol_origin);
//--- if failed to create a custom symbol and this is not error 5304, report this in the journal
ResetLastError();
int error=0;
if(!CustomSymbolCreate(symbol_name, symbol_path, origin))
{
error=GetLastError();
if(error!=5304)
PrintFormat("CustomSymbolCreate(%s, %s, %s) failed. Error %d", symbol_name, symbol_path, origin, error);
}
//--- successful
return(error);
}
//+------------------------------------------------------------------+
//| Remove a custom symbol |
//+------------------------------------------------------------------+
bool DeleteCustomSymbol(const string symbol_name)
{
//--- hide the symbol from the Market Watch window
ResetLastError();
if(!SymbolSelect(symbol_name, false))
{
PrintFormat("SymbolSelect(%s, false) failed. Error %d", GetLastError());
return(false);
}
//--- if failed to delete a custom symbol, report this in the journal and return 'false'
ResetLastError();
if(!CustomSymbolDelete(symbol_name))
{
PrintFormat("CustomSymbolDelete(%s) failed. Error %d", symbol_name, GetLastError());
return(false);
}
//--- successful
return(true);
}
//+------------------------------------------------------------------+
//| Return margin ratios |
//+------------------------------------------------------------------+
bool GetSymbolMarginRate(const string symbol, const ENUM_ORDER_TYPE order_type, double &initial_margin_rate, double &maintenance_margin_rate)
{
ResetLastError();
if(!SymbolInfoMarginRate(symbol, order_type, initial_margin_rate, maintenance_margin_rate))
{
PrintFormat("%s: SymbolInfoMarginRate(%s, %s) failed. Error %d",__FUNCTION__, symbol, EnumToString(order_type), GetLastError());
return false;
}
return true;
}