HI i would like to know how to calculate slippage and transaction cost in a strategy in python code when workin in spyder.
If i can get to know only the code for the same,it wpould be very helpful.
Here is a sample code to calculate slippages and transaction cost in Python. The slippage and transaction cost needs to be adjusted as per the instrument and market you are trading in.
- import numpy as np
- # Slippage per share.
- slippage = 0.1
- # 0.0025 is transaction cost per dollar of traded value.
- trans_cost = 0.005
- # A function to determine the total amount after accouting for slippages and transaction cost
- def cash(price, quantity, slippage, trans_cost):
- # Actual price expected
- price = price + (slippage * np.sign(quantity))
- # Total transaction cost
- cost = quantity * price * trans_cost
- return (price*quantity) + (cost * np.sign(quantity))
- # Buying
- # Number of shares.
- quantity = 10
- # Last traded price of the instrument
- price = 450
- print('Cash paid after buying 10 shares', cash(price, quantity, slippage, trans_cost))
- # Selling
- # Number of shares. A negative number indicates a sell order
- quantity = -10
- print('Cash received after selling 10 shares', cash(price, quantity, slippage, trans_cost))