- https://don't-look-here-go-away.com
- https://twitter.com/onepagecodehttps://don't-look-here-go-away.com
- https://don't-look-here-go-away.comhttps://don't-look-here-go-away.com
- https://don't-look-here-go-away.comhttps://don't-look-here-go-away.com
- https://don't-look-here-go-away.comhttps://don't-look-here-go-away.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Bot(): | |
| """ Main Trading Bot class """ | |
| def __init__(self, config: 'Config'): | |
| self._config = config | |
| self._price_loader = PriceDataLoader() | |
| self._broker = Broker(config.total_cash, self._price_loader) | |
| self._data: None | |
| self._is_running = False | |
| # Initial configurations | |
| self.setup_logging() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def setup_logging(self): | |
| today = datetime.datetime.today() | |
| today_str = today.strftime("%Y-%m-%d") | |
| log_file_name = f"{today_str}_bot_log.txt" | |
| log_full_path = os.path.join(LOG_DIR, log_file_name) | |
| open(log_full_path, 'w').close() | |
| logger.remove() | |
| logger.add(sys.stdout, format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> <red>{level}</red> {message}",colorize=True) | |
| logger.add(log_full_path, format="{time:YYYY-MM-DD HH:mm:ss} {level} {message}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class PriceDataLoader(): | |
| """ | |
| Loader for OLHC | |
| """ | |
| def __init__(self): | |
| self._last_refresh_date = None | |
| self._data_map = {} | |
| def fetch_coin_price_data(self, symbol, currency): | |
| try: | |
| fetch_url = f"https://api.coingecko.com/api/v3/coins/{symbol}/ohlc?vs_currency={currency}&days=7" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Order: | |
| """ Class for managing orders """ | |
| def __init__(self, symbol: str, | |
| position: Positions, | |
| side: Side, | |
| size: float, | |
| limit_price: float = None, | |
| stop_price: float = None): | |
| self._id = str(uuid.uuid4()) | |
| self._symbol = symbol |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Trade: | |
| """ When an `Order` is filled, it results in an active `Trade` """ | |
| def __init__(self, symbol: str, position: Positions, side: Side, size: int, entry_price: float, price_loader: 'PriceDataLoader'): | |
| self._id = str(uuid.uuid4()) | |
| self._symbol = symbol | |
| self._position = position | |
| self._side = side | |
| self._size = size | |
| self._entry_price = entry_price | |
| self._exit_price: Optional[float] = None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Position: | |
| """ Currently held asset position. """ | |
| def __init__(self, broker: 'Broker', symbol: str, position: Positions): | |
| self._id = str(uuid.uuid4()) | |
| self._symbol = symbol | |
| self._broker = broker | |
| self._position = position | |
| def describe(self): | |
| return f"<Position id: {self._id} symbol: {self._symbol} position: {self._position} Size: {self.size} stop: {self.profit_loss}>" | |
| # Getters |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Position: | |
| """ Currently held asset position. """ | |
| def __init__(self, broker: 'Broker', symbol: str, position: Positions): | |
| self._id = str(uuid.uuid4()) | |
| self._symbol = symbol | |
| self._broker = broker | |
| self._position = position | |
| def describe(self): | |
| return f"<Position id: {self._id} symbol: {self._symbol} position: {self._position} Size: {self.size} stop: {self.profit_loss}>" | |
| # Getters |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Broker: | |
| def __init__(self, cash, price_loader): | |
| # Assert inputs | |
| assert 0 < cash, f"Cash should be > 0, is {cash}" | |
| self._cash = cash | |
| self._price_loader = price_loader | |
| self._orders: List[Order] = [] | |
| self._trades: List[Trade] = [] | |
| self._closed_trades: List[Trade] = [] | |
| def describe(self): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Main | |
| if __name__ == '__main__': | |
| # Create new bot | |
| config = Config(symbols=['litecoin', 'ethereum'], cash_per_trade=100, total_cash=10000, data_fetch_interval=60) | |
| bot = Bot(config) | |
| bot.start() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from email.parser import Parser | |
| import os | |
| import sys | |
| conf = open(sys.argv[1]) | |
| config={} | |
| users={} | |
| for line in conf: | |
| if ("," in line): | |
| fields = line.split(",") | |
| key = fields[0].strip().split("=")[1].strip() |