Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

First independent community of traders

First independent community of traders Logo First independent community of traders Logo

First independent community of traders Navigation

  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • Buy Theme
  • Home
  • About Us
  • Blog
  • Contact Us

Share your knowledge in the forex community!

Our community is a completely independent place for communication between forex traders and software developers for trading platforms Metatrader 4 and Metatrader 5

Create A New Account
Home/ Questions/Q 2525
Next
In Process
teetrinker
teetrinker

teetrinker

  • 1 Question
  • 2 Answers
  • 0 Best Answers
  • 20 Points
View Profile
  • 0
teetrinker
Asked: December 26, 20202020-12-26T12:21:14+00:00 2020-12-26T12:21:14+00:00In: Forex Expert Advisors

OrderCalcMargin () and OrderCalcProfit () calculation formulas

  • 0

Good afternoon
Please tell me the formulas for calculating these functions. In particular, we are interested in what happens with crosses – for example, there is an account in USD, and a position is opened on EURGBP.
Also, using the formula OrderCalcProfit (), I would like to calculate Volume and Profit. 
Thanks 

  • 6 6 Answers
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    6 Answers

    • Voted
    • Oldest
    • Recent
    1. teetrinker

      teetrinker

      • 1 Question
      • 2 Answers
      • 0 Best Answers
      • 20 Points
      View Profile
      teetrinker
      2020-12-26T12:21:42+00:00Added an answer on December 26, 2020 at 12:21 pm

      Some time has passed, and the formula for calculating OrderCalcProfit() is still not exactly known. Found the code GetOrderProfit (), the result is slightly different:

      double GetOrderProfit(ENUM_ORDER_TYPE action, string symbol, double volume, double price_open, double price_close){
         double tickValue=SymbolInfoDouble(symbol,SYMBOL_TRADE_TICK_VALUE);

         double profit = NULL;
         if ( action == ORDER_TYPE_BUY || action == ORDER_TYPE_BUY_LIMIT || action == ORDER_TYPE_BUY_STOP ) {
            profit = (price_close - price_open) * volume * tickValue / _Point;
            return profit;
            }
         else if ( action == ORDER_TYPE_SELL || action == ORDER_TYPE_SELL_LIMIT || action == ORDER_TYPE_SELL_STOP ) {
            profit = (price_open - price_close) * volume * tickValue / _Point;
            return profit;
            }
         return profit;
         }

      Tell me what to add so that the results of OrderCalcProfit() and this function match ? 

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    2. elugovoy

      elugovoy

      • 0 Questions
      • 3 Answers
      • 0 Best Answers
      • 26 Points
      View Profile
      elugovoy
      2020-12-26T12:21:38+00:00Added an answer on December 26, 2020 at 12:21 pm

      Let’s say you have a Deposit in USD

      Pairs XXXUSD – will be counted correctly by this formula,

      but for the other pairs, a transfer to the Deposit currency is required at the current exchange rate, depending on the BUY/SELL direction.

      There was a calculation algorithm on MQL4 community, as far as I remember

      Guys, maybe it’s time for MQ to request the function of obtaining the margin of the order ? at least open. sometimes it is very necessary.

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    3. ya_programmer

      ya_programmer

      • 0 Questions
      • 24 Answers
      • 0 Best Answers
      • 78 Points
      View Profile
      ya_programmer
      2020-12-26T12:21:33+00:00Added an answer on December 26, 2020 at 12:21 pm

      Dimitr Trifonov:
      I wrote it myself, conducted tests, but it doesn’t always work exactly on cross-country tracks. Tell me where the error is.

      the crosses are considered differently:

            // USD/CHF point Price = point size * position volume / current rate
            / / EUR / USD point Price = point size * position volume
            / / EUR / CHF point Price = position volume * point size * current quote of the base currency against USD / current exchange rate of the currency pair (cross rate)  

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    4. desead

      desead

      • 0 Questions
      • 3 Answers
      • 0 Best Answers
      • 26 Points
      View Profile
      desead
      2020-12-26T12:21:26+00:00Added an answer on December 26, 2020 at 12:21 pm

      Dimitr Trifonov:
      I wrote it myself, conducted tests, but it doesn’t always work exactly on cross-country tracks. Tell me where the error is.

      here in this formula: return NormalizeDouble(openPrice*volume*SymbolInfoDouble(symbol,SYMBOL_TRADE_CONTRACT_SIZE)/AccountInfoInteger(ACCOUNT_LEVERAGE),0);

      here in this place: AccountInfoInteger(ACCOUNT_LEVERAGE)

      also, it may not always be true. I myself recently encountered such a situation. Here is a detailed reasoning and explanation:  

      From the beginning to page 3, all discussion on this issue.

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    5. scriptong

      scriptong

      • 2 Questions
      • 38 Answers
      • 0 Best Answers
      • 96 Points
      View Profile
      scriptong
      2020-12-26T12:21:22+00:00Added an answer on December 26, 2020 at 12:21 pm

      Dimitr Trifonov:
      I wrote it myself, conducted tests, but it doesn’t always work exactly on cross-country tracks. Tell me where the error is.

      Let’s just say that the cross-country will not be something that “not always”, but always wrong.

      The calculation formula itself is correct: Margin = quote * volume * contract_size / leverage.

      Just instead of quote in different cases, you need to take different quotes. If the base currency of the symbol is quoted through the account currency (for example, EURUSD when the account currency is USD), then as a quote, you really need to take the opening price of the trade: Ask – for Buy and Bid-for Sell. This is the only case for which the calculation method you have given is correct.

      A slightly different way is to get the value of quote characters whose base currency is the same as the account currency (USDCAD when the account currency is USD). In such cases, quote will be equal to: 1.0 / Ask for Buy and 1.0 / Bid for Sell.

      And quote is calculated quite differently for crosses (symbols that do not contain the account currency). So, if the account currency is USD, the Deposit for the EURJPY trade will be calculated at the current price of EURUSD. Since EURUSD and EURJPY have the same base currency, then quote will be defined as in the case of just EURUSD: Ask (from EURUSD) – for Buy and Bid (from EURUSD) – for Sell.

      If the cross is of the CHFJPY type, then quote should be calculated using the USDCHF symbol. Here, the base currencies of the symbol differ. Therefore, we get a quote as in the case of USDCAD: 1.0 / Ask (from USDCHF) – for Buy and 1.0 / Bid (from USDCHF) – for Sell.

      I hope I explained it clearly. At one time, I “entered” this scheme for a very long time. 

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    6. teetrinker

      teetrinker

      • 1 Question
      • 2 Answers
      • 0 Best Answers
      • 20 Points
      View Profile
      teetrinker
      2020-12-26T12:21:18+00:00Added an answer on December 26, 2020 at 12:21 pm

      class COrderCalculateMargin{

      public: double Result;

      public: void COrderCalculateMargin(string symbol,ENUM_ORDER_TYPE orderType,double openPrice,double volume){
         Result=0;
         string currency=AccountInfoString(ACCOUNT_CURRENCY);
         if(StringSubstr(symbol,3,3)==currency)Result=CalcMarginFormula(symbol,openPrice,volume);
         else if(StringSubstr(symbol,0,3)==currency)Result=CalcMarginFormula(symbol,1,volume);
         else{
            string pair=StringSubstr(symbol,0,3)+currency;
            if(orderType==ORDER_TYPE_BUY)Result=CalcMarginFormula(pair,Bid(pair),volume);
            if(orderType==ORDER_TYPE_SELL)Result=CalcMarginFormula(pair,Ask(pair),volume);
            }
         return;
         }

      double CalcMarginFormula(string symbol,double openPrice,double volume){
         return NormalizeDouble(openPrice*volume*SymbolInfoDouble(symbol,SYMBOL_TRADE_CONTRACT_SIZE)/AccountInfoInteger(ACCOUNT_LEVERAGE),0);
         }
        
      };
      I wrote it myself, conducted tests, but it doesn’t always work exactly on cross-country tracks. Tell me where the error is.

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    Leave an answer

    Leave an answer
    Cancel reply

    Browse

    Sidebar

    Ask A Question

    Stats

    • Questions 514
    • Answers 2k
    • Posts 5
    • Comments 0
    • Best Answers 0
    • Users 921
    • Popular
    • Answers
    • Tags
      • On: December 18, 2020
      • Answers: 16

      How to allow trading at certain times

      • On: December 29, 2020
      • Answers: 16

      Example of implementing the OOP pattern Abstract Factory using MQL5 ...

      • On: December 18, 2020
      • Answers: 15

      EMA with a different formula

      • On: December 29, 2020
      • Answers: 15

      Invitation for algo traders.

      • On: December 29, 2020
      • Answers: 14

      Dear Programmers, do not pass by, help fix the error!

    • atma1
      atma1 added an answer Added activation. Thanks. April 8, 2021 at 6:54 am
    • iks_
      iks_ added an answer Atma1: Good day, dear colleagues. Yesterday I updated VMware to… April 8, 2021 at 6:54 am
    • renat
      renat added an answer Atma1: Good day, dear colleagues. Yesterday I updated VMware to… April 8, 2021 at 6:54 am
    • mvs
      mvs added an answer Renat Fatkhullin: You can not immediately send the received data,… April 8, 2021 at 6:53 am
    • anton_m
      anton_m added an answer Renat Fatkhullin: How much data do you read? You can… April 8, 2021 at 6:53 am
    analytics british company computer developers django employee employer english facebook french google interview javascript language life php proforexea programmer programs pro vsa salary university

    Related Questions

    • atma1

      Yesterday I updated VMware to version 16.1.0 and the product ...

      • 3 Answers
    • anton_m

      It is not possible to read data from the server ...

      • 6 Answers
    • s22aa

      Tell me what the difference is.

      • 2 Answers
    • areedbreen

      on the chart in the tester one yield and in ...

      • 2 Answers
    • 1006293

      A question about the intersection of moving averages in MQL5

      • 3 Answers

    Users

    atma1

    atma1

    • 1 Question
    • 1 Answer
    s22aa

    s22aa

    • 1 Question
    • 1 Answer
    areedbreen

    areedbreen

    • 1 Question
    • 1 Answer

    Footer

    First independent community of traders

    About

    An independent community of forex traders. This is where traders communicate. You can ask your questions and you will receive an answer to your question.
    • Terms of Use
    • Privacy Policy
    • Cookie Policy
    • Knowledge Base
    • Support

    © 2021 Forexcommunity.net. All Rights Reserved