I am trying to programme automated lot sizing EA based on % of equity

 

good day I am fairly new to coding. I am trying to get this code to work, so that i can trade with 1% of my equity instead of manually needing to calculate it everytime. I am not sure where I am going wrong. am i missing something?


extern bool MM = TRUE;
extern double Risk = 2;
extern double Lots = 0.1;
extern double LotDigits =2;
double GetLots()

{
double minlot = MarketInfo(Symbol(), MODE_MINLOT);
double maxlot = MarketInfo(Symbol(), MODE_MAXLOT);
double leverage = AccountLeverage();
double lotsize = MarketInfo(Symbol(), MODE_LOTSIZE);
double stoplevel = MarketInfo(Symbol(), MODE_STOPLEVEL);

double MinLots = 0.01; double MaximalLots = 50.0;

if(MM)
{
double lots = Lots;

double lots = NormalizeDouble(AccountFreeMargin() * Risk/100 / 1000.0, LotDigits);
if(lots < minlot) lots = minlot;
if (lots > MaximalLots) lots = MaximalLots;
if (AccountFreeMargin() < Ask * lots * lotsize / leverage) {
Print(“We have no money. Lots = “, lots, ” , Free Margin = “, AccountFreeMargin());
Comment(“We have no money. Lots = “, lots, ” , Free Margin = “, AccountFreeMargin());
}}
else lots=NormalizeDouble(Lots,Digits);
return(lots);
}

 

Please edit your post and

use the code button (Alt+S) when pasting code


I know that it is not obvious, but topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I will move your topic to the MQL4 and Metatrader 4 section.

 
  1. Why did you post your MT4 question in the wrong section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum?
    Next time post in the correct place.

  2. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum 2019.05.06
              Messages Editor

  3. double lots = NormalizeDouble(AccountFreeMargin() * Risk/100 / 1000.0, LotDigits);
    1. 1000 meaningless constant.

    2. 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. 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.10.10
                  Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum 2018.02.11
                  Lot value calculation off by a factor of 100 - MQL5 programming forum 2019.07.19

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

      5. You must also check FreeMargin to avoid stop out

      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.

    3. You used NormalizeDouble, It's use is usually wrong, as it is in your case.
      1. Floating point has infinite number of decimals, it's your not understanding floating point and that some numbers can't be represented exactly. (like 1/10.)
                  Double-precision floating-point format - Wikipedia, the free encyclopedia

        See also The == operand. - MQL4 programming forum

      2. Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

      3. SL/TP (stops) need to be normalized to tick size (not Point) — code fails on metals. (On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum) and abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum

      4. Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on Metals. So do it right: Trailing Bar Entry EA - MQL4 programming forum or Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum

      5. Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.

      6. MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
                  MT4:NormalizeDouble - MQL5 programming forum
                  How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum

      7. Prices you get from the terminal are already normalized.

      8. PIP, Point, or Tick are all different in general.
                  What is a TICK? - MQL4 programming forum 2014.08.03

Reason: