Example #1
0
def closeAnyOpenOrders(stock):
    orders = get_open_orders(stock)
    if orders:
        for order in orders:
            message = 'Canceling order for {amount} shares in {stock}'
            message = message.format(amount=order.amount, stock=stock)
            #log.debug(message)
            cancel_order(order)
def cancel_open_orders(context, data):
    oo = get_open_orders()
    if len(oo) == 0:
        return
    for stock, orders in oo.items():
        for o in orders:
            log.info("Canceling order of {} shares in {}", o.amount, stock)
            cancel_order(o)
def cancel_open_orders(context, data):
    oo = get_open_orders()
    if len(oo) == 0:
        return
    for stock, orders in oo.items():
        for o in orders:
            # message = 'Canceling order of {amount} shares in {stock}'
            # log.info(message.format(amount=o.amount, stock=stock))
            cancel_order(o)
Example #4
0
def submit_buy(stock, context, data, weight):
    
    cash = min(investment_limits(context)['remaining_to_invest'], context.portfolio.cash)

    two_week_price_history = data.history([stock], 'price', 10, '1d')
    one_week_price_history = data.history([stock], 'price', 5, '1d')
    average_two_week_price = float(two_week_price_history.mean())
    average_one_week_price = float(one_week_price_history.mean())
    current_price = float(data.current([stock], 'price'))
    
    context.stock = [ symbol('UGAZ'), symbol('DGAZ') ]
    

    if np.isnan(current_price):
            pass  # probably best to wait until nan goes away
            
    if current_price >= float(1.1 * average_two_week_price): # if the price is 10% greater than or equal to the 10d avg
            buy_price = float(current_price)
            
    if current_price >= float(1.15 * average_two_week_price): # if the price is 15% above the 10d avg
            buy_price = float(current_price)
            
    if current_price < float(0.95 * average_one_week_price): # if the price is 5% below the 5d avg
            buy_price = float(current_price)
            

            
    else:
            buy_price = float(current_price * context.buy_factor)
            shares_to_buy = int(weight * cash / buy_price)


            # This cancels open sales that would prevent these buys from being submitted if running
            # up against the PDT rule
            open_orders = get_open_orders()
            if stock in open_orders:
                for open_order in open_orders[stock]:
                    cancel_order(open_order)

    order(context.stock, shares_to_buy)
Example #5
0
def submit_buy(stock, context, data, weight):
    cash = min(
        investment_limits(context)['remaining_to_invest'],
        context.portfolio.cash)

    price_history = data.history([stock], 'price', 20, '1d')
    average_price = float(price_history.mean())
    current_price = float(data.current([stock], 'price'))

    if np.isnan(current_price):
        pass  # probably best to wait until nan goes away
    else:
        if current_price > float(
                1.25 * average_price):  # if the price is 25% above the 20d avg
            buy_price = float(current_price)
        else:  # Otherwise buy at a discount
            buy_price = float(current_price * context.buy_factor)
        buy_price = float(make_div_by_05(buy_price, buy=True))
        shares_to_buy = int(weight * cash / buy_price)
        max_exposure = int(weight * context.portfolio.portfolio_value /
                           buy_price)

        # Prevent over exposing to a particular stock, never own more than 1/max_buy_orders
        # of our account value
        positions = context.portfolio.positions
        if stock in positions and positions[stock].amount >= max_exposure:
            return

        # This cancels open sales that would prevent these buys from being submitted if running
        # up against the PDT rule
        open_orders = get_open_orders()
        if stock in open_orders:
            for open_order in open_orders[stock]:
                cancel_order(open_order)

        order(stock, shares_to_buy, style=LimitOrder(buy_price))
def cancel_all_orders(context, data):
    for _stock, orders in get_open_orders().items():
        for pending_order in orders:
            cancel_order(pending_order)
def cancel_open_orders(context, data):
    for s in get_open_orders():
        for o in get_open_orders(s):
            cancel_order(o)