Back to Community
Not Using Full Leverage?

Hey everyone, So I have been trading this and I have the max position set at 1.0 but it ever uses like 50% max, usually only 1/3 of the capital really, does anyone know how to make this trade with more money?

Clone Algorithm
4
Loading...
Backtest from to with initial capital
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
from quantopian.pipeline import Pipeline
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.data import morningstar
from quantopian.pipeline.factors import SimpleMovingAverage, AverageDollarVolume
from quantopian.pipeline.filters.morningstar import IsPrimaryShare


def initialize(context):
    #set_commission(commission.PerShare(cost=0.01, min_trade_cost=1.50))
    set_slippage(slippage.VolumeShareSlippage(volume_limit=.20, price_impact=0.0))
    #set_slippage(slippage.FixedSlippage(spread=0.00))
    set_commission(commission.PerTrade(cost=0.00))
    #set_slippage(slippage.FixedSlippage(spread=0.00))
    set_long_only()

#bigger stocks than original robin hood 
    context.MyLeastPrice=3.00
    context.MyMostPrice= 7.00
    context.MaxPositionFactor=0.25


    # Rebalance
    EveryThisManyMinutes=5
    for minutez in xrange(5, 385, EveryThisManyMinutes):
        schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(minutes=minutez))


    # Prevent excessive logging of canceled orders at market close.
    schedule_function(cancel_open_orders, date_rules.every_day(), time_rules.market_close(hours=0, minutes=1))


    # Record variables at the end of each day.
    schedule_function(my_record_vars, date_rules.every_day(), time_rules.market_close())


    # Create our pipeline and attach it to our algorithm.
    my_pipe = make_pipeline(context)
    attach_pipeline(my_pipe, 'my_pipeline')


def make_pipeline(context):
    """
    Create our pipeline.
    """


    # Filter for primary share equities. IsPrimaryShare is a built-in filter.
    primary_share = IsPrimaryShare()


    # Equities listed as common stock (as opposed to, say, preferred stock).
    # 'ST00000001' indicates common stock.
    common_stock = morningstar.share_class_reference.security_type.latest.eq('ST00000001')


    # Non-depositary receipts. Recall that the ~ operator inverts filters,
    # turning Trues into Falses and vice versa
    not_depositary = ~morningstar.share_class_reference.is_depositary_receipt.latest


    # Equities not trading over-the-counter.
    not_otc = ~morningstar.share_class_reference.exchange_id.latest.startswith('OTC')


    # Not when-issued equities.
    not_wi = ~morningstar.share_class_reference.symbol.latest.endswith('.WI')


    # Equities without LP in their name, .matches does a match using a regular
    # expression
    not_lp_name = ~morningstar.company_reference.standard_name.latest.matches('.* L[. ]?P.?$')


    # Equities with a null value in the limited_partnership Morningstar
    # fundamental field.
    not_lp_balance_sheet = morningstar.balance_sheet.limited_partnership.latest.isnull()


    # Equities whose most recent Morningstar market cap is not null have
    # fundamental data and therefore are not ETFs.
    have_market_cap = morningstar.valuation.market_cap.latest.notnull()


    # At least a certain price
    price = USEquityPricing.close.latest
    AtLeastPrice   = (price >= context.MyLeastPrice)
    AtMostPrice    = (price <= context.MyMostPrice)


    # Filter for stocks that pass all of our previous filters.
    tradeable_stocks = (
        primary_share
        & common_stock
        & not_depositary
        & not_otc
        & not_wi
        & not_lp_name
        & not_lp_balance_sheet
        & have_market_cap
        & AtLeastPrice
        & AtMostPrice
    )


    BottomVar=10
    LowVar=6
    HighVar=40


    log.info('\nAlgorithm initialized variables:\n BottomVar %s \n LowVar %s \n HighVar %s'
        % (BottomVar, LowVar, HighVar)
    )


    # High dollar volume filter.
    base_universe = AverageDollarVolume(
        window_length=20,
        mask=tradeable_stocks
    ).percentile_between(LowVar, HighVar)


    # Short close price average.
    ShortAvg = SimpleMovingAverage(
        inputs=[USEquityPricing.close],
        window_length=3,
        mask=base_universe
    )


    # Long close price average.
    LongAvg = SimpleMovingAverage(
        inputs=[USEquityPricing.close],
        window_length=45,
        mask=base_universe
    )


    percent_difference = (ShortAvg - LongAvg) / LongAvg


    # Filter to select securities to long.
    stocks_worst = percent_difference.bottom(BottomVar)
    securities_to_trade = (stocks_worst)


    return Pipeline(
        columns={
            'stocks_worst': stocks_worst
        },
        screen=(securities_to_trade),
    )


