This commit is contained in:
dsyoon
2023-01-24 16:19:15 +09:00
parent 9605634a50
commit 4cc3ba2bbf

View File

@@ -10,7 +10,7 @@ from math import nan
import plotly.io as po
from plotly import subplots
import plotly.graph_objects as go
from datetime import datetime
from datetime import datetime, timedelta
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import StandardScaler, MinMaxScaler
@@ -211,10 +211,10 @@ class Bithumb_minute(HTS):
data = data.astype(
{
'open': 'int',
'high': 'int',
'low': 'int',
'close': 'int',
'open': 'float',
'high': 'float',
'low': 'float',
'close': 'float',
'avg3': 'float',
'avg4': 'float',
'avg5': 'float',
@@ -234,8 +234,8 @@ class Bithumb_minute(HTS):
'disparity_avg20': 'float',
'disparity_avg60': 'float',
'disparity_avg120': 'float',
'buy': 'int',
'sell': 'int',
'buy': 'float',
'sell': 'float',
'slow_k': 'float',
'slow_d': 'float',
'macd': 'float',
@@ -459,142 +459,6 @@ class Bithumb_minute(HTS):
continue
if i > 10:
"""
# 만약 전일 저가와 오늘 종의 차이가 1만원이 넘으면 향후 60일은 분석하지 않는다.
if data1['high'][i] < int(data1['low'][i - 1] * 0.7):
gap_state = True
gap_interval -= 1
continue
if gap_state:
if gap_interval <= 0:
gap_state = False
gap_interval = 60
else:
gap_interval -= 1
continue
if data1['disparity'][i] < 2:
check = True
for l in range(i - 3, i):
if (
data1['gradients_avg60'][l - 1] > data1['gradients_avg60'][l] or
data1['gradients_avg20'][l - 1] > data1['gradients_avg20'][l] or
data1['gradients_low'][l - 1] > data1['gradients_low'][l] or
data1['disparity_avg5'][l - 1] > data1['disparity_avg5'][l] or
data1['disparity'][l - 1] < data1['disparity'][l]
):
check = False
break
if check and 99 < sum(data1['disparity_avg5'][i - 4:i + 1]) / 5 < 100 and 99 < sum(
data1['disparity_avg60'][i - 4:i + 1]) / 5 < 100:
buy = data1['low'][i]
data1['buy'][i] = buy
bsLine['buy'][i] = buy
bsLine['buy_weight'][i] = 0.1
check = True
for l in range(i - 2, i):
if (
data1['gradients_avg60'][l - 1] > data1['gradients_avg60'][l] or
data1['gradients_low'][l - 1] > data1['gradients_low'][l]
):
check = False
break
if (
check and
-0.0011 < data1['gradients_low'][i] < 0 and -0.007 < data1['gradients_avg5'][i] < 0.001 and
-0.0012 < data1['gradients_avg60'][i] < 0 and
98.90 < data1['disparity_avg5'][i] < 101
):
buy = data1['low'][i]
data1['buy'][i] = buy
bsLine['buy'][i] = buy
bsLine['buy_weight'][i] = 0.1
check = True
for l in range(i - 6, i):
if (
data1['gradients_avg60'][l - 1] < data1['gradients_avg60'][l] or
data1['gradients_avg20'][l - 1] < data1['gradients_avg20'][l] or
data1['gradients_low'][l - 1] < data1['gradients_low'][l] or
-0.039 < data1['gradients_low'][l - 1] < -0.35 or
-0.05 < data1['gradients_avg20'][l - 1] < -0.30 or
-0.40 < data1['gradients_avg60'][l - 1] < -0.30
):
check = False
break
if check and 99 < min(data1['disparity_avg5'][i - 6:i]) < max(data1['disparity_avg5'][i - 6:i]) < 101:
buy = data1['low'][i]
data1['buy'][i] = buy
bsLine['buy'][i] = buy
bsLine['buy_weight'][i] = 0.1
check = True
for l in range(i - 3, i):
if (
data1['gradients_low'][l - 1] < data1['gradients_low'][l] or
data1['gradients_avg60'][l - 1] < data1['gradients_avg60'][l] or
data1['gradients_avg20'][l - 1] < data1['gradients_avg20'][l] or
0.01 < data1['gradients_low'][l - 1] < 0.21 or
-0.09 < data1['gradients_avg20'][l - 1] < -0.002 or
0.01 < data1['gradients_avg60'][l - 1] < 0.021
):
check = False
break
if check:
buy = data1['low'][i]
data1['buy'][i] = buy
bsLine['buy'][i] = buy
bsLine['buy_weight'][i] = 0.1
if (data1['disparity'][i] < 5 and 99.0 < data1['disparity_avg60'][i] < 99.1 and
-0.009 < data1['gradients_avg60'][i] < -0.008 and 0.015 < data1['gradients_avg20'][i] < 0.016 and
-0.006 < data1['gradients_avg5'][i] < -0.005 and -0.009 < data1['gradients_low'][i] < -0.008):
check = True
for l in range(i - 5, i):
if (
data1['gradients_avg60'][l - 1] > data1['gradients_avg60'][l] or
data1['gradients_low'][l - 1] > data1['gradients_low'][l] or
data1['disparity'][l - 1] < data1['disparity'][l]
):
check = False
break
if check:
buy = data1['low'][i]
data1['buy'][i] = buy
bsLine['buy'][i] = buy
bsLine['buy_weight'][i] = 0.1
if data1['macd'][i] < -4000:
if data1['macd'][i - 1] < data1['macd'][i]:
if not self.notBuy(data1, i) and data1['slow_k'][i] < 30:
buy = data1['low'][i]
data1['buy'][i] = buy
bsLine['buy'][i] = buy
bsLine['buy_weight'][i] = 0.1
# macd 이전에 없던 바닥인 경우 상승할 찰나 매수
if data1['macds'][i - 1] < min(data1['macds'][:i - 1]):
if data1['macds'][i - 1] < data1['macds'][i]:
if not self.notBuy(data1, i) and data1['slow_k'][i] < 30:
buy = data1['low'][i]
data1['buy'][i] = buy
bsLine['buy'][i] = buy
bsLine['buy_weight'][i] = 0.1
if (
98 < data1['disparity_avg5'][i] < 100 and data1['disparity_avg20'][i] < 93.5 and
data1['disparity_avg60'][i] < 89 and
-0.014 < data1['gradients_avg60'][i] < -0.013 and -0.03 < data1['gradients_avg20'][i] < -0.02 and -0.014 < data1['gradients_low'][i] < -0.013 and
data1['slow_k'][i] < 11
):
if not self.notBuy(data1, i):
buy = data1['low'][i]
data1['buy'][i] = buy
bsLine['buy'][i] = buy
bsLine['buy_weight'][i] = 0.1
"""
if data1['slow_k'][i] < 20 and data1['slow_k'][i - 1] < data1['slow_d'][i - 1] and data1['slow_d'][i] < data1['slow_k'][i]:
buy = data1['low'][i]
@@ -646,8 +510,9 @@ class Bithumb_minute(HTS):
df.set_index('datetime', inplace=True)
return df
def cancel_order(self, log_df, min=10):
df = log_df[pd.DatetimeIndex(log_df.index).minute >= min]
def cancel_order(self, log_df, log_filename, min=5):
now = datetime.now() - timedelta(minutes=min)
df = log_df.loc[(log_df.index <= now) ]
df.reset_index()
if df is not None:
@@ -655,8 +520,14 @@ class Bithumb_minute(HTS):
order = (df['order0'][i], df['order1'][i], df['order2'][i], df['order3'][i])
cancel = self.bithumb.cancel_order(order)
log_df = log_df[pd.DatetimeIndex(log_df.index).minute < min]
return
log_df = log_df.loc[(log_df.index > now)]
if len(log_df) == 0:
log_df["datetime"] = ""
else:
log_df["datetime"] = log_df.index
log_df.to_csv(log_filename, index=False)
return log_df
def getStock(self, ticker, analyzed_day, minute=5):
stock = {"CODE": ticker, "NAME": ticker, "PRICE": []}
@@ -707,14 +578,15 @@ class Bithumb_minute(HTS):
log_filename = os.path.join(RESOURCE_PATH, 'order', "bithumb"+"_"+today + '.log')
if os.path.exists(log_filename):
log_df = pd.read_csv(log_filename)
log_df.columns = ["type", "datetime", "order0", "order1", "order2", "order3", "slow_k", "price", "count"]
log_df.columns = ["type", "order0", "order1", "order2", "order3", "slow_k", "price", "count", "datetime"]
log_df["datetime"] = pd.to_datetime(log_df["datetime"], format='%Y-%m-%d %H:%M:%S')
else:
log_df = pd.DataFrame(columns=["type", "datetime", "order0", "order1", "order2", "order3", "slow_k", "price", "count"])
log_df['datetime'] = pd.to_datetime(log_df['datetime'], unit='s')
log_df.set_index('datetime', inplace=True)
# 10분이 지난 미체결은 취소한다.
self.cancel_order(log_df, 10)
log_df = self.cancel_order(log_df, log_filename, 5)
if isRealTime:
if max(bsLine['buy'][len(bsLine['buy']) - 2:]) > 100:
tmp = self.bithumb.get_balance(ticker)
@@ -724,10 +596,16 @@ class Bithumb_minute(HTS):
# order: ('bid', 'BTC', 'C0101000000322993432', 'KRW')
if len(stock1['close']) > 0:
print(ticker, "/", datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "/", stock1['close'][len(stock1['close']) - 1], "/ BUY / ", stock1['slow_k'][len(stock1['slow_k']) - 1], "/", bsLine['buy'][len(bsLine['buy']) - 1], "/", count)
value = {"type": "BUY", "datetime": datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "order0": order[0], "order1": order[1], "order2": order[2], "order3": order[3], "slow_k": stock1['slow_k'][len(stock1['slow_k']) - 1], "price": bsLine['buy'][len(bsLine['buy']) - 1], "count": count}
log_df = log_df.append(value, ignore_index=True)
log_df['datetime'] = pd.to_datetime(log_df['datetime'], unit='s')
log_df.set_index('datetime', inplace=True)
value = {"type": "BUY", "order0": order[0], "order1": order[1], "order2": order[2], "order3": order[3], "slow_k": stock1['slow_k'][len(stock1['slow_k']) - 1], "price": bsLine['buy'][len(bsLine['buy']) - 1], "count": count}
datetime_value = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
value_df = pd.DataFrame(value, index=[datetime_value])
indexes = log_df.index.tolist()
indexes.append(datetime_value)
log_df = log_df.append(value_df, ignore_index = True)
log_df.index = indexes
log_df['datetime'] = log_df.index
log_df.to_csv(log_filename, index=False)
dirName = os.path.join(RESOURCE_PATH, 'analysis', 'bithumb')
@@ -741,10 +619,16 @@ class Bithumb_minute(HTS):
order = self.bithumb.sell_limit_order(ticker, bsLine['sell'][len(bsLine['sell'])-1], count)
if len(stock1['close'])>0:
print(ticker, "/", datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "/", stock1['close'][len(stock1['close']) - 1], "/ SELL / ", stock1['slow_k'][len(stock1['slow_k']) - 1], "/", bsLine['sell'][len(bsLine['sell']) - 1], "/", count)
value = {"type": "SELL", "datetime": datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "order0": order[0], "order1": order[1], "order2": order[2], "order3": order[3], "slow_k": stock1['slow_k'][len(stock1['slow_k']) - 1], "price": bsLine['buy'][len(bsLine['buy']) - 1], "count": count}
log_df = log_df.append(value, ignore_index=True)
log_df['datetime'] = pd.to_datetime(log_df['datetime'], unit='s')
log_df.set_index('datetime', inplace=True)
value = {"type": "SELL", "order0": order[0], "order1": order[1], "order2": order[2], "order3": order[3], "slow_k": stock1['slow_k'][len(stock1['slow_k']) - 1], "price": bsLine['buy'][len(bsLine['buy']) - 1], "count": count}
datetime_value = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
value_df = pd.DataFrame(value, index=[datetime_value])
indexes = log_df.index.tolist()
indexes.append(datetime_value)
log_df = log_df.append(value_df, ignore_index=True)
log_df.index = indexes
log_df['datetime'] = log_df.index
log_df.to_csv(log_filename, index=False)
dirName = os.path.join(RESOURCE_PATH, 'analysis', 'bithumb')
@@ -780,7 +664,7 @@ if __name__ == "__main__":
#size = len(data_daily)
#if data_daily['slow_k'] < 30:
bithumb.buyRealTime(ticker, analyzed_day, isRealTime)
time.sleep(300)
time.sleep(30)
else:
for ticker in tickers:
bithumb.buyRealTime(ticker, analyzed_day, isRealTime)