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?