Formula to find brokerage and other charges

code4passion
I want to exactly know how much a trade will cost me. I am aware of zerodha brokerage calculator. Was wondering if someone already have pseudocode/code for this calculation.

https://zerodha.com/brokerage-calculator/#tab-equities
https://zerodha.com/charges/#tab-equities

Tagged:
  • asto
    @rakeshr @sujith @Vivek @MAG
    Yeah that would be really helpful :) ... if either the back end code/pseudocode or at least exact calculation method of brokerage calculator can be shared in the forum

    Or if someone has made his/her own calculator with close enough results can share the code (if they are comfortable with it ) or atleast the pseudocode / calculation method

    There are following doubts in making the calculator...

    1. Brokerage(0.03%) is applied only while opening a position or both ( enter price and exit price separately)

    STT is applied on the sell side as per the charges page
    2. Is it applied only while opening a short position or also while closing a long position
    3. Should the STT charge(0.025%) be applied to the price while being sold ( enter /exit depends 2.) or also to the price of inverse action(buy) of the trade.


    4.Transaction charges are applied only while opening a position(0.00345 while entering a new position) or also while closing the position ( 0.00345% per order ,enter and exit )

    5.SEBI charge is given 10 R/cr how is it appplied for order below 1 cr ? 10 /1cr *price of transaction(s) ? or 0 ? r some other method ?

    Stamp charges are applied to Buy side as per the charges page
    6. . Is it applied only while opening a long position or also while closing a short position
    7 should the stamp duty charge to price. while being buying ( enter /exit depends 6.) or also on the inverse action( sell ) of the trade


    would be great if the calculator function can be added to the api .. but i saw in some thread that it not very likely
    in near future :| ( sorry for the long post had LOT of doubts )
    thanx in advance :)
  • rakeshr
    @asto
    We are coming up with charges API soon. Will update here once it's ready.
  • ketansp
    If someone is still looking for brokerage calculation logic, here is my version.


    import { TRANSACTION_TYPE_FOR_TRANSACTION_COSTS_CALCULATION } from "../constants";

    export abstract class TransactionCostsUtils {
    public static calculateTotalTransactionCosts(transactionType: string, buyPrice: number, sellPrice: number, quantity: number): any {
    let totalTransactionCosts = 0;
    switch (transactionType) {
    case TRANSACTION_TYPE_FOR_TRANSACTION_COSTS_CALCULATION.DIRECT_EQUITY_INTRADAY: {
    const turnover = (buyPrice + sellPrice) * quantity;
    const brokerage = Math.min((buyPrice * quantity * 0.0003) + (sellPrice * quantity * 0.0003), 40);
    const stt = Math.round((sellPrice * quantity) * 0.00025);
    const exchangeTransactionCharge = turnover * 0.0000345;
    const sebiCharges = turnover * 0.000001;
    const stampDuty = Math.round((buyPrice * quantity) * 0.00003);
    const gst = 0.18 * (brokerage + exchangeTransactionCharge + sebiCharges);
    totalTransactionCosts = brokerage + stt + exchangeTransactionCharge + sebiCharges + stampDuty +gst;
    break;
    }
    case TRANSACTION_TYPE_FOR_TRANSACTION_COSTS_CALCULATION.DIRECT_EQUITY_DELIVERY: {
    const turnover = (buyPrice + sellPrice) * quantity;
    const stt = Math.round(turnover * 0.001);
    const exchangeTransactionCharge = turnover * 0.0000345;
    const sebiCharges = turnover * 0.000001;
    const stampDuty = Math.round((buyPrice * quantity) * 0.00015);
    const dpCharges = sellPrice > 0 ? 15.93 : 0;
    const gst = 0.18 * (exchangeTransactionCharge + sebiCharges);
    totalTransactionCosts = stt + exchangeTransactionCharge + sebiCharges + stampDuty +gst + dpCharges;
    break;
    }
    case TRANSACTION_TYPE_FOR_TRANSACTION_COSTS_CALCULATION.DIRECT_EQUITY_FUTURES: {
    const turnover = (buyPrice + sellPrice) * quantity;
    const brokerage = Math.min((buyPrice * quantity * 0.0003) + (sellPrice * quantity * 0.0003), 40);
    const stt = Math.round((sellPrice * quantity) * 0.000125);
    const exchangeTransactionCharge = turnover * 0.00002;
    const sebiCharges = turnover * 0.000001;
    const stampDuty = Math.round((buyPrice * quantity) * 0.00002);
    const gst = 0.18 * (brokerage + exchangeTransactionCharge + sebiCharges);
    totalTransactionCosts = brokerage + stt + exchangeTransactionCharge + sebiCharges + stampDuty +gst;
    break;
    }
    case TRANSACTION_TYPE_FOR_TRANSACTION_COSTS_CALCULATION.DIRECT_EQUITY_OPTIONS: {
    const turnover = (buyPrice + sellPrice) * quantity;
    const brokerage = 40;
    const stt = Math.round((sellPrice * quantity) * 0.000625);
    const exchangeTransactionCharge = turnover * 0.000053;
    const sebiCharges = turnover * 0.000001;
    const stampDuty = Math.round((buyPrice * quantity) * 0.00003);
    const gst = 0.18 * (brokerage + exchangeTransactionCharge + sebiCharges);
    totalTransactionCosts = brokerage + stt + exchangeTransactionCharge + sebiCharges + stampDuty +gst;
    break;
    }
    case TRANSACTION_TYPE_FOR_TRANSACTION_COSTS_CALCULATION.CURRENCY_FUTURES: {
    const turnover = (buyPrice + sellPrice) * quantity * 1000;
    const brokerage = Math.min((buyPrice * quantity * 1000 * 0.0003) + (sellPrice * quantity * 1000 * 0.0003), 40);
    const exchangeTransactionCharge = turnover * 0.0000009;
    const sebiCharges = turnover * 0.000001;
    const stampDuty = Math.round((buyPrice * quantity * 1000) * 0.000001);
    const gst = 0.18 * (brokerage + exchangeTransactionCharge + sebiCharges);
    totalTransactionCosts = brokerage + exchangeTransactionCharge + sebiCharges + stampDuty +gst;
    break;
    }
    case TRANSACTION_TYPE_FOR_TRANSACTION_COSTS_CALCULATION.CURRENCY_OPTIONS: {
    const turnover = (buyPrice + sellPrice) * quantity * 1000;
    const brokerage = Math.min((buyPrice * quantity * 1000 * 0.0003) + (sellPrice * quantity * 1000 * 0.0003), 40);
    const exchangeTransactionCharge = turnover * 0.000035;
    const sebiCharges = turnover * 0.000001;
    const stampDuty = Math.round((buyPrice * quantity * 1000) * 0.000001);
    const gst = 0.18 * (brokerage + exchangeTransactionCharge + sebiCharges);
    totalTransactionCosts = brokerage + exchangeTransactionCharge + sebiCharges + stampDuty +gst;
    break;
    }
    case TRANSACTION_TYPE_FOR_TRANSACTION_COSTS_CALCULATION.COMMODITIES_FUTURES: {
    throw new Error('Calculation logic not yet implemented for this transaction type!');
    break;
    }
    case TRANSACTION_TYPE_FOR_TRANSACTION_COSTS_CALCULATION.COMMODITIES_OPTIONS: {
    throw new Error('Calculation logic not yet implemented for this transaction type!');
    break;
    }
    default:
    throw new Error('Can not calculate transaction costs for unknown transaction type!');
    }
    return totalTransactionCosts;
    }
    }

Sign In or Register to comment.