Problems opening lots

 

I am trying to use the method of opening lots based of the percentage of equity I want to risk.

Print("Calculating Lotsize");
            Balance = AccountEquity();
            StopLoss = multiplier*atr24;
            Print("StopLoss"+ StopLoss);
            
         //LotSize = NormalizeDouble(CalcLotSize( EquityPercent, StopLoss),1);
         
            Print("AccountEquity(): "+AccountEquity() );
                      RiskAmount = Balance*(EquityPercent1 / 100.0);
                                Print("RiskAmount1: "+RiskAmount);
                                 TickValue = MarketInfo(Symbol(),MODE_TICKVALUE);
                                Print("TickValue1: "+TickValue);
                                if(Point == 0.001 || Point == 0.00001) TickValue*= 10;
                                 LotSize1 = NormalizeDouble( (RiskAmount/StopLoss)/TickValue,1);
                           Print("TickValue check1: "+TickValue);
                      Print("LotSize1: "+LotSize1);
                      Print("percentage: "+(EquityPercent1 / 100.0));
                      
                      
                      Print("AccountEquity(): "+AccountEquity() );
                      RiskAmount = Balance*(EquityPercent2 / 100.0);
                                Print("RiskAmount2: "+RiskAmount);
                                 TickValue = MarketInfo(Symbol(),MODE_TICKVALUE);
                                Print("TickValue2: "+TickValue);
                                if(Point == 0.001 || Point == 0.00001) TickValue*= 10;
                                 LotSize2 = NormalizeDouble( (RiskAmount/StopLoss)/TickValue,1);
                           Print("TickValue check2: "+TickValue);
                      Print("LotSize2: "+LotSize2);
                      Print("percentage: "+(EquityPercent2 / 100.0));


If I initialize the external double variables EquityPercent1 and EquityPercent2 to 1, it means I want to risk 1 percent. The code executes fine. But if I set them to 22 to risk 22% on lot1 and 22% on lot2

It assigns a value of 0 to both lots and the order fails due to invalid lotsize.

I am using MBtrading.

I used print statements and verified that MarketInfo(Symbol(),MODE_LOTSTEP) == 0.1

MarketInfo(Symbol(),MODE_MAXLOT) == 1000

MarketInfo(Symbol(),MODE_MINLOT) == 0.1

ALL the variables used to calculate LotSize1 and 2 are of type double as well as LotSize1 and 2 themselves. What could the problem be?

 
Print all the variables and then you will know what is causing your problem.
 
  1. using tick value by itself is wrong
  2. Your StopLoss is NOT in pips but your multiplying tick_value to be pip_value.
  3. using Normalize double is wrong, that will only work if minlot and lotstep are both exactly 0.1. Fails for any other multiple
    double NormalizeLots(double lots, string pair=""){
        if (pair == "") pair = Symbol();
        double  lotStep     = MarketInfo(pair, MODE_LOTSTEP),
                minLot      = MarketInfo(pair, MODE_MINLOT);
        lots            = MathRound(lots/ls) * ls;
        if (lots < minLot) lots = 0;    // or minLot
        return(lots);
    }
    

 
RaptorUK:
Print all the variables and then you will know what is causing your problem.


multiplier is intially initialized to 10. in the init function it gets modified

if((MarketInfo(Symbol(),MODE_DIGITS) == 5)||(MarketInfo(Symbol(),MODE_DIGITS) == 3))multiplier = MathPow(multiplier,MarketInfo(Symbol(),MODE_DIGITS)-1);
    else multiplier = MathPow(multiplier,MarketInfo(Symbol(),MODE_DIGITS));

it works perfect when the percents are set to 1% but somehow the number is modified when I set the percents to be 22% of equity

 
jeemba2012:

multiplier is intially initialized to 10. in the init function it gets modified

it works perfect when the percents are set to 1% but somehow the number is modified when I set the percents to be 22% of equity

Probably just a fluke that 1% works . . . your multiplier is clearly wrong . . . find out why.
 
WHRoeder:
  1. using tick value by itself is wrong
  2. Your StopLoss is NOT in pips but your multiplying tick_value to be pip_value.
  3. using Normalize double is wrong, that will only work if minlot and lotstep are both exactly 0.1. Fails for any other multiple

Is "ls" defined as lotStep?
 
RaptorUK:
Probably just a fluke that 1% works . . . your multiplier is clearly wrong . . . find out why.

I am probably doing something silly, but it works for all my other code and I cant see where the error is.

the number I want the multiplier to be is 10000.

I use the multiplier so if I want to find the difference between to prices in pips, I just multiply the price by my multiplier and take the difference excluding the 5th digit after decimal if it exists.

That is what this code is for

if((MarketInfo(Symbol(),MODE_DIGITS) == 5)||(MarketInfo(Symbol(),MODE_DIGITS) == 3))
    {
      multiplier = MathPow(multiplier,MarketInfo(Symbol(),MODE_DIGITS)-1);
    }

So the -1 makes multplier 10000 what I need instead of 100000.

If it is standard 4 digits after decimal then this part of code is executed

else multiplier = MathPow(multiplier,MarketInfo(Symbol(),MODE_DIGITS));

Either way I get my condition for multiplier to be 10000. Now the atr24 is four digits after the decimal so

StopLoss = multiplier*atr24 is supposed to give me the pips I am using to calculate my stop so I can calculate the lots I want to open.

This code is slightly modified from the one presented earlier and it still does not work.

Reason: