def commit_raw_trade(_id_str, port): """ Commit a raw trade by processing and assigning to a portfolio """ # If no portfolio selected then return a warning if port is None: return Result(False, "No portfolio name selected. Please selecet a portfolio for this trade.", "WARNING") # Get the raw trade to be processed by its _id try: raw_trade = Raw.get(_id_str) assert raw_trade except AppError as e: return Result(success=False, message="log.commit_raw_trade: "+str(e), severity="ERROR") except AssertionError: return Result(success=False, message=f"log.commit_raw_trade: Raw trade with id {_id_str} not found", severity='ERROR') # Get the portfolio to which this trade belongs by the port name try: portfolio = Portfolio.get(port) assert portfolio except AppError as e: return Result(success=False, message="log.commit_raw_trade: "+str(e), severity='ERROR') except AssertionError: return Result(success=False, message=f"log.commit_raw_trade: Portfolio {port} not found", severity='ERROR') if 'currency' not in raw_trade.trade.keys(): raw_trade.trade['currency'] = '$' try: # Actually commit the trade result = portfolio.commit(raw_trade) # If it's a new stock add it to the prices table if result['stocks']: Price.new(raw_trade) message = TradeLog._parse_msg(result, raw_trade.trade['Quantity']) except AppError as e: return Result(success=False, message="log.commit_raw_trade: "+str(e), severity='ERROR') return Result(success=True, message=message, severity='SUCCESS')
def price(): message = [] try: for port in TradeLog.get_ports().message: positions = [position for position in TradeLog.get_open_positions(port.name).message] message.append({ 'port': port.name, 'value': sum(price['value'] for price in Price.get_price(positions)) }) except Exception as e: return Result(False, message=str(e), severity='ERROR') return Result(True, message)
def insert(self, release_date, product, unit, max_value, frequent_value, min_value): try: new_price = Price(release_date=release_date, product_id=product, unit_id=unit, max_value=max_value, frequent_value=frequent_value, min_value=min_value) self.ses.add(new_price) self.ses.commit() return new_price except: print("An exception occurred")
def prices(port): global reverse open = Log.get_open_positions(port, None) if not open.success: flash(open.message, open.severity) return redirect('/price') if not open.message: flash("This portfolio contains no open positions", "WARNING") sortby = request.args.get('sortby') if request.args.get('sortby') else 'stock' prices = sorted(Price.get_price(open.message), key=lambda i: i[sortby], reverse=reverse) reverse = not reverse return render_template('prices.html', prices=prices, port=port)
def yahoo(stock, symbol): print(stock, symbol) Price.get(stock).update({'yahoo': symbol}) return render_template('update.html', prices=Log.prices())
def delete_price(stock): try: Price.get(stock).delete() except Exception as e: return Result(success=False, message=f"Failed to delete {stock}: {str(e)}", severity='WARNING') return Result(success=True, message=f"Successfully delete {stock}")
def update(): Price.update_prices()
def prices(): return Price.read({}, True)