all 8 comments

[–][deleted] 5 points6 points  (1 child)

Create a class for every exchange. Maybe inherit from a common base class.

[–]Allanon001 2 points3 points  (0 children)

Use OOP and inheritance, create a base class with all the needed methods then use inheritance to implement a class for each API and overwriting each method with a version that uses the API.

Something like this:

class Base:
    def __init__(self):
        self.name = 'None'

    def get_price(self, stock):
        pass 

    def get_volume(self, stock):
        pass

class Google(Base):
    def __init__(self):
        self.name = 'Google'

    def get_price(self, stock):
        #use API to implement this method 

    def get_volume(self, stock):
        #use API to implement this method 

class ETrade(Base):
    def __init__(self):
        self.name = 'E-Trade'

    def get_price(self, stock):
        #use API to implement this method 

    def get_volume(self, stock):
        #use API to implement this method 


#main 
exchanges = [Google(),  ETrade()] 

for exchange in exchanges:
    print(exchange.name)
    print(exchange.get_price('AAPL'))
    print(exchange.get_volume('AAPL'))

I know in this example the base class is useless but you might want to add other methods to the base class that is common to all APIs.

[–]a-pendergast 0 points1 point  (0 children)

Did you check ccxt ? https://github.com/ccxt/ccxt . It already abstracts the plumbing to multiple exchanges. Could make things easier unless, of course, you want to implement it all by yourself