Metatrader Forum | Forex Expert-Advisor | Broker & Forex Tools

Metatrader Forum | Forex Expert-Advisor | Broker & Forex Tools (http://www.expert-advisor.com/forum/index.php)
-   Programmierung MQL4 (http://www.expert-advisor.com/forum/forumdisplay.php?f=220)
-   -   OrderHistory als Basis für Lotsize (http://www.expert-advisor.com/forum/showthread.php?t=3728)

hylocharis 25.06.14 17:14

OrderHistory als Basis für Lotsize
 
Hallo,

mein EA soll die Lotgröße abhängig vom Ergebnis der letzten Trades bestimmen. Den Anfangswert von lots kann der User selbst eintragen. Die Anfangsgröße von lots soll in meinem Beispiel 0.4 sein.

Fall 1: Letzter Trade war positiv => Lotsize für nächsten Trade soll 1*lots sein
Fall 2: Letzter Trade war negativ => Lotsize für nächsten Trade soll 2*lots sein
Fall 3: Die Letzten beiden Trades waren negativ => Lotsize für nächsten Trade soll 3*lots sein

Mit folgenden Code klappt das auch. Allerdings nur mit den festen Konstanten 0.4, 0.8 und 1.2.

Code:

//+------------------------------------------------------------------+
//|                                                      01_Lot.mqh |
//|                                                      Hylocharis |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Hylocharis"
#property link      "http://www.mql5.com"
#property strict
//----------------------------------------------------------------------------- 1 --
// Function calculating the amount of lots.
// Global variables:
// double lots_new - the amount of lots for new orders (calculated)
// double lots    - the desired amount of lots defined by the user.
// int percent    - max loss of balance in percentage per trade defined by the user
// Returned values:
// true  - if there is enough money for the minimum volume
// false - if there is no enough money for the minimum volume
//----------------------------------------------------------------------------- 2 --
bool Lot()                                    // User-defined function
  {
  string symbol = Symbol();                  // Symbol
  double one_lot = MarketInfo(symbol,MODE_MARGINREQUIRED);//!-lot cost
  double min_lot = MarketInfo(symbol,MODE_MINLOT);// Min. amount of lots
  double step    = MarketInfo(symbol,MODE_LOTSTEP);//Step in volume changing
  double tick_value = MarketInfo(Symbol(),MODE_TICKVALUE);
  double free_margin = AccountFreeMargin();  // Free margin       
  double new_balance =(AccountBalance()+AccountProfit()); // amount of money on the account
  double max_loss;
//----------------------------------------------------------------------------- 3 --
  if (lots>0)                                // Volume is explicitly set..
    {                                        // ..check it
      double money = lots * one_lot;          // Order cost 
      if(money <= AccountFreeMargin())        // Free margin covers it..
        {
        int cnt=0;
        double profit;
        int ls = 1;
        lots_new = lots; //Lot - initiale LotSize
        if (MarketInfo(Symbol(), MODE_LOTSTEP) == 0.01) ls = 2;
 
        for(int i=OrdersHistoryTotal()-1; i>=0; i--)
          {
            if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
              {
              if (OrderSymbol() == Symbol() && OrderMagicNumber() == 3333)
                {
                  profit = OrderProfit();
                  if (profit < 0) cnt++;
                  else
                    {
                    if (cnt == 0)
                      {
                        //lots_new = NormalizeDouble(lots, ls);
                        lots_new = 0.4;
                      }
                    if (cnt == 1)
                      {
                        //lots_new = NormalizeDouble(lots*2, ls);
                        lots_new = 0.8;
                      }
                    if (cnt >= 2)
                      {
                        //lots_new = NormalizeDouble(lots*3, ls);
                        lots_new = 1.2;
                      }
                    break;
                    }

                }
              }
          }
        }

      else                                    // If free margin is not enough..
        lots_new=MathFloor(free_margin/one_lot/step)*step;// Calculate lots
    }
//----------------------------------------------------------------------------- 4 --
  if (lots_new < min_lot)                    // If it is less than allowed..
      lots_new = min_lot;                      // .. then minimum
  if (lots_new * one_lot > AccountFreeMargin())// It isn't enough even..
    {                                        // ..for the min. lot:(
      Inform(11,0,min_lot);                    // Message..
      return(false);                          // ..and exit
    }
  return(true);                              // Exit user-defined function
  }
//----------------------------------------------------------------------------- 6 --

Ich würde gerne die variable Version programmieren (siehe auskommentierte Variante //lots_new = NormalizeDouble(lots*2, ls);). Bei 2 negativen Trades errechnet er eine Lotgröße von 1.6 und nicht 1.2. Irgendwie verdoppelt er immer die letzten wert von lots.

Wie kann ich das richtig programmieren?

PortfolioTrader 25.06.14 18:31

Das ganze nett sich Martingale (bzw eine Form davon) und killt zuverlässig jeden Account.

hylocharis 25.06.14 21:22

Dass die Martingale Strategie irgendwann den Account killt, davon bin ich auch überzeugt. Deshalb möchte ich meine Lotsize auch nur max. 2 mal erhöhen. Von 0.4 auf 0.8 und ggf. auf 1.2. Danach ist Schluss mit erhöhen. Sobald dann ein Trade mit Profit abgeschlossen wird, fange ich wieder bei 0.4 an. Die Backtests mit dieser Strategie haben bisher ein recht vernünftiges Ergebnis abgeliefert.

PortfolioTrader 25.06.14 22:08

Da würde ich die die Recovery Funktion des WSFR empfehlen.
Der verdoppelt nicht sondern erhöht ganz leicht.

Das solltest über die mathematische Steigung (y=mx+t) gut hinbekommen.
Die Steigung m ist dein Risiko pro Trade. Sagen wir 3%.
Und dann die Lotsize im Verhältnis zum Minusabstand der Equity zur Steigung erhöhen.
Ist auf Dauer viel sicherer.


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

Powered by vBulletin® Version 3.8.5 (Deutsch)
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.6.1
Powered by vBCMS® 2.7.0 ©2002 - 2024 vbdesigns.de
Copyright ©2009 - 2023 by Expert-Advisor.com - Das Metatrader Forum