import pandas as pd
import numpy as np
from binance.client import Client
client = Client()
from datetime import datetime
def getdailydata(symbol="BTCUSD", interval='1d', start_time=datetime(2019, 12, 31), end_time=datetime(2020, 12, 31)):
frame = pd.DataFrame(client.get_historical_klines(
symbol,
interval,
start_time.strftime("%-d %b, %Y"),
end_time.strftime("%-d %b, %Y")))
if len(frame) > 0:
frame = frame.iloc[:,:5]
frame.columns = ['DateTime', 'Open', 'High', 'Low', 'Close']
frame = frame.astype('float')
frame['DateTime'] = pd.to_datetime(frame['DateTime'], unit='ms')
return frame
from datetime import date
today = date.today()
price_df = getdailydata('ETHBTC', '1d', datetime(2021,1,1), today)
price_df['Change'] = price_df['Close'] - price_df['Open']
price_df['ChangeR'] = price_df['Change'] / price_df['Open']
price_df['ChangeP'] = price_df['ChangeR'] * 100
for index, row in price_df.iterrows():
change = price_df.loc[index, 'Change']
if index == 0 and change > 0:
price_df.loc[index, 'ConsecutivePositive'] = 1
price_df.loc[index, 'ConsecutiveNegative'] = 0
elif index == 0 and change < 0:
price_df.loc[index, 'ConsecutiveNegative'] = 1
price_df.loc[index, 'ConsecutivePositive'] = 0
elif change > 0:
price_df.loc[index, 'ConsecutivePositive'] = price_df.loc[index - 1,'ConsecutivePositive'] + 1
price_df.loc[index, 'ConsecutiveNegative'] = 0
elif change < 0:
price_df.loc[index, 'ConsecutiveNegative'] = price_df.loc[index - 1,'ConsecutiveNegative'] + 1
price_df.loc[index, 'ConsecutivePositive'] = 0
price_df['CummulativeProfit'] = price_df['Change'].cumsum()
price_df
| DateTime | Open | High | Low | Close | Change | ChangeR | ChangeP | ConsecutiveNegative | ConsecutivePositive | CummulativeProfit | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 2021-01-01 | 0.025464 | 0.025551 | 0.024800 | 0.024850 | -0.000614 | -0.024112 | -2.411247 | 1.0 | 0.0 | -0.000614 |
| 1 | 2021-01-02 | 0.024851 | 0.024955 | 0.023540 | 0.024062 | -0.000789 | -0.031749 | -3.174923 | 2.0 | 0.0 | -0.001403 |
| 2 | 2021-01-03 | 0.024062 | 0.030500 | 0.023083 | 0.029640 | 0.005578 | 0.231818 | 23.181780 | 0.0 | 1.0 | 0.004175 |
| 3 | 2021-01-04 | 0.029639 | 0.035000 | 0.028929 | 0.032551 | 0.002912 | 0.098249 | 9.824893 | 0.0 | 2.0 | 0.007087 |
| 4 | 2021-01-05 | 0.032552 | 0.034555 | 0.031094 | 0.032393 | -0.000159 | -0.004884 | -0.488449 | 1.0 | 0.0 | 0.006928 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 634 | 2022-09-27 | 0.069503 | 0.069972 | 0.068201 | 0.069609 | 0.000106 | 0.001525 | 0.152511 | 0.0 | 2.0 | 0.043781 |
| 635 | 2022-09-28 | 0.069610 | 0.069869 | 0.067550 | 0.068884 | -0.000726 | -0.010430 | -1.042954 | 1.0 | 0.0 | 0.043055 |
| 636 | 2022-09-29 | 0.068884 | 0.069187 | 0.068077 | 0.068188 | -0.000696 | -0.010104 | -1.010394 | 2.0 | 0.0 | 0.042359 |
| 637 | 2022-09-30 | 0.068189 | 0.069088 | 0.067598 | 0.068412 | 0.000223 | 0.003270 | 0.327032 | 0.0 | 1.0 | 0.042582 |
| 638 | 2022-10-01 | 0.068413 | 0.069002 | 0.067825 | 0.068044 | -0.000369 | -0.005394 | -0.539371 | 1.0 | 0.0 | 0.042213 |
639 rows × 11 columns
price_df.plot(x='DateTime', y=['ConsecutiveNegative'], grid=True, figsize=(20,5))
<AxesSubplot: xlabel='DateTime'>
price_df.plot(x='DateTime', y=['ConsecutivePositive'], grid=True, figsize=(20,5))
<AxesSubplot: xlabel='DateTime'>
price_df_non_zero = price_df.replace(0, np.NaN)
price_df_non_zero.hist('ConsecutivePositive', bins=50, figsize=(15,5))
array([[<AxesSubplot: title={'center': 'ConsecutivePositive'}>]],
dtype=object)
price_df_non_zero['ConsecutivePositive'].mean()
1.8789808917197452
price_df_non_zero.hist('ConsecutiveNegative', bins=50, figsize=(15,5))
array([[<AxesSubplot: title={'center': 'ConsecutiveNegative'}>]],
dtype=object)
price_df_non_zero['ConsecutiveNegative'].mean()
1.7938461538461539
import matplotlib.pyplot as plt
price_df.plot(x='DateTime', y=['ChangeP'], grid=True, figsize=(20,5))
<AxesSubplot: xlabel='DateTime'>
price_df['ChangeP'].median()
-0.05214572616448347
price_df['ChangeP'].mean()
0.20077621787429273
price_df['ChangeP'].max()
23.1817804006317
price_df['ChangeP'].min()
-15.517635407937835
price_df['ChangeP'].gt(0).sum()
314
price_df['ChangeP'].lt(0).sum()
325
price_df.boxplot('ChangeP',vert=False, showmeans=True, figsize=(20,2))
<AxesSubplot: >
price_df.hist('ChangeP', bins=50, figsize=(15,5))
array([[<AxesSubplot: title={'center': 'ChangeP'}>]], dtype=object)
from scipy import stats
stats.normaltest(price_df["ChangeP"])
NormaltestResult(statistic=173.94505933937833, pvalue=1.6916491204946663e-38)