Order Size Error

 
My EA is having an occasional issue calculating the correct lot size. Most of the time it functions properly but occasionally it will try to send an order for the maximum lot size that my broker will allow (I can tell this because of the journal tab, along with a 'not enough money' error). The EA calculates risk as a percentage of account equity. I know this is not an issue of price being too close as my stop as my stops are always a minimum of 20 pips. The order is sent on price crossing above or below the 4ema so is an instant signal. Also, the stop loss is a function of the location of the take profit (1:1RR... + open order price - high or low of last 5 bars). Does anyone have insights on this? Again most of the time it works right but the occasional order for a crazy order size is quite scary.
 
  1. I doubt you are calculating the correct size if you are getting those errors.
  2. If you want help with your code, you need to post your code. Always post all relevant code (using Code button) or attach the source file.

    We can't see your broken code.

  3. Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk (on leveraged symbols). Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

    1. 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.

    2. AccountBalance * 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.)

    3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
                MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum (2017)
                Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum (2018)
                Lot value calculation off by a factor of 100 - MQL5 programming forum (2019)

    4. You must normalize lots properly and check against min and max.

    5. You must also check Free Margin to avoid stop out

    6. For MT5, see 'Money Fixed Risk' - MQL5 Code Base (2017)

    Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

 
William Roeder #:
  1. I doubt you are calculating the correct size if you are getting those errors.
  2. If you want help with your code, you need to post your code. Always post all relevant code (using Code button) or attach the source file.

    We can't see your broken code.

  3. Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk (on leveraged symbols). Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

    1. 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.

    2. AccountBalance * 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.)

    3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
                MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum (2017)
                Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum (2018)
                Lot value calculation off by a factor of 100 - MQL5 programming forum (2019)

    4. You must normalize lots properly and check against min and max.

    5. You must also check Free Margin to avoid stop out

    6. For MT5, see 'Money Fixed Risk' - MQL5 Code Base (2017)

    Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

I attached the code of the EA as a source file below. Note that I am calculating risk correctly as it works as designed around 75% of the time. Thank you for your help.
Files:
Code_Help.mq5  27 kb
 
ebarrow123 #:
I attached the code of the EA as a source file below. Note that I am calculating risk correctly as it works as designed around 75% of the time. Thank you for your help.
  1. If you want help with your code, you need to post your code. Always post all relevant code (using Code button) or attach the source file.

    We can't see your broken code.

  2. Code Button (Alt+S)

General rules and best pratices of the Forum. - MQL5 Forum on Trading, automated trading systems and testing trading strategies
General rules and best pratices of the Forum. - MQL5 Forum on Trading, automated trading systems and testing trading strategies
  • 2019.03.05
  • www.mql5.com
Click image to play: about possible technical bugs or impossibility to do something in technical way - it is my personal announcement (and as far as i know - the service desk is working on the same way because they have to reproduce any possible bug to be a real bug in case to fix it): forum on trading, automated trading systems and testing trading strategies. Forum on trading, automated trading systems and testing trading strategies. More about proofs and what to check: post #1. Forum on trading, automated trading systems and testing trading strategies
 
ebarrow123 #:
I attached the code of the EA as a source file below. Note that I am calculating risk correctly as it works as designed around 75% of the time. Thank you for your help.
I would say that sometimes your SL doesn’t work… you should print your SL value and your Resistance() and Suport() functions results … 
 
Daniel Cioca #:
I would say that sometimes your SL doesn’t work… you should print your SL value and your Resistance() and Suport() functions results … 

Sorry I dont know what this means. Im new to learning MQL5 language and used EAbuilder.com to make this. Im thinking it could be an error with how EA builder wrote the code for TP as S or R and the SL as a 1:1.


-How do I check function results

-Is there another way I can go in and write code for The TP as the 20HR High (longs) and 20HR low (shorts) and a SL as a 1:1 function of the TP.


I appreciate everyones help. This is a huge obstacle as im finally making profits as an algo trader and this is my last roadblock in an otherwise good system.

 
I changed the SL function to be ATR based and im still encountering the issue so its not a problem with the Support and Resistance calculation. Any ideas?
 
ebarrow123 #: I changed the SL function to be ATR based and im still encountering the issue so its not a problem with the Support and Resistance calculation. Any ideas?
We are unable to read your mind nor see your computer. So, if you need further assistance then you will need to show your new code and especially your log output (both Journal and Expert logs).
 

Also read the following to improve your code ...

Articles

The checks a trading robot must pass before publication in the Market

MetaQuotes, 2016.08.01 09:30

Before any product is published in the Market, it must undergo compulsory preliminary checks in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.
 
ebarrow123 #I attached the code of the EA as a source file below. Note that I am calculating risk correctly as it works as designed around 75% of the time. Thank you for your help.

Just downloaded and had a look at this first code file you posted and I see that it was generated with EABuilder. Most generators make very bad code and mess things up quite a bit.

Forum on trading, automated trading systems and testing trading strategies

erorr code

William Roeder, 2023.04.02 19:56

Yep. Stop using ChatGPT.
          Help needed to debug and fix an AI EA - Trading Systems - MQL5 programming forum (2023)

ChatGPT (the worst), “Bots Builder”, “EA builder”, “EA Builder Pro”, EATree, “Etasoft forex generator”, “Forex Strategy Builder”, ForexEAdvisor (aka. ForexEAdvisor STRATEGY BUILDER, and Online Forex Expert Advisor Generator), ForexRobotAcademy.com, forexsb, “FX EA Builder”, fxDreema, Forex Generator, FxPro, Molanis, Octa-FX Meta Editor, Strategy Builder FX, Strategy Quant, “Visual Trader Studio”, “MQL5 Wizard”, etc., are all the same. You will get something quick, but then you will spend a much longer time trying to get it right, than if you learned the language up front, and then just wrote it.

Since you haven't learned MQL4/5, therefor there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into yours.

We are willing to HELP you when you post your attempt (using Code button) and state the nature of your problem, but we are not going to debug your hundreds of lines of code. You are essentially going to be on your own.

ChatGPT
  1. Even it says do not use it for coding.*
  2. Mixing MT4 and MT5 code together.
  3. Creating multiple OnCalculate/OnTick functions.
  4. OnCalculate returning a double.
  5. Filling buffers with zero in OnInit (they have no size yet). Setting buffer elements to zero but not setting Empty Value to correspond.
  6. Calling undefined functions.
  7. Sometimes, not using strict (MT4 code).
  8. Code that will not compile.
  9. Creating code outside of functions.*
  10. Creating incomplete code.*
  11. Initialization of Global variables with non-constants.*
  12. Assigning a MT5 handle to a double or missing the buffer and bar indexes in a MT4 call.*
  13. Useing MT4 Trade Functions without first selecting an order.*
  14. Uses NULL in OrderSend.*
bot builder Creating two OnInit() functions.*
EA builder
  1. Counting up while closing multiple orders.
  2. Not useing time in new bar detection.
  3. Not adjusting for 4/5 digit brokers, TP/SL and slippage.*
  4. Not checking return codes.
EATree Uses objects on chart to save values — not persistent storage (files or GV+Flush.) No recovery (crash/power failure.)
ForexEAdvisor
  1. Non-updateing global variables.
  2. Compilation errors.
  3. Not checking return codes.
  4. Not reporting errors.
FX EA Builder
  1. Not checking return codes.
  2. Loosing open tickets on terminal restart. No recovery (crash/power failure.)
  3. Not adjusting stops for the spread .*
  4. Using OrdersTotal directly.
  5. Using the old event handlers.
Reason: