コード例 #1
0
ファイル: views.py プロジェクト: andlister/frano
def _decorate_positions_for_display(positions, showClosedPositions):
  
  symbols = [ position.symbol for position in positions ] + [ CASH_SYMBOL ]
  quotes = dict((quote.symbol, quote) for quote in quotes_by_symbols(symbols))
  as_of_date = min([quote.last_trade.date() for symbol, quote in quotes.items()])
  
  total_market_value = 0
  for position in positions:
    price = (1.0 if position.symbol == CASH_SYMBOL else quotes[position.symbol].price)
    previous_price = (1.0 if position.symbol == CASH_SYMBOL else previous_close_price(quotes[position.symbol]))
    
    decorate_position_with_prices(position, price, previous_price)
    position.show = (showClosedPositions or abs(position.quantity) > 0.01 or position.symbol == CASH_SYMBOL)
    
    total_market_value += position.market_value
    
  for position in positions:
    position.allocation = ((position.market_value / total_market_value * 100) if total_market_value != 0 else 0)
    position.effective_as_of_date = as_of_date
コード例 #2
0
ファイル: demo.py プロジェクト: andlister/frano
def _generate_random_transactions(instruments, total_amount, commission):
  
  # Load historic prices
  quotes = quotes_by_symbols(instruments)
  
  cutoff_date = datetime.now().date() - timedelta(days = DEMO_DAYS_CUTOFF)
  prices = dict([ (quote.symbol, {}) for quote in quotes ])
  dates = set([])
  quote_map = { }
  for quote in quotes:
    quote_map[quote.symbol] = quote
    for history in quote.pricehistory_set.filter(as_of_date__gte = cutoff_date).order_by('as_of_date'):
      cur_date =  history.as_of_date.date()
      prices.get(quote.symbol)[cur_date] = history.price
      dates.add(cur_date)

  # portfolio probabilities
  transaction_probability = DEMO_TARGET_TRANSACTIONS / float(len(dates))
  deposit_probability = DEMO_TARGET_DEPOSITS / float(DEMO_TARGET_TRANSACTIONS)
  buy_sell_probability = DEMO_BUY_SELL_RATIO / float(DEMO_BUY_SELL_RATIO + 1)

  # generate transactions
  transactions = []
  quantities = dict([ (symbol, 0.0) for symbol in instruments ])
  undeposited_cash = total_amount
  cash = 0
  for date in sorted(dates):
    sell_candidates = [ q[0] for q in quantities.items() if q[1] > 0 ]
    
    # see if there is a transaction today or if we are just starting out
    if random() <= transaction_probability or len(transactions) == 0:
      
      # deposits
      if undeposited_cash > 1 and random() <= deposit_probability:
        deposit = min([ undeposited_cash, round(undeposited_cash * (randint(10, 100) / 100.0), -2), total_amount * 0.5 ])
        undeposited_cash -= deposit
        cash += deposit
        transactions.append(Transaction(type = 'DEPOSIT',
            as_of_date = date,
            symbol = CASH_SYMBOL,
            quantity = deposit,
            price = 1.0,
            total = deposit,
          ))
        
      # buys - if we have any cash
      elif random() <= buy_sell_probability:
        amount = min([ cash, round(cash * (randint(20, 100) / 100.0)), total_amount * 0.1 ])
        symbol = choice(instruments)
        price = (prices.get(symbol).get(date) if not quote_map.get(symbol).cash_equivalent else 1.0)
        quantity = floor((amount - commission) / price)
        if quantity > 0:
          total = (quantity * price) + commission
          cash -= total
          quantities[symbol] = quantities.get(symbol) + quantity
          transactions.append(Transaction(type = 'BUY',
              as_of_date = date,
              symbol = symbol,
              quantity = quantity,
              price = price,
              total = total,
            ))
        
      # sells - if there is anything to sell
      elif len(sell_candidates) > 0:
        symbol = choice(sell_candidates)
        price = (prices.get(symbol).get(date) if not quote_map.get(symbol).cash_equivalent else 1.0)
        available_quantity = quantities.get(symbol)
        quantity = min(available_quantity, round(available_quantity * (randint(20, 100) / 100.0)))
        if quantity > 0:
          total = (quantity * price) - commission
          cash += total
          quantities[symbol] = quantities.get(symbol) - quantity
          transactions.append(Transaction(type = 'SELL',
              as_of_date = date,
              symbol = symbol,
              quantity = quantity,
              price = price,
              total = total,
            ))

  return transactions