-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
148 lines (138 loc) · 4.94 KB
/
app.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
import json, config
import re
from flask import Flask,request,jsonify
app = Flask(__name__)
from binance_f import RequestClient
from binance_f.constant.test import *
from binance_f.base.printobject import *
from binance_f.model.constant import *
request_client = RequestClient(api_key=config.API_KEY, secret_key=config.API_SECRET)
coins = {
"ETHUSDT":"None",
"BTCUSDT":"None",
"ADAUSDT":"None",
"DOGEUSDT":"None",
}
lavs = {
"ETHUSDT":"20",
"BTCUSDT":"20",
"ADAUSDT":"20",
"DOGEUSDT":"20",
}
lastOrder = {
"ETHUSDT":{
"amount":"0",
"side":"0"
},
"BTCUSDT":{
"amount":"0",
"side":"0"
},
"ADAUSDT":{
"amount":"0",
"side":"0"
},
"DOGEUSDT":{
"amount":"0",
"side":"0"
}
}
def close_order(coin):
try:
if lastOrder[coin]['side'] == "BUY":
request_client.post_order(symbol=coin, side=OrderSide.SELL, ordertype=OrderType.MARKET, quantity=lastOrder[coin]['amount'],reduceOnly=True)
lastOrder[coin]['side'] = "0"
lastOrder[coin]['amount'] = "0"
elif lastOrder[coin]['side'] == "SELL":
request_client.post_order(symbol=coin, side=OrderSide.BUY, ordertype=OrderType.MARKET, quantity=lastOrder[coin]['amount'],reduceOnly=True)
lastOrder[coin]['side'] = "0"
lastOrder[coin]['amount'] = "0"
return True
except Exception as e:
print("an exception occured - {}".format(e))
return False
return True
def order(coin,amount,leve,position):
if coins[coin] != position :
try:
close_order(coin)
except Exception as e:
print("an exception occured - {}".format(e))
try:
lavs[coin] = leve
result = request_client.change_initial_leverage(coin, lavs[coin])
except Exception as e:
print("an exception occured - {}".format(e))
try:
result = request_client.change_margin_type(coin, marginType=FuturesMarginType.ISOLATED)
except Exception as e:
print("an exception occured - {}".format(e))
try:
if position.lower() == "long" :
result = request_client.post_order(symbol=coin, side=OrderSide.BUY, ordertype=OrderType.MARKET, quantity=amount)
coins[coin] = position
lastOrder[coin]['side'] = "BUY"
lastOrder[coin]['amount'] = amount
if position.lower() == "short":
result = request_client.post_order(symbol=coin, side=OrderSide.SELL, ordertype=OrderType.MARKET, quantity=amount)
coins[coin] = position
lastOrder[coin]['side'] = "SELL"
lastOrder[coin]['amount'] = amount
except Exception as e:
print("an exception occured - {}".format(e))
return True
@app.route('/')
def main_view():
return 'Hello from loseb k.!'
@app.route('/tradehook',methods=['POST'])
def webhook():
resData = json.loads(request.data)
if resData['passkey'] != config.WEBHOOK_PASS:
return {
"code":"error",
"message":"Invalid"
}
order_response = order(resData['coin'],resData['quantity'],resData['leverage'],resData['positionSide'])
return {
"code":"success",
"message" : "We are good here"
}
"""
import config
from binance_f import RequestClient
from binance_f.constant.test import *
from binance_f.base.printobject import *
from binance_f.model.constant import *
request_client = RequestClient(api_key=config.API_KEY, secret_key=config.API_SECRET)
result = request_client.get_account_information()
print(result)
print("canDeposit: ", result.canDeposit)
print("canWithdraw: ", result.canWithdraw)
print("feeTier: ", result.feeTier)
print("maxWithdrawAmount: ", result.maxWithdrawAmount)
print("totalInitialMargin: ", result.totalInitialMargin)
print("totalMaintMargin: ", result.totalMaintMargin)
print("totalMarginBalance: ", result.totalMarginBalance)
print("totalOpenOrderInitialMargin: ", result.totalOpenOrderInitialMargin)
print("totalPositionInitialMargin: ", result.totalPositionInitialMargin)
print("totalUnrealizedProfit: ", result.totalUnrealizedProfit)
print("totalWalletBalance: ", result.totalWalletBalance)
print("updateTime: ", result.updateTime)
print("=== Assets ===")
PrintMix.print_data(result.assets)
print("==============")
print("=== Positions ===")
PrintMix.print_data(result.positions)
print("==============") """
""" import json, config
from binance.client import Client
from binance.enums import *
client = Client(config.API_KEY,config.API_SECRET,testnet=True)
account_balance = client.get_asset_balance(asset='USDT')
print(account_balance)
account_trades = client.get_my_trades(symbol='ETHUSDT')
print(account_trades)
account_orders = client.get_all_orders(symbol='ETHUSDT')
print(account_orders)
order = client.futures_create_order(symbol='ETHUSDT', side='BUY', type='MARKET', quantity=10)
print(order) """