extern double MaximumRisk =0.02; //%account balance to risk per position
extern double DecreaseFactor =3; //lot size divisor(reducer) during loss streak
extern double Lot.Margin =50; //Margin for 1 lot
double LotsOptimized()
{
double lot;
int orders=HistoryTotal();
int losses=0;
lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/Lot.Margin,2);
if(DecreaseFactor>0)
{
for(int i=orders-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
{ Print("Error in history!"); break; }
if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
if(OrderProfit()>0) break;
if(OrderProfit()<0) losses++; }
if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,2); }
if(lot<0.01) lot=0.01;
return(lot); }//end LotsOptimized
发表评论