positioncalculator

Author: Copyright 2015, MetaQuotes Software Corp.
Orders Execution
Checks for the total of open orders
Indicators Used
Indicator of the average true range
0 Views
0 Downloads
0 Favorites
positioncalculator
ÿþ//+------------------------------------------------------------------+

//|                                           PositionCalculator.mq4 |

//|                        Copyright 2015, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2015, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict

#property indicator_separate_window//oR—zãS>f:y

#property  indicator_buffers 2;

#property  indicator_color1 SeaGreen //‡eW[œ˜r‚

#property  indicator_color2 Red //f‹JT‡eW[œ˜r‚

input int ATRPeriod=14;//ATR;m¨RhTg

input int MarginRatio=10;//ÝOÁ‹Ñ‘Ôk‹O Min: 1(1%) Max 100(100%)

int windowsHandle=0;//WindowFind(WindowExpertName());//·ƒÖSS_MRch@b(W—zãS^÷S 0 ;N—zãS

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

//--- indicator buffers mapping

   IndicatorShortName("PositionCalculator("+IntegerToString(ATRPeriod)+","+IntegerToString(MarginRatio)+")");//chh˜˜

   windowsHandle=WindowFind("PositionCalculator("+IntegerToString(ATRPeriod)+","+IntegerToString(MarginRatio)+")");//·ƒÖSS_MRch@b(W—zãS^÷S 0 ;N—zãS

//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

//---

   ObjectsDeleteAll(windowsHandle,OBJ_LABEL);

  }

//+------------------------------------------------------------------+

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

//---

//ObjectsDeleteAll(myWindowsHandle,OBJ_LABEL);

   PositionCalculator();

//--- return value of prev_calculated for next call

   return(rates_total);

  }

//+------------------------------------------------------------------+

//| Timer function                                                   |

//+------------------------------------------------------------------+

void OnTimer()

  {

//---

  }

//+------------------------------------------------------------------+

//| ChartEvent function                                              |

//+------------------------------------------------------------------+

void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

  {

//---

  }

//+------------------------------------------------------------------+

//|                  PositionCalculator Function                     |

//+------------------------------------------------------------------+

/*

ýQpeÿÓNMO¡‹—{hV

*/

