Metatrader Forum | Forex Expert-Advisor | Broker & Forex Tools
Zurück   Metatrader Forum | Forex Expert-Advisor | Broker & Forex Tools > Metatrader 5 > Programmierung MQL5 > Codeschnipsel

Codeschnipsel Codeschnipsel für MQL5.

Login
Benutzername:
Kennwort:


Statistik
Themen: 4978
Beiträge: 43264
Benutzer: 7.223
Aktive Benutzer: 75
Links: 84
Wir begrüßen unseren neuesten Benutzer: Mane
Mit 2.475 Benutzern waren die meisten Benutzer gleichzeitig online (16.01.20 um 22:38).
Neue Benutzer:
vor 17 Stunden
- Mane
vor 2 Wochen
- AlbertZiz
vor 2 Wochen
- michak
vor 2 Wochen
- Amateur
vor 2 Wochen
- infos1982

Onlineuser
'Wer ist online' anzeigen Benutzer: 0
Gäste: 141
Gesamt: 141
Team: 0
Team:  
Benutzer:  
Freunde anzeigen

Empfehlungen

Thema geschlossen
 
Themen-Optionen Thema durchsuchen Ansicht
  #1 (permalink)  
Alt 16.02.22
Neues Mitglied
 
Registriert seit: May 2020
Ort: Duisburg
Beiträge: 28
Aleksi befindet sich auf einem aufstrebenden Ast
Lightbulb CCalc klasse

Immer wieder muss man Preise runden damit es nicht zu Fehlern wie "Invalid Price" oder "Invalid Vloume" kommt. An dieser Stelle hilft die CCalc Klasse

Sie beinhaltet neben den runden der Preise und Volumen auch eine Moneymanagement - Komponente, bei dieser (GetLot) wird das Volumen anhand des SL als double fertig ausgegeben.

Auch kann der aktuelle Start des Tages, der Woche,des Monat oder Jahr abgefragt werden.

Code:
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CCalc
  {
private:

   double            roundLots(string symbol,double& lots);

protected:

   double            Getlot(string symbol,double sl,ENUM_ORDER_TYPE PosType,double riskaspercent);
   double            roundUp(string symbol, double price);
   double            roundDn(string symbol, double price);

   datetime          GetDayStart();
   datetime          GetWeekStart();
   datetime          GetMonthStart();
   datetime          GetYearStart();
public:





  };
//+------------------------------------------------------------------+
//| Matrix Zeitraum Tag                                              |
//+------------------------------------------------------------------+
datetime CCalc::GetDayStart()
  {
   datetime MatrixDay;
   string StartDay="00:00:00";
   return MatrixDay=StringToTime(StartDay);
  }
//+------------------------------------------------------------------+
//| Matrix Zeitraum Woche                                            |
//+------------------------------------------------------------------+
datetime CCalc::GetWeekStart()
  {
   MqlDateTime int2;
   TimeToStruct(TimeTradeServer(),int2);
   int dayofWeekServer=int2.day_of_week;
   string StartDay="00:00:00";
   datetime Day=StringToTime(StartDay);

   if(dayofWeekServer==1)
     {
      Day=StringToTime(StartDay);
     }
   else
      if(dayofWeekServer==2)
        {
         Day=StringToTime(StartDay)-86400;
        }
      else
         if(dayofWeekServer==3)
           {
            Day=StringToTime(StartDay)-172800;
           }
         else
            if(dayofWeekServer==4)
              {
               Day=StringToTime(StartDay)-259200;
              }
            else
               if(dayofWeekServer==5)
                 {
                  Day=StringToTime(StartDay)-345600;
                 }
               else
                  if(dayofWeekServer==6)
                    {
                     Day=StringToTime(StartDay)-432000;
                    }
                  else
                     if(dayofWeekServer==0)
                       {
                        Day=StringToTime(StartDay)-518400;
                       }
   return Day;
  }
//+------------------------------------------------------------------+
//| Matrix Zeitraum Monat                                            |
//+------------------------------------------------------------------+
datetime CCalc::GetMonthStart()
  {
   MqlDateTime stm;
   TimeToStruct(TimeTradeServer(),stm);
   stm.day=1;
   stm.hour=0;
   stm.min=0;
   stm.sec=0;
   return(StructToTime(stm));
  }
//+------------------------------------------------------------------+
//| Matrix Zeitraum Jahr                                             |
//+------------------------------------------------------------------+
datetime CCalc::GetYearStart()
  {
   MqlDateTime stm;
   TimeToStruct(TimeTradeServer(),stm);
   stm.day=1;
   stm.mon=1;
   stm.hour=0;
   stm.min=0;
   stm.sec=0;
   return(StructToTime(stm));
  }
