Help me to correct this function

MQL5 Diğer

İş tamamlandı

Tamamlanma süresi: 2 gün
Geliştirici tarafından geri bildirim
Thank you Sir!
Müşteri tarafından geri bildirim
Nice and quick job, thank you.

Şartname

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.


Yanıtlandı

1
Geliştirici 1
Derecelendirme
(564)
Projeler
845
73%
Arabuluculuk
15
53% / 13%
Süresi dolmuş
193
23%
Serbest
2
Geliştirici 2
Derecelendirme
(25)
Projeler
36
17%
Arabuluculuk
14
14% / 57%
Süresi dolmuş
3
8%
Serbest
3
Geliştirici 3
Derecelendirme
(68)
Projeler
78
27%
Arabuluculuk
13
31% / 62%
Süresi dolmuş
15
19%
Serbest
Yayınlandı: 4 kod
Benzer siparişler
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

Proje bilgisi

Bütçe
10 - 15 USD
Son teslim tarihi
from 1 to 10 gün