Kernel: Python 3 (system-wide)
!pip install python-binance
╔══════════════════════════════╦═════════════════════════════════╗ ║ ⚠ NETWORK DISABLED ⚠ ║ ⚠ NO INTERNET ACCESS ⚠ ║ ╠══════════════════════════════╩═════════════════════════════════╣ ║ This project does not have access to the internet. ║ ║ Add a valid license in order to enable internet access. ║ ║ Otherwise, you cannot pull from a Git repository, use cURL, ║ ║ wget, download Python packages from PyPI, etc. ║ ║ Requests to load data will fail or hang indefinitely. ║ ║ ║ ║ https://cocalc.com/store/site-license ║ ║ ║ ╟────────────────────────────────────────────────────────────────╢ ║ Attempting to install software? It might already be available! ║ ║ https://cocalc.com/software ║ ╟────────────────────────────────────────────────────────────────╢ ║ However, you can become admin in a 𝗖𝗢𝗠𝗣𝗨𝗧𝗘 𝗦𝗘𝗥𝗩𝗘𝗥! ║ ║ Learn more here: https://doc.cocalc.com/compute_server.html ║ ╚════════════════════════════════════════════════════════════════╝ Defaulting to user installation because normal site-packages is not writeable WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7fb77e514700>, 'Connection to pypi.org timed out. (connect timeout=15)')': /simple/python-binance/ WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7fb77e5149d0>, 'Connection to pypi.org timed out. (connect timeout=15)')': /simple/python-binance/
from binance.client import Client
import pandas as pd
client = Client('3HiIRv2T0TghHPHBf6NTljzwEZvnnZOOXkiQi1EnpRabHyXmlHvVmLrSpgVL4cRc','7ViJeoMGZ7Bt6e39LYGsKBzYNSGqqlGKbW9rPhfJDos5IJAKI09aEpO4SGCDOhA3')
pair = 'ADAUSDT'
pd.DataFrame(client.get_historical_klines(pair, '1m', '30 min ago UTC'))
def getminutedata(symbol, interval, lookback): frame = pd.DataFrame(client.get_historical_klines(symbol, interval, lookback+' min ago UTC')) frame = frame.iloc[:,:6] frame.columns = ['Time','Open','High','Low','Close','Volume'] frame = frame.set_index('Time') frame.index = pd.to_datetime(frame.index, unit='ms') frame = frame.astype(float) return frame
test = getminutedata(pair, '1m', '30')
test.Open.plot()
def strategytest(symbol,qty, entried=False): i = 0 df = getminutedata(symbol,'1m','30m') cumulret = (df.Open.pct_change() +1).cumprod() - 1 if not entried: if cumulret[-1] > 0.001: order = client.create_order(symbol=symbol, side='BUY', type='MARKET', quantity=qty) print(order) i += 2 entried=True print('Begin') else: print('No Trade has been executed') if entried: while i > 1: print('1') df = getminutedata(symbol,'1m','30m') sincebuy = df.loc[df.index > pd.to_datetime( order['transactTime'], unit='ms')] if len(sincebuy) > 0: print('2') sincebuyret = (sincebuy.Open.pct_change() +1).cumprod() - 1 if sincebuyret[-1] > 0.3: order = client.create_order(symbol=symbol, side='SELL', type='MARKET', quantity=qty) print(order) print('Trade has been Sold') elif sincebuyret[-1] > -0.25: order = client.create_order(symbol=symbol, side='BUY', type='MARKET', quantity=qty * 2) print(order) print('Trade has been purchased')
strategytest('ADAUSDT', 9)