Calculating Lots to 0.0 decimal places HELP please.

 

Hi All,

Ive been playing with the EA below (Butchered MACD Sample) I want it to place lots based on the size of the available balance, however I want it to be round up to the nearest Lot. ie. if I have £500 in the bank I want it to order 0.10 Lots so I added

Lots=AccountBalance()/10000;

the theory being £500 / 10000 = 0.05 so will give me an order of 0.1 Lots and £1600 / 10000 = 0.16 so will give me an order of 0.2 Lots etc etc. At the minute it only works on full Lots 1.0, 2.0 etc so will only start on a £10,000 balance.

Any ideas???

Ta



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

//| MACD Modified.mq4 |
//+------------------------------------------------------------------+

extern double TakeProfit = 04;
extern double StopLoss=12;
extern double MACDOpenLevel=3;
extern double MATrendPeriod=26;

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
double MacdCurrent, MacdPrevious, SignalCurrent;
double SignalPrevious, MaCurrent, MaPrevious;
int cnt, ticket, total, Lots;

if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<1)
{
Print("TakeProfit less than 1");
return(0); // check TakeProfit
}
// to simplify the coding and speed up access
// data are put into internal variables
MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);
MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);

total=OrdersTotal();
if(total<1)
{
// no opened orders identified
{
Lots=AccountBalance()/10000;
}
{
// check for long position (BUY) possibility
if(MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious<SignalPrevious &&
MathAbs(MacdCurrent)>(MACDOpenLevel*Point) && MaCurrent>MaPrevious)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-StopLoss*Point,Ask+TakeProfit*Point,"-",0,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
// check for short position (SELL) possibility
if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious &&
MacdCurrent>(MACDOpenLevel*Point) && MaCurrent<MaPrevious)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+StopLoss*Point,Bid-TakeProfit*Point,"-",0,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
}
return(0);
}}}
// the end.
 

Use MarketInfo()

You need to check stuff like Broker_Minimum Lots, Broker_Minimum Lot Step, and NormalizeDouble(Lots=AccountBalance()/10000,2)

There's quite a few Lots management codes provided on this forum. I strongly suggest you research some of them.

 
ubzen:

Use MarketInfo()

You need to check stuff like Broker_Minimum Lots, Broker_Minimum Lot Step, and NormalizeDouble(Lots=AccountBalance()/10000,2)

There's quite a few Lots management codes provided on this forum. I strongly suggest you research some of them.


Thank you. Ive had a search and Ive found a few bits and tried them but I cant seem to get anything to work. Im not a programmer just learning. Any chance anyone can help me out a bit more?
 

  1.     double  minLot  = MarketInfo(Symbol(), MODE_MINLOT),
                lotStep = MarketInfo(Symbol(), MODE_LOTSTEP),
            size = MathFloor(size/lotStep)*lotStep;
            if (size < minLot){     EA.status = status;     return(0); }

  2. OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-StopLoss*Point,Ask+TakeProfit*Point
    EAs should automatically adjust to 5 digit brokers, TP, SL, AND slippage. On a ECN broker, you must open with no TP/SL and then modify.
    //++++ These are adjusted for 5 digit brokers.
    double  pips2points,    // slippage  3 pips    3=points    30=points
            pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    

 
WHRoeder:


  1. OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-StopLoss*Point,Ask+TakeProfit*Point
    EAs should automatically adjust to 5 digit brokers, TP, SL, AND slippage. On a ECN broker, you must open with no TP/SL and then modify.


William,

Thanks for your help. Ive tried to put what you said where I think. Im not a coder I just want someone to put it there for me. Call it lazy or whatever but ive spent f******g hours trying to work it out. I cant offer much in return perhaps thats some peoples buzz.


extern double pissed off=100%



//+------------------------------------------------------------------+
//| MACD Modified.mq4 |
//+------------------------------------------------------------------+

