|
| 1 | +# Based on basic_live_algo.py, this algorithm's trading strategy |
| 2 | +# is different in that it will take LONG / SHORT on a basket of |
| 3 | +# technical indicators instead of a "simple" SMA strategy |
| 4 | + |
| 5 | +# Imports |
| 6 | +import json, requests, websocket |
| 7 | +from pprint import pprint |
| 8 | +import numpy as np |
| 9 | + |
| 10 | + |
| 11 | +# Define variables |
| 12 | +SYMBOL = "BTCUSDT" |
| 13 | +INTERVAL = "1m" |
| 14 | +WSS_BASE_URL = "wss://fstream.binance.com" |
| 15 | +WSS_ENDPOINT = "/ws/{}@kline_{}".format( |
| 16 | + SYMBOL.lower(), |
| 17 | + INTERVAL |
| 18 | +) |
| 19 | +data = [] |
| 20 | +closes = [] |
| 21 | +position = 0 |
| 22 | + |
| 23 | + |
| 24 | +# Function to get historical data |
| 25 | +def futures_get_hist(symbol, interval): |
| 26 | + ''' |
| 27 | + Get historical klines for a futures pair |
| 28 | + :symbol: str, i.e "BTCUSDT" |
| 29 | + :interval: str, i.e "1m" |
| 30 | + ''' |
| 31 | + r = requests.get( |
| 32 | + "https://fapi.binance.com" + "/fapi/v1/klines", |
| 33 | + params = { |
| 34 | + 'symbol': symbol, |
| 35 | + 'interval': interval |
| 36 | + } |
| 37 | + ) |
| 38 | + |
| 39 | + req = r.json() |
| 40 | + return req |
| 41 | + |
| 42 | + |
| 43 | +# Technical indicators |
| 44 | +# Trend |
| 45 | +def sma(arr, window): |
| 46 | + ''' |
| 47 | + Standard Moving Average |
| 48 | + :arr: array |
| 49 | + :window: window on which SMA will be calculated |
| 50 | + ''' |
| 51 | + return np.convolve(arr, np.ones(window), 'valid') / window |
| 52 | + |
| 53 | + |
| 54 | +def ewma(arr, alpha, window): |
| 55 | + ''' |
| 56 | + Exponential Weighted Moving Average |
| 57 | + :arr: array |
| 58 | + :param alpha: specify decay [0, 1] |
| 59 | + :window: length of ewma |
| 60 | + ''' |
| 61 | + arr = arr[-(len(arr) - window + 1):] # Re-adjusting length of arr so it has the same len as sma |
| 62 | + ewma_arr = np.zeros_like(arr) # returns an array of zeros the same length as arr |
| 63 | + ewma_arr[0] = arr[0] # first value in list ewma_arr is equal to first value in list arr |
| 64 | + for t in range(1, arr.shape[0]): |
| 65 | + ewma_arr[t] = alpha * arr[t] + (1 - alpha) * ewma_arr[t - 1] |
| 66 | + |
| 67 | + return ewma_arr |
| 68 | + |
| 69 | + |
| 70 | +# Momentum |
| 71 | +def rsi(): |
| 72 | + return rsi |
| 73 | + |
| 74 | +def macd(): |
| 75 | + return macd |
| 76 | + |
| 77 | +# Volatility |
| 78 | +def bbands(arr, window): |
| 79 | + ''' |
| 80 | + Bollinger Bands. Given an array and a certain timeframe (window), returns: |
| 81 | + `bb_mid` : simple moving average |
| 82 | + `bb_top` : bb_mid + 2 standard devs |
| 83 | + `bb_bot` : bb_mid - 2 standard devs |
| 84 | + |
| 85 | + params: |
| 86 | + :arr: array, list object |
| 87 | + :window: int |
| 88 | + ''' |
| 89 | + bb_mid = sma( |
| 90 | + arr, |
| 91 | + window=window |
| 92 | + )[-1] |
| 93 | + |
| 94 | + bb_top = bb_mid + 2 * np.std( |
| 95 | + arr, |
| 96 | + ddof=1 |
| 97 | + ) |
| 98 | + |
| 99 | + bb_bot = bb_mid - 2 * np.std( |
| 100 | + arr, |
| 101 | + ddof=1 |
| 102 | + ) |
| 103 | + |
| 104 | + bollingerbands = [ |
| 105 | + bb_bot, |
| 106 | + bb_mid, |
| 107 | + bb_top |
| 108 | + ] |
| 109 | + |
| 110 | + return bollingerbands |
| 111 | + |
| 112 | + |
| 113 | +# Websocket functions |
| 114 | + |
| 115 | + |
| 116 | +# WebSocketApp |
0 commit comments