<jupyter_start><jupyter_text>Pull sentiment and BTC/USDT data from bitcurate API.Can read more about this API at https://doc.api.bitcurate.com/<jupyter_code>r = requests.get('https://datascience.api.dev.bitcurate.com/social_sentiment?query=(BTC%20OR%20bitcoin)%20AND%20binance&before_date=7/28/2019%200:0')
sentiment = r.json()
sentiment.keys()
r = requests.get('https://datascience.api.dev.bitcurate.com/pair?before_date=7/28/2019%200:0&pair=BTC/USDT&exchange=binance')
btc = r.json()
btc.keys()
plt.figure(figsize=(10,3))
plt.subplot(1,2,1)
sns.distplot(btc['close'])
plt.title('BTC/USDT histogram')
plt.subplot(1,2,2)
sns.distplot(sentiment['sentiment'])
plt.title('sentiment histogram')
plt.show()
df_sentiment = pd.DataFrame(sentiment)
df_btc = pd.DataFrame(btc)
merged = df_sentiment.merge(df_btc, on = 'timestamp')
merged.head()
number_simulation = 100
predict_day = 30
v = merged[['sentiment', 'close']].pct_change(1).dropna().values
variance = np.abs(v.T.dot(v) / v.shape[0])
daily_vol = np.sqrt(variance)
avg_daily_ret = np.mean(v,axis=0)
daily_drift = avg_daily_ret - (variance / 2)
drift = daily_drift - 0.5 * daily_vol ** 2

results_sentiment = pd.DataFrame()
results_close = pd.DataFrame()

for i in tqdm(range(number_simulation)):
    prices = []
    prices.append(merged[['sentiment', 'close']].iloc[-1].values)
    for d in range(predict_day):
        shock = np.random.normal(size=(1,2)).dot(drift + daily_vol)
        price = prices[-1] * np.exp(shock)
        prices.append(price[0])
    prices = np.array(prices)
    results_sentiment[i] = prices[:,0]
    results_close[i] = prices[:,1]
number_simulation = 100
predict_day = 30

close = merged['close'].tolist()
returns = pd.DataFrame(close).pct_change()
last_price = close[-1]
results = pd.DataFrame()
avg_daily_ret = returns.mean()
variance = returns.var()
daily_vol = returns.std()
daily_drift = avg_daily_ret - (variance / 2)
drift = daily_drift - 0.5 * daily_vol ** 2

results = pd.DataFrame()

for i in tqdm(range(number_simulation)):
    prices = []
    prices.append(merged['close'].iloc[-1])
    for d in range(predict_day):
        shock = [drift + daily_vol * np.random.normal()]
        shock = np.mean(shock)
        price = prices[-1] * np.exp(shock)
        prices.append(price)
    results[i] = prices
plt.figure(figsize=(15,5))
plt.subplot(1,2,1)
plt.plot(results_close)
plt.ylabel('Value')
plt.xlabel('Simulated days')
plt.title('multivariate monte carlo on BTC/USDT with sentiment')
plt.subplot(1,2,2)
plt.plot(results)
plt.ylabel('Value')
plt.xlabel('Simulated days')
plt.title('singlevariate monte carlo on BTC/USDT')
plt.show()
raveled = results.values.ravel()
raveled.sort()
cp_raveled = raveled.copy()

raveled_close = results_close.values.ravel()
raveled_close.sort()
cp_raveled_close = raveled_close.copy()

plt.figure(figsize=(17,5))
plt.subplot(1,3,1)
plt.plot(results_close)
plt.ylabel('Value')
plt.xlabel('Simulated days')
plt.subplot(1,3,2)
sns.distplot(close,norm_hist=True)
plt.title('$\mu$ = %.2f, $\sigma$ = %.2f'%(np.mean(close),np.std(close)))
plt.subplot(1,3,3)
sns.distplot(raveled,norm_hist=True,label='univariate monte carlo samples')
sns.distplot(raveled_close,norm_hist=True,label='multivariate monte carlo samples')
sns.distplot(close,norm_hist=True,label='real samples')
plt.title('simulation $\mu$ = %.2f, $\sigma$ = %.2f'%(raveled.mean(),raveled.std()))
plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17343065693430657, 'fraction_numerical': 0.027737226277372264, 'mean_word_length': 5.550669216061186, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '522236', 'license': 'Apache-2.0', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1326, 'n_words': 209, 'path': 'simulation/multivariate-drift-monte-carlo.ipynb', 'repo_name': 'wangbin94/Stock-Prediction-Models-1', 'size': '3425'}