Back to Community
Transaction costs affect % return

Hi guys,

so in the code below ,which only places 2 trades:
if i run it with no transaction cost, the return is 201%
if i run it with transaction costs, the return is 202%

How can you get a larger return? I guess this goes to how the return is calculated but this seems funny to me. It should include the transaction costs as a loss maybe.
In either case, I'd like to plot my costs as a function of time or at least know the total $ value the algo spent. thoughts on how to get this?

Clone Algorithm
15
Loading...
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
def initialize(context):
    context.stocks = symbols('CERN', 'DLTR')
    context.never  = True
    #set_commission(commission.PerTrade(cost=0.03))
    set_commission(commission.PerShare(cost=0.013, min_trade_cost=1.3)) # appears to be IBs US API polic

def handle_data(context, data):
    # This will order as many shares as needed to
    # achieve the desired portfolio allocation.
    # In our case, we end up with 20% allocation for
    # one stock and 80% allocation for the other stock.
    if 0:
        order_target_percent(context.stocks[0], .2)
        order_target_percent(context.stocks[1], .8)
    else:
        if context.never:  # else if u do daily he will just keep ordering
            context.never = False
            order_percent(context.stocks[0], .2)
            order_percent(context.stocks[1], .8)

    # Plot portfolio allocations
    pv = float(context.portfolio.portfolio_value)
    portfolio_allocations = []
    for stock in context.stocks:
        pos = context.portfolio.positions[stock]
        portfolio_allocations.append(
            pos.last_sale_price * pos.amount / pv * 100
        )

    record(perc_stock_0=portfolio_allocations[0],
           perc_stock_1=portfolio_allocations[1])
There was a runtime error.
5 responses

If you don't specify a commission model, it doesn't default to 0, it defaults to Quantopian's default, which happens to be quite steep.

I see, it's 3c / share. mine is 3c/share but 1.3$ minimum.

how could i find out the total $ amount i spend in commissions?
i dont see a method to get all the executed orders for which i could sum order. Commission

I have been doing that in the Research Platform, which you can get access to by submitting an algorithm to the Quantopian Open contest...

Hi Simon,
could you please provide more info how to do this? I'm interested to check, as well, what is the total transaction cost after Full Backtest is performed.
Currently, I can;t find that info.
thanks in advance

Hi Simon, or anyone else,
I think Saki's question is a good one and seems to still be unanswered, or at least not yet addressed in the form that I think would be most useful. Basically, as i run any backtest, i like to monitor the number of open positions and the leverage which is easy enough. I would also like to monitor the ratio of net P&L vs cumulative total transaction costs to date. P&L is just the total account equity minus initial equity but how can we obtain cumulative total transaction costs as a function of time? I'm sorry if this seems like a naieve question, but i'm, a "nerwbie" and still struggling with python. Example code would be very much appreciated. Cheers, thanks, TonyM.