Skip to content

Commit

Permalink
saving progress
Browse files Browse the repository at this point in the history
  • Loading branch information
whittlem committed Jul 16, 2022
1 parent dc0935d commit 14605b0
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 104 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"request": "launch",
"program": "pycryptobot.py",
"console": "integratedTerminal",
"args": []
"args": ["--sim", "fast"]
}
]
}
80 changes: 23 additions & 57 deletions controllers/PyCryptoBot.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import os
import sys
import time
import math
import json
import random
import sched
import signal
import functools
import pandas as pd
from typing import Union
from datetime import datetime, timedelta
from os.path import exists as file_exists
from urllib3.exceptions import ReadTimeoutError
Expand All @@ -31,6 +29,8 @@
from models.helper.LogHelper import Logger
from models.Strategy import Strategy
from views.TradingGraphs import TradingGraphs
from utils.PyCryptoBot import truncate as _truncate
from utils.PyCryptoBot import compare as _compare

try:
# pyright: reportMissingImports=false
Expand Down Expand Up @@ -901,15 +901,15 @@ def execute_job(self):

# Since precision does not change after this point, it is safe to prepare a tailored `truncate()` that would
# work with this precision. It should save a couple of `precision` uses, one for each `truncate()` call.
truncate = functools.partial(self.truncate, n=precision)
truncate = functools.partial(_truncate, n=precision)

if immediate_action:
self.price_text = str(self.price)
else:
self.price_text = "Close: " + str(self.price)
ema_text = ""
if self.disablebuyema is False:
ema_text = self.compare(
ema_text = _compare(
self.df_last["ema12"].values[0],
self.df_last["ema26"].values[0],
"EMA12/26",
Expand All @@ -918,7 +918,7 @@ def execute_job(self):

macd_text = ""
if self.disablebuymacd is False:
macd_text = self.compare(
macd_text = _compare(
self.df_last["macd"].values[0],
self.df_last["signal"].values[0],
"MACD",
Expand Down Expand Up @@ -1081,7 +1081,7 @@ def execute_job(self):
)
)
+ "% |"
+ " CURR self.price is "
+ " CURR price is "
+ str(
round(
(
Expand Down Expand Up @@ -1659,7 +1659,7 @@ def execute_job(self):
+ "\n - TEST BUY at "
+ self.price_text
+ "\n - Buy Size: "
+ str(self.truncate(self.state.last_buy_size, 4))
+ str(_truncate(self.state.last_buy_size, 4))
)

if not self.is_verbose:
Expand Down Expand Up @@ -2177,7 +2177,7 @@ def execute_job(self):
Logger.info(f" Sell Count : {str(self.state.sell_count)}")
Logger.info(f" First Buy : {str(self.state.first_buy_size)}")
Logger.info(
f" Last Buy : {str(self.truncate(self.state.last_buy_size, 4))}"
f" Last Buy : {str(_truncate(self.state.last_buy_size, 4))}"
)
else:
simulation["data"]["sell_count"] = self.state.sell_count
Expand All @@ -2187,12 +2187,12 @@ def execute_job(self):
if self.state.sell_count > 0:
if not self.simresultonly:
Logger.info(
f" Last Sell : {self.truncate(self.state.last_sell_size, 4)}\n"
f" Last Sell : {_truncate(self.state.last_sell_size, 4)}\n"
)
else:
simulation["data"]["last_trade"] = {}
simulation["data"]["last_trade"]["size"] = float(
self.truncate(self.state.last_sell_size, 2)
_truncate(self.state.last_sell_size, 2)
)
else:
if not self.simresultonly:
Expand All @@ -2215,12 +2215,12 @@ def execute_job(self):
+ f" Buy Count: {self.state.buy_count}\n"
+ f" Sell Count: {self.state.sell_count}\n"
+ f" First Buy: {self.state.first_buy_size}\n"
+ f" Last Buy: {str(self.truncate(self.state.last_buy_size, 4))}\n"
+ f" Last Sell: {str(self.truncate(self.state.last_sell_size, 4))}\n"
+ f" Last Buy: {str(_truncate(self.state.last_buy_size, 4))}\n"
+ f" Last Sell: {str(_truncate(self.state.last_sell_size, 4))}\n"
)

if self.state.sell_count > 0:
_last_trade_margin = self.truncate(
_last_trade_margin = _truncate(
(
(
(self.state.last_sell_size - self.state.last_buy_size)
Expand All @@ -2241,13 +2241,13 @@ def execute_job(self):
)
Logger.info("\n")
Logger.info(
f" All Trades Buys ({self.quote_currency}): {self.truncate(self.state.buy_tracker, 2)}"
f" All Trades Buys ({self.quote_currency}): {_truncate(self.state.buy_tracker, 2)}"
)
Logger.info(
f" All Trades Profit/Loss ({self.quote_currency}): {self.truncate(self.state.profitlosstracker, 2)} ({self.truncate(self.state.feetracker,2)} in fees)"
f" All Trades Profit/Loss ({self.quote_currency}): {_truncate(self.state.profitlosstracker, 2)} ({_truncate(self.state.feetracker,2)} in fees)"
)
Logger.info(
f" All Trades Margin : {self.truncate(self.state.margintracker, 4)}%"
f" All Trades Margin : {_truncate(self.state.margintracker, 4)}%"
)
Logger.info("\n")
Logger.info(" ** non-live simulation, assuming highest fees")
Expand All @@ -2261,16 +2261,16 @@ def execute_job(self):
"quote_currency"
] = self.quote_currency
simulation["data"]["all_trades"]["value_buys"] = float(
self.truncate(self.state.buy_tracker, 2)
_truncate(self.state.buy_tracker, 2)
)
simulation["data"]["all_trades"]["profit_loss"] = float(
self.truncate(self.state.profitlosstracker, 2)
_truncate(self.state.profitlosstracker, 2)
)
simulation["data"]["all_trades"]["fees"] = float(
self.truncate(self.state.feetracker, 2)
_truncate(self.state.feetracker, 2)
)
simulation["data"]["all_trades"]["margin"] = float(
self.truncate(self.state.margintracker, 4)
_truncate(self.state.margintracker, 4)
)

## Revised telegram Summary notification to give total margin in addition to last trade margin.
Expand All @@ -2282,7 +2282,7 @@ def execute_job(self):
f"\nOpen Trade Margin at end of simulation: {self.state.open_trade_margin}\n"
)
self.notify_telegram(
f" All Trades Margin: {self.truncate(self.state.margintracker, 4)}%\n ** non-live simulation, assuming highest fees\n ** open trade excluded from margin calculation\n"
f" All Trades Margin: {_truncate(self.state.margintracker, 4)}%\n ** non-live simulation, assuming highest fees\n ** open trade excluded from margin calculation\n"
)
self.telegram_bot.remove_active_bot()

Expand Down Expand Up @@ -2324,10 +2324,10 @@ def execute_job(self):
):
# update margin for telegram bot
self.telegram_bot.add_margin(
str(self.truncate(margin, 4) + "%")
str(_truncate(margin, 4) + "%")
if self.state.in_open_trade is True
else " ",
str(self.truncate(profit, 2)) if self.state.in_open_trade is True else " ",
str(_truncate(profit, 2)) if self.state.in_open_trade is True else " ",
self.price,
change_pcnt_high,
self.state.action,
Expand Down Expand Up @@ -3700,40 +3700,6 @@ def get_maker_fee(self):
else:
return 0.005

def truncate(self, f: Union[int, float], n: Union[int, float]) -> str:
"""
Format a given number ``f`` with a given precision ``n``.
"""

if not isinstance(f, int) and not isinstance(f, float):
return "0.0"

if not isinstance(n, int) and not isinstance(n, float):
return "0.0"

if (f < 0.0001) and n >= 5:
return f"{f:.5f}"

# `{n}` inside the actual format honors the precision
return f"{math.floor(f * 10 ** n) / 10 ** n:.{n}f}"

def compare(self, val1, val2, label="", precision=2):
if val1 > val2:
if label == "":
return f"{self.truncate(val1, precision)} > {self.truncate(val2, precision)}"
else:
return f"{label}: {self.truncate(val1, precision)} > {self.truncate(val2, precision)}"
if val1 < val2:
if label == "":
return f"{self.truncate(val1, precision)} < {self.truncate(val2, precision)}"
else:
return f"{label}: {self.truncate(val1, precision)} < {self.truncate(val2, precision)}"
else:
if label == "":
return f"{self.truncate(val1, precision)} = {self.truncate(val2, precision)}"
else:
return f"{label}: {self.truncate(val1, precision)} = {self.truncate(val2, precision)}"

def get_buy_percent(self):
try:
return int(self.buypercent)
Expand Down
20 changes: 10 additions & 10 deletions models/Trading.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,12 +950,12 @@ def addStochasticRSI(self, period: int) -> None:
# true if sma stochrsi is above the 15
self.df["rsi15"] = self.df["smastoch" + str(period)] > 15
self.df["rsi15co"] = self.df.rsi15.ne(self.df.rsi15.shift())
self.df.loc[self.df["rsi15"] is False, "rsi15co"] = False
self.df.loc[self.df["rsi15"] == False, "rsi15co"] = False

# true if sma stochrsi is below the 85
self.df["rsi85"] = self.df["smastoch" + str(period)] < 85
self.df["rsi85co"] = self.df.rsi85.ne(self.df.rsi85.shift())
self.df.loc[self.df["rsi85"] is False, "rsi85co"] = False
self.df.loc[self.df["rsi85"] == False, "rsi85co"] = False

def addWilliamsR(self, period: int) -> None:
"""Adds the Willams %R to the DataFrame"""
Expand Down Expand Up @@ -1248,29 +1248,29 @@ def addEMABuySignals(self) -> None:
self.df["ema8gtema12"] = self.df.ema8 > self.df.ema12
# true if the current frame is where EMA8 crosses over above
self.df["ema8gtema12co"] = self.df.ema8gtema12.ne(self.df.ema8gtema12.shift())
self.df.loc[self.df["ema8gtema12"] is False, "ema8gtema12co"] = False
self.df.loc[self.df["ema8gtema12"] == False, "ema8gtema12co"] = False

# true if the EMA8 is below the EMA12
self.df["ema8ltema12"] = self.df.ema8 < self.df.ema12
# true if the current frame is where EMA8 crosses over below
self.df["ema8ltema12co"] = self.df.ema8ltema12.ne(self.df.ema8ltema12.shift())
self.df.loc[self.df["ema8ltema12"] is False, "ema8ltema12co"] = False
self.df.loc[self.df["ema8ltema12"] == False, "ema8ltema12co"] = False

# true if EMA12 is above the EMA26
self.df["ema12gtema26"] = self.df.ema12 > self.df.ema26
# true if the current frame is where EMA12 crosses over above
self.df["ema12gtema26co"] = self.df.ema12gtema26.ne(
self.df.ema12gtema26.shift()
)
self.df.loc[self.df["ema12gtema26"] is False, "ema12gtema26co"] = False
self.df.loc[self.df["ema12gtema26"] == False, "ema12gtema26co"] = False

# true if the EMA12 is below the EMA26
self.df["ema12ltema26"] = self.df.ema12 < self.df.ema26
# true if the current frame is where EMA12 crosses over below
self.df["ema12ltema26co"] = self.df.ema12ltema26.ne(
self.df.ema12ltema26.shift()
)
self.df.loc[self.df["ema12ltema26"] is False, "ema12ltema26co"] = False
self.df.loc[self.df["ema12ltema26"] == False, "ema12ltema26co"] = False

def addSMABuySignals(self) -> None:
"""Adds the SMA50/SMA200 buy and sell signals to the DataFrame"""
Expand Down Expand Up @@ -1302,15 +1302,15 @@ def addSMABuySignals(self) -> None:
self.df["sma50gtsma200co"] = self.df.sma50gtsma200.ne(
self.df.sma50gtsma200.shift()
)
self.df.loc[self.df["sma50gtsma200"] is False, "sma50gtsma200co"] = False
self.df.loc[self.df["sma50gtsma200"] == False, "sma50gtsma200co"] = False

# true if the SMA50 is below the SMA200
self.df["sma50ltsma200"] = self.df.sma50 < self.df.sma200
# true if the current frame is where SMA50 crosses over below
self.df["sma50ltsma200co"] = self.df.sma50ltsma200.ne(
self.df.sma50ltsma200.shift()
)
self.df.loc[self.df["sma50ltsma200"] is False, "sma50ltsma200co"] = False
self.df.loc[self.df["sma50ltsma200"] == False, "sma50ltsma200co"] = False

def addMACDBuySignals(self) -> None:
"""Adds the MACD/Signal buy and sell signals to the DataFrame"""
Expand Down Expand Up @@ -1339,15 +1339,15 @@ def addMACDBuySignals(self) -> None:
self.df["macdgtsignalco"] = self.df.macdgtsignal.ne(
self.df.macdgtsignal.shift()
)
self.df.loc[self.df["macdgtsignal"] is False, "macdgtsignalco"] = False
self.df.loc[self.df["macdgtsignal"] == False, "macdgtsignalco"] = False

# true if the MACD is below the Signal
self.df["macdltsignal"] = self.df.macd < self.df.signal
# true if the current frame is where MACD crosses over below
self.df["macdltsignalco"] = self.df.macdltsignal.ne(
self.df.macdltsignal.shift()
)
self.df.loc[self.df["macdltsignal"] is False, "macdltsignalco"] = False
self.df.loc[self.df["macdltsignal"] == False, "macdltsignalco"] = False

def get_fib_ret_levels(self, price: float = 0) -> dict:
# validates price is numeric
Expand Down
20 changes: 10 additions & 10 deletions models/Trading_Pta.py
Original file line number Diff line number Diff line change
Expand Up @@ -932,12 +932,12 @@ def addStochasticRSI(self, period: int) -> None:
# true if sma stochrsi is above the 15
self.df["rsi15"] = self.df["smastoch" + str(period)] > 15
self.df["rsi15co"] = self.df.rsi15.ne(self.df.rsi15.shift())
self.df.loc[self.df["rsi15"] is False, "rsi15co"] = False
self.df.loc[self.df["rsi15"] == False, "rsi15co"] = False

# true if sma stochrsi is below the 85
self.df["rsi85"] = self.df["smastoch" + str(period)] < 85
self.df["rsi85co"] = self.df.rsi85.ne(self.df.rsi85.shift())
self.df.loc[self.df["rsi85"] is False, "rsi85co"] = False
self.df.loc[self.df["rsi85"] == False, "rsi85co"] = False

def addWilliamsR(self, period: int=20) -> None:
"""Adds the Willams %R to the DataFrame"""
Expand Down Expand Up @@ -1236,29 +1236,29 @@ def addEMABuySignals(self) -> None:
self.df["ema8gtema12"] = self.df.ema8 > self.df.ema12
# true if the current frame is where EMA8 crosses over above
self.df["ema8gtema12co"] = self.df.ema8gtema12.ne(self.df.ema8gtema12.shift())
self.df.loc[self.df["ema8gtema12"] is False, "ema8gtema12co"] = False
self.df.loc[self.df["ema8gtema12"] == False, "ema8gtema12co"] = False

# true if the EMA8 is below the EMA12
self.df["ema8ltema12"] = self.df.ema8 < self.df.ema12
# true if the current frame is where EMA8 crosses over below
self.df["ema8ltema12co"] = self.df.ema8ltema12.ne(self.df.ema8ltema12.shift())
self.df.loc[self.df["ema8ltema12"] is False, "ema8ltema12co"] = False
self.df.loc[self.df["ema8ltema12"] == False, "ema8ltema12co"] = False

# true if EMA12 is above the EMA26
self.df["ema12gtema26"] = self.df.ema12 > self.df.ema26
# true if the current frame is where EMA12 crosses over above
self.df["ema12gtema26co"] = self.df.ema12gtema26.ne(
self.df.ema12gtema26.shift()
)
self.df.loc[self.df["ema12gtema26"] is False, "ema12gtema26co"] = False
self.df.loc[self.df["ema12gtema26"] == False, "ema12gtema26co"] = False

# true if the EMA12 is below the EMA26
self.df["ema12ltema26"] = self.df.ema12 < self.df.ema26
# true if the current frame is where EMA12 crosses over below
self.df["ema12ltema26co"] = self.df.ema12ltema26.ne(
self.df.ema12ltema26.shift()
)
self.df.loc[self.df["ema12ltema26"] is False, "ema12ltema26co"] = False
self.df.loc[self.df["ema12ltema26"] == False, "ema12ltema26co"] = False

def addSMABuySignals(self) -> None:
"""Adds the SMA50/SMA200 buy and sell signals to the DataFrame"""
Expand Down Expand Up @@ -1290,15 +1290,15 @@ def addSMABuySignals(self) -> None:
self.df["sma50gtsma200co"] = self.df.sma50gtsma200.ne(
self.df.sma50gtsma200.shift()
)
self.df.loc[self.df["sma50gtsma200"] is False, "sma50gtsma200co"] = False
self.df.loc[self.df["sma50gtsma200"] == False, "sma50gtsma200co"] = False

# true if the SMA50 is below the SMA200
self.df["sma50ltsma200"] = self.df.sma50 < self.df.sma200
# true if the current frame is where SMA50 crosses over below
self.df["sma50ltsma200co"] = self.df.sma50ltsma200.ne(
self.df.sma50ltsma200.shift()
)
self.df.loc[self.df["sma50ltsma200"] is False, "sma50ltsma200co"] = False
self.df.loc[self.df["sma50ltsma200"] == False, "sma50ltsma200co"] = False

def addMACDBuySignals(self) -> None:
"""Adds the MACD/Signal buy and sell signals to the DataFrame"""
Expand Down Expand Up @@ -1327,15 +1327,15 @@ def addMACDBuySignals(self) -> None:
self.df["macdgtsignalco"] = self.df.macdgtsignal.ne(
self.df.macdgtsignal.shift()
)
self.df.loc[self.df["macdgtsignal"] is False, "macdgtsignalco"] = False
self.df.loc[self.df["macdgtsignal"] == False, "macdgtsignalco"] = False

# true if the MACD is below the Signal
self.df["macdltsignal"] = self.df.macd < self.df.signal
# true if the current frame is where MACD crosses over below
self.df["macdltsignalco"] = self.df.macdltsignal.ne(
self.df.macdltsignal.shift()
)
self.df.loc[self.df["macdltsignal"] is False, "macdltsignalco"] = False
self.df.loc[self.df["macdltsignal"] == False, "macdltsignalco"] = False

def get_fib_ret_levels(self, self.price: float = 0) -> dict:
# validates self.price is numeric
Expand Down
Loading

0 comments on commit 14605b0

Please sign in to comment.