//+------------------------------------------------------------------+
//| Ermittlung des Handelsvolumen / OK                               |
//+------------------------------------------------------------------+
double CCalc::Getlot(string symbol,double sl,ENUM_ORDER_TYPE PosType,double riskaspercent)
  {
   double res=0;
   double lot=0;
   double Capitalbase = AccountInfoDouble(ACCOUNT_EQUITY);

   double point=SymbolInfoDouble(symbol,SYMBOL_POINT);
   if(PosType==ORDER_TYPE_BUY)
     {
      //Umrechnung SL in Pip
      double kurs=SymbolInfoDouble(symbol,SYMBOL_ASK);
      int SLinPoint=(int)NormalizeDouble((kurs-sl)/point,0);

      //Berechnung der Lot
      double risk = MathMin(riskaspercent,100.0)/100;
      lot=Capitalbase*risk*kurs/SLinPoint;
      // Print("risk: ",risk," Cap: ",Capitalbase,"SLinPoint: ",SLinPoint,"Lot: ",lot);
     }

   if(PosType==ORDER_TYPE_SELL)
     {
      //Umrechnung SL in Pip
      double kurs=SymbolInfoDouble(symbol,SYMBOL_BID);
      int SLinPoint=(int)NormalizeDouble((sl-kurs)/point,0);

      //Berechnung der Lot
      double risk = MathMin(riskaspercent,100.0)/100;
      lot=Capitalbase*risk*kurs/SLinPoint;
      //Print("risk: ",risk," Cap: ",Capitalbase,"SLinPoint: ",SLinPoint,"Lot: ",lot);
     }
   res=roundLots(symbol,lot);
//Print("Res: ",res);
   return res;
  }

//+------------------------------------------------------------------+
//| Aufunden des Preises /OK                                         |
//+------------------------------------------------------------------+
double CCalc::roundUp(string symbol, double price)
  {
   int Dig=(int)SymbolInfoInteger(symbol,SYMBOL_DIGITS);
   double Points=NormalizeDouble(SymbolInfoDouble(symbol,SYMBOL_POINT),Dig);
   double ticksize =
      NormalizeDouble(SymbolInfoDouble(symbol,SYMBOL_TRADE_TICK_SIZE),Dig);
   price = NormalizeDouble(price,Dig);
   double rest = price - NormalizeDouble(price/ticksize,0) * ticksize;
   rest = NormalizeDouble(rest,Dig);
   if(rest != 0)
     {
      for(double i = Points; i <= ticksize; i += Points)
        {
         price = NormalizeDouble(price + Points,Dig);
         rest = price - NormalizeDouble(price/ticksize,0) * ticksize;
         rest = NormalizeDouble(rest,Dig);
         if(rest == 0)
            break;
        }
     }
   return price;
  }
//+------------------------------------------------------------------+
//| Abunden des Preises /OK                                          |
//+------------------------------------------------------------------+
double CCalc::roundDn(string symbol,double price)
  {
   int Dig=(int)SymbolInfoInteger(symbol,SYMBOL_DIGITS);
   double Points=NormalizeDouble(SymbolInfoDouble(symbol,SYMBOL_POINT),Dig);
   double ticksize =NormalizeDouble(SymbolInfoDouble(symbol,SYMBOL_TRADE_TICK_SIZE),Dig);
   price = NormalizeDouble(price,Dig);
   double rest = price - NormalizeDouble(price/ticksize,0) * ticksize;
   rest = NormalizeDouble(rest,Dig);
   if(rest != 0)
     {
      for(double i = Points; i <= ticksize; i += Points)
        {
         price = NormalizeDouble(price - Points,Dig);
         rest = price - NormalizeDouble(price/ticksize,0) * ticksize;
         rest = NormalizeDouble(rest,Dig);
         if(rest == 0)
            break;
        }
     }
   return price;
  }
//+------------------------------------------------------------------+
//| Runden des zuvor klakulierten Handelsvolumen /OK                 |
//+------------------------------------------------------------------+
double CCalc::roundLots(string symbol,double& lots)
  {
   double startLots = lots;
   double lotstep = NormalizeDouble(SymbolInfoDouble(symbol,SYMBOL_VOLUME_STEP),2);
   lots = NormalizeDouble(lots,2);

   double rest = lots - NormalizeDouble(lots/lotstep,0) * lotstep;
   rest = NormalizeDouble(rest,2);
   if(rest != 0)
     {
      for(double i = 0.01; i <= lotstep; i += 0.01)
        {
         lots = NormalizeDouble(lots + 0.01,2);
         rest = lots - NormalizeDouble(lots/lotstep,0) * lotstep;
         rest = NormalizeDouble(rest,2);
         if(rest == 0)
            break;
        }
     }

   if(lots - startLots > startLots - (lots - lotstep))
     {
      lots -= lotstep;
     }
   lots = MathMin(lots,SymbolInfoDouble(symbol,SYMBOL_VOLUME_MAX));
   lots = MathMax(lots,SymbolInfoDouble(symbol,SYMBOL_VOLUME_MIN));
   return lots;
  }
//+------------------------------------------------------------------+
Man kann natürlich auch die funktionen als Public deklarieren... ich habe diese nur Protected weil diese nur in bestimmten Punkten meiner Klassenhirachie verfügbar sein sollen.

Geändert von Aleksi (16.02.22 um 21:08 Uhr)
Thema geschlossen

Lesezeichen

Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are aus
Pingbacks are aus
Refbacks are aus




Alle Zeitangaben in WEZ +2. Es ist jetzt 02:52 Uhr.





Suchmaschine - Reisen - Wavesnode - Facebook Forum - Spam Firewall
-----------------------------------------------------------------------------------------------------------------------------
Powered by vBulletin® Version 3.8.5 (Deutsch)
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Powered by vBCMS® 2.7.0 ©2002 - 2024 vbdesigns.de
SEO by vBSEO 3.6.1
Copyright ©2009 - 2023 by Expert-Advisor.com - Das Metatrader Forum
MetaTrader bzw. MetaTrader 4 und MetaTrader 5 sind eingetragene Marken der MetaQuotes Software Corp.
-----------------------------------------------------------------------------------------------------------------------------