def _create_position(self, ptype): whom = self.lastsender try: quantity = int(self.values[0]) symbol = self.values[1] except: self.chat("That's not right") return if quantity <= 0: self.chat("Do you think this is a muthafuckin game?") return stock = Stock(symbol) if not stock: self.chat("Stock not found") return if stock.exchange.upper() not in self.config.exchanges: self.chat("Stock exchange %s DENIED!" % stock.exchange) return if stock.price < 0.01: self.chat("No penny stocks") return drinker = Id(whom) if not drinker.cash: drinker.cash = self.config.startupcash if not drinker.positions: drinker.positions = [] cost = stock.price * quantity if cost > drinker.cash: self.chat("You is poor") return position = Position(symbol=stock.symbol, price=stock.price, quantity=quantity, date=datetime.utcnow(), type=ptype) drinker.positions.append(position) drinker.cash -= cost # drinker.save() verb = 'bought' if ptype == 'long' else 'shorted' self.chat("%s %s %d shares of %s (%s) at %s" % (drinker.nick, verb, position.quantity, stock.company, position.symbol, position.price))
def _create_position(self, ptype): whom = self.lastsender try: quantity = int(self.values[0]) symbol = self.values[1].upper() except: self.chat("That's not right") return if quantity <= 0: self.chat("Do you think this is a muthafuckin game?") return stock = Broker(symbol) if not stock: self.chat("Stock not found") return if stock.exchange.upper() in self.config.blacklist: self.chat("Stock exchange %s DENIED!" % stock.exchange) return if stock.price < 0.01: self.chat("No penny stocks") return drinker = Id(self.lastid) if not drinker.cash: drinker.cash = self.config.startupcash if not drinker.positions: drinker.positions = [] cost = stock.price * quantity if cost > drinker.cash: self.chat("You is poor") return position = Position(symbol=stock.symbol, price=stock.price, quantity=quantity, date=datetime.utcnow(), type=ptype) drinker.positions.append(position) drinker.cash -= cost # drinker.save() verb = 'bought' if ptype == 'long' else 'shorted' self.chat("%s %s %d shares of %s (%s) at %s" % (drinker.nick, verb, position.quantity, stock.company, position.symbol, position.price))
def _close_position(self, ptype): whom = self.lastsender try: quantity = int(self.values[0]) symbol = self.values[1] except: self.chat("That's not right") return if quantity <= 0: self.chat("Do you think this is a muthafuckin game?") return stock = Stock(symbol) if not stock: self.chat("Stock not found") return drinker = Id(whom) if not drinker.is_authenticated: self.chat("You don't have a portfolio") return check = [] keep = [] for p in drinker.positions: if p.symbol == stock.symbol and p.type == ptype: check.append(p) else: keep.append(p) if not check: self.chat("I don't see %s in your portfolio" % stock.symbol) return check.sort(key=lambda x: x.date) verb = 'sold' if ptype == 'long' else 'covered' for p in check: if quantity <= 0: keep.append(p) continue q = min(quantity, p.quantity) basis = p.price * q value = stock.price * q if ptype == 'long': drinker.cash += value net = value - basis else: net = basis - value drinker.cash += basis + net quantity -= q p.quantity -= q if p.quantity > 0: keep.append(p) self.chat("%s %s %d shares of %s at %s (net: %.02f)" % (drinker.nick, verb, q, stock.symbol, stock.price, net)) drinker.positions = keep
def _close_position(self, ptype): whom = self.lastsender try: quantity = int(self.values[0]) symbol = self.values[1].upper() except: self.chat("That's not right") return if quantity <= 0: self.chat("Do you think this is a muthafuckin game?") return stock = Broker(symbol) if not stock: self.chat("Stock not found") return drinker = Id(self.lastid) if not drinker.is_authenticated: self.chat("You don't have a portfolio") return check = [] keep = [] for p in drinker.positions: if p.symbol.upper() == stock.symbol and p.type == ptype: check.append(p) else: keep.append(p) if not check: self.chat("I don't see %s in your portfolio" % stock.symbol) return check.sort(key=lambda x: x.date) verb = 'sold' if ptype == 'long' else 'covered' for p in check: if quantity <= 0: keep.append(p) continue q = min(quantity, p.quantity) basis = p.price * q value = stock.price * q if ptype == 'long': drinker.cash += value net = value - basis else: net = basis - value drinker.cash += basis + net quantity -= q p.quantity -= q if p.quantity > 0: keep.append(p) self.chat("%s %s %d shares of %s at %s (net: %.02f)" % (drinker.nick, verb, q, stock.symbol, stock.price, net)) drinker.positions = keep