Skip to content

Instantly share code, notes, and snippets.

import abc
class TradeDetails(metaclass=abc.ABCMeta):
def __init__(self, start_price: float, symbol: str, amount: float, currency: str = "USD"):
self.start_price = start_price
self.symbol = symbol.upper()
self.amount = amount
self.currency = currency
from fasttrade.model.trade import TradeDetails
class LongTrade(TradeDetails):
def __init__(self, start_price: float, symbol: str, amount: float, percent_change: float = 0.5,
currency: str = "USD") -> None:
super().__init__(start_price, symbol, amount, currency)
self.end_price = start_price * (1 + percent_change / 100)
from fasttrade.model.trade import TradeDetails
class ShortTrade(TradeDetails):
def __init__(self, start_price: float, symbol: str, amount: float, percent_change: float = 0.5,
currency: str = "USD") -> None:
super().__init__(start_price, symbol, amount, currency)
self.end_price = start_price * (1 - percent_change / 100)
@property
from ccxt import Exchange, OrderNotFound
class CryptoExchange:
def __init__(self, exchange: Exchange):
self.exchange = exchange
self.exchange.load_markets()
@property
import asyncio
import logging
from ccxt import ExchangeError
from model.longtrade import LongTrade
from model.shorttrade import ShortTrade
class TradeExecutor:
@dev4Fun
dev4Fun / main.py
Last active May 18, 2018 21:02
main.py
import logging
import os
import ccxt
from core.exchange import CryptoExchange
from core.telegrambot import TelegramBot
from core.tradeexcutor import TradeExecutor
if __name__ == '__main__':
TITLES = ['idx', 'type', 'remaining', 'symbol', 'price']
SPACING = [4, 6, 8, 10, 8]
def format_open_orders(orders) -> str:
def join_line(ln):
return ' | '.join(str(item).center(SPACING[i]) for i, item in enumerate(ln))
title_line = join_line(TITLES)
lines = [title_line]
async def _wait_order_complete(self, order_id):
status = 'open'
order = None
while status is 'open':
await asyncio.sleep(self.check_timeout)
order = self.exchange.fetch_order(order_id)
status = order['status']
logging.info(f'Finished order {order_id} with {status} status')
class TelegramBot:
class PrivateUserFiler(BaseFilter):
def __init__(self, user_id):
self.user_id = int(user_id)
def filter(self, message):
return message.from_user.id == self.user_id
def __init__(self, token: str, allowed_user_id, trade_executor: TradeExecutor):
self.updater = Updater(token=token)
def _prepare(self):
# Create our handlers
def show_help(bot, update):
update.effective_message.reply_text('Type /trade to show options ')
def show_options(bot, update):
button_list = [
[InlineKeyboardButton("Short trade", callback_data=SHORT_TRADE),