complete code:
PHP Code
//+------------------------------------------------------------------+
//| CALCULATE LOT SIZE FOR ALL NEW TRADES |
//+------------------------------------------------------------------+
double Kelly = WinRate()-((1-WinRate())/(AvgWin()/AvgLoss()));
Lots=MathFloor((AccountFreeMargin()*AccountLeverage()*Kelly*Point*10)/(Ask*MarketInfo(Symbol(),MODE_LOTSIZE)*MarketInfo(Symbol(),MODE_MINLOT)))*MarketInfo(Symbol(),MODE_MINLOT);
//+------------------------------------------------------------------+
//| COLLECT REAL TIME ACCOUNT INFO |
//+------------------------------------------------------------------+
// Winrate
double WinRate()
{
double CountWins = 0;
int EA_Executed_Trades = 0;
for(int index=0;index<OrdersHistoryTotal();index++)
{
OrderSelect(index,SELECT_BY_POS,MODE_HISTORY);
if(OrderSymbol()==Symbol())
{
if(OrderProfit()>=0)
{
CountWins++;
}
EA_Executed_Trades++;
}
}
if(EA_Executed_Trades == 0) // There was no trade done by this EA, use the manual input value
{
return(mWinRate);
}
if(CountWins > 0)
{
return(MathRound(CountWins/EA_Executed_Trades));
}
else
{
Print("Real Time WinRate not Available");
return (CountWins); // this will return 0.
}
}
// AvgWin
double AvgWin()
{
double CountPips = 0;
int EA_Executed_Trades = 0;
for(int index=0;index<OrdersHistoryTotal();index++) // changed OrdersTotal to OrdersHistoryTotal
{
OrderSelect(index,SELECT_BY_POS,MODE_HISTORY);
if(OrderSymbol()==Symbol() && OrderType()<2) // OrderType()<2 means skip pending orders (case : OrderProfit() = 0)
{
if(OrderProfit()>=0)
{
CountPips += OrderProfit();
}
EA_Executed_Trades++;
}
}
if(EA_Executed_Trades == 0) // There was no trade done by this EA, use the manual input value
{
return(mAvgWin);
}
if(CountPips > 0)
{
return(MathRound(CountPips/EA_Executed_Trades));
}
else
{
Print("Real Time AvgWin not Available");
return (CountPips); // this will return 0.
}
}
// AvgLoss
double AvgLoss()
{
double CountPips = 0;
int EA_Executed_Trades = 0;
for(int index=0;index<OrdersHistoryTotal();index++) // changed OrdersTotal to OrdersHistoryTotal
{
OrderSelect(index,SELECT_BY_POS,MODE_HISTORY);
if(OrderSymbol()==Symbol() && OrderType()<2) // OrderType()<2 means skip pending orders (case : OrderProfit() = 0)
{
if(OrderProfit()<0)
{
CountPips += OrderProfit();
}
EA_Executed_Trades++;
}
}
if(EA_Executed_Trades == 0) // There was no trade done by this EA, use the manual input value
{
return(mAvgLoss);
}
if(CountPips < 0)
{
return(MathRound(CountPips/EA_Executed_Trades));
}
else
{
Print("Real Time AvgLoss not Available");
return (CountPips); // this will return 0.
}
}