Help with Text Indicator

Back to topics list To post a new topic, please log in or register
avatar
45
tparimore 2015.01.24 17:01
 

Could someone kindly look at my code and tell me why I must refresh my screen in order to get my Lot_Size indicator to display? I do not have the same issue with any of my other text writing indicators and they all follow the same construction. The only difference is they are based of chart data and Lot_Size is based on Broker data. Regards  ----Tom

         Screen Shot on start of MT4                             Screen Shot after Time frame is changed        

             

 

//+------------------------------------------------------------------+
//|                                                     Lot_Size.mq4 |
//|                                     Copyright 2014, Tom Parimore |
//|                                              tparimore@yahoo.com |
//+------------------------------------------------------------------+
#property copyright "Tom Parimore"
#property link      "tparimore@yahoo.com"

// Last Updated  1/24/15  10:16 AM

   extern double Stop_Loss    = 10;  // pips
   extern double Percent_Risk = 3.0;
   extern string  xxxxxxxxxxxx  = "**************************************************************";
   extern int Corner = 1;
   extern int XDISTANCE1 = 25;
   extern int YDISTANCE  = 75;
   
   double ml, ls, dblPoints, fm, mr, sp, ab, al, dpl, cppl, rkd, cpp;    

#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  { 
    if (Digits == 3 || Digits == 5) dblPoints = Point * 10;       // Digits - Number of digits after decimal point for the current symbol prices
    else dblPoints = Point;    

    return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
    ObjectDelete("StopLoss");
    ObjectDelete("MaxLotSize");
    ObjectDelete("LotSize");
    ObjectDelete("RiskDollars");
    ObjectDelete("RiskPercent");
    ObjectDelete("DollarPerPIP");
    
    return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
    string Text[6];                     // Declaring a string array
    color  Color[1];                    // Declaring an array of colors      
                     
    Color[0]= Yellow;                   // Object color ..


    ObjectCreate("StopLoss", OBJ_LABEL, 0, 0, 0);                // Creating obj.
    ObjectSet("StopLoss", OBJPROP_CORNER, Corner);               // Reference corner
    ObjectSet("StopLoss", OBJPROP_XDISTANCE, XDISTANCE1);        // X coordinate
    ObjectSet("StopLoss", OBJPROP_YDISTANCE, YDISTANCE);         // Y coordinate

    ObjectCreate("MaxLotSize", OBJ_LABEL, 0, 0, 0);              // Creating obj.
    ObjectSet("MaxLotSize", OBJPROP_CORNER, Corner);             // Reference corner
    ObjectSet("MaxLotSize", OBJPROP_XDISTANCE, XDISTANCE1);      // X coordinate
    ObjectSet("MaxLotSize", OBJPROP_YDISTANCE, YDISTANCE+20);    // Y coordinate
   
    ObjectCreate("LotSize", OBJ_LABEL, 0, 0, 0);                 // Creating obj.
    ObjectSet("LotSize", OBJPROP_CORNER, Corner);                // Reference corner
    ObjectSet("LotSize", OBJPROP_XDISTANCE, XDISTANCE1);         // X coordinate
    ObjectSet("LotSize", OBJPROP_YDISTANCE, YDISTANCE+40);       // Y coordinate
    
    ObjectCreate("RiskDollars", OBJ_LABEL, 0, 0, 0);             // Creating obj.
    ObjectSet("RiskDollars", OBJPROP_CORNER, Corner);            // Reference corner
    ObjectSet("RiskDollars", OBJPROP_XDISTANCE, XDISTANCE1);     // X coordinate
    ObjectSet("RiskDollars", OBJPROP_YDISTANCE, YDISTANCE+60);   // Y coordinate
    
    ObjectCreate("RiskPercent", OBJ_LABEL, 0, 0, 0);             // Creating obj.
    ObjectSet("RiskPercent", OBJPROP_CORNER, Corner);            // Reference corner
    ObjectSet("RiskPercent", OBJPROP_XDISTANCE, XDISTANCE1);     // X coordinate
    ObjectSet("RiskPercent", OBJPROP_YDISTANCE, YDISTANCE+80);   // Y coordinate
    
    ObjectCreate("DollarPerPIP", OBJ_LABEL, 0, 0, 0);            // Creating obj.
    ObjectSet("DollarPerPIP", OBJPROP_CORNER, Corner);           // Reference corner
    ObjectSet("DollarPerPIP", OBJPROP_XDISTANCE, XDISTANCE1);    // X coordinate
    ObjectSet("DollarPerPIP", OBJPROP_YDISTANCE, YDISTANCE+100); // Y coordinate   
                 
    
    fm   = AccountFreeMargin();
    mr   = AccountStopoutLevel();
    sp   = MarketInfo(Symbol(), MODE_SPREAD)/10;                       // spread
    ab   = AccountBalance();                                           // account balance
    al   = AccountLeverage();                                          // account leaverage                     
    dpl  = MarketInfo(Symbol(), MODE_LOTSIZE);                         // Dollars per Lot
    cppl = MarketInfo(Symbol(), MODE_TICKVALUE);                       // cost per pip per lot
    
    
    rkd = ab* Percent_Risk / 100;                                         // dollar risked
    
    if (Digits == 5) ls  =  rkd / ( Stop_Loss * dblPoints * dpl * cppl );         // Lot Size
    if (Digits == 3) ls  =  rkd / ( Stop_Loss * dblPoints * dpl * cppl ) *100;
                                        
    cpp = cppl * ls * 10;                                                // cost per pip
    
    
    
    
    if (Digits == 5) ml =  al*(fm-mr)/(Ask*dpl + al*cppl*Stop_Loss);         // maximum lots
    if (Digits == 3) ml =  al*(fm-mr)/(Ask*dpl/100 + al*cppl*Stop_Loss);     // maximum lots

    
    Text[0]=  "STOP LOSS (PIPS): " + DoubleToStr(Stop_Loss,2);
    Text[1] = "MAX LOT SIZE:          " +  DoubleToStr(ml,2);
    Text[2] = "LOT SIZE:                  " + DoubleToStr( ls,2);
    Text[3] = "RISK ($):                $" + DoubleToStr(rkd,2);
    Text[4] = "RISK (%):               " + DoubleToStr(Percent_Risk,2) + "%";
    Text[5] = "DOLLARS / PIP:      $" + DoubleToStr(cpp,2) ;

   

             
    ObjectSetText("StopLoss",     Text[0],10,"Arial",Color[0]);
    ObjectSetText("MaxLotSize",   Text[1],10,"Arial",Color[0]);
    ObjectSetText("LotSize",      Text[2],10,"Arial",Color[0]);
    ObjectSetText("RiskDollars",  Text[3],10,"Arial",Color[0]);
    ObjectSetText("RiskPercent",  Text[4],10,"Arial",Color[0]);
    ObjectSetText("DollarPerPIP", Text[5],10,"Arial",Color[0]);
    

    
   
   return(0);
  }
