DynamicDecreaseFactor

Orders Execution
Checks for the total of closed orders
0 Views
0 Downloads
0 Favorites
DynamicDecreaseFactor
//+----------------------------------------------------------------------------------------+
//|                                                               DynamicDecreaseFactor.mq4|
//|                                              Copyright 2014, Vadim Konyaev aka JJerboa.|
//|                                                 http://login.mql5.com/ru/users/jjerboa |
//+----------------------------------------------------------------------------------------+
#property library
#property version   "1.00"
#property strict

input double DynamicDecreaseFactor=2;
input double RiskPercent=25.0;

double Decreaz[];
//------------------------------------+


//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//-----------------------------------------------------+
// For Function OnInit() or init():                    |
// ArrayResize(Decreaz,(int)DynamicDecreaseFactor);    |
//-----------------------------------------------------+
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!




//+------------------------------------------------------------------+
//|   function  LotsOptimized()                                      |
//+------------------------------------------------------------------+
double LotsOptimized()export
  {
   double lot,profit;
   int    orders=OrdersHistoryTotal();     // history orders total
   double    losses=0;                  // number of losses orders without a break
   double steplot=MarketInfo(Symbol(),MODE_LOTSTEP);
   double minlot=MarketInfo(Symbol(),MODE_MINLOT);
   double maxlot=MarketInfo(Symbol(),MODE_MAXLOT);

//---- select lot size
   lot=NormalizeDouble(AccountFreeMargin()*RiskPercent/100/1000.0,2);
//---- calcuulate number of losses orders without a break
   if(DynamicDecreaseFactor>0)
     {
      for(int i=orders-1;i>=orders-(int)DynamicDecreaseFactor;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
         if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
         profit=OrderProfit()+OrderCommission()+OrderSwap();
         if(profit<0) losses=losses+1.0;
        }

      if(losses>0)
        {
         _arrayDec(Decreaz);
         Decreaz[0]=losses/DynamicDecreaseFactor;
         double DecFact=Mediana(Decreaz);

         lot=NormalizeDouble(lot-lot*DecFact,2);

        }
     }
//---- return lot size
   lot=NormalizeDouble(lot/steplot,0)*steplot;
   if(lot<minlot) lot=minlot;
   if(lot>maxlot) lot=maxlot;

   return(lot);
  }
//+------------------------------------------------------------------+
//|   Array for Decreaz                                              |
//+------------------------------------------------------------------+
void _arrayDec(double &a[])
  {
   int _size=ArraySize(a);
   for(int i=_size-1; i>0; i--) a[i]=a[i-1];
  }
//+------------------------------------------------------------------+
//|   function Mediana                                               |
//+------------------------------------------------------------------+
double Mediana(double &a[]) // ìåäèàíà
  {
   double res=0.0;
   int _size=ArraySize(a);
   if(_size%2==0)
     {
      res=(a[_size/2-1]+a[_size/2])/2;
     }
   else
     {
      res=a[(_size+1)/2-1];
     }
   return(res);
  }
//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---