Exemplo n.º 1
0
    def create_transaction(self,
                           pfl,
                           ttype,
                           ticker,
                           shares=None,
                           price=None,
                           currency=None,
                           commission=None,
                           date='',
                           notes=None):
        """Create transaction.

    Args:
      pfl: portfolio object.
      ttype: string, transaction type, on of the 'Buy', 'Sell',
             'Short Sell', 'Buy to Cover'.
      shares: [optional] decimal, amount of shares.
      price: [optional] decimal, price of the share.
      currency: [optional] string, portfolio currency by default.
      commission: [optional] decimal, brocker commission.
      date: [optional] string, transaction date,
            datetime.now() by default.
      notes: [optional] string, notes.

    Returns:
      None if transaction created successfully, otherwise error string.
    """
        if not currency:
            currency = pfl.portfolio_data.currency_code
        if date is None:
            # if date is not provided from the command line current date is set
            date = datetime.datetime.now().isoformat()
        elif date is '':
            # special case for create position task. date should be set to None
            # to create empty transaction. See detailed explanations in
            # the _run_create_position function below
            date = None
        else:
            parser = googlecl.calendar.date.DateParser()
            date = parser.parse(date).local.isoformat()

        if price is not None:
            price = Price(money=[Money(amount=price, currency_code=currency)])
        if commission is not None:
            commission = Commission(
                money=[Money(amount=commission, currency_code=currency)])
        txn = TransactionEntry(
            transaction_data=TransactionData(type=ttype,
                                             price=price,
                                             shares=shares,
                                             commission=commission,
                                             date=date,
                                             notes=notes))

        try:
            return self.AddTransaction(txn,
                                       portfolio_id=pfl.portfolio_id,
                                       ticker_id=ticker)
        except RequestError, err:
            LOG.error('Failed to create transaction: %s' % err[0]['body'])
Exemplo n.º 2
0
 def Transact(self, type, portfolio, ticker, date=None, shares=None,
     notes=None, price=None, commission=None, currency_code=None):
   if price is not None:
     price = Price(money=[Money(amount=str(price),
         currency_code=currency_code or
         portfolio.portfolio_data.currency_code)])
   if commission is not None:
     commission = Commission(money=[Money(amount=str(comission),
         currency_code=currency_code or
         portfolio.portfolio_data.currency_code)])
   if date is not None and isinstance(date, datetime.datetime):
     date = date.isoformat()
   if shares is not None:
     shares = str(shares)
   txn = TransactionEntry(transaction_data=TransactionData(type=type,
       date=date, shares=shares, notes=notes, price=price,
       commission=commission))
   return self.client.AddTransaction(txn,
       portfolio_id=portfolio.portfolio_id, ticker_id=ticker)