June 13, 2021

Calculate Charges for NFO and CDS Futures Trading

By equrve

The attached code can be used to auto calculate charges for trades. Can be used for back testing or in live scenarios. The method used here works for zerodha and can be easily modified to match any other brokers. All charges remain same except the brokerage and taxes on brokerage. This script is in javascript as I have automated my system in nodejs, But can be modified easily in any language as there are just simple calculations.

There are two functions calNFOFuture and calCDSFuture.

module.exports = {
    calNFOFuture: function(buy_price, sell_price, quantity) {

        bp = parseFloat(buy_price.toFixed(2));
        sp = parseFloat(sell_price.toFixed(2));
        qty = parseFloat(quantity.toFixed(2));


        var turnover = parseFloat(parseFloat((bp + sp) * qty).toFixed(2));

        var brokerage_buy = ((bp * qty * 0.0003) > 20) ? 20 : parseFloat(parseFloat(bp * qty * 0.0003).toFixed(2));
        var brokerage_sell = ((sp * qty * 0.0003) > 20) ? 20 : parseFloat(parseFloat(sp * qty * 0.0003).toFixed(2));
        var brokerage = parseFloat(parseFloat(brokerage_buy + brokerage_sell).toFixed(2));

        var stt_total = Math.round(parseFloat(parseFloat(sp * qty * 0.0001).toFixed(2)));

        var etc = parseFloat(parseFloat(0.00002 * turnover).toFixed(2));

        var stax = parseFloat(parseFloat(0.18 * (brokerage + etc)).toFixed(2));

        var sebi_charges = parseFloat(parseFloat(turnover * 0.000001).toFixed(2));

        var stamp_charges = parseFloat(parseFloat(bp * qty * 0.00002).toFixed(2));

        var total_tax = parseFloat(parseFloat(brokerage + stt_total + etc + stax + sebi_charges + stamp_charges).toFixed(2));

        var breakeven = parseFloat(parseFloat(total_tax / qty).toFixed(2));
        breakeven = isNaN(breakeven) ? 0 : breakeven

        var net_profit = parseFloat(parseFloat(((sp - bp) * qty) - total_tax).toFixed(2));
        return { 'charges': total_tax, 'net_profit': net_profit };

    },

    calCDSFuture: function(buy_price, sell_price, quantity) {

        bp = parseFloat(buy_price.toFixed(2));
        sp = parseFloat(sell_price.toFixed(2));
        qty = parseFloat(quantity.toFixed(2));


        var turnover = parseFloat(parseFloat((bp + sp) * qty * 1000).toFixed(2));

        var brokerage_buy = ((bp * qty * 1000 * 0.0003) > 20) ? 20 : parseFloat(parseFloat(bp * qty * 1000 * 0.0003).toFixed(2));
        var brokerage_sell = ((sp * qty * 1000 * 0.0003) > 20) ? 20 : parseFloat(parseFloat(sp * qty * 1000 * 0.0003).toFixed(2));
        var brokerage = parseFloat(parseFloat(brokerage_buy + brokerage_sell).toFixed(2));

        var etc = parseFloat(parseFloat(0.000009 * turnover).toFixed(2));
        var cc = 0;
        var total_trans_charge = etc + cc;

        var stax = parseFloat(parseFloat(0.18 * (brokerage + total_trans_charge)).toFixed(2));

        var sebi_charges = parseFloat(parseFloat(turnover * 0.000001).toFixed(2));

        var stamp_charges = parseFloat(parseFloat(bp * qty * 1000 * 0.000001).toFixed(2));

        var total_tax = parseFloat(parseFloat(brokerage + total_trans_charge + stax + sebi_charges + stamp_charges).toFixed(2));

        var breakeven = parseFloat(parseFloat(total_tax / (qty * 1000)).toFixed(2));
        breakeven = isNaN(breakeven) ? 0 : breakeven

        var pips = Math.ceil(parseFloat(breakeven / 0.0025));

        var net_profit = parseFloat(parseFloat(((sp - bp) * qty * 1000) - total_tax).toFixed(2));
        return { 'charges': total_tax, 'net_profit': net_profit };



    }
}

The script returns an array with two keys. One is the charge and the other is net profit or loss after subtracting the charge. In parameters the script takes, buy price, sell price and quantity.

The script should work for all NSE futures (stocks and indexes) and Currency futures like USDINR, GBPINR.