Ejemplo n.º 1
0
def computeOrders(orders, bts, dbars):
    assets = bts['assets']
    total_in_shares = 0.0
    if orders == None:
        equityHist.append(equityHist[-1])
        balanceHist.append(balanceHist[-1])
        datesHist.append(sim_dates[bts['curr']])
        for asset in assets:
            bar = dbars[asset]
            price = b3.getLastPrice(bar)
            total_in_shares = total_in_shares + bts[
                'shares_' +
                asset] * price  # counts the value in asset with no order
        if bts['verbose']:
            print('No orders in time(', bts['curr'], ') = ',
                  sim_dates[bts['curr']], ' capital=', bts['capital'],
                  'total in shares=', total_in_shares)
        return True

    if bts['verbose']:
        print('List of ', len(orders), 'orders in time(', bts['curr'], ') :')
    for asset in assets:
        bar = dbars[asset]
        if bar is None:
            print('Error accesing bar to compute order')
            return False
        price = b3.getLastPrice(bar)
        order = getOrder(orders, asset)
        if order == None:  # if no order for that asset, go to the next
            total_in_shares = total_in_shares + bts[
                'shares_' +
                asset] * price  # counts the value in asset with no order
            continue
        volume = order['volume']
        if b3.isSellOrder(order):
            bts['shares_' + asset] = bts['shares_' + asset] - volume
            bts['capital'] = bts['capital'] + volume * price
            if bts['verbose']:
                print("Order for selling ", volume, "shares of asset=", asset,
                      " at price=", price)
        else:
            bts['shares_' + asset] = bts['shares_' + asset] + volume
            bts['capital'] = bts['capital'] - volume * price
            if bts['verbose']:
                print("Order for buying ", volume, "shares of asset=", asset,
                      " at price=", price)

        total_in_shares = total_in_shares + float(bts[
            'shares_' + asset]) * price  # counts the value in asset with order
    if bts['verbose']:
        print(len(orders), ' order(s) in time(', bts['curr'], ') = ',
              sim_dates[bts['curr']], ' capital=', bts['capital'],
              'total in shares=', total_in_shares, 'equity=',
              bts['capital'] + total_in_shares)
    equityHist.append(bts['capital'] + total_in_shares)
    balanceHist.append(bts['capital'])
    datesHist.append(sim_dates[bts['curr']])
def checkOrder(req,bts,bars):
    if req==None:
        return False
    money=bts['capital']
    asset=req['symbol']
    volume=req['volume']
    price=b3.getLastPrice(bars)
    sell=b3.isSellOrder(req)

    if sell:
        if bts['shares_'+asset]>=volume:
            return True
        else:
            return False
    else:
        if money>=volume*price : # checa se não ficaria negativo com a execução
            return True
        else:
            b3.setLastError('Trade would make the balance negative! Therefore, it does not check!')
        return False