//+------------------------------------------------------------------+

 

avatar
2017
gooly 2015.01.24 18:01 #
 

Your objects are created and set again and again on each tick: Check the return values of your object functions and print out the error if there is any.

avatar
56
WDholic 2015.01.24 18:21 #
 

hey.. try this.. and restart your MT4

//+------------------------------------------------------------------+
//|                                                     Lot_Size.mq4 |
//|                                     Copyright 2014, Tom Parimore |
//|                                              tparimore@yahoo.com |
//+------------------------------------------------------------------+
#property copyright "Tom Parimore"
#property link      "tparimore@yahoo.com"

// Last Updated  1/24/15  10:16 AM

   extern double Stop_Loss    = 10;  // pips
   extern double Percent_Risk = 3.0;
   extern string  xxxxxxxxxxxx  = "**************************************************************";
   extern int Corner = 1;
   extern int XDISTANCE1 = 25;
   extern int YDISTANCE  = 75;
   
   double ml, ls, dblPoints, fm, mr, sp, ab, al, dpl, cppl, rkd, cpp;    

#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  { 
    if (Digits == 3 || Digits == 5) dblPoints = Point * 10;       // Digits - Number of digits after decimal point for the current symbol prices
    else dblPoints = Point;    

    return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
    ObjectDelete("StopLoss");
    ObjectDelete("MaxLotSize");
    ObjectDelete("LotSize");
    ObjectDelete("RiskDollars");
    ObjectDelete("RiskPercent");
    ObjectDelete("DollarPerPIP");
    
    return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {                
    dpl  = MarketInfo(Symbol(), MODE_LOTSIZE);                         // Dollars per Lot
    cppl = MarketInfo(Symbol(), MODE_TICKVALUE);                       // cost per pip per lot
    Alert(dpl,"  ",cppl);
    if (dpl==0||cppl==0)return(0);
    
    string Text[6];                     // Declaring a string array
    color  Color[1];                    // Declaring an array of colors      
                     
    Color[0]= Yellow;                   // Object color ..


    ObjectCreate("StopLoss", OBJ_LABEL, 0, 0, 0);                // Creating obj.
    ObjectSet("StopLoss", OBJPROP_CORNER, Corner);               // Reference corner
    ObjectSet("StopLoss", OBJPROP_XDISTANCE, XDISTANCE1);        // X coordinate
    ObjectSet("StopLoss", OBJPROP_YDISTANCE, YDISTANCE);         // Y coordinate

    ObjectCreate("MaxLotSize", OBJ_LABEL, 0, 0, 0);              // Creating obj.
    ObjectSet("MaxLotSize", OBJPROP_CORNER, Corner);             // Reference corner
    ObjectSet("MaxLotSize", OBJPROP_XDISTANCE, XDISTANCE1);      // X coordinate
    ObjectSet("MaxLotSize", OBJPROP_YDISTANCE, YDISTANCE+20);    // Y coordinate
   
    ObjectCreate("LotSize", OBJ_LABEL, 0, 0, 0);                 // Creating obj.
    ObjectSet("LotSize", OBJPROP_CORNER, Corner);                // Reference corner
    ObjectSet("LotSize", OBJPROP_XDISTANCE, XDISTANCE1);         // X coordinate
    ObjectSet("LotSize", OBJPROP_YDISTANCE, YDISTANCE+40);       // Y coordinate
    
    ObjectCreate("RiskDollars", OBJ_LABEL, 0, 0, 0);             // Creating obj.
    ObjectSet("RiskDollars", OBJPROP_CORNER, Corner);            // Reference corner
    ObjectSet("RiskDollars", OBJPROP_XDISTANCE, XDISTANCE1);     // X coordinate
    ObjectSet("RiskDollars", OBJPROP_YDISTANCE, YDISTANCE+60);   // Y coordinate
    
    ObjectCreate("RiskPercent", OBJ_LABEL, 0, 0, 0);             // Creating obj.
    ObjectSet("RiskPercent", OBJPROP_CORNER, Corner);            // Reference corner
    ObjectSet("RiskPercent", OBJPROP_XDISTANCE, XDISTANCE1);     // X coordinate
    ObjectSet("RiskPercent", OBJPROP_YDISTANCE, YDISTANCE+80);   // Y coordinate
    
    ObjectCreate("DollarPerPIP", OBJ_LABEL, 0, 0, 0);            // Creating obj.
    ObjectSet("DollarPerPIP", OBJPROP_CORNER, Corner);           // Reference corner
    ObjectSet("DollarPerPIP", OBJPROP_XDISTANCE, XDISTANCE1);    // X coordinate
    ObjectSet("DollarPerPIP", OBJPROP_YDISTANCE, YDISTANCE+100); // Y coordinate   
                 
    
    fm   = AccountFreeMargin();
    mr   = AccountStopoutLevel();
    sp   = MarketInfo(Symbol(), MODE_SPREAD)/10;                       // spread
    ab   = AccountBalance();                                           // account balance
    al   = AccountLeverage();                                          // account leaverage      
    
    rkd = ab* Percent_Risk / 100;                                         // dollar risked
    
    if (Digits == 5) ls  =  rkd / ( Stop_Loss * dblPoints * dpl * cppl );         // Lot Size
    if (Digits == 3) ls  =  rkd / ( Stop_Loss * dblPoints * dpl * cppl ) *100;
                                        
    cpp = cppl * ls * 10;                                                // cost per pip
    
    
    
    
    if (Digits == 5) ml =  al*(fm-mr)/(Ask*dpl + al*cppl*Stop_Loss);         // maximum lots
    if (Digits == 3) ml =  al*(fm-mr)/(Ask*dpl/100 + al*cppl*Stop_Loss);     // maximum lots

    
    Text[0]=  "STOP LOSS (PIPS): " + DoubleToStr(Stop_Loss,2);
    Text[1] = "MAX LOT SIZE:          " +  DoubleToStr(ml,2);
    Text[2] = "LOT SIZE:                  " + DoubleToStr( ls,2);
    Text[3] = "RISK ($):                $" + DoubleToStr(rkd,2);
    Text[4] = "RISK (%):               " + DoubleToStr(Percent_Risk,2) + "%";
    Text[5] = "DOLLARS / PIP:      $" + DoubleToStr(cpp,2) ;

   

             
    ObjectSetText("StopLoss",     Text[0],10,"Arial",Color[0]);
    ObjectSetText("MaxLotSize",   Text[1],10,"Arial",Color[0]);
    ObjectSetText("LotSize",      Text[2],10,"Arial",Color[0]);
    ObjectSetText("RiskDollars",  Text[3],10,"Arial",Color[0]);
    ObjectSetText("RiskPercent",  Text[4],10,"Arial",Color[0]);
    ObjectSetText("DollarPerPIP", Text[5],10,"Arial",Color[0]);
    

    
   
   return(0);
  }
//+------------------------------------------------------------------+

 

avatar
45
tparimore 2015.01.24 18:58 #
 
WDholic:

hey.. try this.. and restart your MT4

 

Thanks for you help. Yes, that worked. But, adding the alert maybe a little more trouble. I get an alert for each pair and an alert every time i change time frames. Currently the way I handle the issue is to change to a blank default profile and back to my trading profile. This is done once at the start of trading then everything chart works fine. Thanks again for you input.
avatar
45
tparimore 2015.01.24 19:16 #
 
gooly:

Your objects are created and set again and again on each tick: Check the return values of your object functions and print out the error if there is any.

Yes, I debated on updating the text on a new bar or every tick. I chose every tick because I envisioned the possibility of wanting to enter a second trade right after open one trade. I want to know the new max lots available right away.  It might not be the right choice, but my computer doesn't seem to have an issue with the extra load. Thanks for your input.
avatar
56
WDholic 2015.01.24 19:35 #
 
tparimore:
Thanks for you help. Yes, that worked. But, adding the alert maybe a little more trouble. I get an alert for each pair and an alert every time i change time frames. Currently the way I handle the issue is to change to a blank default profile and back to my trading profile. This is done once at the start of trading then everything chart works fine. Thanks again for you input.

simply just delete that alert.  

i give an alert to show you  what causing error  ( ZERO DEVIDE ). 

avatar
45
tparimore 2015.01.24 23:45 #
 
WDholic:

simply just delete that alert.  

i give an alert to show you  what causing error  ( ZERO DEVIDE ). 

Duh, I am so slow some times. Thank you so much. That works great.   ----Tom
Back to topics list  

To add comments, please log in or register