#非低精度网格.py
#仅适用于低价币,因为确定购买数量时,取整数。高价币不适合。应该为依精度取整为小数。
#ba的取值 挂-买-单起始数量的计算、挂单格差的方向(买卖盘挂单量计算)
#撤销垃圾单的功能有两处 专门撤销函数在于不执行挂单操作时,也能及时清理
#以保证挂单结构的理性 买卖操作时的清理在于处理上次专门函数之后,本次挂单之前
#形势的变化,以求操作精准 这个时间间隔,有可能会很长,因为并非每个循环都会
#执行挂单函数 故而二者都不可省
#全额卖出的操作原本有两处 现已取消买不了就挂全额卖的功能
#程序之初的买卖紧邻 无需买函数代劳全抛
#程序循环中 每次检验状态 都会扫描未挂卖的持仓 其之后 才可能有购买操作
#亦不必购买函数代劳 除非在状态检测和状态修复间隔期内 成交了一笔买单
#但是不同于上面 这个间隔很短 而且即使本次错过一定持仓 下次循环一定会经历
#资产检测环节 会被挂上
import ccxt
import time as t
import math
import sys
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
import re
#初始化交易所
ex = ccxt.binance({
'apiKey': '',
'secret': '',
'timeout': 60000,
'import': True})
#睡眠时间
slp = 0.5
t.sleep(slp)
#交易标的
sbl = "MC/USDT"
sbl_1 = re.findall('(.*?)/', sbl)[0]
sbl_2 = re.findall('/\w+', sbl)[0][1:]
#-买-单起始比例 0用于下跌行情 1 用于上涨或平稳行情 -1适用于SHIB等极端不稳定种类
model_start = 0
#长、短期止损线
stp_s = 0.98
stp_l = 0.97
#买卖盘间隔位 与 精度忽略位数
ba_gap = 1
pcs_modify = 1
#下方1数目/上方1数目 和 上1/下1 (托压力度)
n = 1
#画图跨度 (秒)
interval_plt = 43200
#初次计算资产价值
blc = ex.fetch_balance ()
t.sleep(slp)
prc_lst = ex.fetch_ticker(sbl)['last']
prc_start = prc_lst
start = t.time()
t.sleep(slp)
#投资标的的变动率
rate_target = 0
#价格精度
market = ex.load_markets()[sbl]
pcs = market['precision']['price']
multi = 10 ** (pcs - pcs_modify)
#0启动,即sbl_1或sbl_2为0持仓时,为['sbl_1']或['sbl_2']赋0
_no_value = object()
def start_at_0(content, coin_name_str, branch_str = _no_value):
try:
if branch_str == _no_value:
rtn = content[coin_name_str]
else:
rtn = content[coin_name_str][branch_str]
return rtn
except:
return 0
blc_sbl_1_sa0 = start_at_0(blc, sbl_1, 'total')
blc_sbl_2_sa0 = start_at_0(blc, sbl_2, 'total')
blc = prc_lst * blc_sbl_1_sa0 + blc_sbl_2_sa0
blc_start = blc
blc_peak = blc
blc_lag = blc
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('资产价值:' + str(blc) + ' ' +sbl_2)
print('资产结构:' +
sbl_1 + ' - {:.2%}'. format(prc_lst * blc_sbl_1_sa0 / blc) +
' --- ' + sbl_2 + ' - {:.2%}'. format(blc_sbl_2_sa0 / blc))
df = pd.DataFrame([[0, 0, 0]], columns=['interval', 'portfolio', 'target'])
#资产变动显示锁
asset_change_key = 0
#获取买卖盘
def fetch_order_book_s(symbol):
loop_key = True
prt_key = 1
while loop_key:
try:
ba_dic = ex.fetch_order_book(symbol)
if prt_key == 0:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('获取-买卖盘-成功')
t.sleep(slp)
loop_key = False
except:
if prt_key == 1:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('获取-买卖盘-报错,将重试')
prt_key = 0
return ba_dic
#设置挂买单仓位
def amt_start_dynamic_fuc():
ba_dic = fetch_order_book_s(sbl)
b_5_sum = 0
for i in range(5):
b_5_sum = ba_dic['bids'][i][1] + b_5_sum
for i in range(5):
a_5_sum = ba_dic['asks'][i][1] + b_5_sum
b_over_a = b_5_sum / a_5_sum
if model_start == -2:
return 0.1
if model_start == -1:
if b_over_a < 0.25:
return 0.1
elif b_over_a >= 0.25 and b_over_a < 0.5:
return 0.12
elif b_over_a >= 0.5 and b_over_a < 1:
return 0.13
elif b_over_a >= 1 and b_over_a < 1.5:
return 0.14
else:
return 0.15
if model_start == 0:
if b_over_a < 0.25:
return 0.1
elif b_over_a >= 0.25 and b_over_a < 0.5:
return 0.125
elif b_over_a >= 0.5 and b_over_a < 1:
return 0.15
elif b_over_a >= 1 and b_over_a < 1.5:
return 0.175
elif b_over_a >= 1.5 and b_over_a < 2:
return 0.2
else:
return 0.25
if model_start == 1:
if b_over_a < 0.25:
return 0.1
elif b_over_a >= 0.25 and b_over_a < 0.5:
return 0.15
elif b_over_a >= 0.5 and b_over_a < 1:
return 0.2
elif b_over_a >= 1 and b_over_a < 1.5:
return 0.25
elif b_over_a >= 1.5 and b_over_a < 2:
return 0.3
else:
return 0.35
amt_start_dynamic = amt_start_dynamic_fuc()
#交易数目最大值、最小值与调整步长
amt_start = math.floor(blc * amt_start_dynamic / prc_lst)
amt_min = math.ceil(10 / prc_lst)
step = math.floor((amt_start - amt_min) / 10)
amt_min_useful = amt_start - 8 * step
#限价单成交状态 无挂单视作成交 以[0, 0]起始
status = [0, 0]
#挂单显示控制
sell_s_prt_ctrl = 1
buy_s_prt_ctrl = 1
#ba初始化与获取ba显示控制
ba = [0, 0]
#稳定函数
def fetch_open_orders_s(symbol):
loop_key = True
prt_key = 1
while loop_key:
try:
orders_ls = ex.fetch_open_orders(symbol)
if prt_key == 0:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('获取-敞口订单-成功')
t.sleep(slp)
loop_key = False
except (Exception, BaseException) as e:
if prt_key == 1:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('获取-敞口订单-报错,将重试')
print('错误为:' + str(e))
prt_key = 0
return orders_ls
def fetch_ticker_s(symbol):
loop_key = True
prt_key = 1
while loop_key:
try:
ticker = ex.fetch_ticker(symbol)
if prt_key == 0:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('获取-实时价格-成功')
t.sleep(slp)
loop_key = False
except (Exception, BaseException) as e:
if prt_key == 1:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('获取-实时价格-报错,将重试')
print('错误为:' + str(e))
prt_key = 0
return ticker
def fetch_balance_s():
loop_key = True
prt_key = 1
while loop_key:
try:
blc = ex.fetch_balance ()
if prt_key == 0:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('获取-账户余额-成功')
t.sleep(slp)
loop_key = False
except (Exception, BaseException) as e:
if prt_key == 1:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('获取-账户余额-报错,将重试')
print('错误为:' + str(e))
prt_key = 0
return blc
def cancel_order_s(order_id, symbol):
loop_key = True
prt_key = 1
while loop_key:
try:
ex.cancel_order(order_id, sbl)
if prt_key == 0:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('-取消订单-成功')
t.sleep(slp)
loop_key = False
except (Exception, BaseException) as e:
if prt_key == 1:
if str(e) == r'binance {"code":-2011,"msg":"Unknown order sent."}':
break
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('-取消订单-报错,将重试')
print('错误为:' + str(e))
prt_key = 0
def create_limit_buy_order_s(sbl, amt, prc):
loop_key = True
prt_key = 1
while loop_key:
try:
ex.create_limit_buy_order(symbol=sbl, amount=amt, price=prc)
if prt_key == 0:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('挂单-买-成功')
t.sleep(slp)
loop_key = False
except (Exception, BaseException) as e:
if prt_key == 1:
if str(e) == 'binance Account has insufficient balance for requested action.' or \
'binance {"code":-1013,"msg":"Filter failure: MIN_NOTIONAL"}':
return 0
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('挂单-买-报错,将重试')
print('错误为:' + str(e))
prt_key = 0
return 1
def create_limit_sell_order_s(sbl, amt, prc):
loop_key = True
prt_key = 1
while loop_key:
try:
ex.create_limit_sell_order(symbol=sbl, amount=amt, price=prc)
if prt_key == 0:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('挂单-卖-成功')
t.sleep(slp)
loop_key = False
except (Exception, BaseException) as e:
if prt_key == 1:
if str(e) == 'binance Account has insufficient balance for requested action.' or \
'binance {"code":-1013,"msg":"Filter failure: MIN_NOTIONAL"}':
return 0
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('挂单-卖-报错,将重试')
print('错误为:' + str(e))
prt_key = 0
return 1
#市价售出平仓
def create_market_sell_order_s():
while True:
try:
blc_cmso = fetch_balance_s()
#此处不必用零启动函数,此值若不存在,则证明无需抛售,程序报错终止,正好
amt_sbl_1 = blc_cmso[sbl_1]['total']
ex.create_market_sell_order(sbl, amt_sbl_1)
break
except:
continue
#获取买m卖n 返回列表
def fetch_ba():
global ba
global ba_prt_ctrl
ba_dic = fetch_order_book_s(sbl)
b_base1 = ba_dic['bids'][0][0]
#b_base2 = math.floor(b_base1 * multi) / multi
b_base2 = math.floor(b_base1 * multi) / multi
#print(str(b_base2))
#a_base2 = round((b_base2 + ba_gap / multi) * multi) / multi
a_base2 = round((b_base2 + 1 / multi) * multi) / multi
#print(str(a_base2))
b_vol = 0
i = 0
b_prc_fba = ba_dic['bids'][0][0]
#print(str(b_prc_fba))
while b_prc_fba >= b_base2:
b_vol = b_vol + ba_dic['bids'][i][1]
i += 1
b_prc_fba = ba_dic['bids'][i][0]
#print(str(b_prc_fba))
a_vol = 0
j = 0
a_prc_fba = ba_dic['asks'][0][0]
#print(str(a_prc_fba))
while a_prc_fba <= a_base2:
a_vol = a_vol + ba_dic['asks'][j][1]
j += 1
a_prc_fba = ba_dic['asks'][j][0]
#print(str(a_prc_fba))
if b_vol > n * a_vol:
b = b_base2
#a = round((a_base2 + ba_gap / (2 * multi)) * multi * 10) / (multi * 10)
a = round((a_base2 + ba_gap / multi) * multi * 10) / (multi * 10)
elif b_vol < n * a_vol:
a = a_base2
#b = round((b_base2 - ba_gap / (2 * multi)) * multi * 10) / (multi * 10)
b = round((b_base2 - ba_gap / multi) * multi * 10) / (multi * 10)
else:
b = round((b_base2 - ba_gap / multi) * multi * 10) / (multi * 10)
a = round((a_base2 + ba_gap / multi) * multi * 10) / (multi * 10)
ba_new = [b, a]
if ba_new != ba:
ba = ba_new
print('新ba数列:' + str(ba))
return ba_new
#更新amt_start
def update_amt_start():
global amt_start
global amt_start_dynamic
prc_lst = fetch_ticker_s(sbl)['last']
amt_start_dynamic = amt_start_dynamic_fuc()
amt_start_new = math.floor(blc * amt_start_dynamic / prc_lst)
if amt_start_new != amt_start:
amt_start = amt_start_new
print('amt_start已更新为:' + str(amt_start))
#更新amt_min
def update_amt_min():
global amt_min
prc_lst = fetch_ticker_s(sbl)['last']
if sbl_2 != 'USDT':
prc_sbl_2 = fetch_ticker_s(sbl_2 + "/USDT")['last']
else:
prc_sbl_2 = 1
amt_min_new = math.ceil(10 / (prc_lst * prc_sbl_2))
if amt_min_new != amt_min:
amt_min = amt_min_new
print('amt_min已更新为:' + str(amt_min))
#更新步长
def update_step():
global step
update_amt_start()
update_amt_min()
step_new = math.floor((amt_start - amt_min) / 10)
if step_new != step:
step = step_new
print('step已更新为:' + str(step))
#更新amt_min_useful
def update_amt_min_useful():
global amt_min_useful
amt_min_useful_new = amt_start - 8 * step
if amt_min_useful_new != amt_min_useful:
amt_min_useful = amt_min_useful_new
print('amt_min_useful已更新为:' + str(amt_min_useful))
#挂单买
#预先检查有无同方向敞口订单 若有且低于将要下的价 先取消之 若有且等或高于将要下的价 则不挂单
#资金不足而无法挂单时 则不挂单
def buy(amt, b_prc):
buy_s_new_ord_prt_ctrl = 1
orders_ls = fetch_open_orders_s(sbl)
if len(orders_ls) != 0:
for order in orders_ls:
if order['info']['side'] == 'BUY':
if float(order['price']) < b_prc:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('有同方向敞口单,原-买-单低于将报,将取消之')
cancel_order_s(order['id'], sbl)
buy_s_new_ord_prt_ctrl = 1
else:
if buy_s_new_ord_prt_ctrl == 1:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('有同方向敞口单,原-买-单高或等于将报,不新挂-买-单')
buy_s_new_ord_prt_ctrl = 0
return 1 #不缩小数目继续挂单
if buy_s_prt_ctrl == 1:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('将以 ' + str(b_prc) + ' 的价格,挂-买-单')
rst = create_limit_buy_order_s(sbl, amt, b_prc)
if rst == 1:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('挂单成功')
return 1 #不缩小数目继续挂单
else:
if buy_s_prt_ctrl == 1:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('资金不足')
return 0 #缩小数目继续挂单
#不至于购买14枚(例)时无法挂单 若因处于全仓状态而不能挂单,则补一个额外卖单
def buy_s(b_prc):
global buy_s_prt_ctrl
#更新amt_start
update_amt_start()
amt = amt_start
#更新amt_min
update_amt_min()
while amt >= amt_min:
rst = buy(amt, b_prc)
if rst == 0:
if buy_s_prt_ctrl == 1:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('挂-买-单失败,数量为 {} 枚'. format(amt))
#更新amt_min_useful
update_amt_min_useful()
if amt <= amt_min_useful:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('无能力下最小数目-买-单')
buy_s_prt_ctrl = 0
return 0 #没钱 不下了 进行后边
update_step()
amt -= step
else:
return 1 #成功
#挂单卖
#预先检查有无同方向敞口订单 若有且高于将要下的价 先取消之
#资金不足而无法挂单时 则不挂单
def sell(amt, a_prc):
orders_ls = fetch_open_orders_s(sbl)
if len(orders_ls) != 0:
for order in orders_ls:
if order['info']['side'] == 'SELL':
if float(order['info']['price']) > a_prc:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('有同方向敞口单,原-卖-单高于将报,将取消之')
cancel_order_s(order['info']['orderId'], sbl)
if sell_s_prt_ctrl == 1:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('将以 ' + str(a_prc) + ' 的价格,挂-卖-单')
rst = create_limit_sell_order_s(sbl, amt, a_prc)
if rst == 1:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('挂单成功')
return 1
else:
if sell_s_prt_ctrl == 1:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('资金不足')
print(1/0) #使报错
#卖时永远不限制数目 故amt_*于卖无用
def sell_s(a_prc):
global sell_s_prt_ctrl
#读取最大可售额
blc_amf = fetch_balance_s()
blc_amf_sbl_1_sa0 = start_at_0(blc_amf, sbl_1, 'free')
amt = blc_amf_sbl_1_sa0
try:
sell(amt, a_prc)
sell_s_prt_ctrl = 1
return 1 #成功
except (Exception, BaseException) as e:
if str(e) == 'division by zero':
if sell_s_prt_ctrl == 1:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('按最大持仓 ' + str(amt) + ' 下单失败,金额不够 10 USDT')
sell_s_prt_ctrl = 0
return 0 #没钱 不下了 进行后边
#全撤 (如果当时没有敞口订单 open_orders会是[] 不会报错)
def cancel_all():
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('挂单失败')
open_orders = fetch_open_orders_s(sbl)
if len(open_orders) != 0:
for order in open_orders:
order_id = order['info']['orderId']
cancel_order_s(order_id, sbl)
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('已全部撤单')
else:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('无单可撤')
#把右侧纵轴数据改为百分数格式
def to_percent(temp, position):
return '%1.1f'%(100*temp)
#监测资产
def asset_monitor():
global blc
global blc_peak
global blc_lag
global df
global asset_change_key
global rate_target
blc_amf = fetch_balance_s()
now = t.time()
interval = now - start
prc_lst = fetch_ticker_s(sbl)['last']
blc_amf_sbl_1_sa0 = start_at_0(blc_amf, sbl_1, 'total')
blc_amf_sbl_2_sa0 = start_at_0(blc_amf, sbl_2, 'total')
blc_amf = prc_lst * blc_amf_sbl_1_sa0 + blc_amf_sbl_2_sa0
if blc_amf != blc:
rate_portfolio = (blc_amf - blc_start) / blc_start
rate_target = (prc_lst - prc_start) / prc_start
blc_lag = blc
blc = blc_amf
asset_change_key = 1
if blc_amf > blc_peak:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print(' --- 新 高 --- 新 高 --- 新 高 --- ')
print(' --- 新 高 --- 新 高 --- 新 高 --- ')
print(' --- 新 高 --- 新 高 --- 新 高 --- ')
blc_peak = blc_amf
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('资产价值:' + str(blc_amf) + ' ' +sbl_2)
print('资产变动率:{:.2%}'. format(rate_portfolio))
print('标的变动率:{:.2%}'. format(rate_target))
print('资产结构:' +
sbl_1 + ' --- {:.2%}'. format(prc_lst * blc_amf_sbl_1_sa0 / blc_amf) +
' --- * --- ' + sbl_2 + ' --- {:.2%}'. format(blc_amf_sbl_2_sa0 / blc_amf))
#收集数据 画图 仅对interval_plt内数据的数据
df = df.append({'interval': interval, 'portfolio': rate_portfolio, 'target': rate_target}, ignore_index = True)
if interval <= interval_plt:
df_plt = df
if interval > interval_plt:
df_plt = df[df['interval'] > interval - interval_plt]
plt.title(sbl + ' 变动率对比') # 折线图标题
plt.rcParams['font.sans-serif'] = ['SimHei'] # 显示汉字
#plt.xticks(rotation=45) #X轴刻度值转动45度
plt.xlabel('时间 (h)') # x轴标题
plt.ylabel('变动率 (%)') # y轴标题
plt_x = df_plt['interval'] / 3600
plt_y1 = df_plt['portfolio']
plt_y2 = df_plt['target']
plt.gca().yaxis.set_major_formatter(FuncFormatter(to_percent))
line_p, = plt.plot(plt_x, plt_y1, marker=',', markersize=2, label = 'Portfolio')
line_t, = plt.plot(plt_x, plt_y2, marker=',', markersize=2, label = 'Target')
#plt.text(interval / 3600, plt_y1[len(plt_y1) - 1], '%.2f' %(rate_portfolio * 100), ha='center', va='bottom', fontsize=15) # 设置数据标签位置及大小
plt.legend(handles = [line_p, line_t], labels = ['Portfolio', 'Target'], loc='upper left') # 设置折线名称
plt.show()
#监测限价单状态 以及 多头持仓(若有未挂单的多仓,应全部挂单)
#卖单是不用专门监测操心的 只要检测到有货 就卖
#这里主要监测并补齐买单
def status_monitor():
global status
#更新买卖盘 若有未挂单的多仓,应全部挂单
ba = fetch_ba()
sell_s_rst = sell_s(ba[1])
if sell_s_rst == 1:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('已补充额外卖单,多头持仓已全部挂卖')
orders_ls = fetch_open_orders_s(sbl)
orders_side_ls = []
for order in orders_ls:
orders_side_ls.append(order['info']['side'])
status_old = status
if 'BUY' in orders_side_ls:
status[0] = 1
else:
status[0] = 0
if 'SELL' in orders_side_ls:
status[1] = 1
else:
status[1] = 0
if status_old != status:
if status == [0, 0]:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('全部成交 或 被撤销 或尚未挂单')
elif status == [1, 1]:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('两侧皆有有未成交单')
elif status == [0, 1]:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('仅-买-单成交 或 之前仅挂-卖-单')
else:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('仅-卖-单成交 或 之前仅挂-买-单')
#根据限价单状态补单 主要是买单
def status_fixer():
#监测限价单状态
status_monitor()
#补齐缺单 主要是买单
if status[0] == 0:
#更新买卖盘
ba = fetch_ba()
buy_s(ba[0])
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('已补充一个买单')
#撤销过时单
#不要撤价格低的买单 和 价格高的买单
def cancel_junk_orders():
orders_ls = fetch_open_orders_s(sbl)
if len(orders_ls) != 0:
for order in orders_ls:
#更新买卖盘
ba = fetch_ba()
if order['info']['side'] == 'BUY' and float(order['info']['price']) < ba[0]:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('-买-单已过时,将被撤销')
cancel_order_s(order['info']['orderId'], sbl)
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('撤销成功')
if order['info']['side'] == 'SELL' and float(order['info']['price']) > ba[1]:
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('-卖-单已过时,将被撤销')
cancel_order_s(order['info']['orderId'], sbl)
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('撤销成功')
#止损
def stp_lss():
global asset_change_key
global rate_target
if blc < stp_s * blc_lag:
cancel_all()
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print(' --- !!! --- !!! --- !!! --- !!! --- ')
print('短期亏损大于 ' + '%.2f' %(1 - stp_s) + ' ,已全部撤单,将市价平仓')
print(' --- !!! --- !!! --- !!! --- !!! --- ')
create_market_sell_order_s()
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print(' --- !!! --- !!! --- !!! --- !!! --- ')
print('短期亏损大于 ' + '%.2f' %(1 - stp_s) + ' ,已市价平仓,程序即将终止')
print(' --- !!! --- !!! --- !!! --- !!! --- ')
sys.exit()
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('未检测到短期亏损大于' + '%.2f' %(1 - stp_s) + ' ,blc较上期变动:' + '%.2f' %(100 * (blc - blc_lag) / blc_lag) + ' %')
if blc < stp_l * blc_peak:
cancel_all()
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('最大回撤大于 ' + '%.2f' %(1 - stp_l) + ' ,已市价平仓,将市价平仓')
print(' --- !!! --- !!! --- !!! --- !!! --- ')
create_market_sell_order_s()
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print(' --- !!! --- !!! --- !!! --- !!! --- ')
print('最大回撤大于 ' + '%.2f' %(1 - stp_l) + ' ,已市价平仓,程序即将终止')
print(' --- !!! --- !!! --- !!! --- !!! --- ')
sys.exit()
print(t.strftime('%Y-%m-%d %H:%M:%S'))
print('未检测到最大回撤大于' + '%.2f' %(1 - stp_l) + ' ,较最高值变动:' + '%.2f' %(100 * (blc - blc_peak) / blc_peak) + ' %')
asset_change_key = 0
#主程序
def main():
#获取买卖盘并挂单
ba = fetch_ba()
buy_s(ba[0])
sell_s(ba[1])
while True:
#撤销过时单
cancel_junk_orders()
#监测挂单状态 成交后及时补单
status_fixer()
#资产监测
asset_monitor()
#止损
if asset_change_key == 1:
stp_lss()
#运行
main()