please help to fix this issue which appears when I re-open the metatrade terminal.

 

Hi. I want to check account used margin so I write this code and use it as an indicator:


#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Blue


double Buf_Exposure[];             // Declaring arrays (for indicator buffers)
//--------------------------------------------------------------------
void init()                          // Special function init()
  {
   SetIndexBuffer(0,Buf_Exposure);         // Assigning an array to a buffer
   SetIndexStyle (0,DRAW_HISTOGRAM,STYLE_SOLID,2);// Line style
   IndicatorDigits(Digits);
   return;                          // Exit the special funct. init()
  }
   
//---------------------------------------------------------------------
void start()                         // Special function start()
  {
     int i,                           // Bar index
       Counted_bars;                // Number of counted bars
//--------------------------------------------------------------------
   Counted_bars=IndicatorCounted(); // Number of counted bars
   i=Bars-Counted_bars-1;           // Index of the first uncounted
//---------------------------------------------------------------------

   while(i>=0)                      // Loop for uncounted bars
     {
//---------------------------------------------------------------------
      double FreeMargin = AccountFreeMargin();
      double Equity = AccountEquity();
      double Exposure = (Equity - FreeMargin) / FreeMargin;
      Buf_Exposure[i] = Exposure * 100;
//-------------------------------------------------------------------------
      i--;                          // Calculating index of the next bar
     }
//---------------------------------------------------------------------

   return;                          // Exit the special funct. start()
  }
//---------------------------------------------------------------------

Everything is Ok, but when I close Metatrader terminal and I open it again, the values disappear and the indicator values are null and nothing is calculated (Look at the below picture). So I have to run the indicator again. What is the reason and how can I fix it? 

I appreciate any help or advise. 

Thanks.

 

Also when I Compile the code AGAIN in MetaEditor, Indicator is fixed in Metatrader.


Any Opinion? 

 
   double FreeMargin = AccountFreeMargin();
      double Equity = AccountEquity();
These are constant throughout the loop. Posting your histogram on previous bars is nonsense.
 
William Roeder #:
These are constant throughout the loop. Posting your histogram on previous bars is nonsense.

It works fine until the terminal is open. it change constantly and no problem with it.

The problem appears when I close the terminal and re-open it.


By the way,


AccountFreeMargin()
AccountEquity()

 Dont have any argument. How can I use them as variable?

 
Stable_Profit # Dont have any argument. How can I use them as variable?

That is your problem; you can't.

 
#property strict
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 clrBlue
#property indicator_style1 STYLE_SOLID
#property indicator_type1  DRAW_HISTOGRAM
#property indicator_width1 2

double g_adbExposure[];

int OnInit()
{
   SetIndexBuffer( 0, g_adbExposure, INDICATOR_DATA );
   return( INIT_SUCCEEDED );
};

int OnCalculate( const int rates_total, const int prev_calculated, const datetime &time[],
   const double &open[], const double &high[], const double &low[], const double &close[],
   const long &tick_volume[], const long &volume[], const int &spread[] )
{
   if( rates_total > 0 )
   {
      double dbAccountMarginFree = AccountInfoDouble( ACCOUNT_MARGIN_FREE );
      g_adbExposure[ 0 ] = ( dbAccountMarginFree > 0.0 ) || ( dbAccountMarginFree < 0.0 )
                         ? 100.0 * ( AccountInfoDouble( ACCOUNT_EQUITY ) - dbAccountMarginFree ) / dbAccountMarginFree
                         : 0.0;
   };
   return rates_total;
};
 
Fernando Carreiro #:

Thanks a lot. Works fine.

Reason: