首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从两个exchange (Python)获取数据

从两个exchange (Python)获取数据
EN

Code Review用户
提问于 2022-10-31 18:35:12
回答 1查看 128关注 0票数 0

我正试图从两个交易所得到最新的价格,并根据价格做进一步的行动。

exchanges.py:

代码语言:javascript
复制
from abc import ABC, abstractmethod
from bybit import usdt_perpetual
import requests


class Exchange(ABC):
    @abstractmethod
    def get_latest_price(self):
        pass

class Bybit(Exchange):
    session_unauth = usdt_perpetual.HTTP(endpoint="https://api.bybit.com")

    def __init__(self, symbol: str):
        self.symbol = symbol

    def get_latest_price(self) -> float:
        """
        Get the latest symbol's bid price from Bybit.
        :return: latest bid price
        """
        result = self.session_unauth.orderbook(symbol=self.symbol)["result"]
        return float(result[0]["price"])


class Fin(Exchange):
    url = "fakeurl"

    def __init__(self, symbol):
        self.symbol = symbol[:-1]

    def get_latest_price(self) -> float:
        """
        Get the latest symbol's bid price from the exchange.
        :return: latest bid price
        """
        x = requests.get(self.url)
        return x

app.py:

代码语言:javascript
复制
from exchanges import Fin, Bybit

def main(symbol: str):
    fin = Fin(symbol=symbol)
    bybit = Bybit(symbol=symbol)
    while True:
        fin_price = fin.get_latest_price()
        bybit_price = bybit.get_latest_price()
        # use both price to do futher action...

if __name__ == "__main__":
     main(symbol="BTCUSDT")

有什么方法可以提高代码的可读性和效率?FinBybit似乎非常相似。我能改进这个程序的结构吗?

EN

回答 1

Code Review用户

发布于 2022-11-17 22:00:35

以下是我的建议。

exchanges.py:

代码语言:javascript
复制
from abc import ABC, abstractmethod
import bybit
import requests


class Exchange(ABC):
    @abstractmethod
    def get_latest_price(self, symbol: str):
        # 1. add the symbol to  params, so you need no separate object
        # instance for every symbol.
        pass


class Bybit(Exchange):
    """Consult price at ByBit exchange."""
    # 2. add class level documentation

    def __init__(self):
        # 3. move session into init, tying it to the object (instance)
        # enabling a refresh/disconnect and easier unit tests.

        # 4. refer explicitly to module, rather than blanket import
        self.session_unauth = bybit.usdt_perpetual.HTTP(
            endpoint="https://api.bybit.com")

    def get_latest_price(self, symbol: str) -> float:
        """
        Get the latest symbol's bid price from Bybit.
        :return: latest bid price
        """
        result = self.session_unauth.orderbook(symbol=symbol)["result"]
        return float(result[0]["price"])


class Fin(Exchange):
    """Consult price at Fin exchange."""
    # see 2.

    FIN_EXCHANGE_URL = "fakeurl"
    # 5. rename constant to uppercase, and descriptive name

    # def __init__(self, symbol):
    #     self.symbol = symbol[:-1]
    # 6. remove unused code.

    def get_latest_price(self, symbol:str) -> float:
        """
        Get the latest symbol's bid price from the exchange.
        :return: latest bid price
        """
        # 7. inline x, removing a non-descriptive variable while reducing LoC
        return requests.get(self.FIN_EXCHANGE_URL)

app.py:

代码语言:javascript
复制
from exchanges import Fin, Bybit

# 8. rename "main" to descriptive method name
def get_symbol_from_multiple_exchanges(symbol: str):
    # 9. since both exchanges are of the same base class, you can
    # refer to them as instances of the base class:
    exchanges = [Fin(), Bybit()]
    while True:
        prices_of_symbol = [exchange.get_latest_price(symbol) for exchange in exchanges]
        # use both price to do futher action...


if __name__ == "__main__":
    # apply 8. here as well
    get_symbol_from_multiple_exchanges(symbol="BTCUSDT")
票数 0
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/280863

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档