-
Notifications
You must be signed in to change notification settings - Fork 368
Expand file tree
/
Copy pathtrading-after.py
More file actions
52 lines (38 loc) · 1.44 KB
/
trading-after.py
File metadata and controls
52 lines (38 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from abc import ABC, abstractmethod
from typing import List
class TradingBot(ABC):
def connect(self):
print(f"Connecting to Crypto exchange...")
def get_market_data(self, coin: str) -> List[float]:
return [10, 12, 18, 14]
def check_prices(self, coin: str):
self.connect()
prices = self.get_market_data(coin)
should_buy = self.should_buy(prices)
should_sell = self.should_sell(prices)
if should_buy:
print(f"You should buy {coin}!")
elif should_sell:
print(f"You should sell {coin}!")
else:
print(f"No action needed for {coin}.")
@abstractmethod
def should_buy(self, prices: List[float]) -> bool:
pass
@abstractmethod
def should_sell(self, prices: List[float]) -> bool:
pass
class AverageTrader(TradingBot):
def list_average(self, l: List[float]) -> float:
return sum(l) / len(l)
def should_buy(self, prices: List[float]) -> bool:
return prices[-1] < self.list_average(prices)
def should_sell(self, prices: List[float]) -> bool:
return prices[-1] > self.list_average(prices)
class MinMaxTrader(TradingBot):
def should_buy(self, prices: List[float]) -> bool:
return prices[-1] == min(prices)
def should_sell(self, prices: List[float]) -> bool:
return prices[-1] == max(prices)
application = MinMaxTrader()
application.check_prices("BTC/USD")