Download MetaTrader 5

Lot size calculation code?

To add comments, please log in or register
Documentation is available in CHM and PDF. Download it in any convenient form!
Santiago Lopez Ballesteros
129
Santiago Lopez Ballesteros 2014.01.18 16:53 
Hi, I am trying to include a money management function in an EA (actuallly it shouldn't be called MM, as it should only calculates how many lots can I buy so that when I hit my stop loss I only loss a certain percentage of my account).
I created the EA with a code generator, I`m not a programmer, so I dont know how to code it and where to include it. EA is attached, of course it can be modified.
Any help would be appreciated, thank you!
Files:
3x13.mq4 7 kb
Fernando Carreiro
2349
Fernando Carreiro 2014.01.18 17:39  
whroeder1
14514
whroeder1 2014.01.18 17:39  

Do the algebra: AccountBalance * Percent = RISK = (OrderOpenPrice() - OrderClosePrice())*DIR * OrderLots() * DeltaValuePerLot()

Jack Thomas
503
Jack Thomas 2014.01.18 17:50  

Your base lot size is being determined by this variable: Lots=0.1

You then have a function called AdvancedMM() that function appears to be your money management function.

Beyond that, you should probably just learn to program, and if you need technical support contact the developer of the EA.

MQL Documentation: https://docs.mql4.com/

MQL Book: https://book.mql4.com/

Santiago Lopez Ballesteros
129
Santiago Lopez Ballesteros 2014.01.19 19:29  
CodeMonkey:

Your base lot size is being determined by this variable: Lots=0.1

You then have a function called AdvancedMM() that function appears to be your money management function.

Beyond that, you should probably just learn to program, and if you need technical support contact the developer of the EA.

MQL Documentation: https://docs.mql4.com/

MQL Book: https://book.mql4.com/


I create the EAs with a code generator, but when I add a code for lot size calculation the EAs doesn`t work, I think I am not including correctly, I need the code and how to include it, that`s why I asked for assistance thank you.
whroeder1
14514
whroeder1 2014.01.19 22:22  
argento: I create the EAs with a code generator,
https://www.mql5.com/en/forum/147986
john
213
john 2014.01.20 03:19  
argento:

I create the EAs with a code generator, but when I add a code for lot size calculation the EAs doesn`t work, I think I am not including correctly, I need the code and how to include it, that`s why I asked for assistance thank you.

Argento, As CodeMonkey stated, presently you are manually inputting the DEFAULT lot size of 0.1. That size has different meanings with different brokers (i.e. MBTrading vs FXCM).So you should have a Lot size function in your EA for several reasons.


Your present code uses your manually input 0.1 size and places that in the AdvancedMM() function. You could continue to use this ADVANCEDMM() function with a new lot size function, if you replace the variable "Lots" that ADVANCEDMM() is currently using with the new lot size function variable.

Or you could call the new lot size function by it self (without using ADVANCEDMM(). Call it from your "OrderSend()" function. You do this by replacing ADVANCEDMM() with the new AdjustLotSize() function.

All you need is to create the Lot size function to replace the Manually input 0.1;

I've taken your code and placed a lot size function in it- AdjustLotSize(), to get you started, which you can play around with. I've also put the new lot size variable in the ADVANCEDMM() function. I've also added an init() function and pips sizer. so that the new AdjustLotSize() function might work properly. I haven't tested it on your EA, (I have no data on my platform to test it with presently, use at your own risk) My purpose is just to give you some code as a starting point in case you want to try and build a function yourself or learn how to. The added code requires you to decide the amounts, so be sure and look through it and make any changes needed, I just tossed it in sort of quickly.

Be advised, I'm not suggesting that you don't follow the advice of the others that have given you suggestions on this thread. In fact I do suggest you follow what they tell you. This code will just give you something to play around with.



Santiago Lopez Ballesteros
129
Santiago Lopez Ballesteros 2014.01.20 17:15  
Thank you moneycode! I`ll try it and let you know how it went, thanks again.
john
213
john 2014.01.21 05:24  
argento:
Thank you moneycode! I`ll try it and let you know how it went, thanks again.

Argento, I finally got some data on my platform, so I was able to test the code. It needed a few changes to fit your EA, so I fixed it up. I changed some of the names of the inputs so it's easier to understand at a glance.


It's working now in this attachment with the new AdjustLotSize() function. Keep in mind, your StopLoss input is actually a pip loss distance amount. 150=150 pips away from entry.

Your PercentPrincipleToRisk is your percentage of the Principle you choose to risk. So a $2000 Principle and a 0.1 percent of risk would be a $200 dollar stop, at 150 pips away from entry, and the lot size would auto adjust to fit those criteria.

The Principle you chose to trade with on that pair can be manually input or you can put the PrincipleToApply at 0, and it will then use your entire account for calculations. Check the inputs to make sure they're what you want them at.

I tested it on a 1 min chart of EURGBP.

p.s. I couldn't resist making a few tweeks to get the profit up on that pair, but you can always change them back.

reminder: I offered it for educational purposes only, use at your own risk. Also I noticed that your AdvancedMMLots() function looks like a major disaster waiting to happen. It's coded so that it could potentially double up on lot sizes (over and over) during sequential losses . Not sure what the point is of that Function, but I wouldn't use it. Your EA generator is great for evaluating trading rules, but apparently it's not really a safe way to make a perfected EA imo.

Files:
3x13-5new.mq4 10 kb
To add comments, please log in or register