-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate_method_pattern.py
More file actions
111 lines (80 loc) · 3.49 KB
/
Copy pathtemplate_method_pattern.py
File metadata and controls
111 lines (80 loc) · 3.49 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""
Template Method Pattern
It helps you standarize a process.
In particular suitable for things where you have a fixed process but the steps in the process
may be different.
For example, a CustomerSupportTickets. The process is fixed, but may be one case need a technical
assitent in the home of the customer, so change determinated steps in the process.
Here the process trading is the same, check prices, check if buy or sell, and then action.
But, the strategy process may be different.
We will implement here the template method.
"""
# now I can add More trading strategies without modify anything, only add classes.
# check_prices now is complete independent from the process im going through.
##################### Bridge pattern design
"""
The problem now is that currently in trading bot we have also methods for things that a tradingbot
should not really do as connecting to an exchange or get data.
the thing bridge solve is, if you have two separated things that vary, different exchanges, different
trading bots. give you the capabilities of adding an extra exchange or extra tradingbot, without
to do anything other side of bridge.
Bridge allow you to have two separated class hierarchies, two variations that can change independently
from each other.
"""
from abc import ABC,abstractmethod
from typing import List
class Exchange(ABC):
@abstractmethod
def connect(self):
pass
@abstractmethod
def get_market_data(self,coin:str) -> List[float]:
pass
class BinanceExchange(Exchange):
def connect(self):
print("connecting to binance...")
def get_market_data(self,coin:str) -> List[float]:
return [10,12,18,14]
class CoinbaseExchange(Exchange):
def connect(self):
print("connecting to coinbase...")
def get_market_data(self,coin:str) -> List[float]:
return [192,203,240,280]
class TradingBot(ABC):
def __init__(self,exchange: Exchange) -> None:
self.exchange = exchange # This is the BRIDGE!
# the connection between the tradingBot and Exchange.
# this connection happen in an abstract level.
# now you can have different types of tradingBot and different
# types of exchanges. This is the idea with Bridge Pattern.
@abstractmethod
def should_buy(self,prices: List[float]) -> bool:
pass
@abstractmethod
def should_sell(self,prices: List[float]) -> bool:
pass
def check_prices(self,coin:str):
self.exchange.connect()
prices = self.exchange.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}")
class AverageTrader(TradingBot):
def list_average(self,list: List[float]) -> float:
return sum(list) / len(list)
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 = AverageTrader(CoinbaseExchange())
application.check_prices("BTC/USD")