-
Notifications
You must be signed in to change notification settings - Fork 0
/
arb_conduct.py
191 lines (178 loc) · 8.57 KB
/
arb_conduct.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import pandas as pd
import sqlalchemy
from binance.client import Client
import time
import warnings
warnings.filterwarnings('ignore')
def get_initial_amount(symbol,start):
price = float(client.futures_historical_klines(symbol, '1d', start)[0][1])
return price
def price_SQL_updater():
colums_name = ['Time']
price_list = [time.time()]
for x in coinlist:
price_list.append(float((client.futures_symbol_ticker(symbol=x)['price'])))
colums_name.append(x)
frame = pd.DataFrame(columns=colums_name)
frame.loc[len(frame)] = price_list
frame.to_sql('pricetable',engine,if_exists='append')
def order_SQL_updater(order):
df = pd.DataFrame(order,index=[0])
df.to_sql('order',engine,if_exists='append')
def check_position_information(symbol, miniamount):
account = client.futures_account()
positionlist = []
existshortposition = False
existlongposition = False
int(miniamount)
longpositionamount = 0
shortpositionamount = 0
long_position_roe = 0
short_position_roe = 0
for b1 in account['positions']:
if b1['symbol'] == symbol and float(b1['initialMargin']) > 0:
d = dict()
d['initialmargin'] = float(b1['initialMargin'])
d['unrealizedprofit'] = float(b1['unrealizedProfit'])
d['amt'] = float(b1['positionAmt'])
d['roe'] = round(float(b1['unrealizedProfit']) / float(b1['initialMargin']), 3)
d['side'] = b1['positionSide']
side = b1['positionSide']
if side == 'LONG':
longpositionamount = float(b1['positionAmt'])
long_position_roe = d['roe']
if longpositionamount >= miniamount:
existlongposition = True
else:
existlongposition = False
elif side == 'SHORT':
shortpositionamount = abs(float(b1['positionAmt']))
short_position_roe = d['roe']
if shortpositionamount >= miniamount:
existshortposition = True
else:
existshortposition = False
positionlist.append(d)
positionlist1 = {'positioninfo': positionlist,
'existshortposition': existshortposition,
'existlongposition': existlongposition,
'exist_shortposition_amount': shortpositionamount,
'exist_longposition_amount': longpositionamount,
'long_position_roe': long_position_roe,
'short_position_roe': short_position_roe}
return positionlist1
def simple_strategy(symbol, lookback):
df = pd.read_sql(symbol, engine)
EMA144 = round(df['0'].iloc[-1], 4)
df_price = pd.read_sql('arbpricetable', engine)
price1 = df_price[symbol].iloc[-1]
position = check_position_information(symbol = symbol, miniamount=2/3*simple_strategy.qty)
print(simple_strategy.qty,position)
price2 = float((client.futures_symbol_ticker(symbol=symbol)['price']))
price_list.append(price2)
colums_name = ['time', 'ARBUSDT']
frame = pd.DataFrame(columns=colums_name)
frame.loc[len(frame)] = price_list
frame.to_sql('arbpricetable', engine, if_exists='append')
print(price1, price2, EMA144)
if price2 > EMA144 and EMA144 >= price1 and round(float(position['exist_longposition_amount']),0)==0:
if round(float(position['exist_shortposition_amount']), 0) > 0:
simple_strategy.counter += 1
order = client.futures_create_order(symbol=symbol,
type ='MARKET',
side ='BUY',
quantity =round(float(position['exist_shortposition_amount']), 0),
positionSide ='SHORT')
print(order)
order1 = client.futures_create_order(symbol=symbol,
type='MARKET',
side='BUY',
quantity=round(simple_strategy.qty, 0),
positionSide='LONG')
print(order1)
order_SQL_updater(order1)
simple_strategy.qty = 2 * simple_strategy.qty
else:
simple_strategy.counter += 1
order1 = client.futures_create_order(symbol=symbol,
type='MARKET',
side='BUY',
quantity=round(simple_strategy.qty,0),
positionSide='LONG')
print(order1)
simple_strategy.qty = 2 * simple_strategy.qty
if price1 >= EMA144 and price2 < EMA144 and round(float(position['exist_shortposition_amount']),0)==0:
if round(float(position['exist_longposition_amount']), 0) > 0:
simple_strategy.counter += 1
order = client.futures_create_order(symbol=symbol,
type = 'MARKET',
side = 'SELL',
quantity = round(float(position['exist_longposition_amount']), 0),
positionSide='LONG')
print(order)
order1 = client.futures_create_order(symbol=symbol,
type='MARKET',
side='SELL',
quantity= round(simple_strategy.qty,0),
positionSide='SHORT')
simple_strategy.qty = 2 * simple_strategy.qty
else:
simple_strategy.counter += 1
order1 = client.futures_create_order(symbol=symbol,
type='MARKET',
side='SELL',
quantity= round(simple_strategy.qty,0),
positionSide='SHORT')
print(simple_strategy.qty)
print(order1)
order_SQL_updater(order1)
simple_strategy.qty = 2 * simple_strategy.qty
else:
if position['long_position_roe'] < -0.08:
client.futures_create_order(symbol=symbol,
type='MARKET',
side='SELL',
quantity=round(float(position['exist_longposition_amount']),0),
positionSide='LONG')
elif position['long_position_roe'] > 0.3 and simple_strategy.qty == 0:
client.futures_create_order(symbol=symbol,
type='MARKET',
side='SELL',
quantity=round(simple_strategy.qty*0.3, 0),
positionSide='LONG')
elif position['short_position_roe'] < -0.08:
client.futures_create_order(symbol=symbol,
type='MARKET',
side='BUY',
quantity=round(float(position['exist_shortposition_amount']), 0),
positionSide='SHORT')
elif position['short_position_roe'] > 0.3 and simple_strategy.qty == 0:
client.futures_create_order(symbol=symbol,
type='MARKET',
side='BUY',
quantity=round(simple_strategy.qty*0.3, 0),
positionSide='SHORT')
if position['long_position_roe']>0.1 or position['long_position_roe']>0.1:
simple_strategy.qty = 6
## initial information
client = Client('Public_Key', 'Private_key', {"verify": False, "timeout":5})
engine = sqlalchemy.create_engine('sqlite:///crypto0316_db.db')
coinlist = ['ARBUSDT']
amountlist = dict()
start = '2023-03-26'
usdt_amount = 0.5
leverage = 12
simple_strategy.counter = 0
simple_strategy.qty = 6
for x in coinlist:
client.futures_change_leverage(symbol=x, leverage=leverage)
price = get_initial_amount(symbol=x, start=start)
qty = round((leverage*usdt_amount/len(coinlist)/price),0)
amountlist[x] = qty
print(amountlist)
while True:
try:
price_list=[time.time()]
simple_strategy(symbol='ARBUSDT', lookback=144)
except:
print('error')