Help me to correct this function

MQL5 Altro

Lavoro terminato

Tempo di esecuzione 2 giorni
Feedback del dipendente
Thank you Sir!
Feedback del cliente
Nice and quick job, thank you.

Specifiche

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.


Con risposta

1
Sviluppatore 1
Valutazioni
(564)
Progetti
845
73%
Arbitraggio
15
53% / 13%
In ritardo
193
23%
Gratuito
2
Sviluppatore 2
Valutazioni
(25)
Progetti
36
17%
Arbitraggio
14
14% / 57%
In ritardo
3
8%
Gratuito
3
Sviluppatore 3
Valutazioni
(68)
Progetti
78
27%
Arbitraggio
13
31% / 62%
In ritardo
15
19%
Gratuito
Pubblicati: 4 codici
Ordini simili
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

Informazioni sul progetto

Budget
10 - 15 USD
Scadenze
da 1 a 10 giorno(i)