Add custom symbol synthetic formula programmatically

 

Formula USDX Index = 50.14348112*pow(ask(EURUSD),-0.576)*pow(USDJPY,0.136)*pow(ask(GBPUSD),-0.119)*pow(USDCAD,0.091)*pow(USDSEK,0.042)*pow(USDCHF,0.036)

PROBLEM: is that the synthetic instrument formula is empty therefore no quotes are available. The custom symbol and the existing symbols quotes needed are available , visible and live on Market Watch . What am i missing  in order to have  the quotes for the custom symbol  in real-time. 

NOTE: When i create the custom symbol manually and add the  custom symbol synthetic formula manually it works fine and no problem at all.

//USDX_Index
input string SName1="USDX";
input string CurrencyName1="DXY";
input string CurrencyFullName1="DXY Index";
input string BaseName1="EURUSD";
string usdxFormula = "50.14348112*pow(ask(EURUSD),-0.576)*pow(USDJPY,0.136)*pow(ask(GBPUSD),-0.119)*pow(USDCAD,0.091)*pow(USDSEK,0.042)*pow(USDCHF,0.036)";

int OnInit()
  {
//--- Initializing expert
   if(!ExtExpert.Init(Symbol(),Period(),Expert_EveryTick,Expert_MagicNumber))
     {
      //--- failed
      printf(__FUNCTION__+": error initializing expert");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }

     //Create Custom Symbols 
   //CreateSymbol();
   CreateSymbol(SName1, CurrencyName1, CurrencyFullName1, BaseName1, usdxFormula);
   
   
//--- ok
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   ExtExpert.Deinit();
  }

void OnTick()
  {
   ExtExpert.OnTick();
  }

void CreateSymbol(string SName, string CurrencyName, string CurrencyFullName, string BaseName, string SymbolFormula)
  {
   ResetLastError();
// create symbol
   if(!CustomSymbolCreate(SName,"\\Forex"))
     {
      if(SymbolInfoInteger(SName,SYMBOL_CUSTOM))
         Print("Symbol ",SName," already exists!");
      else
         Print("Error creating symbol. Error code: ",GetLastError());
     }
   else
     {
      if(BaseName=="")// create new
        { 
         
         // String type properties
         if((SetProperty(SName,SYMBOL_CURRENCY_BASE,CurrencyName,"")) && // base currency
            (SetProperty(SName,SYMBOL_CURRENCY_PROFIT,"USD",""))&&                         // profit currency
            (SetProperty(SName,SYMBOL_CURRENCY_MARGIN,"USD",""))&&                         // margin currency
            (SetProperty(SName,SYMBOL_DESCRIPTION,CurrencyName,""))&&                      // the string description of the symbol (full name)
            (SetProperty(SName,SYMBOL_BASIS,"","")) &&                                     // the name of the underlaying asset for the custom symbol
            (SetProperty(SName,SYMBOL_FORMULA,"","")) &&                        // the formula used for custom symbol pricing
            (SetProperty(SName,SYMBOL_ISIN,"","")) &&                                      // the name of a trading symbol in the ISIN system
            (SetProperty(SName,SYMBOL_PAGE,"","")) &&                                      // the web page containing symbol information
            // Integer type properties
            (SetProperty(SName,SYMBOL_CHART_MODE,SYMBOL_CHART_MODE_BID,"")) &&             // plotting charts by Bid prices
            (SetProperty(SName,SYMBOL_SPREAD,3,"")) &&                                     // spread
            (SetProperty(SName,SYMBOL_SPREAD_FLOAT,true,"")) &&                            // floating spread
            (SetProperty(SName,SYMBOL_DIGITS,5,"")) &&                                     // precision
            (SetProperty(SName,SYMBOL_TICKS_BOOKDEPTH,10,"")) &&                           // depth of the order book
            (SetProperty(SName,SYMBOL_BACKGROUND_COLOR,White,""))&&                        // the color of the background used for the symbol in Market Watch
            (SetProperty(SName,SYMBOL_TRADE_MODE,SYMBOL_TRADE_MODE_FULL,""))&&             // order execution type: full access
            (SetProperty(SName,SYMBOL_TRADE_EXEMODE,SYMBOL_TRADE_EXECUTION_INSTANT,""))&&  // deal execution mode: instant execution
            (SetProperty(SName,SYMBOL_ORDER_GTC_MODE,SYMBOL_ORDERS_GTC,""))&&              // expiration of StopLoss and TakeProfit of orders: good until canceled
            (SetProperty(SName,SYMBOL_FILLING_MODE,SYMBOL_FILLING_FOK,""))&&               // order execution mode: fill or kill
            (SetProperty(SName,SYMBOL_EXPIRATION_MODE,SYMBOL_EXPIRATION_GTC,""))&&         // order expiration mode: unlimited in time, until canceled explicitly
            (SetProperty(SName,SYMBOL_ORDER_MODE,127,"")) &&                               // order types: all order types
            (SetProperty(SName,SYMBOL_TRADE_CALC_MODE,SYMBOL_CALC_MODE_FOREX,""))&&        // method of calculating the contract value
            (SetProperty(SName,SYMBOL_MARGIN_HEDGED_USE_LEG,false,""))&&                   // calculation of the hedged margin using the larger leg
            (SetProperty(SName,SYMBOL_SWAP_MODE,SYMBOL_SWAP_MODE_POINTS,""))&&             // swap calculation model: calculation of swap in points
            (SetProperty(SName,SYMBOL_SWAP_ROLLOVER3DAYS,WEDNESDAY,"")) &&                 // triple-swap day
            (SetProperty(SName,SYMBOL_OPTION_MODE,0,"")) &&                                // option type
            (SetProperty(SName,SYMBOL_OPTION_RIGHT,0,"")) &&                               // option right
            (SetProperty(SName,SYMBOL_TRADE_STOPS_LEVEL,0,"")) &&                          // minimum distance in points from the current close price for setting Stop orders
            (SetProperty(SName,SYMBOL_TRADE_FREEZE_LEVEL,0,"")) &&                         // freeze distance for trading operations (in points)
            (SetProperty(SName,SYMBOL_START_TIME,0,"")) &&                                 // symbol trading start date (usually used for futures)
            (SetProperty(SName,SYMBOL_EXPIRATION_TIME,0,"")) &&                            // symbol trading end date (usually used for futures)
            // Double type properties
            (SetProperty(SName,SYMBOL_OPTION_STRIKE,0,"")) &&                              // option strike price
            (SetProperty(SName,SYMBOL_SESSION_PRICE_LIMIT_MAX,0,"")) &&                    // the minimum allowable price value for the session 
            (SetProperty(SName,SYMBOL_SESSION_PRICE_LIMIT_MIN,0,"")) &&                    // the maximum allowable price value for the session
            (SetProperty(SName,SYMBOL_SESSION_PRICE_SETTLEMENT,0,"")) &&                   // the settlement price of the current session
            (SetProperty(SName,SYMBOL_TRADE_ACCRUED_INTEREST,0,"")) &&                     // accrued interest (for bonds)
            (SetProperty(SName,SYMBOL_TRADE_FACE_VALUE,0,"")) &&                           // face value (for bonds)
            (SetProperty(SName,SYMBOL_TRADE_LIQUIDITY_RATE,0,"")) &&                       // liquidity rate(used for collateral symbols)
            (SetProperty(SName,SYMBOL_TRADE_TICK_SIZE,0.00001,"")) &&                      // the minimum price change
            (SetProperty(SName,SYMBOL_TRADE_TICK_VALUE,1,"")) &&                           // tick value
            (SetProperty(SName,SYMBOL_TRADE_CONTRACT_SIZE,100000,"")) &&                   // trade contract size
            (SetProperty(SName,SYMBOL_POINT,0.00001,"")) &&                                // point value
            (SetProperty(SName,SYMBOL_VOLUME_MIN,0.01,"")) &&                              // the minimum volume for deal execution
            (SetProperty(SName,SYMBOL_VOLUME_MAX,500.00,"")) &&                            // the maximum volume for deal execution
            (SetProperty(SName,SYMBOL_VOLUME_STEP,0.01,"")) &&                             // the minimum volume change step for deal execution
            (SetProperty(SName,SYMBOL_VOLUME_LIMIT,0,"")) &&                               // the maximum allowed total volume of an open position and pending orders in one direction (either buy or sell) for this symbol 
            (SetProperty(SName,SYMBOL_MARGIN_INITIAL,0,"")) &&                             // initial margin 
            (SetProperty(SName,SYMBOL_MARGIN_MAINTENANCE,0,"")) &&                         // maintenance margin 
            (SetProperty(SName,SYMBOL_MARGIN_HEDGED,100000,"")) &&                         // size of a contract or margin for one lot of oppositely directed positions for one symbol 
            (SetProperty(SName,SYMBOL_SWAP_LONG,-0.7,"")) &&                               // Long swap value
            (SetProperty(SName,SYMBOL_SWAP_SHORT,-1,"")))                                  // Short swap value
            Print("Symbol ",SName," created successfully");
         else
            Print("Error setting symbol properties  zzz1. Error code: ",GetLastError());
        }
      else// create based on the base symbol
        {
         if((SetProperty(SName,SYMBOL_CURRENCY_BASE,CurrencyName,"")) && 
            (SetProperty(SName,SYMBOL_CURRENCY_PROFIT,"USD",BaseName)) && 
            (SetProperty(SName,SYMBOL_CURRENCY_MARGIN,"USD",BaseName)) && 
            (SetProperty(SName,SYMBOL_DESCRIPTION,CurrencyFullName,"")) && 
            (SetProperty(SName,SYMBOL_BASIS,"",BaseName)) && 
            (SetProperty(SName,SYMBOL_FORMULA,SymbolFormula,BaseName)) && 
            (SetProperty(SName,SYMBOL_ISIN,"",BaseName)) && 
            (SetProperty(SName,SYMBOL_PAGE,"",BaseName)) && 

            (SetProperty(SName,SYMBOL_CHART_MODE,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_SPREAD,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_SPREAD_FLOAT,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_DIGITS,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_TICKS_BOOKDEPTH,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_BACKGROUND_COLOR,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_TRADE_MODE,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_TRADE_EXEMODE,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_ORDER_GTC_MODE,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_FILLING_MODE,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_EXPIRATION_MODE,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_ORDER_MODE,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_TRADE_CALC_MODE,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_MARGIN_HEDGED_USE_LEG,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_SWAP_MODE,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_SWAP_ROLLOVER3DAYS,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_OPTION_MODE,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_OPTION_RIGHT,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_TRADE_STOPS_LEVEL,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_TRADE_FREEZE_LEVEL,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_START_TIME,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_EXPIRATION_TIME,0,BaseName)) && 

            (SetProperty(SName,SYMBOL_OPTION_STRIKE,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_SESSION_PRICE_LIMIT_MAX,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_SESSION_PRICE_LIMIT_MIN,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_SESSION_PRICE_SETTLEMENT,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_TRADE_ACCRUED_INTEREST,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_POINT,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_TRADE_CONTRACT_SIZE,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_TRADE_FACE_VALUE,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_TRADE_LIQUIDITY_RATE,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_TRADE_TICK_SIZE,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_TRADE_TICK_VALUE,0,BaseName)) && 
            //(SetProperty(SName,SYMBOL_VOLUME_MIN,0,BaseName)) &&  //Or this error 34
            (SetProperty(SName,SYMBOL_VOLUME_MAX,0,BaseName)) && 
            //(SetProperty(SName,SYMBOL_VOLUME_STEP,0,BaseName)) &&  //Error 34
            (SetProperty(SName,SYMBOL_VOLUME_LIMIT,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_MARGIN_INITIAL,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_MARGIN_MAINTENANCE,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_MARGIN_HEDGED,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_SWAP_LONG,0,BaseName)) && 
            (SetProperty(SName,SYMBOL_SWAP_SHORT,0,BaseName)))
            Print("Symbol ",SName," created successfully");
         else
            Print("Error setting symbol properties zzz2. Error code: ",GetLastError());
        }
      if(SymbolSelect(SName,true))
         Print("Symbol ",SName," selected in Market Watch");
      else
         Print("Error selecting symbol in Market Watch. Error code: ",GetLastError());
     }
  }
  
  
// function for setting the symbol properties
bool SetProperty(string SymName,ENUM_SYMBOL_INFO_STRING SProp,string PropValue,string BaseSymName)
  {
   ResetLastError();
   //Print("SProp1 = ", SProp, " PropValue = " , PropValue);
   
   if(BaseSymName=="")
     {
      if(CustomSymbolSetString(SymName,SProp,PropValue))
        { 
         return true;}
      else
         Print("Error setting symbol property: ",SProp,". Error code: ",GetLastError());
     }
   else
     {
      string SValue=SymbolInfoString(BaseSymName,SProp);
      //Print("SProp2 = ", SProp, " SValue = " , SValue);
      if(CustomSymbolSetString(SymName,SProp,SValue))
      {
         return true;}
      else
         Print("Error setting symbol property: ",SProp,". Error code: ",GetLastError());
     }
     
     
   return false;
  }

bool SetProperty(string SymName,ENUM_SYMBOL_INFO_INTEGER IProp,long PropValue,string BaseSymName)
  {
   ResetLastError();
   //Print("IProp1 = ", IProp, " PropValue = " , PropValue);
   
   if(BaseSymName=="")
     {
      if(CustomSymbolSetInteger(SymName,IProp,PropValue))
         {
         return true;}
      else
         Print("Error setting symbol property: ",IProp,". Error code: ",GetLastError());
     }
   else
     {
      long IValue=SymbolInfoInteger(BaseSymName,IProp);
      //Print("IProp2 = ", IProp, " IValue = " , IValue);
      if(CustomSymbolSetInteger(SymName,IProp,IValue))
      {
         return true;}
      else
         Print("Error setting symbol property: ",IProp,". Error code: ",GetLastError());
     }
     
     
     
   return false;
  }

bool SetProperty(string SymName,ENUM_SYMBOL_INFO_DOUBLE DProp,double PropValue,string BaseSymName)
  {
   ResetLastError();
   //Print("DProp1 = ", DProp, " PropValue = " , PropValue);
   
   if(BaseSymName=="")
     {
      if(CustomSymbolSetDouble(SymName,DProp,PropValue))
      {
         return true;}
      else
         Print("Error setting symbol property: ",DProp,". Error code: ",GetLastError());
     }
   else
     {
      double DValue=SymbolInfoDouble(BaseSymName,DProp);
      //Print("DProp2 = ", DProp, " DValue = " , DValue);
      if(CustomSymbolSetDouble(SymName,DProp,DValue))
      {
         return true;}
      else
         Print("Error setting symbol property: ",DProp,". Error code: ",GetLastError());
     }
     
     
     
   return false;
  }
 
Tester Algos:

Formula USDX Index = 50.14348112*pow(ask(EURUSD),-0.576)*pow(USDJPY,0.136)*pow(ask(GBPUSD),-0.119)*pow(USDCAD,0.091)*pow(USDSEK,0.042)*pow(USDCHF,0.036)

PROBLEM: is that the synthetic instrument formula is empty therefore no quotes are available. The custom symbol and the existing symbols quotes needed are available , visible and live on Market Watch . What am i missing  in order to have  the quotes for the custom symbol  in real-time. 

NOTE: When i create the custom symbol manually and add the  custom symbol synthetic formula manually it works fine and no problem at all.


Almost similar to:

I suggest the reading. Although slightly different, the topic below contains good documentation and reference about what you need.


https://www.mql5.com/en/forum/217512

How to create custom symbols and feed it's chart data from a remote server in real time
How to create custom symbols and feed it's chart data from a remote server in real time
  • 2017.10.14
  • www.mql5.com
Hello there, I'm trying to learn how to create a MQL5 library for MT5 (I think it should be a library, but not so sure) to create new symbols and f...
 
rrocchi:


Almost similar to:

I suggest the reading. Although slightly different, the topic below contains good documentation and reference about what you need.


https://www.mql5.com/en/forum/217512

i read it already. That reference uses external server quotes and not quotes already available to the metaquotes MT5 terminal . The formula is simply calculating its quotes from already  existing EURUSD, GBPUSD, USDJPY, USDCAD, USDCHF, USDSEK quotes . What am i missing ? 


NOTE: Why does MT5 make a very simple process extremely confusing and hard to do. If its simple and possible Manually , why not programmatically ? 

 
Tester Algos:
i read it already. That reference uses external server quotes and not quotes already available to the metaquotes MT5 terminal . The formula is simply calculating its quotes from already  existing EURUSD, GBPUSD, USDJPY, USDCAD, USDCHF, USDSEK quotes . What am i missing ? 


Have you tried to insert the formula directly on your function?

Can you try to do it ? Post the results after testing.


SetProperty(SName,SYMBOL_FORMULA,"50.14348112*pow(ask(EURUSD),-0.576)*pow(USDJPY,0.136)*pow(ask(GBPUSD),-0.119)*pow(USDCAD,0.091)*pow(USDSEK,0.042)*pow(USDCHF,0.036)");
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
  • www.mql5.com
To obtain the current market information there are several functions: SymbolInfoInteger(), SymbolInfoDouble() and SymbolInfoString(). The first parameter is the symbol name, the values of the second function parameter can be one of the identifiers of ENUM_SYMBOL_INFO_INTEGER, ENUM_SYMBOL_INFO_DOUBLE and ENUM_SYMBOL_INFO_STRING. Some symbols...
Reason: