Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Ordering 100% percent portfolio value of shares exhibiting bizarre results

Here we have an algorithm that only buys the maximum amount of shares of DDM we can with our portfolio value. Instead, it buys this amount and then sells that amount like everyday.

All I want to do is get the sharpe ratio and maximum drawdown for DDM over this period.

4 responses

Partial orders, more common than most realize, undoubtedly. Run this and see log. Here, first month only.
Then try buying just once. This, for that date, will take three minutes to fill, you may want to change to just order(context.ddm, 1)

def handle_data(context, data):  
    if context.ddm not in context.portfolio.positions:  
        order_target_percent(context.ddm, 1)  

Thank you for the help! So now that partial orders are accounted for, why is your algorithm outperforming the benchmark by over 665%?

So now that partial orders are accounted for, why is your algorithm outperforming the benchmark

In the chart above, you're seeing the first month with your ordering, every minute. Run that. Once you see in the log what the orders were doing, and please think about what you will see in the log once you have done that, then make the suggested change to see the fix. Notice especially the minute of the day in the first column. I have the order ids option turned on, they are the far right on each line. I would copy that output to Notepad++, then clicking an order id will also highlight its other instances that match, making it easier to see when orders are made vs when they were filled. You can also see when margin started etc.

Time Models,

Try this:

# buying just once 

stock = symbol('DDM') 

def initialize(context):  
    set_benchmark(stock)  
    schedule_function(trade, date_rules.every_day(),time_rules.market_open())


def trade(context, data):  
    if get_open_orders(stock): return  
    if stock not in context.portfolio.positions:  
        order_target_percent(stock, 1.0) 

    record(leverage = context.account.leverage)  

or this:

# rebalance monthly

stock = symbol('DDM') 

def initialize(context):  
    set_benchmark(stock)  
    schedule_function(trade,date_rules.month_start(),time_rules.market_open())

def trade(context, data):  
    if get_open_orders(stock): return  
    if data.can_trade(stock):  
        order_target_percent(stock, 1.0) 

    record(leverage = context.account.leverage)