def sensor(): print('in sensor and alive') try: if os.path.exists('owid-covid-data.xlsx'): os.remove('owid-covid-data.xlsx') if os.path.exists('owid-covid-data.csv'): os.remove('owid-covid-data.csv') if os.path.exists('data.csv'): os.remove('data.csv') if os.path.exists('datawoarea.csv'): os.remove('datawoarea.csv') if os.path.exists('last.csv'): os.remove('last.csv') except: print('not') global actcase global sstate global cs global d global de actcase, sstate, cs, d, de = pull_data.pull() df = pd.read_excel('owid-covid-data.xlsx', sheet_name='Sheet1') df.to_csv('owid-covid-data.csv') df = df[df['location'].str.contains("Bahrain", na=False)] df['date'] = pd.to_datetime(df['date']) case = df['total_cases'] date = df['date'] fig1 = go.Figure() fig1.add_trace( go.Scatter(x=list(date), y=list(case), name='Increment In Case'))
def single_plot(axes, symbol): days_ago = active_window["days_ago"] ticks = active_window["ticks"] alpha = active_window["alpha"] df = pull(start=n_days_ago(days_ago), symbol=symbol, bin_size=ticks) df['log_ret'] = np.log(df.close) - np.log(df.close.shift(1)) df["ema_log_ret"] = df.log_ret.ewm(alpha=alpha).mean() df["ema_log_var"] = df.log_ret.ewm(alpha=alpha).var() ax1, ax2 = axes #ax1.plot(df.index, df.log_ret, lw=1, alpha=0.8) ax1.plot(df.index, df.ema_log_ret, lw=1, alpha=0.8, label="logreturn %s" % symbol) ax2.plot(df.index, df.ema_log_var, label="%s variance" % symbol)
n = 200 nm_rolling = 40 # get stock data from yahoo finance stock = yf.Ticker("GOOG") dfyh = stock.history(period=f"{n+nm_rolling}d", interval="1d") dfyh = dfyh.filter(['Close'], axis=1) dfyh = dfyh.rename(columns={"Close": "goog_close"}) # set timezone to None in dfyh dfyh.index = dfyh.index.tz_localize(tz=None) sd = dfyh.index[0:1] start_date = dfyh.index[0:1].to_pydatetime()[0].astimezone(tz=None) # get bitcoin/usd data from bitmex dfbtc = pull(start=start_date, bin_size="1d") dfbtc.to_pickle("bitmex.pkl") dfbtc = pd.read_pickle("bitmex.pkl") # set timezone to None in dfbtc dfbtc.index = dfbtc.index.tz_localize(tz=None) dfbtc = dfbtc.filter(['close'], axis=1) dfbtc = dfbtc.rename(columns={"close": "btc_close"}) df = dfbtc.join(dfyh) df = df.dropna() df.btc_close = df.btc_close / df.btc_close.max() df.goog_close = df.goog_close / df.goog_close.max()
import numpy as np import pandas as pd from mplfinance.original_flavor import candlestick_ohlc from pull_data import n_days_ago, n_hours_ago, pull #import crosshair #df = pull(start=n_days_ago(200), bin_size='1d') #df.to_pickle("bitmex-daily-200d.pkl") #df = pull(start=n_hours_ago(4)) #df.to_pickle("bitmex.pkl") symbol = "XBTUSD" #symbol = "ETHUSD" df = pull(start=n_days_ago(5), symbol=symbol) df.to_pickle("bitmex.pkl") #df = pd.read_pickle("bitmex-daily-200d.pkl") df = pd.read_pickle("bitmex.pkl") # bin size = 1 minute # window = time / bin_size #window = 5 * 60 #df["sma-5hr"] = df["vwap"].rolling(window=window).mean() #window = 1 * 24 * 60 #df["sma-1day"] = df["vwap"].rolling(window=window).mean() #window = 5 * 24 * 60 #df["sma-5day"] = df["vwap"].rolling(window=window).mean() short_window = 5 * 60 long_window = 1 * 24 * 60
import matplotlib.pyplot as plt import math import numpy as np import pandas as pd import random from pull_data import n_days_ago, pull current_price = 11000 df = pull(start=n_days_ago(60), bin_size="1d") df.to_pickle("bitmex-daily-60d.pkl") df = pd.read_pickle("bitmex-daily-60d.pkl") percent_changes = df["vwap"].pct_change() log_return = np.log(df.vwap) - np.log(df.vwap.shift(1)) print(log_return) mean, stddev = log_return.mean(), log_return.std() print("Mean=%s Stddev=%s" % (mean, stddev)) liquidation_price = 7000 def run_single_simulation(start_price, stop_price): price = start_price for i in range(30): price_log_return = random.normalvariate(mean, stddev) price = price * math.exp(price_log_return) if price < stop_price: return price return price
import matplotlib.pyplot as plt import pandas as pd import time from pull_data import n_days_ago, n_hours_ago, pull plt.style.use('dark_background') days_ago = 10 symbol_perp = "XBTUSD" df = pull(start=n_days_ago(days_ago), symbol=symbol_perp, bin_size="1m") df.to_pickle("bitmex.pkl") df = pd.read_pickle("bitmex.pkl") #time.sleep(1) symbol_futures = "XBTZ20" df2 = pull(start=n_days_ago(days_ago), symbol=symbol_futures, bin_size="1m") df2.to_pickle("bitmex-%s.pkl" % symbol_futures) df2 = pd.read_pickle("bitmex-%s.pkl" % symbol_futures) df = pd.concat([df.close, df2.close], keys=["XBTUSD", "XBTZ20"], axis=1) df["spread"] = (df.XBTZ20 - df.XBTUSD) / df.XBTUSD print(df) print(df.spread.min()) print(df.spread.max()) print(df.spread.mean()) fig, (ax1, ax2) = plt.subplots(2, sharex=True, gridspec_kw={
import matplotlib.pyplot as plt import pandas as pd from pull_data import n_days_ago, n_hours_ago, pull # 1. First we fetch the data from the source # If you've already run this and have the file bitmex.pkl # then you can comment these lines below and just load straight from disk. # Pull the data from the exchange df = pull(start=n_days_ago(5), symbol="XBTUSD") # Save it to a file df.to_pickle("bitmex.pkl") # 2. We load the data # Load it from the file df = pd.read_pickle("bitmex.pkl") # 3. Do some processing... # 4. Graph the data plt.style.use('dark_background') plt.title("BTC spot price") plt.plot(df.close) plt.show()
import matplotlib.pyplot as plt import pandas as pd from pull_data import n_days_ago, pull df = pull(start=n_days_ago(10)) df.to_pickle("bitmex.pkl") df = pd.read_pickle("bitmex.pkl") lookback = 14 * 60 d_fast_period = 3 * 60 d_slow_period = 3 * 60 #lookback = 48 * 60 #d_fast_period = 3 * 60 #d_slow_period = 6 * 60 lows = df["low"].rolling(lookback).min() highs = df["high"].rolling(lookback).max() k = (df["close"] - lows) / (highs - lows) d_fast = k.rolling(d_fast_period).mean() d_slow = d_fast.rolling(d_slow_period).mean() print(lows) print(highs) plt.style.use('dark_background') fig, (ax1, ax2) = plt.subplots(2, sharex=True, gridspec_kw={ 'hspace': 0,