extern double TakeProfit = 04;
extern double StopLoss=12;
extern double MACDOpenLevel=3;
extern double MATrendPeriod=26;

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
double MacdCurrent, MacdPrevious, SignalCurrent;
double SignalPrevious, MaCurrent, MaPrevious;
double minLot = MarketInfo(Symbol(), MODE_MINLOT),
lotStep = MarketInfo(Symbol(), MODE_LOTSTEP),
size = MathFloor(size/lotStep)*lotStep;
if (size < minLot)
int cnt, ticket, total, Lots;

if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<1)
{
Print("TakeProfit less than 1");
return(0); // check TakeProfit
}
// to simplify the coding and speed up access
// data are put into internal variables
MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);
MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);

total=OrdersTotal();
if(total<1)
{
// no opened orders identified
{
Lots=AccountBalance()/10000;
{
// check for long position (BUY) possibility
if(MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious<SignalPrevious &&
MathAbs(MacdCurrent)>(MACDOpenLevel*Point) && MaCurrent>MaPrevious)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-StopLoss*Point,Ask+TakeProfit*Point,"-",0,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
// check for short position (SELL) possibility
if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious &&
MacdCurrent>(MACDOpenLevel*Point) && MaCurrent<MaPrevious)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+StopLoss*Point,Bid-TakeProfit*Point,"-",0,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
}
return(0);
}}}}
// the end.
 
im still struggling with this one. Can anyone heelp me please?
 

Simplified approach:

double RiskRate      = 0.05                                            ; // 01
                                                                         // 02
double dOrderSize ()                                                     // 03
{                                                                        // 04
                                                                         // 05
//< Content                  />                                          // 06
//< 1. Result Initialization />                                          // 07
//< 2. Data Feed             />                                          // 08
//< 3. Size Limit Computing  />                                          // 09
//< 4. Result Validation     />                                          // 10
//< 5. Result Returning      />                                          // 11
                                                                         // 12
//< 1. Result Initialization />                                          // 13
double Result        = 0                                               ; // 14
                                                                         // 15
//< 2. Data Feed             />                                          // 16
double MaxLots       = MarketInfo  ( Symbol () , MODE_MAXLOT         ) ; // 17
double MinLots       = MarketInfo  ( Symbol () , MODE_MINLOT         ) ; // 18
double LotStep       = MarketInfo  ( Symbol () , MODE_LOTSTEP        ) ; // 19
double NominalMargin = MarketInfo  ( Symbol () , MODE_MARGINREQUIRED ) ; // 20
                                                                         // 21
//< !!! Sample !!! 3. Size Limit Computing !!! Sample !!! />             // 22
double RiskValue     = RiskRate    * AccountEquity ()                  ; // 23
double SizeLimit     = RiskValue   / NominalMargin                     ; // 24
                                                                         // 25
//< 4. Result Validation     />                                          // 26
if   ( SizeLimit    >= MinLots )                                         // 27
     { int Steps     = MathFloor ( ( SizeLimit - MinLots ) / LotStep ) ; // 28
       Result        = MinLots     + Steps     * LotStep               ; // 29
     } // if                                                             // 30
                                                                         // 31
if   ( Result       >= MaxLots )                                         // 32
       Result        = MaxLots                                         ; // 33
                                                                         // 34
//< 5. Result Returning      />                                          // 35
return ( Result )                                                      ; // 36
                                                                         // 37
} // end of function                                                     // 38
 
  

.


Explanations:

https://www.mql5.com/en/forum/112782

https://www.mql5.com/en/forum/116326/page2#157591

.

Additionally:

A Sample: Lot size computing

A Sample: Lot size adjusting

 

Ais


Thank you very much. I had found a couple of your posts so I knew you were the man to ask. :) Ive managed to solve my problem using

double lot1 = AccountFreeMargin()/10000;
double LotStep = MarketInfo(Symbol(), MODE_LOTSTEP);
double Lots = MathFloor(lot1/LotStep)*LotStep;

its crude but it works. I just need to work out minilots etc.


Again thanks.

Reason: