Calculating a trade lotsize by Max Loss in base currency

 

Hi, still working on my trader managed to fix my trade timer idea using a method combining HonestKnave and Wroeders help, so thanks guys!

G‌ot another bit of it that needs improving where i currently crudely work out a lot size for a trade and it makes sense but doesnt work on anything other than currencies that include your base currency.

‌What I'm trying to do is work something out where in MT4 if you have a trade open and click and drag your SL or TP it shows you the net profit of your trade I need to work this out before hand in order to calculate the right Lot Size so it only risks that maximum amount per trade it opens based on the variable stoploss value that is set by the ATR indicator.

M‌ine works at the moment but its a bit crude and in some cases its Value Loss can be way more than the fixed loss amount.

m‌y code is here:

int RiskManagement ()
                                {
                                        //  Element type: Variable Element name:Variable3 20:46
//  
                                        SLCalcValue=_BB_CHART_ATR  /  _ChartPoint;
                                        //  Element type: Variable Element name:Variable4 20:47
//  
                                        TotalLossAtSLinPounds=LossPer1LotPerPip  *  SLCalcValue;
                                        //  Element type: Variable Element name:Variable5 20:49
//  
                                        RecLotSize=MaxLossInPounds  /  TotalLossAtSLinPounds;
                                        //  The fnFixLots function converts the input lots value to a corrected lot value that can be used to open or modify an order.
                                        _RecLotSizeReal = fnFixLots(  RecLotSize );
                                        //  Returns text string with the specified numerical value converted into a specified precision format.//      //  Sample//  //        string value=DoubleToStr(1.28473418, 5);//        // the value is "1.28473"
                                        _RecommendedLots = DoubleToStr(  _RecLotSizeReal, 2 );
                                        string msg7078;
                                        msg7078 = StringConcatenate(msg7078,"Recommended Lot Size","\n");
                                        msg7078 = StringConcatenate(msg7078,"Value of _RecommendedLots:",_RecommendedLots,"\n");
                                        chartcomment6969 = StringConcatenate(chartcomment6969,msg7078, "\n" );
                                        return( 0 );

 

Hi Guys,

W‌hile waiting and trying to look through William Roeders posts about delta per point I cant quite work it out for myself. I think ive got something close but dont understand how and why to program the Delta per point bit.

I‌ve come up with this which on the face of it seems to work but not sure if this will work for everything including spicy pairs and JPY's where digits are 3 even on a 5 digit broker. Note this EA runs on 1 chart at a time so only trades the pair of the chart it is on.

int RiskManagement ()
                                {
                                       
                                        _TickValue = MarketInfo(  Symbol(), MODE_TICKVALUE );
                                        string msg8756;
                                        msg8756 = StringConcatenate(msg8756,"TickValue","\n");
                                        msg8756 = StringConcatenate(msg8756,"Value of _TickValue:",DoubleToStr(_TickValue, 6),"\n");
                                        chartcomment7246 = StringConcatenate(chartcomment7246,msg8756, "\n" );
                                       
                                        _ChartPoint = MarketInfo(  Symbol(), MODE_POINT );
                                        _TickSize = MarketInfo(  Symbol(), MODE_TICKSIZE );
                                        _AccountEquity2 = AccountEquity();
                                        _AccountBalance1 = AccountBalance();
                                        _dblAccountTradeableBalance = MathMin(  _AccountEquity2, _AccountBalance1 );
                                       
                                       
                                        dblValueRisk=_dblAccountTradeableBalance  *  AccountRiskPercentage  / 100;
                                       
                                        SLCalcValue=_BB_CHART_ATR  /  _ChartPoint;
                                       
                                        RecLotSize=( dblValueRisk  /  _TickValue   )  *  _TickSize  * SLCalcValue;
                                        _RecLotSizeReal = fnFixLots(  RecLotSize );
                                        _RecommendedLots = DoubleToStr(  _RecLotSizeReal, 2 );
                                        string msg7255;
                                        msg7255 = StringConcatenate(msg7255,"Recommended Lot Size","\n");
                                        msg7255 = StringConcatenate(msg7255,"Value of _RecommendedLots:",_RecommendedLots,"\n");
                                        chartcomment7246 = StringConcatenate(chartcomment7246,msg7255, "\n" );
                                        return( 0 );
                              

 
  • You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
  • Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
  • Do NOT use TickValue by itself - DeltaPerLot
  • You must normalize lots properly and check against min and max.
  • You must also check FreeMargin to avoid stop out
 
whroeder1:
  • You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
  • Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
  • Do NOT use TickValue by itself - DeltaPerLot
  • You must normalize lots properly and check against min and max.
  • You must also check FreeMargin to avoid stop out


Hi Will,

i‌n my EA the Stop is set dynamically using the ATR indicator and some maths behind that. I do that in an int called StopLossCalculations. then that fixed stop loss is how im trying to work this back.

I‌s there a simple snippet of Code of how to do DeltaPerLot?

h‌ow I code myself is make things in sort of Modules. a bit like this is within an Int called RiskManagement that way if and when i make EA's for different purposes I can simply drag and drop in the module into a new EA and takes me little time to do.

I‌ use a Visual composer to quite easy for me but you still have to actually code it to get it to do what you want.

W‌hat I cant actually get is what is the calculation for DeltaPerLot!!

 
Thehallster: I‌s there a simple snippet of Code of how to do DeltaPerLot?
Perhaps you should click on the links already provided.
 
whroeder1:
Perhaps you should click on the links already provided.


Your Cryptic approach/response often comes across mega sarchastic which is why i think ive struggled to follow the answers you have given to others, although i do see some pretty no interest in learning comments just wanting you to code it for them. I have followed the links though and where im getting stuck is the logic to it i feel like im not seeing the relevance of DPP in the maths im looking at or for.

I‌'m struggling to understand the relevance of some of the code though that's probably where i'm going wrong. because i'm not using TickValue by itself I use TickSize also.

I‌ see your argument in the other posts where people are using Point etc to work things out which obviously wont work on a 1 digit chart and they should have used TickValue and TickSize.

B‌ut in my above I dont use that, so where is the relevance of DeltaPerPoint within mine?

T‌aking the notes out this is what I see: 

double  DeltaValuePerLot(string pair=""){
     if (pair == "") pair = Symbol();
    return(  MarketInfo(pair, MODE_TICKVALUE) / MarketInfo(pair, MODE_TICKSIZE) ); // Not Point.

‌So in the instance Tick Value = 0.768745 and TickSize = 0.00010 DeltaValuePerLot= 7687.45

‌‌‌maybe im looking at it shorted sighted seeing where to use this from here feels the odd bit to get to the LotSize

 
Thehallster:


Your Cryptic approach/response often comes across mega sarchastic which is why i think ive struggled to follow the answers you have given to others, although i do see some pretty no interest in learning comments just wanting you to code it for them. I have followed the links though and where im getting stuck is the logic to it i feel like im not seeing the relevance of DPP in the maths im looking at or for.

I‌'m struggling to understand the relevance of some of the code though that's probably where i'm going wrong. because i'm not using TickValue by itself I use TickSize also.

I‌ see your argument in the other posts where people are using Point etc to work things out which obviously wont work on a 1 digit chart and they should have used TickValue and TickSize.


You don't need to use the same approach as WHRoeder (or anyone) but understand it and apply it for your needs.

To calculate a lot size from a risk I am using such code :

//--- compute from sl
   double tickSize      = SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE);
   double tickValue     = SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE);
   double capital       = AccountInfoDouble(ACCOUNT_EQUITY);
   double tickCount     = sldistance/tickSize;
   double valueToRisk   = capital*risk/100;
   double lots          = (tickCount*tickValue)!=0 ? valueToRisk/(tickCount*tickValue) : 0;

‌Obviously you can use Balance or whatever you want for "capital".

"sldistance" is a decimal value like 0.0012 (12 pips). "Risk" is for example 2 (for 2% of capital to use).‌

 
  1. Thehallster: So in the instance Tick Value = 0.768745 and TickSize = 0.00010 DeltaValuePerLot= 7687.45
    That times your stop loss is your risk per lot (in your account currency.) 100 pips * 7687.45 = ¤76.87/per lot
    Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot)
  2. If your values were Tick Value = 0.0768745 and TickSize = 0.00001 DeltaValuePerLot doesn't change.

  3. Thehallster: Your Cryptic approach/response often comes across mega sarchastic w
    I am being sarcastic because of your laziness. If you had used this you would have found your answer without having to post. If you had clicked on the links, you would have found your "snippet of Code" without having to ask again
 
Alain Verleyen:

You don't need to use the same approach as WHRoeder (or anyone) but understand it and apply it for your needs.

To calculate a lot size from a risk I am using such code :

//--- compute from sl
   double tickSize      = SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE);
   double tickValue     = SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE);
   double capital       = AccountInfoDouble(ACCOUNT_EQUITY);
   double tickCount     = sldistance/tickSize;
   double valueToRisk   = capital*risk/100;
   double lots          = (tickCount*tickValue)!=0 ? valueToRisk/(tickCount*tickValue) : 0;

‌Obviously you can use Balance or whatever you want for "capital".

"sldistance" is a decimal value like 0.0012 (12 pips). "Risk" is for example 2 (for 2% of capital to use).‌


Thanks Alain, Thats what im talking about! but i think Wroeder has got the wrong end of the stick.

‌His reply sent me to was to look at his previous posting about this where he responded to not just use Tick Value on its own and to use Delta Per point but i didnt understand as I wasnt using TickValue on its own. I do get his DeltaPerPoint now but I dont see that you need to go to that level to do what you have just done above thats what i was trying to do.

 
Alain Verleyen #:
//--- compute from sl
   double tickSize      = SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE);
   double tickValue     = SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE);
   double capital       = AccountInfoDouble(ACCOUNT_EQUITY);
   double tickCount     = sldistance/tickSize;
   double valueToRisk   = capital*risk/100;
   double lots          = (tickCount*tickValue)!=0 ? valueToRisk/(tickCount*tickValue) : 0;


if tickValue is 100, for example for AUDJPY, USDJPY pairs, then I am getting wrong lot size using the above code. Could you please suggest any solution? I have used two different brokers and getting the same result.


 But, if I use the following code: 

 if (digits== 3) {
      tickValue = tickValue/100;
      
   }

then, i am getting right lot size.

Thank you in advance for your suggestions and time!

Reason: