mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-11-29 04:32:43 +00:00
add 'add' and candle plots
This commit is contained in:
@@ -3,12 +3,77 @@ __docformat__ = "numpy"
|
||||
|
||||
import argparse
|
||||
from typing import List
|
||||
import matplotlib.pyplot as plt
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
from binance.client import Client
|
||||
from binance.exceptions import BinanceAPIException
|
||||
from gamestonk_terminal.main_helper import parse_known_args_and_warn
|
||||
from gamestonk_terminal.config_terminal import BINANCE_API_KEY, BINANCE_SECRET_KEY
|
||||
from gamestonk_terminal.cryptocurrency.binance_view import plot_order_book
|
||||
from gamestonk_terminal.cryptocurrency.binance_view import plot_order_book, plot_candles
|
||||
|
||||
|
||||
def check_valid_binance_str(symbol: str):
|
||||
"""Check if symbol is in defined binance"""
|
||||
client = Client(BINANCE_API_KEY, BINANCE_SECRET_KEY)
|
||||
try:
|
||||
client.get_avg_price(symbol=symbol.upper())
|
||||
return symbol.upper()
|
||||
except BinanceAPIException:
|
||||
raise argparse.ArgumentTypeError(f"{symbol} is not a valid binance symbol")
|
||||
|
||||
|
||||
def add_binance_coin(other_args:List[str]):
|
||||
"""
|
||||
Define current_coin from binance
|
||||
Parameters
|
||||
----------
|
||||
other_args: List[str]
|
||||
Argparse arguments
|
||||
|
||||
Returns
|
||||
-------
|
||||
coin: str
|
||||
Coin that is defined on binance
|
||||
"""
|
||||
parser = argparse.ArgumentParser(
|
||||
prog="add_coin",
|
||||
add_help=False,
|
||||
description="Define the coin to be used from binance",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-c", "--coin", help="Coin to get", dest="coin", type=str, default="BTC"
|
||||
)
|
||||
parser.add_argument(
|
||||
"-q",
|
||||
"--quote",
|
||||
help="Quote currency (what to view coin vs)",
|
||||
dest="quote",
|
||||
type=str,
|
||||
default="USDT",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-s",
|
||||
"--symbol",
|
||||
help="binance symbol (overrides other arguments)",
|
||||
dest="symbol",
|
||||
default=None,
|
||||
type=check_valid_binance_str,
|
||||
)
|
||||
try:
|
||||
ns_parser = parse_known_args_and_warn(parser, other_args)
|
||||
if not ns_parser:
|
||||
return None
|
||||
|
||||
if ns_parser.symbol:
|
||||
return ns_parser.symbol
|
||||
|
||||
coin = ns_parser.coin + ns_parser.quote
|
||||
return check_valid_binance_str(coin)
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
print("")
|
||||
return None
|
||||
|
||||
|
||||
def order_book(coin: str, other_args: List[str]):
|
||||
@@ -34,24 +99,6 @@ def order_book(coin: str, other_args: List[str]):
|
||||
plot_order_book(bids, asks, coin)
|
||||
|
||||
|
||||
def recent_trades(coin: str, other_args: List[str]):
|
||||
"""
|
||||
Get recent trades for currency
|
||||
Parameters
|
||||
----------
|
||||
coin: str
|
||||
Coin to get recent trades for
|
||||
other_args: List[str]
|
||||
Argparse arguments
|
||||
|
||||
Returns
|
||||
-------
|
||||
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
def candles(coin: str, other_args: List[str]):
|
||||
"""
|
||||
Get klines/candles for coin
|
||||
@@ -66,12 +113,51 @@ def candles(coin: str, other_args: List[str]):
|
||||
-------
|
||||
|
||||
"""
|
||||
pass
|
||||
client = Client(BINANCE_API_KEY, BINANCE_SECRET_KEY)
|
||||
try:
|
||||
candles = client.get_klines(
|
||||
symbol=coin, interval=Client.KLINE_INTERVAL_30MINUTE
|
||||
)
|
||||
|
||||
# Response
|
||||
# 1499040000000, // Open time
|
||||
# "0.01634790", // Open
|
||||
# "0.80000000", // High
|
||||
# "0.01575800", // Low
|
||||
# "0.01577100", // Close
|
||||
# "148976.11427815", // Volume
|
||||
# 1499644799999, // Close time
|
||||
# "2434.19055334", // Quote asset volume
|
||||
# 308, // Number of trades
|
||||
# "1756.87402397", // Taker buy base asset volume
|
||||
# "28.46694368", // Taker buy quote asset volume
|
||||
# "17928899.62484339" // Ignore.
|
||||
|
||||
candles_df = pd.DataFrame(candles).astype(float).iloc[:, :7]
|
||||
|
||||
candles_df.columns = [
|
||||
"Time0",
|
||||
"Open",
|
||||
"High",
|
||||
"Low",
|
||||
"Close",
|
||||
"Volume",
|
||||
"Time1",
|
||||
]
|
||||
candles_df.index = pd.to_datetime(
|
||||
(candles_df["Time0"] + candles_df["Time1"]) / 2, unit="ms"
|
||||
)
|
||||
|
||||
plot_candles(candles_df, "")
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
print("")
|
||||
|
||||
|
||||
def balance(coin: str, other_args: List[str]):
|
||||
"""
|
||||
Get accont holdings for asset
|
||||
Get account holdings for asset
|
||||
Parameters
|
||||
----------
|
||||
coin: str
|
||||
@@ -83,4 +169,9 @@ def balance(coin: str, other_args: List[str]):
|
||||
-------
|
||||
|
||||
"""
|
||||
pass
|
||||
client = Client(BINANCE_API_KEY, BINANCE_SECRET_KEY)
|
||||
try:
|
||||
client.get_asset_balance(asset=coin)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
print("")
|
||||
|
||||
@@ -3,6 +3,8 @@ __docformat__ = "numpy"
|
||||
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import mplfinance as mpf
|
||||
import pandas as pd
|
||||
from gamestonk_terminal.main_helper import plot_autoscale
|
||||
from gamestonk_terminal.config_plot import PLOT_DPI
|
||||
from gamestonk_terminal.feature_flags import USE_ION as ion
|
||||
@@ -37,7 +39,40 @@ def plot_order_book(bids: np.array, asks: np.array, coin: str):
|
||||
plt.xlabel("Price")
|
||||
plt.ylabel("Size (Coins) ")
|
||||
plt.title(f"Order Book for {coin}")
|
||||
# if ion:
|
||||
# plt.ion()
|
||||
if ion:
|
||||
plt.ion()
|
||||
plt.show()
|
||||
print("")
|
||||
|
||||
|
||||
def plot_candles(candles_df: pd.DataFrame, coin: str):
|
||||
"""
|
||||
Plot candle chart from dataframe
|
||||
Parameters
|
||||
----------
|
||||
candles_df: pd.DataFrame
|
||||
Dataframe containing time and OHLVC
|
||||
coin: str
|
||||
Coin plotting for
|
||||
|
||||
Returns
|
||||
-------
|
||||
|
||||
"""
|
||||
mpf.plot(
|
||||
candles_df,
|
||||
type="candle",
|
||||
volume=True,
|
||||
title=f"{coin}",
|
||||
xrotation=10,
|
||||
style="binance",
|
||||
figratio=(10, 7),
|
||||
figscale=1.10,
|
||||
figsize=(plot_autoscale()),
|
||||
update_width_config=dict(
|
||||
candle_linewidth=1.0, candle_width=0.8, volume_linewidth=1.0
|
||||
),
|
||||
)
|
||||
if ion:
|
||||
plt.ion()
|
||||
plt.show()
|
||||
|
||||
@@ -59,8 +59,7 @@ class CryptoController:
|
||||
print("")
|
||||
print("Binance:")
|
||||
print(" add add coin as current")
|
||||
print(" book get order book")
|
||||
print(" trades get recent trades")
|
||||
print(" book show order book")
|
||||
print(" candle get klines/candles and plot")
|
||||
print(" balance show coin balance")
|
||||
print("")
|
||||
@@ -118,10 +117,6 @@ class CryptoController:
|
||||
"""Book command - get order book from binance"""
|
||||
binance_model.order_book(self.current_coin, other_args)
|
||||
|
||||
def call_trades(self, other_args):
|
||||
"""Trades command - get recent trades from binance"""
|
||||
binance_model.recent_trades(self.current_coin, other_args)
|
||||
|
||||
def call_candle(self, other_args):
|
||||
"""Candle command - show candle chart from binance"""
|
||||
binance_model.candles(self.current_coin, other_args)
|
||||
@@ -130,9 +125,11 @@ class CryptoController:
|
||||
"""Balance command - check current holdings of coin in binance"""
|
||||
binance_model.balance(self.current_coin, other_args)
|
||||
|
||||
def call_add(self, _):
|
||||
self.current_coin = input("Coin?").upper()
|
||||
|
||||
def call_add(self, other_args):
|
||||
"""Add command - set current coin for binance operations"""
|
||||
self.current_coin = binance_model.add_binance_coin(other_args)
|
||||
print(f"{self.current_coin} loaded")
|
||||
print("")
|
||||
|
||||
def menu():
|
||||
crypto_controller = CryptoController()
|
||||
|
||||
Reference in New Issue
Block a user