Skip to content

Commit

Permalink
Updated .pre-commit-config.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
edeng23 committed May 5, 2023
1 parent 3eaf949 commit caca9ad
Show file tree
Hide file tree
Showing 18 changed files with 111 additions and 346 deletions.
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
minimum_pre_commit_version: 1.15.2
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.1.0
rev: v4.1.0
hooks:
- id: check-merge-conflict # Check for files that contain merge conflict strings.
- id: trailing-whitespace # Trims trailing whitespace.
Expand All @@ -22,26 +22,26 @@ repos:
files: ^supported_coin_list$

- repo: https://github.com/asottile/pyupgrade
rev: v2.10.0
rev: v2.29.1
hooks:
- id: pyupgrade
name: Rewrite Code to be Py3.6+
args: [--py36-plus]

- repo: https://github.com/pycqa/isort
rev: 5.6.3
rev: 5.11.5
hooks:
- id: isort
args: [--profile, black, --line-length, '120']

- repo: https://github.com/psf/black
rev: 20.8b1
rev: 23.3.0
hooks:
- id: black
args: [-l, '120']

- repo: https://github.com/asottile/blacken-docs
rev: v1.7.0
rev: v1.11.0
hooks:
- id: blacken-docs
args: [--skip-errors]
Expand All @@ -56,7 +56,7 @@ repos:
args: [requirements.txt, dev-requirements.txt]

- repo: https://github.com/pre-commit/mirrors-pylint
rev: v2.4.4
rev: v3.0.0a5
hooks:
- id: pylint
name: PyLint
Expand Down
4 changes: 1 addition & 3 deletions .pre-commit-hooks/sort-coins-file.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
def sort():
in_contents = SUPPORTED_COIN_LIST.read_text()
out_contents = ""
out_contents += "\n".join(
sorted([line.upper() for line in in_contents.splitlines()])
)
out_contents += "\n".join(sorted(line.upper() for line in in_contents.splitlines()))
out_contents += "\n"
if in_contents != out_contents:
SUPPORTED_COIN_LIST.write_text(out_contents)
Expand Down
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
extension-pkg-whitelist=

# Specify a score threshold to be exceeded before program exits with error.
fail-under=10.0
fail-under=6.0

# Add files or directories to the blacklist. They should be base names, not
# paths.
Expand Down
4 changes: 1 addition & 3 deletions backtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,5 @@
print("TIME:", manager.datetime)
print("BALANCES:", manager.balances)
print("BTC VALUE:", btc_value, f"({btc_diff}%)")
print(
f"{manager.config.BRIDGE.symbol} VALUE:", bridge_value, f"({bridge_diff}%)"
)
print(f"{manager.config.BRIDGE.symbol} VALUE:", bridge_value, f"({bridge_diff}%)")
print("------")
19 changes: 4 additions & 15 deletions binance_trade_bot/api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ def filter_period(query, model): # pylint: disable=inconsistent-return-statemen
def value_history(coin: str = None):
session: Session
with db.db_session() as session:
query = session.query(CoinValue).order_by(
CoinValue.coin_id.asc(), CoinValue.datetime.asc()
)
query = session.query(CoinValue).order_by(CoinValue.coin_id.asc(), CoinValue.datetime.asc())

query = filter_period(query, CoinValue)

Expand All @@ -61,12 +59,7 @@ def value_history(coin: str = None):
return jsonify([entry.info() for entry in values])

coin_values = groupby(query.all(), key=lambda cv: cv.coin)
return jsonify(
{
coin.symbol: [entry.info() for entry in history]
for coin, history in coin_values
}
)
return jsonify({coin.symbol: [entry.info() for entry in history] for coin, history in coin_values})


@app.route("/api/total_value_history")
Expand All @@ -82,9 +75,7 @@ def total_value_history():
query = filter_period(query, CoinValue)

total_values: List[Tuple[datetime, float, float]] = query.all()
return jsonify(
[{"datetime": tv[0], "btc": tv[1], "usd": tv[2]} for tv in total_values]
)
return jsonify([{"datetime": tv[0], "btc": tv[1], "usd": tv[2]} for tv in total_values])


@app.route("/api/trade_history")
Expand Down Expand Up @@ -142,9 +133,7 @@ def coins():
with db.db_session() as session:
_current_coin = session.merge(db.get_current_coin())
_coins: List[Coin] = session.query(Coin).all()
return jsonify(
[{**coin.info(), "is_current": coin == _current_coin} for coin in _coins]
)
return jsonify([{**coin.info(), "is_current": coin == _current_coin} for coin in _coins])


@app.route("/api/pairs")
Expand Down
72 changes: 15 additions & 57 deletions binance_trade_bot/auto_trader.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ def transaction_through_bridge(self, pair: Pair):
"""
can_sell = False
balance = self.manager.get_currency_balance(pair.from_coin.symbol)
from_coin_price = self.manager.get_ticker_price(
pair.from_coin + self.config.BRIDGE
)
from_coin_price = self.manager.get_ticker_price(pair.from_coin + self.config.BRIDGE)

if balance and balance * from_coin_price > self.manager.get_min_notional(
pair.from_coin.symbol, self.config.BRIDGE.symbol
Expand All @@ -43,10 +41,7 @@ def transaction_through_bridge(self, pair: Pair):
else:
self.logger.info("Skipping sell")

if (
can_sell
and self.manager.sell_alt(pair.from_coin, self.config.BRIDGE) is None
):
if can_sell and self.manager.sell_alt(pair.from_coin, self.config.BRIDGE) is None:
self.logger.info("Couldn't sell, going back to scouting mode...")
return None

Expand All @@ -65,26 +60,16 @@ def update_trade_threshold(self, coin: Coin, coin_price: float):
"""

if coin_price is None:
self.logger.info(
"Skipping update... current coin {} not found".format(
coin + self.config.BRIDGE
)
)
self.logger.info(f"Skipping update... current coin {coin + self.config.BRIDGE} not found")
return

session: Session
with self.db.db_session() as session:
for pair in session.query(Pair).filter(Pair.to_coin == coin):
from_coin_price = self.manager.get_ticker_price(
pair.from_coin + self.config.BRIDGE
)
from_coin_price = self.manager.get_ticker_price(pair.from_coin + self.config.BRIDGE)

if from_coin_price is None:
self.logger.info(
"Skipping update for coin {} not found".format(
pair.from_coin + self.config.BRIDGE
)
)
self.logger.info(f"Skipping update for coin {pair.from_coin + self.config.BRIDGE} not found")
continue

pair.ratio = from_coin_price / coin_price
Expand All @@ -100,26 +85,14 @@ def initialize_trade_thresholds(self):
continue
self.logger.info(f"Initializing {pair.from_coin} vs {pair.to_coin}")

from_coin_price = self.manager.get_ticker_price(
pair.from_coin + self.config.BRIDGE
)
from_coin_price = self.manager.get_ticker_price(pair.from_coin + self.config.BRIDGE)
if from_coin_price is None:
self.logger.info(
"Skipping initializing {}, symbol not found".format(
pair.from_coin + self.config.BRIDGE
)
)
self.logger.info(f"Skipping initializing {pair.from_coin + self.config.BRIDGE}, symbol not found")
continue

to_coin_price = self.manager.get_ticker_price(
pair.to_coin + self.config.BRIDGE
)
to_coin_price = self.manager.get_ticker_price(pair.to_coin + self.config.BRIDGE)
if to_coin_price is None:
self.logger.info(
"Skipping initializing {}, symbol not found".format(
pair.to_coin + self.config.BRIDGE
)
)
self.logger.info(f"Skipping initializing {pair.to_coin + self.config.BRIDGE}, symbol not found")
continue

pair.ratio = from_coin_price / to_coin_price
Expand All @@ -137,16 +110,10 @@ def _get_ratios(self, coin: Coin, coin_price):
ratio_dict: Dict[Pair, float] = {}

for pair in self.db.get_pairs_from(coin):
optional_coin_price = self.manager.get_ticker_price(
pair.to_coin + self.config.BRIDGE
)
optional_coin_price = self.manager.get_ticker_price(pair.to_coin + self.config.BRIDGE)

if optional_coin_price is None:
self.logger.info(
"Skipping scouting... optional coin {} not found".format(
pair.to_coin + self.config.BRIDGE
)
)
self.logger.info(f"Skipping scouting... optional coin {pair.to_coin + self.config.BRIDGE} not found")
continue

self.db.log_scout(pair, pair.ratio, coin_price, optional_coin_price)
Expand All @@ -161,16 +128,11 @@ def _get_ratios(self, coin: Coin, coin_price):

if self.config.USE_MARGIN == "yes":
ratio_dict[pair] = (
(1 - transaction_fee) * coin_opt_coin_ratio / pair.ratio
- 1
- self.config.SCOUT_MARGIN / 100
(1 - transaction_fee) * coin_opt_coin_ratio / pair.ratio - 1 - self.config.SCOUT_MARGIN / 100
)
else:
ratio_dict[pair] = (
coin_opt_coin_ratio
- transaction_fee
* self.config.SCOUT_MULTIPLIER
* coin_opt_coin_ratio
coin_opt_coin_ratio - transaction_fee * self.config.SCOUT_MULTIPLIER * coin_opt_coin_ratio
) - pair.ratio
return ratio_dict

Expand All @@ -196,19 +158,15 @@ def bridge_scout(self):
bridge_balance = self.manager.get_currency_balance(self.config.BRIDGE.symbol)

for coin in self.db.get_coins():
current_coin_price = self.manager.get_ticker_price(
coin + self.config.BRIDGE
)
current_coin_price = self.manager.get_ticker_price(coin + self.config.BRIDGE)

if current_coin_price is None:
continue

ratio_dict = self._get_ratios(coin, current_coin_price)
if not any(v > 0 for v in ratio_dict.values()):
# There will only be one coin where all the ratios are negative. When we find it, buy it if we can
if bridge_balance > self.manager.get_min_notional(
coin.symbol, self.config.BRIDGE.symbol
):
if bridge_balance > self.manager.get_min_notional(coin.symbol, self.config.BRIDGE.symbol):
self.logger.info(f"Will be purchasing {coin} using bridge coin")
self.manager.buy_alt(coin, self.config.BRIDGE)
return coin
Expand Down
28 changes: 10 additions & 18 deletions binance_trade_bot/backtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,11 @@ def get_ticker_price(self, ticker_symbol: str):
if end_date > datetime.now():
end_date = datetime.now()
end_date = end_date.strftime("%d %b %Y %H:%M:%S")
self.logger.info(
f"Fetching prices for {ticker_symbol} between {self.datetime} and {end_date}"
)
self.logger.info(f"Fetching prices for {ticker_symbol} between {self.datetime} and {end_date}")
for result in self.binance_client.get_historical_klines(
ticker_symbol, "1m", target_date, end_date, limit=1000
):
date = datetime.utcfromtimestamp(result[0] / 1000).strftime(
"%d %b %Y %H:%M:%S"
)
date = datetime.utcfromtimestamp(result[0] / 1000).strftime("%d %b %Y %H:%M:%S")
price = float(result[1])
cache[f"{ticker_symbol} - {date}"] = price
cache.commit()
Expand All @@ -79,14 +75,12 @@ def buy_alt(self, origin_coin: Coin, target_coin: Coin):
target_balance = self.get_currency_balance(target_symbol)
from_coin_price = self.get_ticker_price(origin_symbol + target_symbol)

order_quantity = self._buy_quantity(
origin_symbol, target_symbol, target_balance, from_coin_price
)
order_quantity = self._buy_quantity(origin_symbol, target_symbol, target_balance, from_coin_price)
target_quantity = order_quantity * from_coin_price
self.balances[target_symbol] -= target_quantity
self.balances[origin_symbol] = self.balances.get(
origin_symbol, 0
) + order_quantity * (1 - self.get_fee(origin_coin, target_coin, False))
self.balances[origin_symbol] = self.balances.get(origin_symbol, 0) + order_quantity * (
1 - self.get_fee(origin_coin, target_coin, False)
)
self.logger.info(
f"Bought {origin_symbol}, balance now: {self.balances[origin_symbol]} - bridge: "
f"{self.balances[target_symbol]}"
Expand All @@ -107,13 +101,11 @@ def sell_alt(self, origin_coin: Coin, target_coin: Coin):
origin_balance = self.get_currency_balance(origin_symbol)
from_coin_price = self.get_ticker_price(origin_symbol + target_symbol)

order_quantity = self._sell_quantity(
origin_symbol, target_symbol, origin_balance
)
order_quantity = self._sell_quantity(origin_symbol, target_symbol, origin_balance)
target_quantity = order_quantity * from_coin_price
self.balances[target_symbol] = self.balances.get(
target_symbol, 0
) + target_quantity * (1 - self.get_fee(origin_coin, target_coin, True))
self.balances[target_symbol] = self.balances.get(target_symbol, 0) + target_quantity * (
1 - self.get_fee(origin_coin, target_coin, True)
)
self.balances[origin_symbol] -= order_quantity
self.logger.info(
f"Sold {origin_symbol}, balance now: {self.balances[origin_symbol]} - bridge: "
Expand Down
Loading

0 comments on commit caca9ad

Please sign in to comment.