SLAndLots





//+------------------------------------------------------------------+
//|                                                    SLAndLots.mq4 |
//|                                           Copyright © 2009, DKeN |
//|                                              http://www.dken.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, DKeN"
#property link      "http://www.dken.biz"

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 0.1
extern double cfg_iRiskPercent=1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
  InitAll();
      
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   if(ObjectFind("SLAndLots_SL")!=-1) ObjectDelete("SLAndLots_SL");
   if(ObjectFind("SLAndLots_Open")!=-1) ObjectDelete("SLAndLots_Open");
   if(ObjectFind("SLAndLots_Comment")!=-1) ObjectDelete("SLAndLots_Comment");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   double sl=0,open=0;
//----
   if(ObjectFind("SLAndLots_SL")!=-1){
      sl=ObjectGet("SLAndLots_SL",OBJPROP_PRICE1);
   }else{
      if(ObjectCreate("SLAndLots_SL",OBJ_HLINE,0,0,Bid-500*Point)){
      ObjectSet("SLAndLots_SL",OBJPROP_STYLE,STYLE_DOT);
      ObjectSet("SLAndLots_SL",OBJPROP_COLOR,Red);
      }
   }
   
   if(ObjectFind("SLAndLots_Open")!=-1){
      open=ObjectGet("SLAndLots_Open",OBJPROP_PRICE1);
     
   }else{
      if(ObjectCreate("SLAndLots_Open",OBJ_HLINE,0,0,Ask)){
      ObjectSet("SLAndLots_Open",OBJPROP_STYLE,STYLE_DOT);
      ObjectSet("SLAndLots_Open",OBJPROP_COLOR,Blue);
      }
    
   }
   if(ObjectFind("SLAndLots_Comment")==-1)
   if(ObjectCreate("SLAndLots_Comment",OBJ_LABEL,WindowFind("SLAndLots"),0,Bid)){
      ObjectSetText("SLAndLots_Comment","Test text", 9, "Times New Roman", Red);
      }     
      
   double lot=get_lot(Symbol(),sl,open);
   //Print(sl,open);
  // Comment("SLAndLots v1.0 Risk = ",cfg_iRiskPercent," Open = ",open, " SL = ",sl, " Lot = ",lot);
   string cmt="Risk = "+DoubleToStr(cfg_iRiskPercent,2)+
   "% Open = "+DoubleToStr(open,Digits)+ 
   " SL = "+DoubleToStr(sl,Digits)+ 
   " Lot = "+DoubleToStr(lot,2);
   
   ObjectSet("SLAndLots_Comment",OBJPROP_CORNER,0);
   ObjectSet("SLAndLots_Comment",OBJPROP_YDISTANCE,5);
   ObjectSetText("SLAndLots_Comment",cmt, 9, "Times New Roman", Red);
//----
   return(0);
  }
 
 void InitAll(){
 
  if(ObjectCreate("SLAndLots_Open",OBJ_HLINE,0,0,Ask)){
      ObjectSet("SLAndLots_Open",OBJPROP_STYLE,STYLE_DOT);
      ObjectSet("SLAndLots_Open",OBJPROP_COLOR,Blue);
      Print("Open");
      }
   if(ObjectCreate("SLAndLots_SL",OBJ_HLINE,0,0,Bid-500*Point)){
      ObjectSet("SLAndLots_SL",OBJPROP_STYLE,STYLE_DOT);
      ObjectSet("SLAndLots_SL",OBJPROP_COLOR,Red);
      }     
       
 }
 
 double get_lot(string name,double stoplevel,double openlevel){
   double lot=0;
   if(stoplevel<=0 || openlevel<=0) return (0);
   
   
   int    TipInstrumenta=0;  
   double BazCours=0;
   double dCof=1;
   string SMB;   
   double stop=0;
     
 // --------- âû÷èñëÿåì êàêàÿ ïàðà (ïðÿìàÿ, îáðàòíàÿ, êðîññ) --------------
   TipInstrumenta=StringFind(name,"USD",0); 
   SMB=StringSubstr(name,0,3); //áàçîâàÿ âàëþòà
   BazCours=MarketInfo(SMB+"USD",MODE_BID);     
   double Dig10=1;
   if(MarketInfo(name,MODE_DIGITS)==5) Dig10=10;
   double dBid=MarketInfo(name,MODE_BID);
   
   if(TipInstrumenta==3){//ïàðà ïðÿìàÿ, íàïð. EurUsd
     dCof=10;
     Dig10=10;
   }
   if(TipInstrumenta==0){//ïàðà îáðàòíàÿ, íàïð. UsdJpy
     dCof=10/dBid;
     Dig10=10;
   }  
   if(TipInstrumenta==-1){//Êðîññ-ïàðà, íàïð EurJpy
     dCof=10*BazCours/dBid;
     Dig10=10;
   }  
     double dPoint=MarketInfo(name,MODE_POINT);
     
     lot=NormalizeDouble(0.001*cfg_iRiskPercent*AccountFreeMargin()*dPoint*Dig10/MathAbs(openlevel-stoplevel),1);       
     
     if(lot>MarketInfo(name,MODE_MAXLOT)) lot=MarketInfo(name,MODE_MAXLOT);
     if(lot<MarketInfo(name,MODE_MINLOT)) lot=MarketInfo(name,MODE_MINLOT);
      
     //Print(lot); 
     if(lot*100*dBid>0.8*AccountFreeMargin()) Print("LOT: ",lot," ActiveMargin > FreeMargin");
      
     return (lot);     
  }  
  
//+------------------------------------------------------------------+



Sample





Analysis



Market Information Used:



Indicator Curves created:


Indicators Used:



Custom Indicators Used:

Order Management characteristics:

Other Features: