Author: Copyright © 2017, Андрей Мишустин
0 Views
0 Downloads
0 Favorites
MAX_LOT
ÿþ//+------------------------------------------------------------------+

//|                                                      MAX_LOT.mq5 |

//|                                Copyright © 2017, =4@59 8HCAB8= | 

//|                                                                  | 

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

#property copyright "Copyright © 2017, =4@59 8HCAB8="

#property link "" 

//---- =><5@ 25@A88 8=48:0B>@0

#property version   "1.00"

#property description "!:@8?B 45;05B @0AGQB <0:A8<0;L=>9 25;8G8=K ;>B0, :>B>@K9 <>6=> 8A?>;L7>20BL 4;O >B:@K20=8O ?>78F88 =0 2AN 25;8G8=C A2>1>4=KE A@54AB2 45?>78B0."

#property description " 0AG8B0==>5 7=0G5=85 >B>1@0605BAO ?> C<>;G0=8N 2 B5G5=85 10 A5:C=4 2 ?@02>< 25@E=5< C3;C 3@0D8:0."

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

//|  1JO2;5=85 :>=AB0=B                         |

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

#define NAMES_SYMBOLS_FONT  "Georgia"                  // (@8DB 4;O 8=48:0B>@0

//---- ?>:07K20BL 2E>4=K5 ?0@0<5B@K

#property script_show_inputs

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

//| %+  " + ! "                    |

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

input ENUM_POSITION_TYPE PosType=POSITION_TYPE_BUY;    // "8? ?>78F88

//---- =0AB@>9:8 287C0;L=>3> >B>1@065=8O 8=48:0B>@0

input string Symbols_Sirname="MAX_LOT_Label_";         // 0720=85 4;O <5B>: 8=48:0B>@0

input color IndName_Color=clrMediumSlateBlue;          // &25B 8=48:0B>@0

input uint Font_Size=15;                               //  07<5@ H@8DB0 8=48:0B>@0

input uint X_=15;                                      // !<5I5=85 ?> 3>@87>=B0;8

input int Y_=30;                                       // !<5I5=85 ?> 25@B8:0;8

input ENUM_BASE_CORNER  WhatCorner=CORNER_RIGHT_UPPER; // #3>; @0A?>;>65=8O

input uint INFOTIME=10;                                // @5<O >B>1@065=8O 8=D>@<0F88 2 A5:C=40E

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

//| start function                                                   |

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

void OnStart()

  {

//----

   double MaxLot=LotCount(Symbol(),PosType,1.0);

   string strMaxLot;

   if(MaxLot>0) strMaxLot="0:A8<0;L=K9 ;>B 4;O >B:@K20=8O ?>78F88 "+DoubleToString(MaxLot,4);

   else strMaxLot="5B A2>1>4=KE A@54AB2 4;O >B:@K20=8O ?>78F88!!!";

   SetTLabel(0,Symbols_Sirname,0,WhatCorner,ENUM_ANCHOR_POINT(2*WhatCorner),X_,Y_,strMaxLot,strMaxLot,IndName_Color,NAMES_SYMBOLS_FONT,Font_Size);

   ChartRedraw(0);

   Sleep(INFOTIME*1000);

   ObjectDelete(0,Symbols_Sirname);

   ChartRedraw(0);

//----

  }

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

//|  0AGQB @07<5@0 ;>B0 4;O >B:@K20=8O ;>=30                         |  

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

double LotCount

(

 string symbol,

 ENUM_POSITION_TYPE postype,

 double Money_Management

 )

// (string symbol, double Money_Management)

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

  {

//----

   double margin,Lot;



//----  0AGQB ;>B0 >B A2>1>4=KE A@54AB2 =0 AGQB5

   margin=AccountInfoDouble(ACCOUNT_MARGIN_FREE)*Money_Management;

   if(!margin) return(-1);



   Lot=GetLotForOpeningPos(symbol,postype,margin);



//---- =>@<8@>20=85 25;8G8=K ;>B0 4> 1;8609H53> AB0=40@B=>3> 7=0G5=8O 

   if(!LotCorrect(symbol,Lot,postype)) return(-1);

//----

   return(Lot);

  }

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

//| :>@@5:F8O @07<5@0 >B;>65==>3> >@45@0 4> 4>?CAB8<>3> 7=0G5=8O     |

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

