Forex_Strategy_Builder.Backtester.CommissionInMoney C# (CSharp) Метод

CommissionInMoney() публичный статический Метод

Calculates the commission in currency.
public static CommissionInMoney ( double lots, double price, bool isPosClosing ) : double
lots double
price double
isPosClosing bool
Результат double
        public static double CommissionInMoney(double lots, double price, bool isPosClosing)
        {
            double commission = 0;

            if (InstrProperties.Commission == 0)
                return 0;

            if (InstrProperties.CommissionTime == Commission_Time.open && isPosClosing)
                return 0; // Commission is not applied to the position closing

            if (InstrProperties.CommissionType == Commission_Type.pips)
                commission = InstrProperties.Commission * InstrProperties.Point * InstrProperties.LotSize / AccountExchangeRate(price);

            else if (InstrProperties.CommissionType == Commission_Type.percents)
            {
                commission = lots * InstrProperties.LotSize * price * (InstrProperties.Commission / 100) / AccountExchangeRate(price);
                return commission;
            }

            else if (InstrProperties.CommissionType == Commission_Type.money)
                commission = InstrProperties.Commission / AccountExchangeRate(price);

            if (InstrProperties.CommissionScope == Commission_Scope.lot)
                commission *= lots; // Commission per lot

            return commission;
        }

Usage Example

        /// <summary>
        /// Calculates the result
        /// </summary>
        private void Calculate()
        {
            bool         isLong       = (CbxDirection.SelectedIndex == 0);
            PosDirection posDir       = isLong ? PosDirection.Long : PosDirection.Short;
            int          lotSize      = Data.InstrProperties.LotSize;
            var          lots         = (double)NUDLots.Value;
            var          entryPrice   = (double)NUDEntryPrice.Value;
            var          exitPrice    = (double)NUDExitPrice.Value;
            var          daysRollover = (int)NUDDays.Value;
            double       point        = Data.InstrProperties.Point;
            string       unit         = " " + Configs.AccountCurrency;
            double       entryValue   = lots * lotSize * entryPrice;
            double       exitValue    = lots * lotSize * exitPrice;

            // Required margin
            double requiredMargin = (lots * lotSize / Configs.Leverage) *
                                    (entryPrice / Backtester.AccountExchangeRate(entryPrice));

            AlblOutputValues[0].Text = requiredMargin.ToString("F2") + unit;

            // Gross Profit
            double grossProfit = (isLong ? exitValue - entryValue : entryValue - exitValue) /
                                 Backtester.AccountExchangeRate(exitPrice);

            AlblOutputValues[1].Text = grossProfit.ToString("F2") + unit;

            // Spread
            double spread = Data.InstrProperties.Spread * point * lots * lotSize / Backtester.AccountExchangeRate(exitPrice);

            AlblOutputValues[2].Text = spread.ToString("F2") + unit;

            // Entry Commission
            double entryCommission = Backtester.CommissionInMoney(lots, entryPrice, false);

            AlblOutputValues[3].Text = entryCommission.ToString("F2") + unit;

            // Exit Commission
            double exitCommission = Backtester.CommissionInMoney(lots, exitPrice, true);

            AlblOutputValues[4].Text = exitCommission.ToString("F2") + unit;

            // Rollover
            double rollover = Backtester.RolloverInMoney(posDir, lots, daysRollover, exitPrice);

            AlblOutputValues[5].Text = rollover.ToString("F2") + unit;

            // Slippage
            double slippage = Data.InstrProperties.Slippage * point * lots * lotSize / Backtester.AccountExchangeRate(exitPrice);

            AlblOutputValues[6].Text = slippage.ToString("F2") + unit;

            // Net Profit
            double netProfit = grossProfit - entryCommission - exitCommission - rollover - slippage;

            AlblOutputValues[7].Text = netProfit.ToString("F2") + unit;
        }