From df270c23653f5a1062e418ba877cf5b0f0c86764 Mon Sep 17 00:00:00 2001 From: Death916 Date: Tue, 25 May 2021 04:24:18 -0700 Subject: [PATCH] integrate kraken --- README.md | 19 ++++++++++ emailtrade/getsignal.py | 16 +++++++- emailtrade/kraken.py | 54 +++++++++++++++++---------- emailtrade/trade.py | 0 emailtrade/trex.py | 82 ++++++++++++++++++++--------------------- 5 files changed, 108 insertions(+), 63 deletions(-) delete mode 100644 emailtrade/trade.py diff --git a/README.md b/README.md index 270d96e..c786a75 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,22 @@ # emailtrade Automatically buy/sell crypto from tradingview alert email Create keys.json file in project root and add bittrex or kraken keys +and email and password for gmail + +If you like this software: +Get me a beer + ETH: +0x8De9E8409F427602533c9493a5bAFa5E4F7A8266 + +BTC: +bc1qhxqk8g72rs8yrvselfauztxr2emdc7sje3dkta + +DOGE: +DLdv7fgdz3ADhsMmszGsXGQSmRP2pWJzPE + +LBC: +lbc1qgmtmg498m353zyj6n9cwq0ueeakyx8pj2h6sr0 + + + Say thanks + twitter.com/sacoftrees916 \ No newline at end of file diff --git a/emailtrade/getsignal.py b/emailtrade/getsignal.py index e493f53..e6c1dfc 100644 --- a/emailtrade/getsignal.py +++ b/emailtrade/getsignal.py @@ -1,10 +1,22 @@ import glogin -import trade +import trex import history as hist - +import kraken import time +MARKET = input("Pease enter what market you want to use: Kraken or bittrex").lower() +if MARKET == "kraken": + trade = kraken +elif MARKET == "bittrex": + trade = trex + +# TODO make above cleaner in get_signal + + + +# TODO add try/except for market to make sure its correct + # login to server diff --git a/emailtrade/kraken.py b/emailtrade/kraken.py index 71c4961..829feed 100644 --- a/emailtrade/kraken.py +++ b/emailtrade/kraken.py @@ -4,60 +4,76 @@ import history as hist import os import krakenex from pykrakenapi import KrakenAPI +import pandas as pd +price = 0 with open(os.getcwd() + "/keys.json") as k: keys = json.load(k) API_KEY = keys["public"] PRIV_KEY = keys["priv"] -TICKER = "ETHUSD" -API = krakenex.API(API_KEY, PRIV_KEY) -# BUY_COIN = input("what coin to buy") -# SELL_COIN = input("what to buy it with") +TICKER= "ETHUSD" +API = krakenex.api(API_KEY, PRIV_KEY) +#BUY_CURRENCY = input().capitalize() + kraken = KrakenAPI(API) +buyprice = "" +sellprice = "" -def marketcheck(): - markets = kraken.get_ohlc_data(TICKER) +def marketcheck(TICKER): + markets = kraken.get_ohlc_data(TICKER) price = markets[0]['close'][0] print(TICKER, "price is ", price) return price -# done -buyprice = marketcheck() - +#done def open_trade(): + buyprice = marketcheck(TICKER) balancedf = kraken.get_account_balance() balance = balancedf.vol["ZUSD"] buy_amount = (balance / buyprice) - (balance / buyprice) * 0.0025 - # kraken.add_standard_order(TICKER, "buy", "market", balance) - print("buying", buy_amount, "of", TICKER, " for ", balance) + # kraken.add_standard_order(TICKER,) + print("buying", buy_amount, "of", TICKER) hist.tradehist( - "bought " + str(buy_amount) + " of " + TICKER + " at " + str(buyprice) + "bought " + str(buy_amount) + " of " + TICKER+ " at " + str(buyprice) ) - return buy_amount + return def close_trade(): - sell_price = marketcheck() - balancedf = kraken.get_account_balance() - sell_amount = balancedf.vol['XETH'] - # print(kraken.add_standard_order(TICKER, "sell", "market", sell_amount)) + sellprice = marketcheck(TICKER) + if TICKER == 'ETHUSD': + symbol = 'XETH' + if TICKER == 'XBTUSD': + symbol ='XXBT' + + + balance = kraken.get_account_balance() + + sell_amount = balance.vol[symbol] + # print(trex.sell_limit(TICKER, sell_amount, rate=sellprice)) print("selling", sell_amount, "of", TICKER) hist.tradehist( - "sold " + str(sell_amount) + " of " + TICKER + " at " + str(sell_price) + "sold " + str(sell_amount) + " of " + TICKER+ " at " + str(sellprice) ) hist.tradehist( "profit = " - + "{:.25f}".format((sell_price * sell_amount) - (buyprice * sell_amount)) + + "{:.25f}".format((sellprice * sell_amount) - (buyprice * sell_amount)) ) def open_orders(): kraken.get_open_orders(True) return + +def open_positions(): + kraken.get_open_positions() + return kraken.get_open_positions() + +# TODO uncomment buy/sells \ No newline at end of file diff --git a/emailtrade/trade.py b/emailtrade/trade.py deleted file mode 100644 index e69de29..0000000 diff --git a/emailtrade/trex.py b/emailtrade/trex.py index c07cdb9..0272dc5 100644 --- a/emailtrade/trex.py +++ b/emailtrade/trex.py @@ -2,7 +2,7 @@ import json from bittrex import Bittrex import history as hist import os -import kraken + price = 0 @@ -10,55 +10,53 @@ with open(os.getcwd() + "/keys.json") as k: keys = json.load(k) -MARKET = input("Pease enter what market you want to use: Kraken or bittrex").lower() -# TODO add try/except for market to make sure its correct - -class bittrex_trade(): - - api_key = keys["api_key"] - priv_key = keys["priv_key"] - ticker = "BTC-ETH" - trex = Bittrex(api_key, priv_key) - def marketcheck(self, ticker): - markets = trex.get_ticker(ticker) - price = markets["result"]["Ask"] - print(ticker, "price is ", price) - return price + +api_key = keys["api_key"] +priv_key = keys["priv_key"] +ticker = "BTC-ETH" +trex = Bittrex(api_key, priv_key) - buyprice = marketcheck(ticker) +def marketcheck(ticker): + markets = trex.get_ticker(ticker) + price = markets["result"]["Ask"] + print(ticker, "price is ", price) + return price - def open_trade(self): - - balance = trex.get_balance("BTC")["result"]["Available"] - buy_amount = (balance / buyprice) - (balance / buyprice) * 0.0025 - print(trex.buy_limit(ticker, buy_amount, rate=buyprice)) - print("buying", buy_amount, "of", ticker) - hist.tradehist( - "bought " + str(buy_amount) + " of " + ticker + " at " + str(buyprice) - ) - return +buyprice = marketcheck(ticker) - def close_trade(): +def open_trade(): - sell_price = marketcheck(ticker) - - sell_amount = trex.get_balance("ETH")["result"]["Available"] - print(trex.sell_limit(ticker, sell_amount, rate=sell_price)) - print("selling", sell_amount, "of", ticker) - hist.tradehist( - "sold " + str(sell_amount) + " of " + ticker + " at " + str(sell_price) - ) - hist.tradehist( - "profit = " - + "{:.25f}".format((sell_price * sell_amount) - (buyprice * sell_amount)) - ) + balance = trex.get_balance("BTC")["result"]["Available"] + buy_amount = (balance / buyprice) - (balance / buyprice) * 0.0025 + print(trex.buy_limit(ticker, buy_amount, rate=buyprice)) + print("buying", buy_amount, "of", ticker) + hist.tradehist( + "bought " + str(buy_amount) + " of " + ticker + " at " + str(buyprice) + ) + return - def open_orders(): - trex.get_open_orders() - return +def close_trade(): + + sell_price = marketcheck(ticker) + + sell_amount = trex.get_balance("ETH")["result"]["Available"] + print(trex.sell_limit(ticker, sell_amount, rate=sell_price)) + print("selling", sell_amount, "of", ticker) + hist.tradehist( + "sold " + str(sell_amount) + " of " + ticker + " at " + str(sell_price) + ) + hist.tradehist( + "profit = " + + "{:.25f}".format((sell_price * sell_amount) - (buyprice * sell_amount)) + ) + + +def open_orders(): + trex.get_open_orders() + return