Download MetaTrader 5
To add comments, please log in or register
Sign up for MQL5.community to post your Forum topic
JimFX
9
JimFX 2014.02.08 06:57 

I have been with Oanda and using FXTrade platform for entering a trade, and MT4 just for analyzing the charts. So over the years I have not had to work with lot sizes due to the fact Oanda works with units - and I enter the exact amount of units for my position to equal 2% risk. However, now I am trying out a few ECN brokers that deal with lots and I am just trying to adjust. I found this indicator called "lot size calculator" which is very simple - just enter your stop loss and risk and it calculates the position size.

Lot Size Calc

I like it very much, but I would like multiple instances of it on the chart at once so I can see how many lots I need for a particular stop loss. For instance I would like to have it on the chart 3 times for SLs of 20, 50, and 80 pips so I can just instantly see how many lots I need. The issue is when I add another instance of the indicator it makes the first one disappear.

I have no experience with code, but I did some searching and could not come up with a fix. I even tried to add multiple stop losses all in on go using buffers, but could not get it to work. I did add X and Y co-ordinates to it, as well as unit conversion so I could compare it to Oanda for accuracy. Would someone be kind enough to either just make it so it won't disappear after adding it a second time, or add multiple inputs for stop losses so the indicator just has to be added once on the chart. Any help appreciated!

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

#property indicator_chart_window

extern double RiskPercent = 2;
extern double StopLoss    = 30;
extern int xDistance=10;
extern int yDistance=20;

//
//
//
//
//

int init()   { 

   ObjectCreate("Lot Size Calc", OBJ_LABEL, 0, 0, 0);

   ObjectSet("Lot Size Calc", OBJPROP_XDISTANCE, xDistance);
   ObjectSet("Lot Size Calc", OBJPROP_YDISTANCE, yDistance);

return(0); }


int deinit() { return(0); }
int start()  
{
   double pipValue = MarketInfo(Symbol(),MODE_TICKVALUE); if (Digits==3 || Digits==5) pipValue *= 10;
   double step     = MarketInfo(Symbol(),MODE_LOTSTEP);
      int norm     = 0;
            if (step==1)    norm = 0;
            if (step==0.1)  norm = 1;
            if (step==0.01) norm = 2;
   double minLot = MarketInfo(Symbol(),MODE_MINLOT);
   double maxLot = MarketInfo(Symbol(),MODE_MAXLOT);
   double units  = (AccountBalance()*(RiskPercent)/(StopLoss*pipValue))*1000;
   double lots   = AccountBalance()*(RiskPercent/100.0)/(StopLoss*pipValue);
          lots   = NormalizeDouble(lots,norm);
          
          //
          //
          //
          //
          //
          
          double actualRisk; 
          string comment =DoubleToStr(StopLoss,0)+"SL - "+DoubleToStr(RiskPercent,0)+"% Risk - "+DoubleToStr(units,0)+"Units - "+DoubleToStr(lots,norm)+"Lots";
          ObjectSetText("Lot Size Calc",comment, 9, "Arial", White);
          
   return(0);
}
Simon Gniadkowski
Moderator
17962
Simon Gniadkowski 2014.02.08 09:46  
Jimfx:

I have been with Oanda and using FXTrade platform for entering a trade, and MT4 just for analyzing the charts. So over the years I have not had to work with lot sizes due to the fact Oanda works with units - and I enter the exact amount of units for my position to equal 2% risk. However, now I am trying out a few ECN brokers that deal with lots and I am just trying to adjust. I found this indicator called "lot size calculator" which is very simple - just enter your stop loss and risk and it calculates the position size.

I like it very much, but I would like multiple instances of it on the chart at once so I can see how many lots I need for a particular stop loss. For instance I would like to have it on the chart 3 times for SLs of 20, 50, and 80 pips so I can just instantly see how many lots I need. The issue is when I add another instance of the indicator it makes the first one disappear.

I have no experience with code, but I did some searching and could not come up with a fix. I even tried to add multiple stop losses all in on go using buffers, but could not get it to work. I did add X and Y co-ordinates to it, as well as unit conversion so I could compare it to Oanda for accuracy. Would someone be kind enough to either just make it so it won't disappear after adding it a second time, or add multiple inputs for stop losses so the indicator just has to be added once on the chart. Any help appreciated!

It creates a line with a fixed name, so the second instance will try to do the same thing . . . you need to edit the code to add value to distinguish the instances from each other and add this value to the Object names.
/
To add comments, please log in or register