Help me to correct this function

MQL5 Autre

Tâche terminée

Temps d'exécution 2 jours
Commentaires de l'employé
Thank you Sir!
Commentaires du client
Nice and quick job, thank you.

Spécifications

Hello,

I've made this function which calculates lot size. The goal is that every trade has the same loss.

So, providing price, stop loss and %risk, I want to find the lot size so that all losing trades have the same loss.

This looks easy but I am unable to find why it does not work. With the lollowing function, I have losses from 1000$ to 3000$. I expect differences due to commission, spread or slippage, but not so much.


double CalcVolumeByStopLoss(double price, double sl, double risk)
{
   double delta=MathAbs(price-sl);
   int contract=SymbolInfoDouble(Symbol(),SYMBOL_TRADE_CONTRACT_SIZE);
   delta=delta*contract;
   bool convert=false;
   if(SymbolInfoInteger(Symbol(),SYMBOL_TRADE_CALC_MODE)==SYMBOL_CALC_MODE_FOREX)convert=true;
   string accntcurr=AccountInfoString(ACCOUNT_CURRENCY);
   string tradecurr=StringSubstr(Symbol(),3,3);
   MqlRates Rates[];
   if(convert && tradecurr!=accntcurr)
   {
      if(CopyRates(accntcurr+tradecurr,0,0,1,Rates)>0)
      {
         delta=delta*Rates[0].close;
      }
      else
      {
         if(CopyRates(tradecurr+accntcurr,0,0,1,Rates)>0)
         {
            if(Rates[0].close>0)
            {
               delta=delta/Rates[0].close;
            }
         }
      }
   }  
   double budget=AccountInfoDouble(ACCOUNT_MARGIN_FREE);
   budget=budget*risk/100;
   double lot=NormalizeDouble(budget/delta,2); //*SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE);
   if(lot<0.01)lot=0.01;
   if(lot>SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX))lot=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);
   if(lot<SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN))lot=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);
   Alert("Lot="+lot);
   return(lot);
}


I'd like someone to find what is wrong in the function and make corrections.


Thank you.


Répondu

1
Développeur 1
Évaluation
(564)
Projets
845
73%
Arbitrage
15
53% / 13%
En retard
193
23%
Gratuit
2
Développeur 2
Évaluation
(25)
Projets
36
17%
Arbitrage
14
14% / 57%
En retard
3
8%
Gratuit
3
Développeur 3
Évaluation
(68)
Projets
78
27%
Arbitrage
13
31% / 62%
En retard
15
19%
Gratuit
Publié : 4 codes
Commandes similaires
To build a Self-Hosted MT4/MT5 Cross-Broker Trade Copier I am looking to develop a trading and copy trading system based on an existing MT4/MT5 Client API or SDK, deployed on my own server(Windows Serve). The system should connect to standard trading accounts across different brokers and automatically copy trades from one master account to multiple follower accounts. Core requirements: The entire backend must operate
I am looking to acquire an existing, fully developed and tested XAUUSD trading signal system, either an MT5 indicator or an Expert Advisor. I am NOT looking for new strategy development, custom coding, or experimental systems. My objective is to own and independently operate a reliable gold trading signal source for personal trading and an international paid subscription service. Requirements: 1. An existing
TRADE COPIER 40+ USD
I WANT A CUSTOMIZED TRADE COPIER THAT CAN COPY TRADE TO DIFFERENT MT5 ACCOUNT WITHOUT LAG. THAT IS ENTRY, STOP LOSS AND TAKE PROFIT. LIMIT AND STOP ORDERS INCLUSIVE. EXECUTION TIME SHOULD BE ABOUT 2SECONDS AT MOST. MY BUDGET IS $40
Hello, How are you doing? I’m looking for an experienced blockchain developer to assist with the smart contract development and on-chain functionality for a Web3 fundraising platform. I’d like to see an overview of your pricing, development process, and estimated timeline for a project of this nature. Additionally, I’d like to understand how you approach smart contract testing, security reviews or audits, and

Informations sur le projet

Budget
10 - 15 USD
Délais
de 1 à 10 jour(s)