//+------------------------------------------------------------------+
//|                                          CustomSymbolManager.mqh |
//+------------------------------------------------------------------+
#ifndef CUSTOMSYMBOLMANAGER_MQH
#define CUSTOMSYMBOLMANAGER_MQH

//+------------------------------------------------------------------+
//| CCustomSymbolManager                                             |
//| Creates and prepares the custom symbol that will receive the     |
//| aggregated Renko bricks, and registers it in Market Watch.       |
//+------------------------------------------------------------------+
class CCustomSymbolManager
  {
private:
   string            m_custom_name;   // name of the custom symbol this instance manages

   bool              SymbolAlreadyExists(const string custom_name);

public:
                     CCustomSymbolManager(void);
                    ~CCustomSymbolManager(void);

   bool                 Create(const string base_symbol, const string custom_name);
   bool                 AddToMarketWatch(void);
   string               CustomName(void) const;
  };

//+------------------------------------------------------------------+
//| CCustomSymbolManager constructor                                 |
//| Leaves the managed symbol name empty until Create is called.     |
//+------------------------------------------------------------------+
CCustomSymbolManager::CCustomSymbolManager(void)
  {
   m_custom_name = "";
  }

//+------------------------------------------------------------------+
//| CCustomSymbolManager destructor                                  |
//| The manager does not delete the custom symbol on destruction, so |
//| its bar history survives across EA restarts. The engine's        |
//| anchor and direction are not part of that history and are not    |
//| restored on restart. The writer re-seeds its sequencer from the  |
//| last stored bar.                                                 |
//+------------------------------------------------------------------+
CCustomSymbolManager::~CCustomSymbolManager(void)
  {
  }

//+------------------------------------------------------------------+
//| SymbolAlreadyExists                                              |
//| Tests whether a custom symbol of this name is already backed by  |
//| the terminal, using a property query that only succeeds for a    |
//| symbol that genuinely exists.                                    |
//+------------------------------------------------------------------+
bool CCustomSymbolManager::SymbolAlreadyExists(const string custom_name)
  {
   long     digits_probe  = 0;
   bool     query_result  = ::SymbolInfoInteger(custom_name, SYMBOL_DIGITS, digits_probe);

   return(query_result);
  }

//+------------------------------------------------------------------+
//| Create                                                           |
//| Creates the custom symbol by cloning a base symbol's properties, |
//| then explicitly sets digits and point. Succeeds without error if |
//| the custom symbol already exists from a previous run.            |
//+------------------------------------------------------------------+
bool CCustomSymbolManager::Create(const string base_symbol, const string custom_name)
  {
   m_custom_name = custom_name;

//--- an existing custom symbol from a previous session is treated as success, not failure
   if(SymbolAlreadyExists(custom_name))
     {
      ::Print("CCustomSymbolManager::Create: custom symbol ", custom_name, " already exists, reusing it");
      return(true);
     }

//--- clone the base symbol's properties into a brand new custom symbol
   bool     created = ::CustomSymbolCreate(custom_name, "Renko\\Custom", base_symbol);
   if(!created)
     {
      ::Print("CCustomSymbolManager::Create failed: CustomSymbolCreate returned error ", ::GetLastError());
      return(false);
     }

//--- digits and point drive every price the pipeline computes, so set them explicitly
   long     base_digits = (long)::SymbolInfoInteger(base_symbol, SYMBOL_DIGITS);
   double   base_point  = ::SymbolInfoDouble(base_symbol, SYMBOL_POINT);

   bool     digits_set = ::CustomSymbolSetInteger(custom_name, SYMBOL_DIGITS, base_digits);
   bool     point_set  = ::CustomSymbolSetDouble(custom_name, SYMBOL_POINT, base_point);

   if(!digits_set || !point_set)
     {
      ::Print("CCustomSymbolManager::Create warning: could not set digits or point on ", custom_name);
     }

   return(true);
  }

//+------------------------------------------------------------------+
//| AddToMarketWatch                                                 |
//| Registers the custom symbol in Market Watch so a chart window    |
//| can be opened on it, wrapping SymbolSelect.                      |
//+------------------------------------------------------------------+
bool CCustomSymbolManager::AddToMarketWatch(void)
  {
   if(m_custom_name == "")
     {
      ::Print("CCustomSymbolManager::AddToMarketWatch failed: no custom symbol has been created yet");
      return(false);
     }

   bool     selected = ::SymbolSelect(m_custom_name, true);
   if(!selected)
     {
      ::Print("CCustomSymbolManager::AddToMarketWatch failed: SymbolSelect returned error ", ::GetLastError());
      return(false);
     }

   return(true);
  }

//+------------------------------------------------------------------+
//| CustomName                                                       |
//| Returns the name of the custom symbol this instance manages.     |
//+------------------------------------------------------------------+
string CCustomSymbolManager::CustomName(void) const
  {
   return(m_custom_name);
  }

#endif // CUSTOMSYMBOLMANAGER_MQH
//+------------------------------------------------------------------+