Cleaned up code, using enums now for state, fixed OHLC interval, removed Herobrine

This commit is contained in:
Logan G 2021-02-10 16:08:40 -07:00
parent fcd3f76702
commit 8fdf742e70
Signed by: logan
GPG key ID: E328528C921E7A7A
3 changed files with 97 additions and 15 deletions

View file

@ -15,7 +15,7 @@ class Kraken:
# Gets a table of the price history of the pair since time at intervals of interval minutes
def get_ohlc_prices(self, pair, time=round(time.time())-(20*24*3600), interval=20):
def get_ohlc_prices(self, pair, time=round(time.time())-(20*24*3600), interval=15):
self.prices = self.kraken_wrapper.get_ohlc_data(pair, interval=interval, since=time)[0].close.iloc[::-1]

43
main.py
View file

@ -1,16 +1,23 @@
import time, threading, pandas, numpy
import time, threading, pandas, numpy, enum
from api.kraken import Kraken
from api.fake import Fake
class state(enum.Enum):
UNKNOWN = enum.auto(),
BUYING = enum.auto(),
SELLING = enum.auto()
if __name__ == "__main__":
current_state = "Unknown"
previous_state = "Unknown"
current_state = state.UNKNOWN
previous_state = state.UNKNOWN
currency_buy = "XXDG"
currency_buy = "XETH"
currency_sell = "ZUSD"
currency_pair = "XDGUSD"
currency_pair = "ETHUSD"
#buy_price = 0.0
k = Kraken()
print(1)
@ -31,33 +38,41 @@ if __name__ == "__main__":
k.get_current_price(currency_pair)
k.calculate_ema()
current_short_ema = k.short_ema.tail(1)[0][0] * (1-k.fee/100)
current_long_ema = k.long_ema.tail(1)[0][0]
current_price = k.prices.tail(1)[0][0]
# If we have crypto, check for sell behaviour, if we have fiat, check for buy behavior, otherwise, get really confused
if k.balances["vol"][currency_buy] > 10**-5:
if current_state == "Buying":
previous_state = "Buying"
# If the current (now previous) state was buying, set the previous state to the "current" state, then set the current state to the current one (selling)
if current_state == state.BUYING:
previous_state = state.BUYING
current_state = "Selling"
current_state = state.SELLING
# Sell shit
# If the short term EMA (accounting for the fee) is less than the long term EMA,
# and there is more than a 0.1% difference between short and long term EMA (accounting for the fee), sell
if k.short_ema.tail(1)[0][0] * (1-k.fee/100) < k.long_ema.tail(1)[0][0] and k.long_ema.tail(1)[0][0]/(k.short_ema.tail(1)[0][0] * (1-k.fee/100)) - 1 > 0.001:
if (current_short_ema < current_long_ema and current_long_ema/current_short_ema - 1 > 0.001):
print("Selling shit")
k.sell_order(currency_pair, currency_buy, currency_sell, k.balances["vol"][currency_buy])
print(k.balances)
elif k.balances["vol"][currency_sell] > 10**-2:
if current_state == "Selling":
previous_state = "Selling"
# If the current (now previous) state was selling, set the previous state to the "current" state, then set the current state to the current one (buying)
if current_state == state.SELLING:
previous_state = state.SELLING
current_state = "Buying"
current_state = state.BUYING
# Buy shit
# If the previous state is known, and the short term EMA (accounting for the fee) is greater than the long term EMA,
# and there is more than a 0.1% difference between short and long term EMA (accounting for the fee), buy
if previous_state != "Unknown" and k.short_ema.tail(1)[0][0] * (1-k.fee/100) > k.long_ema.tail(1)[0][0] and (k.short_ema.tail(1)[0][0] * (1-k.fee/100))/k.long_ema.tail(1)[0][0] - 1 > 0.001:
if previous_state != state.UNKNOWN and current_short_ema > current_long_ema and current_short_ema/current_long_ema - 1 > 0.001:
print("Buying shit")
k.buy_order(currency_pair, currency_buy, currency_sell, k.balances["vol"][currency_sell] * 0.95 * (1/k.prices.tail(1)[0][0]))
k.buy_order(currency_pair, currency_buy, currency_sell, k.balances["vol"][currency_sell] * 0.98 * (1/current_price))
#buy_price = k.prices.tail(1)[0][0]
print(k.balances)
else:

67
test.py Normal file
View file

@ -0,0 +1,67 @@
from pykrakenapi import KrakenAPI
import time, threading, pandas, krakenex, numpy
class Kraken:
def __init__(self):
self.krakenAPI = krakenex.API()
self.krakenAPI.load_key("kraken.key")
self.krakenWrapper = KrakenAPI(self.krakenAPI)
self.getBalances()
# Updates the current wallet prices from the internet
def getBalances(self):
__newBalances = self.krakenWrapper.get_account_balance()
#print(self.balances.loc["XXBT","vol"]-__newBalances.loc["XXBT","vol"])
#print(self.balances.loc["XXDG","vol"]-__newBalances.loc["XXDG","vol"])
#print(self.balances.loc["ZUSD","vol"]-__newBalances.loc["ZUSD","vol"])
self.balances = __newBalances
# Gets a table of the price history of the pair since time at intervals of interval minutes
def getPrices(self, pair, time=round(time.time())-(30*24*3600), interval=60):
self.prices = self.krakenWrapper.get_ohlc_data(pair, interval=interval, since=time)[0].close.iloc[::-1]
def buyOrder(self, currency_pair, currency_buy, currency_sell, amount, ordertype="market"):
self.krakenWrapper.add_standard_order(currency_pair, "buy", ordertype, volume=amount, validate=False)
#self.balances["vol"][currency_buy] += amount
#self.balances["vol"][currency_sell] -= amount * self.prices.tail(1)[0]
time.sleep(1)
self.getBalances()
def sellOrder(self, currency_pair, currency_buy, currency_sell, amount, ordertype="market"):
self.krakenWrapper.add_standard_order(currency_pair, "sell", ordertype, volume=amount, validate=False)
#self.balances["vol"][currency_buy] -= amount
#self.balances["vol"][currency_sell] += amount * self.prices.tail(1)[0]
time.sleep(1)
self.getBalances()
def calculateEMA(self, shortTime=round(time.time())-(7*24*3600), longTime=round(time.time())-(30*24*3600), interval=60):
self.longEMA = self.prices.ewm(span=12).mean()
self.shortEMA = self.prices.ewm(span=3).mean()
if __name__ == "__main__":
currency_buy = "XXBT"
currency_sell = "ZUSD"
currency_pair = "XXBTZUSD"
k = Kraken()
k.getPrices(currency_pair)
# OFFENSIVE CODE
current_price = numpy.float64(k.krakenWrapper.get_ticker_information(currency_pair)["c"][0][0])
new_row = pandas.DataFrame([[current_price]], index=[pandas.to_datetime(time.time(), unit = "s")])
k.prices = pandas.concat([k.prices, pandas.DataFrame(new_row)], ignore_index=False)
# END OFFENSIVE CODE
k.calculateEMA()
if k.balances["vol"][currency_buy] > 10**-5:
k.sellOrder(currency_pair, currency_buy, currency_sell, k.balances["vol"][currency_buy])
elif k.balances["vol"][currency_sell] > 10**-2:
k.buyOrder(currency_pair, currency_buy, currency_sell, k.balances["vol"][currency_sell] * (1/k.prices.tail(1)[0][0]))