include trade.py in getsignal and add functions to trade

This commit is contained in:
Death916 2018-06-22 05:04:20 -07:00
parent 2b4cda19d0
commit fb37e718b3
2 changed files with 65 additions and 41 deletions

View file

@ -6,6 +6,7 @@ import glogin
import subprocess import subprocess
import time import time
import psutil import psutil
import trade
# login to server # login to server
@ -22,7 +23,7 @@ def gconnect():
msg = imap.fetch([uid], [b'BODY[]', b'FLAGS']) msg = imap.fetch([uid], [b'BODY[]', b'FLAGS'])
global message global message
message = pyzmail.PyzMessage.factory(msg[uid][b'BODY[]']) message = pyzmail.PyzMessage.factory(msg[uid][b'BODY[]'])
print('connected')
last_alert = 0 last_alert = 0
@ -38,66 +39,74 @@ def kill(proc_pid):
def start_buy(): def start_buy():
print('trade is a buy') print('trade is a buy')
proc = subprocess.Popen(['taskmgr'], stdout=subprocess.PIPE, shell=True) trade.open_trade(1)
global last_alert global last_alert
last_alert = "buy" last_alert = "buy"
time.sleep(5)
kill(proc.pid)
def start_sell(): def start_sell():
print('trade is a sell') print('trade is a sell')
proc = subprocess.Popen(['taskmgr'], stdout=subprocess.PIPE, shell=True) trade.close_trade(1)
global last_alert global last_alert
last_alert = "sell" last_alert = "sell"
time.sleep(5)
kill(proc.pid)
def get_signal(): def get_signal():
try: try:
if 'strategy says sell now' in message.get_subject() or last_alert == 'buy': if 'strategy says sell now' in message.get_subject() or last_alert == 'buy':
print('sell signal found') print('sell signal found')
# print(message.get_subject()) coin = message.get_subject()
print('got sell signal') print(coin)
return "sell" return "sell"
elif 'strategy says buy now' in message.get_subject(): elif last_alert == 'sell' or last_alert == 0:
print('buy signal found') print('buy signal found')
# print(message.get_subject()) coin = message.get_subject()
"""if 'ETHBTC' in coin: # TODO check for current ticker to use
print('true')
else:
print('false') """
print(coin)
return "buy" return "buy"
except: except:
print('failed') print('failed')
last_uid = 0 last_uid = 0
def main():
while True:
while True: global uid
gconnect()
print(uid)
signal = get_signal()
print(time.ctime())
global uid try:
gconnect() global last_uid
print(uid) if last_uid != uid:
signal = get_signal()
print(time.ctime())
try: if signal == "buy" and last_alert != 'buy':
start_buy()
print(last_alert)
last_uid = uid
elif signal == "sell" and last_alert == "buy":
start_sell()
print(last_alert)
last_uid = uid
else:
print('same uid')
except:
print(' fail')
print(trade.open_orders())
time.sleep(600)
if last_uid != uid: if __name__ == '__main__':
main()
if signal == "buy" and last_alert != 'buy':
start_buy()
print(last_alert)
last_uid = uid
elif signal == "sell" and last_alert == "buy":
start_sell()
print(last_alert)
last_uid = uid
else:
print('same uid')
except:
print(' fail')
time.sleep(600)

View file

@ -1,27 +1,42 @@
#! python3 #! python3
import json import json
from bittrex.bittrex import Bittrex, API_V2_0 from bittrex.bittrex import Bittrex
amount = 0 amount = 0
price = 0 price = 0
with open('D:/code/emailtrade/keys.json') as k: with open('D:/code/emailtrade/keys.json') as k:
keys = json.load(k) keys = json.load(k)
api_key = keys['api_key'] api_key = keys['api_key']
priv_key = keys['priv_key'] priv_key = keys['priv_key']
ticker = 'BTC-ETH'
trex = Bittrex(api_key, priv_key)
trex = Bittrex(api_key, priv_key, api_version=API_V2_0)
def marketcheck(ticker): def marketcheck(ticker):
markets = trex.get_ticker(ticker) markets = trex.get_ticker(ticker)
price = markets['result']['Ask'] price = markets['result']['Ask']
print(markets['result']) print(markets['result'])
print(price)
return price return price
marketcheck('BTC-LTC')
def open_trade(amount):
marketcheck(ticker)
trex.buy_limit(ticker, amount, rate=None)
print('buying', amount, 'of', ticker)
def close_trade(amount):
marketcheck(ticker)
trex.sell_limit(ticker, amount, rate =None)
print('selling', amount, 'of', ticker)
def open_orders():
trex.get_open_orders()
#open_buy('1')
#def open_buy():
# trex.buy_limit(ticker, amount, price)