Help me to correct this function

MQL5 其它

工作已完成

执行时间2 天
员工反馈
Thank you Sir!
客户反馈
Nice and quick job, thank you.

指定

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.


反馈

1
开发者 1
等级
(564)
项目
845
73%
仲裁
15
53% / 13%
逾期
193
23%
空闲
2
开发者 2
等级
(25)
项目
36
17%
仲裁
14
14% / 57%
逾期
3
8%
空闲
3
开发者 3
等级
(68)
项目
78
27%
仲裁
13
31% / 62%
逾期
15
19%
空闲
发布者: 4 代码
相似订单
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

项目信息

预算
10 - 15 USD
截止日期
从 1 到 10 天