Help me to correct this function

MQL5 Outros

Trabalho concluído

Tempo de execução 2 dias
Comentário do cliente
Nice and quick job, thank you.
Comentário do desenvolvedor
Thank you Sir!

Termos de Referência

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.


Respondido

1
Desenvolvedor 1
Classificação
(550)
Projetos
827
73%
Arbitragem
15
53% / 13%
Expirado
193
23%
Trabalhando
2
Desenvolvedor 2
Classificação
(25)
Projetos
36
17%
Arbitragem
14
14% / 57%
Expirado
3
8%
Livre
3
Desenvolvedor 3
Classificação
(68)
Projetos
78
27%
Arbitragem
13
31% / 54%
Expirado
15
19%
Trabalhando
Pedidos semelhantes
A “grid” display style like the attached image. User can choose up to 3 EMA’s to monitor on up to 6 Time Frames. If the EMA slope is greater than 0 = Green If the EMA slope is less than 0 = Red If the EMA slope is 0 = Black The grid should be customizable on it’s size, colors, and placement on the chart. Each EMA should update per tick. A label at the top of the grid should read “SLOPES” Indicator 2: A “grid” display
I'm looking for a skilled developer to help build a custom trading strategy for NinjaTrader 8 (N8). The project involves developing and fine-tuning a strategy tailored to specific market conditions. Experience with NinjaScript, strategy optimization, and backtesting is highly preferred. If you're available and have expertise in N8 strategy development, please get in touch to discuss further details
Hello, How are you doing?. i am Looking for a skilled developer to integrate my broker into MetaTrader 5 (MT5). Currently, my broker is not available on the platform, and I need someone with expertise in creating or adapting broker connections for MT5. The ideal candidate will have a strong understanding of MT5 architecture. If you are proficient at working with APls and have a background in trading platform
Hello, How are you doing?. i am Looking for a skilled developer to integrate my broker into MetaTrader 5 (MT5). Currently, my broker is not available on the platform, and I need someone with expertise in creating or adapting broker connections for MT5. The ideal candidate will have a strong understanding of MT5 architecture. If you are proficient at working with APls and have a background in trading platform
I need a programmer for converting a MetaTrader indicator into ea for TradeLocker. Additionally, I would like to have a new EA built in TradeLocker that mirrors the logic of the original MetaTrader, including the specific rules and conditions I need. Please contact me only if you have prior experience converting scripts from MetaTrader to TradeLocker
Hello, How are you doing? I want an EA that will send trade signals from my MT4 to my Telegram channel. So whatever trade I take on MT4 I want the EA to then automatically send the trade details of the trade I took to the Telegram channel in a message right away. Kindly bid if this is what you can do. Thanks
Hello, How are you doing? I am planning to develop a Blockchain DEX platform through which cryptocurrencies will be exchanged, and I require experienced developers in blockchain and backend development. Kindly bid if this is what you can do. Thanks
Hello, How are you doing? I am planning to develop a Blockchain DEX platform through which cryptocurrencies will be exchanged, and I require experienced developers in blockchain and backend development. Kindly bid if this is what you can do. Thanks
I've use most of code from this script, add some more filter, and added different kind of moving avg instead of rsi input .. please check the source code .. . 70-80% is similar to it. Please check can you convert And are you familiar with backtest.py library? Oky. This library has ML approach to get the optimized backtest parameters right ? included for this but let me know what you think? can you install backtest.py
Hello, How are you doing? I am planning to develop a Blockchain DEX platform through which cryptocurrencies will be exchanged, and I require experienced developers in blockchain and backend development. Kindly bid if this is what you can do. Thanks

Informações sobre o projeto

Orçamento
10 - 15 USD
Desenvolvedor
9 - 13.5 USD
Prazo
de 1 para 10 dias