bool StopCorrect(string symbol,int &Stop)

  {

//----

   long Extrem_Stop;

   if(!SymbolInfoInteger(symbol,SYMBOL_TRADE_STOPS_LEVEL,Extrem_Stop)) return(false);

   if(Stop<Extrem_Stop) Stop=int(Extrem_Stop);

//----

   return(true);

  }

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

//| LotCorrect() function                                            |

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

bool LotCorrect

(

 string symbol,

 double &Lot,

 ENUM_POSITION_TYPE trade_operation

 )

//LotCorrect(string symbol, double& Lot, ENUM_POSITION_TYPE trade_operation)

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

  {



   double LOTSTEP=SymbolInfoDouble(symbol,SYMBOL_VOLUME_STEP);

   double MaxLot=SymbolInfoDouble(symbol,SYMBOL_VOLUME_MAX);

   double MinLot=SymbolInfoDouble(symbol,SYMBOL_VOLUME_MIN);

   if(!LOTSTEP || !MaxLot || !MinLot) return(0);



//---- =>@<8@>20=85 25;8G8=K ;>B0 4> 1;8609H53> AB0=40@B=>3> 7=0G5=8O 

   Lot=LOTSTEP*MathFloor(Lot/LOTSTEP);



//---- ?@>25@:0 ;>B0 =0 <8=8<0;L=>5 4>?CAB8<>5 7=0G5=85

   if(Lot<MinLot) Lot=MinLot;

//---- ?@>25@:0 ;>B0 =0 <0:A8<0;L=>5 4>?CAB8<>5 7=0G5=85       

   if(Lot>MaxLot) Lot=MaxLot;



//---- ?@>25@:0 A@54AB2 =0 4>AB0B>G=>ABL

   if(!LotFreeMarginCorrect(symbol,Lot,trade_operation))return(false);

//----

   return(true);

  }

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

//| LotFreeMarginCorrect() function                                  |

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

bool LotFreeMarginCorrect

(

 string symbol,

 double &Lot,

 ENUM_POSITION_TYPE trade_operation

 )

//(string symbol, double& Lot, ENUM_POSITION_TYPE trade_operation)

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

  {

//----  

//---- ?@>25@:0 A@54AB2 =0 4>AB0B>G=>ABL

   double freemargin=AccountInfoDouble(ACCOUNT_FREEMARGIN);

   if(freemargin<=0) return(false);

   double LOTSTEP=SymbolInfoDouble(symbol,SYMBOL_VOLUME_STEP);

   double MinLot=SymbolInfoDouble(symbol,SYMBOL_VOLUME_MIN);

   if(!LOTSTEP || !MinLot) return(0);

   double maxLot=GetLotForOpeningPos(symbol,trade_operation,freemargin);

//---- =>@<8@>20=85 25;8G8=K ;>B0 4> 1;8609H53> AB0=40@B=>3> 7=0G5=8O 

   maxLot=LOTSTEP*MathFloor(maxLot/LOTSTEP);

   if(maxLot<MinLot) return(false);

   if(Lot>maxLot) Lot=maxLot;

//----

   return(true);

  }

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

//| @0AGQB @07<5@ ;>B0 4;O >B:@K20=8O ?>78F88 A <0@6>9 lot_margin    |

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

double GetLotForOpeningPos(string symbol,ENUM_POSITION_TYPE direction,double lot_margin)

  {

//----

   double price=0.0,n_margin;

   if(direction==POSITION_TYPE_BUY)  price=SymbolInfoDouble(symbol,SYMBOL_ASK);

   if(direction==POSITION_TYPE_SELL) price=SymbolInfoDouble(symbol,SYMBOL_BID);

   if(!price) return(NULL);



   if(!OrderCalcMargin(ENUM_ORDER_TYPE(direction),symbol,1,price,n_margin) || !n_margin) return(0);

   double lot=lot_margin/n_margin;



//---- ?>;CG5=85 B>@3>2KE :>=AB0=B

   double LOTSTEP=SymbolInfoDouble(symbol,SYMBOL_VOLUME_STEP);

   double MaxLot=SymbolInfoDouble(symbol,SYMBOL_VOLUME_MAX);

   double MinLot=SymbolInfoDouble(symbol,SYMBOL_VOLUME_MIN);

   if(!LOTSTEP || !MaxLot || !MinLot) return(0);



//---- =>@<8@>20=85 25;8G8=K ;>B0 4> 1;8609H53> AB0=40@B=>3> 7=0G5=8O 

   lot=LOTSTEP*MathFloor(lot/LOTSTEP);



//---- ?@>25@:0 ;>B0 =0 <8=8<0;L=>5 4>?CAB8<>5 7=0G5=85

   if(lot<MinLot) lot=0;

//---- ?@>25@:0 ;>B0 =0 <0:A8<0;L=>5 4>?CAB8<>5 7=0G5=85       

   if(lot>MaxLot) lot=MaxLot;

//----

   return(lot);

  }

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

//|  !>740=85 B5:AB>2>9 <5B:8                                        |

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

void CreateTLabel(long   chart_id,         // 845=B8D8:0B>@ 3@0D8:0

                  string name,             // 8<O >1J5:B0

                  int    nwin,             // 8=45:A >:=0

                  ENUM_BASE_CORNER corner, // ?>;>65=85 C3;0 ?@82O7:8

                  ENUM_ANCHOR_POINT point, // ?>;>65=85 B>G:8 ?@82O7:8

                  int    X,                // 48AB0=F8O 2 ?8:A5;OE ?> >A8 X >B C3;0 ?@82O7:8

                  int    Y,                // 48AB0=F8O 2 ?8:A5;OE ?> >A8 Y >B C3;0 ?@82O7:8

                  string text,             // B5:AB

                  string textTT,           // B5:AB 2A?;K20NI59 ?>4A:07:8

                  color  Color,            // F25B B5:AB0

                  string Font,             // H@8DB B5:AB0

                  int    Size)             // @07<5@ H@8DB0

  {

//----

   ObjectCreate(chart_id,name,OBJ_LABEL,0,0,0);

   ObjectSetInteger(chart_id,name,OBJPROP_CORNER,corner);

   ObjectSetInteger(chart_id,name,OBJPROP_ANCHOR,point);

   ObjectSetInteger(chart_id,name,OBJPROP_XDISTANCE,X);

   ObjectSetInteger(chart_id,name,OBJPROP_YDISTANCE,Y);

   ObjectSetString(chart_id,name,OBJPROP_TEXT,text);

   ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color);

   ObjectSetString(chart_id,name,OBJPROP_FONT,Font);

   ObjectSetInteger(chart_id,name,OBJPROP_FONTSIZE,Size);

   ObjectSetString(chart_id,name,OBJPROP_TOOLTIP,textTT);

   ObjectSetInteger(chart_id,name,OBJPROP_BACK,true); //>1J5:B =0 704=5< ?;0=5

//----

  }

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

//|  5@5CAB0=>2:0 B5:AB>2>9 <5B:8                                   |

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

void SetTLabel(long   chart_id,         // 845=B8D8:0B>@ 3@0D8:0

               string name,             // 8<O >1J5:B0

               int    nwin,             // 8=45:A >:=0

               ENUM_BASE_CORNER corner, // ?>;>65=85 C3;0 ?@82O7:8

               ENUM_ANCHOR_POINT point, // ?>;>65=85 B>G:8 ?@82O7:8

               int    X,                // 48AB0=F8O 2 ?8:A5;OE ?> >A8 X >B C3;0 ?@82O7:8

               int    Y,                // 48AB0=F8O 2 ?8:A5;OE ?> >A8 Y >B C3;0 ?@82O7:8

               string text,             // B5:AB

               string textTT,           // B5:AB 2A?;K20NI59 ?>4A:07:8

               color  Color,            // F25B B5:AB0

               string Font,             // H@8DB B5:AB0

               int    Size)             // @07<5@ H@8DB0

  {

//----

   if(ObjectFind(chart_id,name)==-1)

     {

      CreateTLabel(chart_id,name,nwin,corner,point,X,Y,text,textTT,Color,Font,Size);

     }

   else

     {

      ObjectSetString(chart_id,name,OBJPROP_TEXT,text);

      ObjectSetInteger(chart_id,name,OBJPROP_XDISTANCE,X);

      ObjectSetInteger(chart_id,name,OBJPROP_YDISTANCE,Y);

      ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color);

      ObjectSetInteger(chart_id,name,OBJPROP_FONTSIZE,Size);

     }

//----

  }

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

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