Author: Copyright � 2008, Kharko
Currency

This script is designed as an automated risk management tool for traders using the MetaTrader platform. Its primary purpose is to calculate the maximum allowable "Lot" size (the amount of a currency position you can open) based on your current account balance and a specific risk percentage.

Core Functionality

1. Risk Awareness and Account Protection The script takes a user-defined percentage (defaulting to 20%) of your available free margin. It uses this percentage to determine how much capital you are willing to commit to a single trade at any given moment. By doing this, it helps prevent "over-leveraging," which is when a trader opens a position so large that a small move in the market could wipe out their account.

2. Automated Lot Sizing Instead of forcing the trader to do manual math for every different currency pair, the script automatically calculates the correct volume. It considers several technical factors:

  • Margin Requirement: How much "collateral" the broker demands to hold the position.
  • Tick Value and Spread: The actual cost of moving the price and the difference between the buy and sell prices.
  • Minimum Lot Steps: Ensures that the calculated size is compatible with the broker's specific rules (e.g., if a broker only allows increments of 0.1, the script will round accordingly).

3. Multi-Currency Analysis The tool contains a pre-defined list of common currency pairs (such as EURUSD, GBPJPY, USDCHF, etc.). Depending on your settings, it can perform two different actions:

  • Global Scan: It can generate a comprehensive report for all listed currency pairs, showing the cost and maximum lot size for each.
  • Targeted Analysis: It can focus specifically on the single currency pair you currently have open on your screen.

4. Data Logging and Reporting The script performs these calculations in real-time. It creates a file (CSV format) within the platform's "files" folder. This file acts as a ledger, listing each currency pair alongside its specific margin costs and the maximum allowed lot size calculated by the script. Simultaneously, it provides a live update on your trading chart, showing you exactly how much of your capital is being utilized for the current pair based on your chosen risk percentage.

Summary for the User

In simple terms, this tool acts as a safety calculator. You tell it what percentage of your money you want to risk, and the script looks at the current market conditions for your selected currencies to tell you exactly how much you are allowed to trade. It records these limits into a file so you can reference them later, ensuring your trading stays within your predetermined safety boundaries.

Miscellaneous
Uses files from the file systemIt writes information to file
11 Views
0 Downloads
0 Favorites
Currency
//+------------------------------------------------------------------+
//|                                                     Currency.mq4 |
//|                                         Copyright © 2008, Kharko |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Kharko"
#property link      ""
 
#property indicator_chart_window
//---- input parameters
extern string Zametki1="ÇÀÏÈÑÜ Â ÔÀÉË ÏÐÎÈÇÂÎÄÈÒÑß Â ÌÎÌÅÍÒ ÍÀÁÐÀÑÛÂÀÍÈß ÍÀ ÃÐÀÔÈÊ. ÔÀÉË ÍÀÕÎÄÈÒÑß experts\files"; 
extern bool      All_Pairs=true;
extern double    Procent=20.0;
 
string      FileName;
string      Para[]={"EURUSD","EURGBP","EURCHF","EURJPY","EURAUD","EURCAD","EURNZD",
                    "GBPUSD","USDCHF","USDJPY","AUDUSD","USDCAD","NZDUSD","GBPCHF",
                    "GBPJPY","GBPAUD","GBPCAD","GBPNZD","CHFJPY","AUDCHF","CADCHF",
                    "NZDCHF","AUDJPY","CADJPY","NZDJPY","AUDCAD","AUDNZD","NZDCAD"};
int         KolPara  = 28,count=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   Comment("");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
//----
   if(!IsConnected())return(0);
 
   if(MarketInfo(Symbol(),MODE_TRADEALLOWED) != 1)return(0);
 
   if(count!=1)
   {
      FileName="Currency.csv";
 
      int handle=FileOpen(FileName,FILE_CSV|FILE_WRITE,';');
      if(handle<1)
      {
         handle=FileOpen(FileName,FILE_CSV|FILE_WRITE,';');
      }
      else
      {
         FileClose(handle);
         FileDelete(FileName);
         handle=FileOpen(FileName,FILE_CSV|FILE_WRITE,';');
      }
   
      FileWrite(handle,"Ïàðà","Ñòîèìîñòü ëîòà","Ðàçìåð ëîòà");
 
      if(All_Pairs)
      {
         for(int i=0;i<KolPara;i++)
         {
            if(MarketInfo(Para[i],MODE_TRADEALLOWED) != 1)
               continue;
 
            FileWrite(handle,Para[i],MarketInfo(Para[i],MODE_MARGINREQUIRED),value(i));
            Print(Para[i]," : ",MarketInfo(Para[i],MODE_MARGINREQUIRED)," : ",value(i));
 
         }
         Print("Ïàðà | Ñòîèìîñòü ëîòà | Ðàçìåð ëîòà");
      }
      else
      {
         FileWrite(handle,Symbol(),MarketInfo(Symbol(),MODE_MARGINREQUIRED),value1());
         Print(Symbol()," : ",MarketInfo(Symbol(),MODE_MARGINREQUIRED)," : ",value1());
         Print("Ïàðà | Ñòîèìîñòü ëîòà | Ðàçìåð ëîòà");
      }
 
      FileClose(handle);
      if(MarketInfo(Symbol(),MODE_MARGINREQUIRED)>0)
         count=1;
   }
   Comment("\n",Symbol()," : ",value1()," ëîò ïðè ",Procent,"% çàëîãà îò áàëàíñà");
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
double value(int i)
  {
  int j=1;
  double summa, Lots;
  
  Lots=MarketInfo(Para[i],MODE_LOTSTEP);
//----
   while(j>0)
   {
      summa=0;
      summa=j*Lots*(MarketInfo(Para[i],MODE_MARGINREQUIRED)+MarketInfo(Para[i],MODE_TICKVALUE)*MarketInfo(Para[i],MODE_SPREAD));
      
      if(summa>Procent*AccountFreeMargin()/100)
      {
         j--;
         if(j<1)j=1;
         return(j*Lots);
      }
      j++;
   }
//----
  }
//+------------------------------------------------------------------+
double value1()
  {
  int j=1;
  double summa, Lots;
  
  Lots=MarketInfo(Symbol(),MODE_LOTSTEP);
//----
   while(j>0)
   {
      summa=0;
      summa+=j*Lots*(MarketInfo(Symbol(),MODE_MARGINREQUIRED)+MarketInfo(Symbol(),MODE_TICKVALUE)*MarketInfo(Symbol(),MODE_SPREAD));
      
      if(summa>Procent*AccountFreeMargin()/100)
      {
         j--;
         if(j<1)j=1;
         return(j*Lots);
      }
      j++;
   }
//----
  }

Comments