def my_compute_weights(context):
    """
    Compute ordering weights.
    """
    # Compute even target weights for our long positions and short positions.
    stocks_worst_weight = 1.00/len(context.stocks_worst)


    return stocks_worst_weight


def before_trading_start(context, data):
    # Gets our pipeline output every day.
    context.output = pipeline_output('my_pipeline')


    context.stocks_worst = context.output[context.output['stocks_worst']].index.tolist()


    context.stocks_worst_weight = my_compute_weights(context)
    pass


def my_rebalance(context, data):
    TakeProfitFactor=1.10
    GetOutFactor=.95
    GetInFactor=.97


    cash=context.portfolio.cash
    MaxPositionValue=context.portfolio.portfolio_value*context.MaxPositionFactor


    # Maybe Take Profit or Get Out because no longer in pipeline
    for stock in context.portfolio.positions:
        if data.can_trade(stock):
            Curr_P = float(data.current([stock], 'price'))
            if (
                Curr_P>context.portfolio.positions[stock].cost_basis*TakeProfitFactor
                or
                stock not in context.stocks_worst
            ):
                if get_open_orders(stock):
                    cancel_open_order(stock)
                StockShares = context.portfolio.positions[stock].amount
                StockSharesValue = StockShares * Curr_P
                order(stock, -StockShares,
                    style=LimitOrder(Curr_P*GetOutFactor)
                )
                cash += StockSharesValue


    if cash >= 10:
        for stock in context.stocks_worst:
            if data.can_trade(stock):
                Curr_P = float(data.current([stock], 'price'))
                if Curr_P>=context.MyLeastPrice+0.10:
                    StockShares = context.stocks_worst_weight*cash/Curr_P/76
                    if StockShares<1:
                        StockShares=1
                    if MaxPositionValue>StockShares*Curr_P:
                        order(stock, StockShares,
                            style=LimitOrder(Curr_P*GetInFactor)
                        )


def my_record_vars(context, data):
    """
    Record variables at the end of each day.
    """


    # Record our variables.
    record(leverage=context.account.leverage)
    longs = shorts = 0
    for position in context.portfolio.positions.itervalues():
        if position.amount > 0:
            longs += 1
        if position.amount < 0:
            shorts += 1
    record(long_count=longs, short_count=shorts)




def log_open_order(StockToLog):
    oo = get_open_orders()
    if len(oo) == 0:
        return
    for stock, orders in oo.iteritems():
        if stock == StockToLog:
            for order in orders:
                message = 'Found open order for {amount} shares in {stock}'
                log.info(message.format(amount=order.amount, stock=stock))


def log_open_orders():
    oo = get_open_orders()
    if len(oo) == 0:
        return
    for stock, orders in oo.iteritems():
        for order in orders:
            message = 'Found open order for {amount} shares in {stock}'
            log.info(message.format(amount=order.amount, stock=stock))


def cancel_open_order(StockToCancel):
    oo = get_open_orders()
    if len(oo) == 0:
        return
    for stock, orders in oo.iteritems():
        if stock == StockToCancel:
            for order in orders:
                #message = 'Canceling order of {amount} shares in {stock}'
                #log.info(message.format(amount=order.amount, stock=stock))
                cancel_order(order)
def cancel_open_orders(context, data):
    oo = get_open_orders()
    if len(oo) == 0:
        return
    for stock, orders in oo.iteritems():
        for order in orders:
            #message = 'Canceling order of {amount} shares in {stock}'
            #log.info(message.format(amount=order.amount, stock=stock))
            cancel_order(order)


# This is the every minute stuff
def handle_data(context, data):
    pass
There was a runtime error.
2 responses

One thing that's limiting how much you order is the line of code

                    StockShares = context.stocks_worst_weight*cash/Curr_P/76

Any particular reason that's dividing by 76? You already go through the effort to calculate the weight (ie context.stocks_worst_weight).

Ya not sure why that was in there, thanks!