void PositionCalculator()

  {

//&7báOo`

   if(IsConnected())

     {

      string symbol=Symbol();

      double marginValue=MarketInfo(symbol,MODE_MARGINREQUIRED);

      double BuyPrice=MarketInfo(symbol,MODE_ASK);

      double SellPrice=MarketInfo(symbol,MODE_BID);

      double SpreadValue=MarketInfo(symbol,MODE_SPREAD);

      double SpreadPrice=MathAbs(BuyPrice-SellPrice);



      double ATRIndexValue=iATR(symbol,0,ATRPeriod,0);//ATR chû‹pe

      double Account_Equity=AccountEquity()*MarginRatio/100;//&7bDÑ‘

      double Account_FreeMargin=AccountFreeMargin()*MarginRatio/100;////Am¨RDÑ‘

      double TickValue=MarketInfo(symbol,MODE_TICKVALUE);//US¹p÷N<P

      int Account_Leverage=AccountLeverage();//`gFg

      if(Account_Leverage == 0) return;

      double myDvol=ATRIndexValue*TickValue;//Ý~ù[âlE^

      double  MinLot=MarketInfo(symbol,MODE_MINLOT);//g\¤Nfϑ

      double UnitLots=iFundsToHands(symbol,(0.01*Account_Equity*(1/Account_Leverage))/myDvol);//USMO4Yø[

      if(UnitLots<MinLot) UnitLots=MinLot;//MQ¡‹—{•ï‹

      double MaxLots=iFundsToHands(Symbol(),Account_Equity);

      //if(MaxLots<MinLot) MaxLots=MinLot;

      double MaxNum=MaxLots/UnitLots;//g'Y4Yø[peϑ

      double MaxFreeLots=iFundsToHands(Symbol(),Account_FreeMargin);

      double MaxFreeNum=MaxFreeLots/UnitLots;//g'Y4Yø[peϑ



      iDisplayInfo("symbol","S_MR'^ÿ"+symbol,0,15,20,8,"",indicator_color1);

      iDisplayInfo("marginValue","hÆQÝOÁ‹Ñ‘ÿ"+DoubleToStr(marginValue,2),0,15,35,8,"",indicator_color1);

      iDisplayInfo("SpreadValue","nm¨R¹pî]ÿ"+DoubleToStr(SpreadValue,0),0,15,50,8,"",indicator_color1);

      iDisplayInfo("SpreadPrice","pNVS¹pî]ÿ"+DoubleToStr(SpreadPrice,Digits),0,15,65,8,"",indicator_color1);

      iDisplayInfo("SpreadPrice","pNVS¹pî]ÿ"+DoubleToStr(SpreadPrice,Digits),0,15,65,8,"",indicator_color1);

      iDisplayInfo("MinLot","g\NUSϑÿ"+DoubleToStr(MinLot,2),0,15,80,8,"",indicator_color1);



      iDisplayInfo("Leverage","`gFgÔk‹Oÿ1:"+IntegerToString(Account_Leverage),0,150,20,8,"",indicator_color1);

      iDisplayInfo("TickValue","US¹p÷N<Pÿ"+DoubleToStr(TickValue,Digits),0,150,35,8,"",indicator_color1);

      iDisplayInfo("ATRIndexReadings","ATRû‹peÿ"+DoubleToStr(ATRIndexValue,Digits),0,150,50,8,"",indicator_color1);

      iDisplayInfo("Dvol","Ý~ù[âlE^ÿ"+DoubleToStr(myDvol,Digits),0,150,65,8,"",indicator_color1);

      iDisplayInfo("Unit","USMO4Yø[ÿ"+DoubleToStr(UnitLots,2),0,150,80,8,"",indicator_color1);



      iDisplayInfo("AccountEquity","&7bDÑ‘ÿ"+DoubleToStr(Account_Equity,2),0,285,20,8,"",indicator_color1);

      iDisplayInfo("AccountFreeMargin","ê1uDÑ‘ÿ"+DoubleToStr(Account_FreeMargin,2),0,285,35,8,"",indicator_color2);

      iDisplayInfo("MaxLots","g'Y4Yø[ÿ"+DoubleToStr(MaxLots,2)+"/"+DoubleToStr(MaxNum,0),0,285,50,8,"",indicator_color1);

      iDisplayInfo("newLots","ê1u4Yø[ÿ"+DoubleToStr(MaxFreeLots,2)+"/"+DoubleToStr(MaxFreeNum,0),0,285,65,8,"",indicator_color2);



      int TradeCount=OrdersTotal();//¢‹USpe

      long PositionSizeCount=0;//¢‹USϑ

      double PositionSizeTotal=0.0;//¢‹USž[ϑ

      long symbolTradeTotal=0; //'^USpe 

      double symbolPositionSize=0.00;//'^USϑ

      if(TradeCount>0)

        {

         for(int OrderIndex=0;OrderIndex<=TradeCount-1;OrderIndex++)

           {

            if(OrderSelect(OrderIndex,SELECT_BY_POS,MODE_TRADES))

              {

               string Order_Symbol=OrderSymbol();

               double Order_Lots=OrderLots();

               double  OrderSymbolMinLot=MarketInfo(Order_Symbol,MODE_MINLOT);//g\¤Nfϑ

               PositionSizeCount+=StringToInteger(DoubleToStr(Order_Lots/OrderSymbolMinLot,0));

               PositionSizeTotal+=(Order_Lots/OrderSymbolMinLot)*MinLot;

               if((Order_Symbol==symbol))

                 {

                  symbolTradeTotal++;

                  symbolPositionSize+=Order_Lots;

                 }

              }

           }

        }

      iDisplayInfo("TradeCount","¢‹US;`peÿ"+IntegerToString(TradeCount),0,420,20,8,"",indicator_color2);

      iDisplayInfo("PositionSizeTotal","¢‹US;`ϑÿ"+DoubleToStr(PositionSizeTotal,2)+"("+DoubleToStr(PositionSizeTotal*100.00/MaxLots,2)+"%)",0,420,35,8,"",indicator_color2);

      if(TradeCount>0)

        {

         iDisplayInfo("symbolTradeTotal","'^USpeÿ"+IntegerToString(symbolTradeTotal)+"("+DoubleToStr(symbolTradeTotal*100.00/TradeCount,2)+"%)",0,420,50,8,"",indicator_color2);

        }

      else

        {

         iDisplayInfo("symbolTradeTotal","'^USpeÿ"+IntegerToString(symbolTradeTotal)+"("+DoubleToStr(symbolTradeTotal*100.00/100,2)+"%)",0,420,50,8,"",indicator_color2);

        }

      iDisplayInfo("symbolPositionSize","'^USϑÿ"+DoubleToStr(symbolPositionSize,2)+"("+DoubleToStr(symbolPositionSize*100.00/MaxLots,2)+"%)",0,420,65,8,"",indicator_color2);



     }

  }

//+------------------------------------------------------------------+

//|          iDisplayInfo Function                                   |

//+------------------------------------------------------------------+

void iDisplayInfo(string LableName,string LableDoc,int LableCorner,

                  int LableX,int LableY,int DocSize,string DocStyle,color DocColor)

  {

   if(LableCorner == -1 ) return;

   LableName=LableName+DoubleToStr(windowsHandle,0);

   ObjectCreate(LableName,OBJ_LABEL,windowsHandle,0,0);//ú^ËzwShù[aŒ

   ObjectSetText(LableName,LableDoc,DocSize,DocStyle,DocColor);//š[INù[aŒ^\'`

   ObjectSet(LableName,OBJPROP_CORNER,LableCorner);//nxš[PWhŸS¹p00 æ]
N҉ 1 óS
N҉ 2 æ]N҉ 3 óSN҉ -1 
N>f:y

   ObjectSet(LableName,OBJPROP_XDISTANCE,LableX);//š[IN*jPWhÿUSMOÿÏP }

   ObjectSet(LableName,OBJPROP_YDISTANCE,LableY);//š[INµ~PWhÿUSMOÿÏP }

  }

//+------------------------------------------------------------------+

//|              iFundsToHands Function                              |

//+------------------------------------------------------------------+

/*

ýQpeÿё˜lbcKbpe

ÂSpeÿsymbol '^ù[

      priceFunds DÑ‘úWpe

*/

double iFundsToHands(string symbol,double priceFunds)

  {

//¡‹—{ïS_ÓNKbpe 1hÆQKb_ÓNê1uÝOÁ‹Ñ‘

   double openLots=priceFunds/MarketInfo(symbol,MODE_MARGINREQUIRED);

//Kbpelteb_ g\AQ¸‹hÆQKbpe

   openLots=MathFloor(openLots/MarketInfo(symbol,MODE_MINLOT))*MarketInfo(symbol,MODE_MINLOT);

   return openLots;

  }

//+------------------------------------------------------------------+

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 ---