def trades(api_key, ticker): account = Account.authenticate_api(api_key) ticker = get_price(ticker) if not account: return jsonify({'invalid': ticker}), 404 position = account.get_position_for_json(ticker) return jsonify({'position': position})
def alltrades(api_key): account = Account.authenticate_api(api_key) if not account: return jsonify({"error": "authentication error"}), 401 else: all_trades = account.get_all_trades_json() return jsonify({"trades": all_trades})
def position(api_key, ticker): account = Account.authenticate_api(api_key) if not account: return jsonify({"error": "authentication error"}), 401 positions = account.get_positions() for posish in positions: if posish.ticker == ticker: return jsonify({ "position": posish.ticker, "shares": posish.shares }) return jsonify({"error": "404 not found"}), 404
def sell(api_key, ticker, amount): account = Account.authenticate_api(api_key) position = account.get_position_for(ticker) if request.json['amount'] and request.json['ticker'] and (position.shares >= int(amount)): account.sell(ticker, amount) return jsonify({ "username": account.username, "balance": round(account.balance) }) else: return jsonify({"error": "bad request"}), 400
def deposit(api_key): account = Account.authenticate_api(api_key) if not account: return jsonify({"error": "authentication error"}), 401 if not request.json: return jsonify({"error": "bad request"}), 400 try: amount = request.json['amount'] if amount < 0.0: raise ValueError account.balance += amount except (ValueError, KeyError): return jsonify({"error": "bad request"}), 400 account.save() return jsonify({"username": account.username, "balance": account.balance})
def sell(api_key, ticker, amount): account = Account.authenticate_api(api_key) if not account: return jsonify({'error': 'authentication error'}), 401 if not request.json: return jsonify({'error': 'bad request'}) price = get_price(ticker) total_cost = int(amount) * int(price) try: if request.json['amount'] and request.json['ticker']: if account.balance > total_cost: account.sell(ticker, int(amount)) except (ValueError, KeyError): return jsonify({"error": "bad request"}), 400 return jsonify({"username": account.username, "balance": account.balance})
def sell(api_key, ticker, amount): account = Account.authenticate_api(api_key) position = account.get_position_for(ticker) if not account: return jsonify({'error': 'authentication error'}), 401 if not ticker: return jsonify({'error': "bad ticker data"}) if not request.json: return jsonify({'error': 'bad request'}) try: if request.json['amount'] and request.json['ticker']: if position.shares > int(amount): account.sell(ticker, amount) except (ValueError, KeyError): return jsonify({"error": "bad request"}), 400 return jsonify({"username": account.username, "balance": account.balance})
def buy(api_key, ticker, amount): account = Account.authenticate_api(api_key) price = util.get_price(ticker)[1] purchase = int(amount) * int(price) if not account: return jsonify({"error": "authentication error"}), 401 if not price: return jsonify({"error": "bad ticker data"}), 400 if not request.json: return jsonify({"error": "bad request"}), 400 try: if request.json['amount'] and request.json['ticker']: if account.balance > purchase: account.buy(ticker, int(amount), int(price), purchase) except (ValueError, KeyError): return jsonify({'error': 'bad request'}), 400 return jsonify({'username': account.username, "balance": account.balance})
def sell(api_key, ticker, amount): account = Account.authenticate_api(api_key) position = account.get_position_for(ticker) # ticker, ticker_price = get_price(ticker)[0], get_price(ticker)[1] # cash = integer(ticker_price) * amount if not account: return jsonify({"error": "authentication error"}), 401 if not ticker: return jsonify({"error": "bad ticker data"}) if not request.json: return jsonify({"error": "bad request"}), 400 try: if request.json['amount'] and request.json['ticker']: if position.shares > int(amount): account.sell(ticker, amount) except (ValueError, KeyError): return jsonify({"error": "bad request"}), 400 return jsonify({"username": account.username, "balance": account.balance})
def buy(api_key, ticker, amount): account = Account.authenticate_api(api_key) price = round(get_price(ticker)[1]) purchase = int(amount) * price if account.balance == None or account.balance < purchase: return jsonify({"error": "bad request"}) elif not price: return jsonify({"error": "bad ticker data"}) if request.json['amount'] and request.json[ 'ticker'] and account.balance > int(purchase): account.buy(ticker, amount, price, purchase) return jsonify({ "username": account.username, "balance": round(account.balance) }) else: return jsonify({"error": "bad ticker data"}) if not account: return jsonify({"error": "authentication error"}), 401
def trades(api_key, ticker): account = Account.authenticate_api(api_key) trade = account.trades_for_json(ticker) if not account: return jsonify({"error": "authentication error"}), 400 return jsonify({"trade": trade})
def positions(api_key): account = Account.authenticate_api(api_key) positions = account.get_positions_json() if not account: return jsonify({"error": "authentication error"}), 401 return jsonify({"positions": positions})
def position(api_key, ticker): account = Account.authenticate_api(api_key) position = account.get_position_for_json(ticker) if not account: return jsonify({"error": "authentication error"}), 400 return jsonify({"position": position})
def balance(api_key): account = Account.authenticate_api(api_key) if not account: return jsonify({"error": "authentication error"}), 400 return jsonify({"username": account.username, "balance": account.balance})
def trades(api_key, ticker): account = Account.authenticate_api(api_key) if not account: return jsonify({"error": "authentication error"}), 401 ticker_trades = account.get_trades_by_ticker_json(ticker) return jsonify({"trades": ticker_trades})
def all_trades(api_key): account = Account.authenticate_api(api_key) if not account: return jsonify({'error': 'authentication error'}), 404 trades = account.get_trades_json() return jsonify({'trades': trades})