Could someone help on my code? PLS. I am newbie.

 
I try to writing indicator which try to help me calculate lot size for my position on chart. this is my code. i compile it and error. idk y. Thank for you help.


//+------------------------------------------------------------------+
//|                                            LotSizeCalculator     |
//|                               Copyright 2023, MetaQuotes Software Corp.|
//|                                                    https://www.mql5.com|
//+------------------------------------------------------------------+
#property copyright "2023, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window

input double RiskPercentage = 1.0;  // Risk percentage per trade
input double StopLossPips = 50.0;   // Stop loss in pips
input double EntryPrice = 1.2000;   // Example entry price

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   IndicatorShortName("Lot Size Calculator");

   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
// Calculate lot size
   double accountBalance = AccountBalance();
   double riskAmount = (accountBalance * RiskPercentage) / 100.0;
   double oneLotValue = MarketInfo(Symbol(), MODE_MARGINREQUIRED);
   double lotSize = riskAmount / (StopLossPips * MarketInfo(Symbol(), MODE_POINT) * oneLotValue);

// Display lot size and other information on the chart
   string infoText = StringFormat("Entry Price: %.4f\nStop Loss: %.4f\nLot Size: %.2f", EntryPrice, EntryPrice - (StopLossPips * MarketInfo(Symbol(), MODE_POINT)), lotSize);
   ObjectCreate("InfoLabel", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("InfoLabel", infoText, 9, "Arial", clrBlack);
   ObjectSet("InfoLabel", OBJPROP_CORNER, 2);
   ObjectSet("InfoLabel", OBJPROP_XDISTANCE, 10);
   ObjectSet("InfoLabel", OBJPROP_YDISTANCE, 20);

   return(rates_total);
  }
//+------------------------------------------------------------------+

Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2023.08.26
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 

Please edit your post to insert your code by using the CODE button (Alt-S)!

Use the CODE button

 
Sergey Golubev #:

Please edit your post to insert your code by using the CODE button (Alt-S)!


Done

 

there's an EA for this 

https://www.mql5.com/en/forum/384938

Maxlot indicator for mt5
Maxlot indicator for mt5
  • 2021.12.25
  • www.mql5.com
Hi guys, Merry Christmas to those who celebrate it. I would like to see whether anybody has this mt5 script as an indicator : https://www.mql5...
 
phade #:

there's an EA for this 

https://www.mql5.com/en/forum/384938

Sorry to mislead. I try to calculate lot size for example i want to buy @1.0 price and stoploss @ 1.5 price the pipe is 500 and i want 1 % of my port what's lot size i sloud use. 

 
poogun.aph #:

Sorry to mislead. I try to calculate lot size for example i want to buy @1.0 price and stoploss @ 1.5 price the pipe is 500 and i want 1 % of my port what's lot size i sloud use. 

the lot size you can use will also depend on the leverage you're using, I can try to help later maybe...but I find your use case hard to understand. You want to find a lot size based on your SL and a risk percentage, but it doesn't make much sense.

Instead I would take your starting balance, and make an excel table with headings: Starting balance, Leverage, Number of points/pips the trade should achieve for TP, Risk amount %, Standard lots (start at the one you want to start with), Currency pair (need to know the exchange rate for accurate values), profit, Ending balance.

Then you have a money management guide.


For a calculator, you should probably calculate SL for selected lot size and the Risk Percentage, and TP for selected lot size and the pips you want to achieve, instead of a calculator that calculates a lot size for SL.

 
phade #: the lot size you can use will also depend on the leverage you're using,

Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin or 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. Then you compute your lot size.

  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 #:

Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin or 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.

I'm not talking about risk. I'm talking about volume in standard lots. And the volume you can use does depend on the account leverage.

The lot size we use in trading is more of a subjective thing limited by our account leverage and equity, it's not an objective number that can be calculated based on parameters of risk percentage and SL. This is why I think a risk management table with a starting lot size makes more sense
 
phade #: I'm not talking about risk. I'm talking about volume in standard lots. And the volume you can use does depend on the account leverage. The lot size we use in trading is more of a subjective thing limited by our account leverage and equity, it's not an objective number that can be calculated based on parameters of risk percentage and SL. This is why I think a risk management table with a starting lot size makes more sense

You are incorrect or misinformed. Please read William's post again in detail and apply it.

 
phade #: the volume you can use does depend on the account leverage
“Can use” doesn't mean you should. It's your money, control your risk.
 
Fernando Carreiro #:

You are incorrect or misinformed. Please read William's post again in detail and apply it.

He misinterpreted what I was saying. The discussion was not about risk, it was only about lot size.

Reason: