-
Notifications
You must be signed in to change notification settings - Fork 26
/
compair.py
90 lines (84 loc) · 3.11 KB
/
compair.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import pandas as pd
import time
import numpy as np
import pyupbit
import time
from datetime import datetime
from pytz import timezone
import pandas as pd
import telegram # pip install python-telegram-bot
import json
from dotenv import load_dotenv # pip install python-dotenv
import os
load_dotenv()
access = os.getenv('UPBIT_API_ACCESS_KEY')
secret = os.getenv('UPBIT_API_SECRET_KEY')
upbit = pyupbit.Upbit(access, secret)
coin_list = ["KRW-BTC", "KRW-ETH", "KRW-DOGE"]
days = 14
k_range = [0.4,0.5,0.6]
ma_range = [0,5,10,15]
total_list = []
values = []
dfs = []
not_1 = []
not_1_cnt = 0
not_1_total = 0
stay_total_list = []
for coin in coin_list:
time.sleep(0.5)
dfs.append(pyupbit.get_ohlcv(coin, count=days))#, to="20201019"))
print(days, "days")
for k in k_range:
for ma in ma_range:
total = 0
stay_total = 0
for idx, coin in enumerate(coin_list):
# print(coin)
# time.sleep(0.1)
df = dfs[idx] # pyupbit.get_ohlcv(coin, count=days)
time.sleep(0.01)
df2 = pyupbit.get_ohlcv(coin, count=days+ma+1)#, to="20201019")
# print(coin)
time.sleep(0.01)
df2['range1'] = (df2['high'] - df2['low']) * k
df['target1'] = df2['open'] + df2['range1'].shift(1)
# df['ror1'] = np.where(df['high'] >= df['target1'],
# df['close']*0.997 / df['target1'] ,
# 1)
# df2 = pyupbit.get_ohlcv(coin, count=days+ma)
time.sleep(0.05)
close = df2['close']
df_ma = close.rolling(window=ma).mean()
df['ma15'] = df_ma.shift(1)
df['ma15ortarget'] = np.where(df['target1'] >= df['ma15'], df['target1'], df['ma15'])
df['ror1'] = np.where(df['high'] >= df['ma15ortarget'],
(df['close'])*0.995/df['ma15ortarget'],
1
)
if ma == 0:
df['ma15'] = 0
df['ma15ortarget'] = np.where(df['target1'] >= df['ma15'], df['target1'], df['ma15'])
df['ror1'] = np.where(df['high'] >= df['ma15ortarget'],
(df['close'])*0.995/df['ma15ortarget'],
1
)
df['hpr1'] = df['ror1'].cumprod()
hpr1 = df['hpr1'][-1]
total += hpr1
df['hpr2'] = df['close'][-1]*0.995/df['open'][0]
hpr2 = df['hpr2'][-1]
stay_total += hpr2
# if hpr1 != 1:
# # print(f'{"%5s"%coin} ---- {hpr1} -> {hpr1*100 - 100}')
# not_1.append((hpr1, coin))
# not_1_cnt += 1
# not_1_total += hpr1
total_list.append((total/len(coin_list),k,ma,stay_total/len(coin_list)))
s = sorted(total_list, key = lambda x: (x[0]))
# for i in sorted(not_1, key = lambda x: x[0]):
# print(f'{"%9s"%i[1]} -> {"%2.3f"%(i[0]*100-100)}%')
for i in s:
print(f'{"% 3.2f"%(i[0]*100-100)}% ||| k {"%2.2f"%i[1]} ||| ma {"%2d"%i[2]} ||| 존버 {"%2.2f"%(i[3]*100-100)}%')
# print(f'"예상 평균 손익 %는 "{not_1_total/not_1_cnt*100-100}%')
# df.to_csv('testing2.csv')