Calculator_Forex

Download script
Author: Copyright � 2007, DRKNN
Calculator_Forex

This script is a financial calculation tool designed for the MetaTrader 4 platform. Its primary purpose is to help traders automatically determine their risk parameters—specifically, how many units (lots) to trade based on a desired stop-loss distance, or conversely, how wide a stop-loss should be based on a specific lot size and a fixed percentage of capital risk.

The logic follows a structured workflow to ensure that any calculation remains within the practical limits of a live trading account:

1. Input Validation and Safety Checks

Before performing any calculations, the script verifies that the user's inputs are realistic and valid. It checks to ensure that:

  • The risk percentage is greater than zero.
  • The requested lot size falls within the minimum and maximum limits allowed by the specific broker/instrument.
  • The stop-loss distance meets the "Stop Level" requirements (the minimum distance required by the broker to place an order). If any of these conditions are not met, the script will notify the user via a pop-up alert and stop further processing.

2. Capital and Risk Normalization

The script establishes the financial "base" for the calculation:

  • Deposit: If the user provides a specific amount, it uses that; otherwise, it automatically fetches the current account balance.
  • Risk Amount: It converts the risk percentage into a concrete dollar (or account currency) value. For example, if you risk 3% of a $10,000 account, the script identifies that the total allowed loss is $300.

3. Currency Pair Analysis

The logic is sophisticated enough to recognize different types of currency pairs, as the cost of price movement (a "pip" or "point") varies depending on the pair:

  • Direct Pairs: (e.g., EUR/USD) Standard calculation.
  • Inverse Pairs: (e.g., USD/JPY) Adjusts for the base currency being the quote currency.
  • Cross Pairs: (e.g., EUR/JPY) Automatically identifies and factors in the exchange rate of the base currency against the US Dollar to ensure accuracy.

4. Dual-Mode Calculation Logic

The script provides two distinct types of results simultaneously:

A. Calculating Stop-Loss based on Lot Size: If you decide exactly how many lots you want to trade, the script calculates the maximum "safe" distance your stop-loss can be. It takes your total risk amount and divides it by the cost of a single point for that specific lot size. This tells you exactly how much price movement you can tolerate before hitting your risk limit.

B. Calculating Lot Size based on Stop-Loss: If you decide exactly how many points away your stop-loss must be (e.g., "I want to place my stop 15 points away"), the script performs the reverse math. It determines how many lots can be opened so that the distance of those 15 points equals your total risk amount.

5. Results Output

Finally, the script provides a comprehensive summary via on-screen alerts. It displays:

  • The cost of a point for both your chosen lot size and the minimum allowed lot size.
  • The exact dollar amount you are risking based on your percentage.
  • The calculated maximum lot size for your specific stop-loss.
  • The calculated maximum stop-loss distance for your specific lot size.
Miscellaneous
It issuies visual alerts to the screen
14 Views
0 Downloads
0 Favorites
Calculator_Forex
//+------------------------------------------------------------------+
//|                                             Calculator_Forex.mq4 |
//|                                          Copyright © 2007, DRKNN |
//|                                                    drknn@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, DRKNN"
#property link      "drknn@mail.ru"

#property show_inputs
extern double  Depozit=0.0;//êàêèì êîëè÷åñòâîì êàïèòàëà áóäåì ðèñêîâàòü
extern double  Risk=3.0;//êàêèì êîëè÷åñòâîì êàïèòàëà áóäåì ðèñêîâàòü
extern string  t11="------- Äëÿ ðàññ÷¸òà ñòîï-ëîññà ïî ëîòó --------";
extern double  Lot=0.2;//ñ êàêèì ëîòîì áóäåì îòêðûâàòü ïîçèöèþ. 
extern string  t1="------- Äëÿ ðàññ÷¸òà ëîòà ïî ñòîï-ëîññó --------";
extern double  StopLoss=15.0;

/*
   ôîðìóëà ðàññ÷¸òà ìàêñèìàëüíîãî ñòîï-ëîññà äëÿ óêàçàííîãî ïîëüçîâàòåëåì ëîòà:
   SL_PNT=normRisk*normDepozit/PointPrise/Lot; 
*/


//+------------------------------------------------------------------+
//|                 Íà÷àëî ðàáîòû ñêðèïòà                            |
//+------------------------------------------------------------------+
int start()
  {
   string SMB=Symbol();
   double normRisk=0;//ñîäåðæèò íîðìàëèçîâàííûé ðèñê
   double SL_PNT=0;//ñòîï-ëîññ â ïóíêòàõ
   double MinLot=MarketInfo(SMB,MODE_MINLOT);//ìèíèìàëüíûé ðàçìåð ëîòà        
   double MaxLot=MarketInfo(SMB,MODE_MAXLOT);//ìàêñèìàëüíûé ðàçìåð ëîòà
   double ryn_Lot=0;//ðàññ÷èòûâàåìûé ëîò äëÿ óêàçíààíîãî ïîëüçîâàòåëåì ñòîï-ëîññà
   double LotStep=MarketInfo(SMB,MODE_LOTSTEP);//øàã èçìåíåíèÿ ëîòà
   double normDepozit=0;
   int    TipInstrumenta=10000;
   double PointPrise=0;
   string SMB1,SMB2;
   double BazCours=0;// êóðñ áàçîâîé âàëþòû ïî îòíîøåíèþ ê äîëëàðó
   double ryn_PointPrise=0;//ñòîèìîñòü ïóíêòà ïðè ìèíèìàëüíîì ëîòå
   double ryn_SL=0;//ñòîï-ëîññ äëÿ ìèíèìàëüíîãî ëîòà
   int    MinLevel=MarketInfo(SMB,MODE_STOPLEVEL);//Ìèíèìàëüíî äîïóñòèìûé óðîâåíü ñòîï-ëîññà (òåéê-ïðîôèòà) â ïóíêòàõ
   string t1="=============================";
   
   
   

   
   
   //------------------------------------------------------------------------------------------------------------------------
   Alert (t1);
   //-------- Çàùèòà îò äóðàêà ----------------------
   
   if(Risk<=0){
      Alert(" Îøèáêà! Risk äîëæåí áûòü áîëüøå íóëÿ.");
      return(0);
   }
   if(Lot<MinLot){
      Alert(" Îøèáêà! Lot íå äîëæåí áûòü ìåíüøå ",MinLot);
      return(0);
   }
   if(Lot>MaxLot){
      Alert(" Îøèáêà! Lot íå äîëæåí áûòü áîëüøå ",MaxLot);
      return(0);
   }
   
   if(StopLoss<MinLevel){
     Alert(" Îøèáêà! StopLoss íå äîëæåí áûòü ìåíüøå ",MinLevel," ïóíêòîâ");
     return(0);
   }
   if(Lot>=MinLot && Risk!=0 && StopLoss>=MinLevel){
   
   //------ íîðìàëèçóåì äåïîçèò ------------
   if(Depozit==0){
     normDepozit=AccountBalance();//Áàëàíñ ñ÷¸òà
   }
   if(Depozit>0){
     normDepozit=Depozit; 
   }
   //------ íîðìàëèçóåì ïðîöåíò ðèñêà ------------
   normRisk=Risk/100;
   // ------- ñìîòðèì, Risk," ïðîöåíòîâ îò äåïîçèòà ýòî ñêîëüêî â öåíòàõ -----------------
   double normRiskPersent=NormalizeDouble(normDepozit*normRisk,2);
   
   // --------- âû÷èñëÿåì êàêàÿ ïàðà (ïðÿìàÿ, îáðàòíàÿ, êðîññ) --------------
   TipInstrumenta=StringFind( SMB,"USD",0); 
   
   //------ èçâëåêàåì èìÿ ïåðâîé âàëþòíîé ïàðû ------------
   SMB1=StringSubstr(SMB,0,3); //áàçîâàÿ âàëþòà
   //SMB2=StringSubstr(SMB,3,3);// âàëþòà êîòèðîâàíèÿ.
   
   // ------- òåêóùàÿ êîòèðîâêà áàçîâîé âàëþòû ê äîëëàðó ÑØÀ -------------
   BazCours=MarketInfo(SMB1+"USD",MODE_BID);
   //Alert("Tåêóùàÿ êîòèðîâêà áàçîâîé âàëþòû ê äîëëàðó ÑØÀ = ",BazCours);
   
   
   
   // --------- ðàññ÷èòûâàåì ñòîèìîñòü ïóíêòà -------------------------
   if(TipInstrumenta==3){//ïàðà ïðÿìàÿ, íàïð. EurUsd
     PointPrise=1000000*Lot/10*Point;
     ryn_PointPrise=1000000*MinLot/10*Point;//ñòîèìîñòü ïóíêòà ïðè ìèíèìàëüíîì ëîòå
   }
   if(TipInstrumenta==0){//ïàðà îáðàòíàÿ, íàïð. UsdJpy
     PointPrise=1000000*Lot/10*Point/Bid;
     ryn_PointPrise=1000000*MinLot/10*Point/Bid;//ñòîèìîñòü ïóíêòà ïðè ìèíèìàëüíîì ëîòå
   }
   if(TipInstrumenta==-1){//Êðîññ-ïàðà, íàïð EurJpy
     PointPrise=1000000*Lot/10*Point*BazCours/Bid;
     ryn_PointPrise=1000000*MinLot/10*Point*BazCours/Bid;
   }
   // ----- íîðìàëèçóåì ïîëó÷åííóþ ñòîèìîñòü ïóíêòà ------
   PointPrise=NormalizeDouble(PointPrise,2);
   ryn_PointPrise=NormalizeDouble(ryn_PointPrise,2);
   
   // ========== âû÷èñëÿåì ìàêñèìàëüíûé ñòîï-ëîññ äëÿ óêàçàííîãî ïîëüçîâàòåëåì ëîòà =========================================
   
   //------ ñìîòðèì, ñêîëüêî ðàç ìèíèìàëüíûé ëîò óêëàäûâàåòñÿ â ïîëüçîâàòåëüñêîì -------------
   double iMinLot=MinLot;
   int    SchLots=0;//ñêîëüêî ìèíèìàëüíûõ ëîòîâ óêëàäûâàåòñÿ â ïîëüçîâàòåëüñêîì
   
   if(Lot==MinLot){SchLots=1;}
   else if(Lot>MinLot){
     while(iMinLot<Lot){
       iMinLot=iMinLot+LotStep;
       SchLots++;
     }
   }  
   //òåïåðü âû÷èñëÿåì óðîâåíü ñòîï-ëîññà
   SL_PNT=normRisk*normDepozit/PointPrise/SchLots;
   SL_PNT=NormalizeDouble(SL_PNT,0);
   
  
   
   
   // ========== âû÷èñëÿåì ìàêñèìàëüíûé ëîò äëÿ óêàçàííîãî ïîëüçîâàòåëåì ñòîï-ëîññà =========================================
   
   // ----- ñìîòðèì, êàêîé ñòîï-ëîññ ïîëó÷èòñÿ ïðè ìèíèìàëüíîì ëîòå -------------
   double jj=1;
   ryn_SL=normRisk*normDepozit/ryn_PointPrise/jj;
   ryn_SL=NormalizeDouble(ryn_SL,0);
   // ----- ñìîòðèì, ñêîëüêî ðàç ïîëüçîâàòåëüñêèé ñòîï-ëîññ óìåùàåòñÿ â ñòîï-ëîññå ìèíèìàëüíîãî ëîòà
   int    SchSL=0;//ñêîëüêî ïîëüçîâàòåëüñêèõ ñòîï-ëîññîâ óìåùàåòñÿ â ìàêñèìàëüíîì ñòîï-ëîññå ïðè ìèíèìàëüíîì ëîòå
   
   if(StopLoss==ryn_SL){SchSL=1;}
   else if(StopLoss<ryn_SL){
     double iStopLoss=StopLoss;
     while(iStopLoss<ryn_SL){
       iStopLoss=iStopLoss+iStopLoss;
       SchSL++;
     }
   }  
   //----- òåïåðü ïðîñ÷èòûâàåì èñêîìûé ëîò -------------
   ryn_Lot=SchSL*MinLot;
   
   
   
   
   // ------------ âûâîäèì ðåçóëüòàòû ----------------
   Alert("Ìèíèìàëüíî äîïóñòèìûé óðîâåíü ñòîï-ëîññà = ",MinLevel," ïóíêòîâ");
   Alert("Ñòîèìîñòü ïóíêòà ïðè ìèíèìàëüíîì ",MinLot," ëîòå = ",ryn_PointPrise," $");
   Alert("Ñòîèìîñòü ïóíêòà ïðè ëîòå ",Lot," = ",PointPrise," $");
   Alert(Risk," ïðîöåíòîâ îò äåïîçèòà = ",normRiskPersent," $");
   Alert("Äåíåã íà ñ÷åòó = ",normDepozit," $");
   Alert("--------");
   Alert("Ìàêñèìàëüíûé ëîò = ",ryn_Lot);
   Alert(SMB,"  Ðàññ÷¸ò ëîòà äëÿ ñòîï-ëîññà = ",StopLoss," ïóíêòîâ :");
   Alert("--------");
   Alert("Ìàêñèìàëüíûé ñòîï-ëîññ = ",SL_PNT," ïóíêòîâ");
   Alert(SMB,"  Ðàññ÷¸ò ñòîï-ëîññà äëÿ ëîòà = ",Lot," :");
   Alert (t1);
  }
  
   return(0);
  }
//+------------------------------------------------------------------+
//|                 Êîíåö ðàáîòû ñêðèïòà                             |
//+------------------------------------------------------------------+

Comments