import
datetime
print
(datetime)
import
random
print
(random)
import
time
print
(time)
SleepS
=
int
(datetime.datetime.now().strftime(
'%S'
))
SleepMin
=
0.1
print
(
'启动中!等待账户登录后:'
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
])
+
"/"
+
str
(SleepS)
+
'秒 启动'
)
pass
print
(
'import 环境模块加载完成:######################################'
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
]))
def
parameter():
print
(
'def parameter 模块加载完成:###############################'
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
]))
pass
def
init(context):
print
(
'def init 模块加载完成####################################:'
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
]))
pass
def
before_trading(context):
print
(
'def before_trading 模块加载完成###########################:'
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
]))
pass
def
after_trading(context):
print
(
'def after_trading 模块加载完成############################:'
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
]))
pass
def
order_status(context,order):
Orders
=
get_orders (order_book_id
=
"all"
,
type
=
1
, account
=
"
") #合约代码,或者为"
all
"全部合约,必填项
0
:只取未成交订单
1
:取全部订单,必须填指定具体的交易帐号,若不指定帐号,则取默认登录帐号
if
order.status
=
=
'submitted'
:
print
(
'全部订单列表:'
+
str
(order.order_book_id)
+
':'
+
str
(order.status)
+
'/submitted"已报单未成交'
\
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
]))
if
order.status
=
=
'tradeing"已成交'
:
print
(
'全部订单列表:'
+
str
(order.order_book_id)
+
':'
+
str
(order.status)
+
'/tradeing"已成交'
\
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
]))
pass
if
order.status
=
=
'filled'
:
print
(
'全部订单列表:'
+
str
(order.order_book_id)
+
':'
+
str
(order.status)
+
'/filled"全部成交'
\
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
]))
pass
if
order.status
=
=
'cancelled'
:
print
(
'全部订单列表:'
+
str
(order.order_book_id)
+
':'
+
str
(order.status)
+
'/cancelled"已撤单'
\
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
]))
pass
if
order.status
=
=
'inactive'
:
print
(
'全部订单列表:'
+
str
(order.order_book_id)
+
':'
+
str
(order.status)
+
'/inactive"无效单'
\
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
]))
pass
if
order.status
=
=
'connected'
:
print
(
'全部订单列表:'
+
str
(order.order_book_id)
+
':'
+
str
(order.status)
+
'/connected"已连接'
\
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
]))
pass
if
order.status
=
=
'disconnected'
:
print
(
'全部订单列表:'
+
str
(order.order_book_id)
+
':'
+
str
(order.status)
+
'/disconnected"连接断开'
\
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
]))
return
pass
pass
def
handle_bar(context):
id_list
=
get_orders(order_book_id
=
"all"
,
type
=
1
, account
=
"")
if
not
(id_list
=
=
None
):
id_last
=
get_orders_id (id_list[
-
1
].order_id)
if
id_last.status
=
=
'submitted'
:
cancel_order (id_last.order_id)
pass
Test
=
0
TimeCha
=
0
ZongFengXianDu
=
1
ZongYingKuiLv
=
0.1
ZongHuiCheBiLi
=
0.01
ZongChengBenBi
=
10
pass
HuiLuo
=
0.02
ZhangDieFu
=
0.02
ZhenFu
=
0.015
YingKuiLv
=
0.001
FengXianDu
=
0.09
pass
BuyFengXianDu
=
0
SellFengXianDu
=
0
BuyAvgHoldingPrice
=
0
SellAvgHoldingPrice
=
0
pass
BuyVol
=
1
SellVol
=
1
BuyMaxVol
=
1
SellMaxVol
=
1
FangXiang
=
0
pass
lastbuy
=
0
lastbuyping
=
0
lastsell
=
0
lastsellping
=
0
pass
NewAmount
=
255
OrdAmount
=
255
KeYongAmount
=
255
GetAccountBook
=
len
(get_account_book())
AccountBook
=
(isaccount())
pass
if
1
=
=
0
or
GetAccountBook !
=
1
:
SleepS
=
int
(datetime.datetime.now().strftime(
'%S'
))
print
(
'账户列表:'
+
str
(GetAccountBook)
+
'登录账户:'
+
str
(AccountBook)
+
'系统错误!账户断开!交易中断!等待'
\
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
])
+
'/'
+
str
(SleepS)
+
'秒启动'
)
return
else
:
if
1
!
=
0
:
DianNaoTime
=
datetime.datetime.now()
DianNaoTime
=
int
(DianNaoTime.strftime(
"%H%M%S"
))
OpenTime0900_2300
=
(
90000
<
=
DianNaoTime <
=
101500
)
or
(
103000
<
=
DianNaoTime <
=
113000
) \
or
(
133000
<
=
DianNaoTime <
=
150000
)
or
(
210000
<
=
DianNaoTime <
=
230000
)
OpenTime2300_0100
=
(
230000
<
=
DianNaoTime <
=
240000
)
or
(
0
<
=
DianNaoTime <
=
10000
)
OpenTime0100_0230
=
(
10000
<
=
DianNaoTime <
=
23000
)
pass
OpenTimeQuJian
=
100
*
5
OpenTime2100
=
210000
<
=
DianNaoTime <
=
(
210000
+
OpenTimeQuJian)
OpenTime0900
=
90000
<
=
DianNaoTime <
=
(
90000
+
OpenTimeQuJian)
OpenTime1030
=
103000
<
=
DianNaoTime <
=
(
103000
+
OpenTimeQuJian)
OpenTime1330
=
133000
<
=
DianNaoTime <
=
(
133000
+
OpenTimeQuJian)
pass
CloseTimeQuJian
=
100
*
9
CloseTime2300
=
(
225959
-
CloseTimeQuJian) <
=
DianNaoTime <
=
230000
CloseTime0100
=
(
5959
-
CloseTimeQuJian) <
=
DianNaoTime <
=
10000
CloseTime0230
=
(
22959
-
CloseTimeQuJian) <
=
DianNaoTime <
=
23000
CloseTime1015
=
(
101459
-
CloseTimeQuJian) <
=
DianNaoTime <
=
101500
CloseTime1130
=
(
112959
-
CloseTimeQuJian) <
=
DianNaoTime <
=
113000
CloseTime1500
=
(
145959
-
CloseTimeQuJian) <
=
DianNaoTime <
=
150000
RunInfoBaseBookId
=
context.run_info.base_book_id
BaseGetInblocks
=
get_inblocks(RunInfoBaseBookId)
pass
Universe
=
context.universe
pass
if
1
!
=
0
:
OpenCloseTime
=
CloseTime2300
=
=
1
or
CloseTime0100
=
=
1
or
CloseTime0230
=
=
1
or
CloseTime1015
=
=
1
\
or
CloseTime1130
=
=
1
or
CloseTime1500
=
=
1
or
OpenTime2100
=
=
1
or
OpenTime0900
=
=
1
or
OpenTime1030
=
=
1
\
or
OpenTime1330
=
=
1
if
OpenCloseTime
=
=
1
:
GetBlocks
=
get_portfolio_book(
2
,"")
random.shuffle(GetBlocks)
BanKuai
=
'KaiCang_10分钟结算持仓'
else
:
if
OpenTime0900_2300
=
=
1
:
GetBlocks
=
get_blocks (
'主力合约板块'
,
1
)
random.shuffle(GetBlocks)
BanKuai
=
'KaiCang_0900_2300自选股'
else
:
if
OpenTime2300_0100
=
=
1
:
GetBlocks
=
[
'SQSS00'
,
'SQZN00'
,
'SQSN00'
,
'SQCU00'
,
'SQPB00'
,
'SQNI00'
,
'SQAL00'
,
'SQAG00'
,
'SQAU00'
,
'INSC0000'
]
random.shuffle(GetBlocks)
BanKuai
=
'KaiCang_2300_0100自设股'
else
:
if
OpenTime0100_0230
=
=
1
:
GetBlocks
=
[
'SQAG00'
,
'SQAU00'
,
'INSC0000'
]
random.shuffle(GetBlocks)
BanKuai
=
'KaiCang_0100_0230自定义'
else
:
GetBlocks
=
get_portfolio_book(
2
,"")
random.shuffle(GetBlocks)
BanKuai
=
'KaiCang_收市检查'
Test
=
1
MaxNum
=
len
(GetBlocks)
if
MaxNum
=
=
0
:
print
(
str
(BanKuai)
+
str
(MaxNum))
return
Num
=
random.randrange(
0
,MaxNum,
1
)
FengXianDu
=
round
(ZongFengXianDu
/
MaxNum,
6
)
for
index
in
range
(
len
(GetBlocks)):
s
=
GetBlocks[Num]
s1
=
get_dynainf(s,
210
)
Name
=
get_dynainf(s1,
219
)
else
:
print
(
'获取指定品种的所属板块:'
+
str
(BaseGetInblocks))
print
(
'合约池没有正常设置!自选股必须是连续合约,合约池导入自选股列表,基准合约为空'
)
return
if
get_account(
6
) <
=
255
or
get_account(
26
) <
=
255
or
get_account(
19
) <
=
255
:
SleepS
=
int
(datetime.datetime.now().strftime(
'%S'
))
print
(
'账户异常!未登录!退出交易!'
'等待'
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
])\
+
'/'
+
str
(SleepS)
+
'秒启动'
)
return
else
:
NewZuoShouJia
=
round
(get_dynainf(s1,
3
),
6
)
NewOpenJia
=
round
(get_dynainf(s1,
4
),
6
)
NewHighJia
=
round
(get_dynainf(s1,
5
),
6
)
NewLowJia
=
round
(get_dynainf(s1,
6
),
6
)
NewJia
=
round
(get_dynainf(s1,
7
),
6
)
NewJunJia
=
round
(get_dynainf(s1,
11
),
6
)
NewZhenFu
=
round
(get_dynainf(s1,
13
),
6
)
NewZhangDieFu
=
round
(get_dynainf(s1,
14
),
6
)
NewBuyJia
=
round
(get_dynainf(s1,
20
),
6
)
NewSellJia
=
round
(get_dynainf(s1,
21
),
6
)
NewZhangSu
=
round
(get_dynainf(s1,
24
),
6
)
NewBuyOneVol
=
round
(get_dynainf(s1,
25
),
6
)
NewSellOneVol
=
round
(get_dynainf(s1,
31
),
6
)
NewChiCangLiang
=
round
(get_dynainf(s1,
45
),
6
)
NewZhangTing
=
round
(get_dynainf(s1,
54
),
6
)
NewDieTing
=
round
(get_dynainf(s1,
55
),
6
)
NewQianChiCangLiang
=
round
(get_dynainf(s1,
60
),
6
)
NewJinJieSuan
=
round
(get_dynainf(s1,
61
),
6
)
NewQianJieSuan
=
round
(get_dynainf(s1,
62
),
6
)
NewAmountFuDongYingKui
=
round
(get_account(
4
),
0
)
NewAmount
=
round
(get_account(
6
),
0
)
KeYongAmount
=
round
(get_account(
19
),
0
)
OrdAmount
=
round
(get_account(
26
),
0
)
BaoZhengJinAmount
=
round
(get_account(
28
),
0
)
NewAmountPingCangYingKui
=
round
(get_account(
30
),
0
)
NewAmountShouXuFei
=
round
(get_account(
31
),
0
)
ZongYinKuiAmount
=
round
((NewAmountFuDongYingKui
+
NewAmountPingCangYingKui
-
NewAmountShouXuFei),
0
)
if
ZongYinKuiAmount
=
=
0
or
BaoZhengJinAmount
=
=
0
or
NewAmount
=
=
0
:
AmountZongYingKuiLV
=
0
AmountZongFengXianDu
=
0
else
:
AmountZongYingKuiLV
=
round
(ZongYinKuiAmount
/
BaoZhengJinAmount,
6
)
if
AmountZongYingKuiLV
=
=
0
:
AmountZongFengXianDu
=
0
else
:
AmountZongFengXianDu
=
round
(BaoZhengJinAmount
/
NewAmount,
6
)
pass
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'/动态权益:'
+
str
(NewAmount)
+
'/期初余额:'
+
str
(OrdAmount)\
+
'/可用资金:'
+
str
(KeYongAmount)
+
'/手续费:'
+
str
(NewAmountShouXuFei)
+
'/平仓盈亏:'
+
str
(NewAmountPingCangYingKui)
+
'/浮动盈亏:'
\
+
str
(NewAmountFuDongYingKui)
+
'/总盈亏率:'
+
str
(AmountZongYingKuiLV)
+
'/总风险度:'
+
str
(AmountZongFengXianDu))
pass
portfolio
=
get_portfolio (s1,
2
)
pass
BuyQuantity
=
round
(portfolio.buy_quantity,
4
)
BuyMargin
=
round
(portfolio.buy_margin,
4
)
BuyAvgHoldingPrice
=
round
(portfolio.buy_avg_holding_price,
4
)
BuyPnl
=
round
(portfolio.pnl,
4
)
pass
SellQuantity
=
round
(portfolio.sell_quantity,
4
)
SellMargin
=
round
(portfolio.sell_margin,
4
)
SellAvgHoldingPrice
=
round
(portfolio.sell_avg_holding_price,
4
)
SellPnl
=
round
(portfolio.pnl,
4
)
pass
if
BuyPnl
=
=
SellPnl
and
BuyPnl
+
SellPnl !
=
0
:
ZongPnl
=
round
((BuyPnl
+
SellPnl)
/
2
,
4
)
else
:
ZongPnl
=
0
pass
instruments
=
get_instruments(s1)
BuyBaoZhengJinLv
=
round
(instruments.buy_margin_rate,
6
)
SellBaoZhengJinLv
=
round
(instruments.short_margin_rate,
6
)
HeYueDanWei
=
round
(instruments.multipliter,
6
)
MinJia
=
round
(instruments.mintick,
6
)
MeiShouDanWei
=
round
(instruments.round_lot,
6
)
KaiShouXuFei
=
round
(get_charge(s1,get_dynainf(s1,
7
),
1
,
0
),
6
)
PingShouXuFei
=
round
(get_charge(s1,get_dynainf(s1,
7
),
1
,
1
),
6
)
if
1
!
=
0
and
\
MinJia
*
HeYueDanWei
*
MeiShouDanWei
=
=
0
or
NewQianJieSuan
*
HeYueDanWei
*
BuyBaoZhengJinLv
*
SellBaoZhengJinLv
=
=
0
\
or
FengXianDu
*
OrdAmount
=
=
0
or
(KaiShouXuFei
+
PingShouXuFei)
=
=
0
:
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'!!!!!!!!!!!!!!!合约信息错误!!!更新合约信息!!!!!!!!!!!!!!!!!'
)
return
else
:
MinJiaCha
=
round
(MinJia
*
HeYueDanWei
*
MeiShouDanWei,
6
)
pass
BuyBaoZhengJin
=
round
(NewQianJieSuan
*
HeYueDanWei
*
BuyBaoZhengJinLv
*
1
,
0
)
SellBaoZhengJin
=
round
(NewQianJieSuan
*
HeYueDanWei
*
SellBaoZhengJinLv
*
1
,
0
)
MinChenBenBi
=
round
(MinJiaCha
/
(KaiShouXuFei
+
PingShouXuFei),
6
)
OrdBuyMaxVol
=
round
(FengXianDu
*
OrdAmount
/
BuyBaoZhengJin ,
0
)
OrdSellMaxVol
=
round
(FengXianDu
*
OrdAmount
/
SellBaoZhengJin ,
0
)
pass
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'/昨天:'
+
'最大持仓量:'
+
str
(OrdBuyMaxVol)
+
':'
+
str
(OrdSellMaxVol)
+
'/空保证金:'
+
str
(SellBaoZhengJin)
+
'/空保证金率:'
\
+
str
(SellBaoZhengJinLv)
+
'/多保证金:'
+
str
(BuyBaoZhengJin)
+
'/多保证金率:'
+
str
(BuyBaoZhengJinLv)
+
'/最小成本比'
+
str
(MinChenBenBi)\
+
'/开手续费:'
+
str
(KaiShouXuFei)
+
'/平手续费:'
+
str
(PingShouXuFei)
+
'/最小单位:'
+
str
(MinJia)
+
'/最小定价:'
+
str
(MinJiaCha)\
+
'/每手单位:'
+
str
(MeiShouDanWei)
+
'/单位乘数:'
+
str
(HeYueDanWei)
+
"/"
)
if
BuyQuantity>
0
:
pass
BuyPnl
=
\
round
((NewSellJia
-
BuyAvgHoldingPrice)
/
MinJia
*
MinJiaCha
*
BuyQuantity
-
(KaiShouXuFei
+
PingShouXuFei)
*
BuyQuantity,
4
)
if
BuyMargin
=
=
0
or
get_account(
6
)
=
=
0
:
BuyFengXianDu
=
0
else
:
BuyFengXianDu
=
round
(BuyMargin
/
get_account(
6
),
6
)
pass
if
BuyPnl
=
=
0
or
BuyMargin
=
=
0
or
BuyAvgHoldingPrice
=
=
0
:
BuyYingKuiLv
=
0
NewBuyBaoZhengJinLv
=
0
else
:
BuyYingKuiLv
=
round
(BuyPnl
/
BuyMargin,
6
)
NewBuyBaoZhengJinLv
=
round
(BuyMargin
/
BuyAvgHoldingPrice
/
HeYueDanWei
/
BuyQuantity ,
6
)
pass
if
FengXianDu>
0
and
KeYongAmount>
255
and
BuyMargin>
0
and
BuyQuantity>
0
:
BuyMaxVol
=
round
((FengXianDu
*
KeYongAmount)
/
(BuyMargin
/
BuyQuantity),
0
)
if
1
< MinChenBenBi < BuyMaxVol
and
BuyVol !
=
BuyMaxVol:
BuyVol
=
round
(
int
(MinChenBenBi),
0
)
pass
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'/多:最大持:'
+
str
(BuyMaxVol)
+
'/持:'
+
str
(BuyQuantity)
+
'/均价:'
\
+
str
(BuyAvgHoldingPrice)
+
'/总浮盈亏:'
+
str
(ZongPnl)
+
'/持盈亏:'
+
str
(BuyPnl)
+
'/盈亏率:'
+
str
(BuyYingKuiLv)
+
'/持风险度:'
\
+
str
(BuyFengXianDu)
+
'/持保证金率:'
+
str
(NewBuyBaoZhengJinLv)
+
'/'
)
if
SellQuantity>
0
:
pass
SellPnl
=
\
round
((SellAvgHoldingPrice
-
NewBuyJia)
/
MinJia
*
MinJiaCha
*
SellQuantity
-
(KaiShouXuFei
+
PingShouXuFei)
*
SellQuantity,
4
)
if
SellMargin
=
=
0
or
get_account(
6
)
=
=
0
:
SellFengXianDu
=
0
else
:
SellFengXianDu
=
round
(SellMargin
/
get_account(
6
),
6
)
pass
if
SellPnl
=
=
0
or
SellMargin
=
=
0
or
SellAvgHoldingPrice
=
=
0
:
SellYingKuiLv
=
0
NewSellBaoZhengJinLv
=
0
else
:
SellYingKuiLv
=
round
(SellPnl
/
SellMargin,
6
)
NewSellBaoZhengJinLv
=
round
(SellMargin
/
SellAvgHoldingPrice
/
HeYueDanWei
/
SellQuantity,
6
)
pass
if
FengXianDu >
0
and
KeYongAmount >
255
and
SellMargin >
0
and
SellQuantity >
0
:
SellMaxVol
=
round
((FengXianDu
*
KeYongAmount)
/
(SellMargin
/
SellQuantity),
0
)
if
1
< MinChenBenBi < SellMaxVol
and
SellVol !
=
SellMaxVol:
SellVol
=
round
(
int
(MinChenBenBi),
0
)
pass
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'/空:最大持:'
+
str
(SellMaxVol)
+
'/持:'
+
str
(SellQuantity)\
+
'/均价:'
+
str
(SellAvgHoldingPrice)
+
'/总浮盈亏:'
+
str
(ZongPnl)
+
'/持盈亏:'
+
str
(SellPnl)
+
'/盈亏率:'
+
str
(SellYingKuiLv)\
+
'/持风险度:'
+
str
(SellFengXianDu)
+
'/持保证金率:'
+
str
(NewSellBaoZhengJinLv)
+
'/'
)
if
not
istradertime(s1):
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
"/不在交易时间/不进入交易段"
)
if
Test
=
=
1
:
return
else
:
return
else
:
pass
if
1
!
=
0
:
pass
if
1
!
=
0
:
DianNaoTime
=
datetime.datetime.now()
DianNaoTime
=
int
(DianNaoTime.strftime(
"%H%M%S"
))
ChengJIaoTime
=
int
(
round
(get_dynainf(s1,
207
),
0
))
DianNaoTimeH
=
round
(DianNaoTime
/
10000
,
0
)
ChengJIaoTimeH
=
round
(ChengJIaoTime
/
10000
,
0
)
XiaoShiShiCha
=
(DianNaoTimeH
-
ChengJIaoTimeH)
if
DianNaoTimeH!
=
ChengJIaoTimeH:
if
DianNaoTimeH
=
=
ChengJIaoTimeH:
TimeCha
=
round
(DianNaoTime
-
ChengJIaoTime,
0
)
else
:
if
DianNaoTimeH > ChengJIaoTimeH:
TimeCha
=
round
(DianNaoTime
-
(ChengJIaoTime
+
abs
(XiaoShiShiCha
*
10000
)),
0
)
pass
if
DianNaoTimeH < ChengJIaoTimeH:
TimeCha
=
round
(DianNaoTime
-
(ChengJIaoTime
-
abs
(XiaoShiShiCha
*
10000
)),
0
)
pass
if
NewAmount !
=
0
or
NewAmountShouXuFei !
=
0
or
OrdAmount !
=
0
:
pass
if
SellQuantity >
=
0
or
BuyQuantity >
=
0
:
AmountHuiCheJinE
=
round
(NewAmount
-
NewAmountShouXuFei
-
OrdAmount,
0
)
HuiCheJinE
=
round
(OrdAmount
*
ZongHuiCheBiLi,
6
)
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'设定回撤比例:'
+
str
(ZongHuiCheBiLi)
+
'/设定回撤金额:'
+
str
(HuiCheJinE) )
if
AmountHuiCheJinE <
-
HuiCheJinE
or
AmountHuiCheJinE > HuiCheJinE
*
9
:
FangXiang
=
0
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'设定回撤比例:'
\
+
str
(ZongHuiCheBiLi)
+
'/账户当日回撤:'
+
str
(AmountHuiCheJinE)
+
'/大于触发阀值:'
+
str
(HuiCheJinE)
+
'执行风控退出平仓交易!'
)
else
:
if
ZongPnl
=
=
0
or
BaoZhengJinAmount
=
=
0
:
NewZongHuiCheBiLi
=
0
else
:
NewZongHuiCheBiLi
=
round
(ZongPnl
/
BaoZhengJinAmount,
6
)
if
NewZongHuiCheBiLi <
-
ZongHuiCheBiLi:
FangXiang
=
0
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'设定回撤比例:'
\
+
str
(ZongHuiCheBiLi)
+
'/账户当日盈亏率:'
+
str
(NewZongHuiCheBiLi)
+
'/大于回撤比例:'
+
str
(ZongHuiCheBiLi)\
+
'执行风控重启平仓交易!'
)
else
:
if
abs
(
int
(TimeCha)) >
10
or
NewChiCangLiang <
50000
or
NewQianChiCangLiang <
50000
or
NewBuyJia >
=
NewZhangTing
or
NewSellJia <
=
NewDieTing:
FangXiang
=
0
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'/时差:'
+
str
(TimeCha)\
+
'秒/######################报价不活跃=======平仓######################'
)
pass
else
:
if
1
!
=
0
and
( OpenCloseTime
=
=
1
):
print
(
str
(BanKuai)
+
str
(Name)
+
'=====================测试收盘平仓========================'
)
pass
if
1
!
=
0
:
if
NewJia > NewLowJia > NewQianJieSuan:
FangXiang
=
1
pass
if
NewJia < NewHighJia < NewQianJieSuan:
FangXiang
=
-
1
pass
if
NewZhenFu < ZhenFu:
FangXiang
=
0
pass
if
CloseTime1500
=
=
1
:
FangXiang
=
0
pass
if
FangXiang
=
=
0
:
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
"/"
+
"风控:0:"
+
str
(FangXiang)
+
'/动态盈亏率:'
+
str
(YingKuiLv))
pass
if
FangXiang
=
=
1
:
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
"/"
+
"趋势:1:"
+
str
(FangXiang)
+
'/动态盈亏率:'
+
str
(YingKuiLv))
pass
if
FangXiang
=
=
2
:
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
"/"
+
"震荡:2:"
+
str
(FangXiang)
+
'/动态盈亏率:'
+
str
(YingKuiLv))
pass
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'下单对接盘口之前:'
+
' BuyVol:'
+
str
(BuyVol)
+
' SellVol:'
+
str
(SellVol))
if
BuyVol > NewSellOneVol >
0
:
BuyVol
=
int
(NewSellOneVol)
else
:
BuyVol
=
int
(BuyVol)
if
SellVol > NewBuyOneVol >
0
:
SellVol
=
int
(NewBuyOneVol)
else
:
SellVol
=
int
(SellVol)
if
BuyQuantity !
=
0
:
if
BuyQuantity > NewSellOneVol >
0
:
NewBuyVol
=
int
(NewSellOneVol)
else
:
NewBuyVol
=
int
(BuyQuantity)
if
SellQuantity !
=
0
:
if
SellQuantity > NewBuyOneVol >
0
:
NewSellVol
=
int
(NewBuyOneVol)
else
:
NewSellVol
=
int
(SellQuantity)
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'下单对接盘口之后:'
+
' BuyVol:'
+
str
(BuyVol)
+
' SellVol:'
+
str
(SellVol))
if
1
!
=
0
\
and
(FangXiang
=
=
0
):
if
BuyQuantity !
=
0
:
sell_close(s1,
"ThisClose"
,volume
=
NewBuyVol,repeat
=
1
,serial_id
=
1
)
pass
if
SellQuantity !
=
0
:
buy_close(s1,
"ThisClose"
,volume
=
NewSellVol,repeat
=
1
,serial_id
=
2
)
pass
if
1
!
=
1
\
and
( (BuyMaxVol !
=
BuyVol
and
SellMaxVol !
=
SellVol)
or
BuyPnl !
=
0
or
SellPnl !
=
0
or
KaiShouXuFei !
=
0
\
or
PingShouXuFei !
=
0
or
BuyQuantity > BuyVol
or
SellQuantity > SellVol ):
ChengBen
=
(KaiShouXuFei
+
PingShouXuFei)
*
(BuyQuantity
+
SellQuantity)
*
ZongChengBenBi
BuyChengBen
=
(KaiShouXuFei
+
PingShouXuFei)
*
BuyQuantity
*
MinChenBenBi
SellChengBen
=
(KaiShouXuFei
+
PingShouXuFei)
*
SellQuantity
*
MinChenBenBi
pass
if
ZongPnl > ChengBen
and
(BuyPnl > BuyChengBen
and
SellPnl > SellChengBen):
pass
if
BuyQuantity >
0
and
SellQuantity >
0
:
if
BuyQuantity >
0
:
pass
buy_close(s1,
"ThisClose"
,volume
=
NewBuyVol,repeat
=
0
,serial_id
=
3
)
if
SellQuantity >
0
:
pass
sell_close(s1,
"ThisClose"
,volume
=
NewSellVol,repeat
=
0
,serial_id
=
4
)
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'多空 同时持仓盈利 风控趋势震荡对平持仓 结束循环重启!'
)
pass
elif
ZongPnl > ChengBen
and
(BuyPnl > BuyChengBen
or
SellPnl > SellChengBen):
pass
if
BuyQuantity >
0
or
SellQuantity >
0
:
if
BuyQuantity >
0
:
pass
buy_close(s1,
"ThisClose"
,volume
=
NewBuyVol,repeat
=
0
,serial_id
=
5
)
if
SellQuantity >
0
:
pass
sell_close(s1,
"ThisClose"
,volume
=
NewSellVol,repeat
=
0
,serial_id
=
6
)
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'多空 分别 (单向或者双向) 持仓合并盈利 风控趋势震荡对平持仓 结束循环重启!'
)
elif
ZongPnl <
-
ChengBen
and
(BuyPnl <
-
BuyChengBen
or
SellPnl <
-
SellChengBen):
pass
if
BuyQuantity >
0
or
SellQuantity >
0
:
if
BuyQuantity >
0
:
pass
buy_close(s1,
"ThisClose"
,volume
=
NewBuyVol,repeat
=
0
,serial_id
=
7
)
if
SellQuantity >
0
:
pass
sell_close(s1,
"ThisClose"
,volume
=
NewSellVol,repeat
=
0
,serial_id
=
8
)
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'多空 分别 (单向或者双向) 持仓合并亏损 风控趋势震荡对平持仓 结束循环重启!'
)
else
:
pass
pass
if
( NewZhangSu <
-
(NewZhenFu
*
ZhenFu) )
and
FangXiang
=
=
1
:
if
BuyQuantity
=
=
0
:
buy_open(s1,
"ThisClose"
,volume
=
BuyVol,repeat
=
0
,serial_id
=
9
)
pass
if
BuyQuantity >
0
and
FangXiang
=
=
1
:
if
BuyQuantity > BuyMaxVol:
sell_close(s1,
"ThisClose"
,volume
=
BuyVol,repeat
=
0
,serial_id
=
10
)
pass
pass
if
( NewZhangSu > (NewZhenFu
*
ZhenFu) )
and
FangXiang
=
=
-
1
:
if
SellQuantity
=
=
0
:
sell_open(s1,
"ThisClose"
,volume
=
SellVol,repeat
=
0
,serial_id
=
11
)
pass
if
SellQuantity >
0
and
FangXiang
=
=
-
1
:
if
SellQuantity > SellMaxVol:
buy_close(s1,
"ThisClose"
,volume
=
SellVol,repeat
=
0
,serial_id
=
12
)
pass
pass
if
BuyQuantity
=
=
0
and
FangXiang
=
=
2
:
if
NewZhangSu <
-
(NewZhenFu
*
ZhenFu):
buy_open(s1,
"ThisClose"
,volume
=
BuyVol,repeat
=
0
,serial_id
=
13
)
pass
if
BuyQuantity !
=
0
and
FangXiang
=
=
2
:
if
BuyQuantity < BuyMaxVol
and
NewZhangSu <
-
(NewZhenFu
*
ZhenFu):
buy_open(s1,
"ThisClose"
,volume
=
BuyVol,repeat
=
0
,serial_id
=
14
)
pass
if
SellQuantity
=
=
0
and
FangXiang
=
=
2
:
if
NewZhangSu > (NewZhenFu
*
ZhenFu):
sell_open(s1,
"ThisClose"
,volume
=
SellVol,repeat
=
0
,serial_id
=
15
)
pass
if
SellQuantity !
=
0
and
FangXiang
=
=
2
:
if
SellQuantity < SellMaxVol
and
NewZhangSu > (NewZhenFu
*
ZhenFu):
sell_open(s1,
"ThisClose"
,volume
=
SellVol,repeat
=
0
,serial_id
=
16
)
pass
return
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!持仓管理模块!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
import
datetime
print
(datetime)
import
random
print
(random)
import
time
print
(time)
SleepS
=
int
(datetime.datetime.now().strftime(
'%S'
))
SleepMin
=
0.1
print
(
'启动中!等待账户登录后:'
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
])
+
"/"
+
str
(SleepS)
+
'秒 启动'
)
pass
print
(
'import 环境模块加载完成:######################################'
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
]))
def
parameter():
print
(
'def parameter 模块加载完成:###############################'
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
]))
pass
def
init(context):
print
(
'def init 模块加载完成####################################:'
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
]))
pass
def
before_trading(context):
print
(
'def before_trading 模块加载完成###########################:'
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
]))
pass
def
after_trading(context):
print
(
'def after_trading 模块加载完成############################:'
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
]))
pass
def
order_status(context,order):
Orders
=
get_orders (order_book_id
=
"all"
,
type
=
1
, account
=
"
") #合约代码,或者为"
all
"全部合约,必填项
0
:只取未成交订单
1
:取全部订单,必须填指定具体的交易帐号,若不指定帐号,则取默认登录帐号
if
order.status
=
=
'submitted'
:
print
(
'全部订单列表:'
+
str
(order.order_book_id)
+
':'
+
str
(order.status)
+
'/submitted"已报单未成交'
\
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
]))
if
order.status
=
=
'tradeing"已成交'
:
print
(
'全部订单列表:'
+
str
(order.order_book_id)
+
':'
+
str
(order.status)
+
'/tradeing"已成交'
\
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
]))
pass
if
order.status
=
=
'filled'
:
print
(
'全部订单列表:'
+
str
(order.order_book_id)
+
':'
+
str
(order.status)
+
'/filled"全部成交'
\
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
]))
pass
if
order.status
=
=
'cancelled'
:
print
(
'全部订单列表:'
+
str
(order.order_book_id)
+
':'
+
str
(order.status)
+
'/cancelled"已撤单'
\
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
]))
pass
if
order.status
=
=
'inactive'
:
print
(
'全部订单列表:'
+
str
(order.order_book_id)
+
':'
+
str
(order.status)
+
'/inactive"无效单'
\
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
]))
pass
if
order.status
=
=
'connected'
:
print
(
'全部订单列表:'
+
str
(order.order_book_id)
+
':'
+
str
(order.status)
+
'/connected"已连接'
\
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
]))
pass
if
order.status
=
=
'disconnected'
:
print
(
'全部订单列表:'
+
str
(order.order_book_id)
+
':'
+
str
(order.status)
+
'/disconnected"连接断开'
\
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
]))
return
pass
pass
def
handle_bar(context):
id_list
=
get_orders(order_book_id
=
"all"
,
type
=
1
, account
=
"")
if
not
(id_list
=
=
None
):
id_last
=
get_orders_id (id_list[
-
1
].order_id)
if
id_last.status
=
=
'submitted'
:
cancel_order (id_last.order_id)
pass
Test
=
0
TimeCha
=
0
ZongFengXianDu
=
1
ZongYingKuiLv
=
0.1
ZongHuiCheBiLi
=
0.01
ZongChengBenBi
=
10
pass
HuiLuo
=
0.02
ZhangDieFu
=
0.02
ZhenFu
=
0.015
YingKuiLv
=
0.02
FengXianDu
=
0.09
pass
BuyFengXianDu
=
0
SellFengXianDu
=
0
BuyAvgHoldingPrice
=
0
SellAvgHoldingPrice
=
0
pass
BuyVol
=
1
SellVol
=
1
BuyMaxVol
=
1
SellMaxVol
=
1
FangXiang
=
0
pass
lastbuy
=
0
lastbuyping
=
0
lastsell
=
0
lastsellping
=
0
pass
NewAmount
=
255
OrdAmount
=
255
KeYongAmount
=
255
GetAccountBook
=
len
(get_account_book())
AccountBook
=
(isaccount())
pass
if
1
=
=
0
or
GetAccountBook !
=
1
:
SleepS
=
int
(datetime.datetime.now().strftime(
'%S'
))
print
(
'账户列表:'
+
str
(GetAccountBook)
+
'登录账户:'
+
str
(AccountBook)
+
'系统错误!账户断开!交易中断!等待'
\
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
])
+
'/'
+
str
(SleepS)
+
'秒启动'
)
return
else
:
if
1
!
=
0
:
DianNaoTime
=
datetime.datetime.now()
DianNaoTime
=
int
(DianNaoTime.strftime(
"%H%M%S"
))
OpenTime0900_2300
=
(
90000
<
=
DianNaoTime <
=
101500
)
or
(
103000
<
=
DianNaoTime <
=
113000
) \
or
(
133000
<
=
DianNaoTime <
=
150000
)
or
(
210000
<
=
DianNaoTime <
=
230000
)
OpenTime2300_0100
=
(
230000
<
=
DianNaoTime <
=
240000
)
or
(
0
<
=
DianNaoTime <
=
10000
)
OpenTime0100_0230
=
(
10000
<
=
DianNaoTime <
=
23000
)
pass
OpenTimeQuJian
=
100
*
5
OpenTime2100
=
210000
<
=
DianNaoTime <
=
(
210000
+
OpenTimeQuJian)
OpenTime0900
=
90000
<
=
DianNaoTime <
=
(
90000
+
OpenTimeQuJian)
OpenTime1030
=
103000
<
=
DianNaoTime <
=
(
103000
+
OpenTimeQuJian)
OpenTime1330
=
133000
<
=
DianNaoTime <
=
(
133000
+
OpenTimeQuJian)
pass
CloseTimeQuJian
=
100
*
9
CloseTime2300
=
(
225959
-
CloseTimeQuJian) <
=
DianNaoTime <
=
230000
CloseTime0100
=
(
5959
-
CloseTimeQuJian) <
=
DianNaoTime <
=
10000
CloseTime0230
=
(
22959
-
CloseTimeQuJian) <
=
DianNaoTime <
=
23000
CloseTime1015
=
(
101459
-
CloseTimeQuJian) <
=
DianNaoTime <
=
101500
CloseTime1130
=
(
112959
-
CloseTimeQuJian) <
=
DianNaoTime <
=
113000
CloseTime1500
=
(
145959
-
CloseTimeQuJian) <
=
DianNaoTime <
=
150000
RunInfoBaseBookId
=
context.run_info.base_book_id
BaseGetInblocks
=
get_inblocks(RunInfoBaseBookId)
pass
Universe
=
context.universe
pass
if
1
!
=
0
:
OpenCloseTime
=
CloseTime2300
=
=
1
or
CloseTime0100
=
=
1
or
CloseTime0230
=
=
1
or
CloseTime1015
=
=
1
\
or
CloseTime1130
=
=
1
or
CloseTime1500
=
=
1
or
OpenTime2100
=
=
1
or
OpenTime0900
=
=
1
or
OpenTime1030
=
=
1
\
or
OpenTime1330
=
=
1
if
OpenCloseTime
=
=
1
:
GetBlocks
=
get_portfolio_book(
2
,"")
random.shuffle(GetBlocks)
BanKuai
=
'ChiCang_10分钟结算持仓'
else
:
if
OpenTime0900_2300
=
=
1
:
GetBlocks
=
get_portfolio_book(
2
,"")
random.shuffle(GetBlocks)
BanKuai
=
'ChiCang_0900_2300自选股'
else
:
if
OpenTime2300_0100
=
=
1
:
GetBlocks
=
get_portfolio_book(
2
,"")
random.shuffle(GetBlocks)
BanKuai
=
'ChiCang_2300_0100自设股'
else
:
if
OpenTime0100_0230
=
=
1
:
GetBlocks
=
get_portfolio_book(
2
,"")
random.shuffle(GetBlocks)
BanKuai
=
'ChiCang_0100_0230自定义'
else
:
GetBlocks
=
get_portfolio_book(
2
,"")
random.shuffle(GetBlocks)
BanKuai
=
'ChiCang_收市检查'
Test
=
1
MaxNum
=
len
(GetBlocks)
if
MaxNum
=
=
0
:
print
(
str
(BanKuai)
+
str
(MaxNum))
return
Num
=
random.randrange(
0
,MaxNum,
1
)
FengXianDu
=
round
(ZongFengXianDu
/
MaxNum,
6
)
for
index
in
range
(
len
(GetBlocks)):
s
=
GetBlocks[Num]
s1
=
get_dynainf(s,
210
)
Name
=
get_dynainf(s1,
219
)
else
:
print
(
'获取指定品种的所属板块:'
+
str
(BaseGetInblocks))
print
(
'合约池没有正常设置!自选股必须是连续合约,合约池导入自选股列表,基准合约为空'
)
return
if
get_account(
6
) <
=
255
or
get_account(
26
) <
=
255
or
get_account(
19
) <
=
255
:
SleepS
=
int
(datetime.datetime.now().strftime(
'%S'
))
print
(
'账户异常!未登录!退出交易!'
'等待'
+
str
(datetime.datetime.now().strftime(
'%Y-%m-%d %H:%M:%S.%f'
)[:
-
3
])\
+
'/'
+
str
(SleepS)
+
'秒启动'
)
return
else
:
NewZuoShouJia
=
round
(get_dynainf(s1,
3
),
6
)
NewOpenJia
=
round
(get_dynainf(s1,
4
),
6
)
NewHighJia
=
round
(get_dynainf(s1,
5
),
6
)
NewLowJia
=
round
(get_dynainf(s1,
6
),
6
)
NewJia
=
round
(get_dynainf(s1,
7
),
6
)
NewJunJia
=
round
(get_dynainf(s1,
11
),
6
)
NewZhenFu
=
round
(get_dynainf(s1,
13
),
6
)
NewZhangDieFu
=
round
(get_dynainf(s1,
14
),
6
)
NewBuyJia
=
round
(get_dynainf(s1,
20
),
6
)
NewSellJia
=
round
(get_dynainf(s1,
21
),
6
)
NewZhangSu
=
round
(get_dynainf(s1,
24
),
6
)
NewBuyOneVol
=
round
(get_dynainf(s1,
25
),
6
)
NewSellOneVol
=
round
(get_dynainf(s1,
31
),
6
)
NewChiCangLiang
=
round
(get_dynainf(s1,
45
),
6
)
NewZhangTing
=
round
(get_dynainf(s1,
54
),
6
)
NewDieTing
=
round
(get_dynainf(s1,
55
),
6
)
NewQianChiCangLiang
=
round
(get_dynainf(s1,
60
),
6
)
NewJinJieSuan
=
round
(get_dynainf(s1,
61
),
6
)
NewQianJieSuan
=
round
(get_dynainf(s1,
62
),
6
)
NewAmountFuDongYingKui
=
round
(get_account(
4
),
0
)
NewAmount
=
round
(get_account(
6
),
0
)
KeYongAmount
=
round
(get_account(
19
),
0
)
OrdAmount
=
round
(get_account(
26
),
0
)
BaoZhengJinAmount
=
round
(get_account(
28
),
0
)
NewAmountPingCangYingKui
=
round
(get_account(
30
),
0
)
NewAmountShouXuFei
=
round
(get_account(
31
),
0
)
ZongYinKuiAmount
=
round
((NewAmountFuDongYingKui
+
NewAmountPingCangYingKui
-
NewAmountShouXuFei),
0
)
if
ZongYinKuiAmount
=
=
0
or
BaoZhengJinAmount
=
=
0
or
NewAmount
=
=
0
:
AmountZongYingKuiLV
=
0
AmountZongFengXianDu
=
0
else
:
AmountZongYingKuiLV
=
round
(ZongYinKuiAmount
/
BaoZhengJinAmount,
6
)
if
AmountZongYingKuiLV
=
=
0
:
AmountZongFengXianDu
=
0
else
:
AmountZongFengXianDu
=
round
(BaoZhengJinAmount
/
NewAmount,
6
)
pass
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'/动态权益:'
+
str
(NewAmount)
+
'/期初余额:'
+
str
(OrdAmount)\
+
'/可用资金:'
+
str
(KeYongAmount)
+
'/手续费:'
+
str
(NewAmountShouXuFei)
+
'/平仓盈亏:'
+
str
(NewAmountPingCangYingKui)
+
'/浮动盈亏:'
\
+
str
(NewAmountFuDongYingKui)
+
'/总盈亏率:'
+
str
(AmountZongYingKuiLV)
+
'/总风险度:'
+
str
(AmountZongFengXianDu))
pass
portfolio
=
get_portfolio (s1,
2
)
pass
BuyQuantity
=
round
(portfolio.buy_quantity,
4
)
BuyMargin
=
round
(portfolio.buy_margin,
4
)
BuyAvgHoldingPrice
=
round
(portfolio.buy_avg_holding_price,
4
)
BuyPnl
=
round
(portfolio.pnl,
4
)
pass
SellQuantity
=
round
(portfolio.sell_quantity,
4
)
SellMargin
=
round
(portfolio.sell_margin,
4
)
SellAvgHoldingPrice
=
round
(portfolio.sell_avg_holding_price,
4
)
SellPnl
=
round
(portfolio.pnl,
4
)
pass
if
BuyPnl
=
=
SellPnl
and
BuyPnl
+
SellPnl !
=
0
:
ZongPnl
=
round
((BuyPnl
+
SellPnl)
/
2
,
4
)
else
:
ZongPnl
=
0
pass
instruments
=
get_instruments(s1)
BuyBaoZhengJinLv
=
round
(instruments.buy_margin_rate,
6
)
SellBaoZhengJinLv
=
round
(instruments.short_margin_rate,
6
)
HeYueDanWei
=
round
(instruments.multipliter,
6
)
MinJia
=
round
(instruments.mintick,
6
)
MeiShouDanWei
=
round
(instruments.round_lot,
6
)
KaiShouXuFei
=
round
(get_charge(s1,get_dynainf(s1,
7
),
1
,
0
),
6
)
PingShouXuFei
=
round
(get_charge(s1,get_dynainf(s1,
7
),
1
,
1
),
6
)
if
1
!
=
0
and
\
MinJia
*
HeYueDanWei
*
MeiShouDanWei
=
=
0
or
NewQianJieSuan
*
HeYueDanWei
*
BuyBaoZhengJinLv
*
SellBaoZhengJinLv
=
=
0
\
or
FengXianDu
*
OrdAmount
=
=
0
or
(KaiShouXuFei
+
PingShouXuFei)
=
=
0
:
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'!!!!!!!!!!!!!!!合约信息错误!!!更新合约信息!!!!!!!!!!!!!!!!!'
)
return
else
:
MinJiaCha
=
round
(MinJia
*
HeYueDanWei
*
MeiShouDanWei,
6
)
pass
BuyBaoZhengJin
=
round
(NewQianJieSuan
*
HeYueDanWei
*
BuyBaoZhengJinLv
*
1
,
0
)
SellBaoZhengJin
=
round
(NewQianJieSuan
*
HeYueDanWei
*
SellBaoZhengJinLv
*
1
,
0
)
MinChenBenBi
=
round
(MinJiaCha
/
(KaiShouXuFei
+
PingShouXuFei),
6
)
OrdBuyMaxVol
=
round
(FengXianDu
*
OrdAmount
/
BuyBaoZhengJin ,
0
)
OrdSellMaxVol
=
round
(FengXianDu
*
OrdAmount
/
SellBaoZhengJin ,
0
)
pass
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'/昨天:'
+
'最大持仓量:'
+
str
(OrdBuyMaxVol)
+
':'
+
str
(OrdSellMaxVol)
+
'/空保证金:'
+
str
(SellBaoZhengJin)
+
'/空保证金率:'
\
+
str
(SellBaoZhengJinLv)
+
'/多保证金:'
+
str
(BuyBaoZhengJin)
+
'/多保证金率:'
+
str
(BuyBaoZhengJinLv)
+
'/最小成本比'
+
str
(MinChenBenBi)\
+
'/开手续费:'
+
str
(KaiShouXuFei)
+
'/平手续费:'
+
str
(PingShouXuFei)
+
'/最小单位:'
+
str
(MinJia)
+
'/最小定价:'
+
str
(MinJiaCha)\
+
'/每手单位:'
+
str
(MeiShouDanWei)
+
'/单位乘数:'
+
str
(HeYueDanWei)
+
"/"
)
if
BuyQuantity>
0
:
pass
BuyPnl
=
\
round
((NewSellJia
-
BuyAvgHoldingPrice)
/
MinJia
*
MinJiaCha
*
BuyQuantity
-
(KaiShouXuFei
+
PingShouXuFei)
*
BuyQuantity,
4
)
if
BuyMargin
=
=
0
or
get_account(
6
)
=
=
0
:
BuyFengXianDu
=
0
else
:
BuyFengXianDu
=
round
(BuyMargin
/
get_account(
6
),
6
)
pass
if
BuyPnl
=
=
0
or
BuyMargin
=
=
0
or
BuyAvgHoldingPrice
=
=
0
:
BuyYingKuiLv
=
0
NewBuyBaoZhengJinLv
=
0
else
:
BuyYingKuiLv
=
round
(BuyPnl
/
BuyMargin,
6
)
NewBuyBaoZhengJinLv
=
round
(BuyMargin
/
BuyAvgHoldingPrice
/
HeYueDanWei
/
BuyQuantity ,
6
)
pass
if
FengXianDu>
0
and
KeYongAmount>
255
and
BuyMargin>
0
and
BuyQuantity>
0
:
BuyMaxVol
=
round
((FengXianDu
*
KeYongAmount)
/
(BuyMargin
/
BuyQuantity),
0
)
if
1
< MinChenBenBi < BuyMaxVol
and
BuyVol !
=
BuyMaxVol:
BuyVol
=
round
(
int
(MinChenBenBi),
0
)
pass
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'/多:最大持:'
+
str
(BuyMaxVol)
+
'/持:'
+
str
(BuyQuantity)
+
'/均价:'
\
+
str
(BuyAvgHoldingPrice)
+
'/总浮盈亏:'
+
str
(ZongPnl)
+
'/持盈亏:'
+
str
(BuyPnl)
+
'/盈亏率:'
+
str
(BuyYingKuiLv)
+
'/持风险度:'
\
+
str
(BuyFengXianDu)
+
'/持保证金率:'
+
str
(NewBuyBaoZhengJinLv)
+
'/'
)
if
SellQuantity>
0
:
pass
SellPnl
=
\
round
((SellAvgHoldingPrice
-
NewBuyJia)
/
MinJia
*
MinJiaCha
*
SellQuantity
-
(KaiShouXuFei
+
PingShouXuFei)
*
SellQuantity,
4
)
if
SellMargin
=
=
0
or
get_account(
6
)
=
=
0
:
SellFengXianDu
=
0
else
:
SellFengXianDu
=
round
(SellMargin
/
get_account(
6
),
6
)
pass
if
SellPnl
=
=
0
or
SellMargin
=
=
0
or
SellAvgHoldingPrice
=
=
0
:
SellYingKuiLv
=
0
NewSellBaoZhengJinLv
=
0
else
:
SellYingKuiLv
=
round
(SellPnl
/
SellMargin,
6
)
NewSellBaoZhengJinLv
=
round
(SellMargin
/
SellAvgHoldingPrice
/
HeYueDanWei
/
SellQuantity,
6
)
pass
if
FengXianDu >
0
and
KeYongAmount >
255
and
SellMargin >
0
and
SellQuantity >
0
:
SellMaxVol
=
round
((FengXianDu
*
KeYongAmount)
/
(SellMargin
/
SellQuantity),
0
)
if
1
< MinChenBenBi < SellMaxVol
and
SellVol !
=
SellMaxVol:
SellVol
=
round
(
int
(MinChenBenBi),
0
)
pass
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'/空:最大持:'
+
str
(SellMaxVol)
+
'/持:'
+
str
(SellQuantity)\
+
'/均价:'
+
str
(SellAvgHoldingPrice)
+
'/总浮盈亏:'
+
str
(ZongPnl)
+
'/持盈亏:'
+
str
(SellPnl)
+
'/盈亏率:'
+
str
(SellYingKuiLv)\
+
'/持风险度:'
+
str
(SellFengXianDu)
+
'/持保证金率:'
+
str
(NewSellBaoZhengJinLv)
+
'/'
)
if
not
istradertime(s1):
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
"/不在交易时间/不进入交易段"
)
if
Test
=
=
1
:
return
else
:
return
else
:
pass
if
1
!
=
0
:
pass
if
1
!
=
0
:
DianNaoTime
=
datetime.datetime.now()
DianNaoTime
=
int
(DianNaoTime.strftime(
"%H%M%S"
))
ChengJIaoTime
=
int
(
round
(get_dynainf(s1,
207
),
0
))
DianNaoTimeH
=
round
(DianNaoTime
/
10000
,
0
)
ChengJIaoTimeH
=
round
(ChengJIaoTime
/
10000
,
0
)
XiaoShiShiCha
=
(DianNaoTimeH
-
ChengJIaoTimeH)
if
DianNaoTimeH!
=
ChengJIaoTimeH:
if
DianNaoTimeH
=
=
ChengJIaoTimeH:
TimeCha
=
round
(DianNaoTime
-
ChengJIaoTime,
0
)
else
:
if
DianNaoTimeH > ChengJIaoTimeH:
TimeCha
=
round
(DianNaoTime
-
(ChengJIaoTime
+
abs
(XiaoShiShiCha
*
10000
)),
0
)
pass
if
DianNaoTimeH < ChengJIaoTimeH:
TimeCha
=
round
(DianNaoTime
-
(ChengJIaoTime
-
abs
(XiaoShiShiCha
*
10000
)),
0
)
pass
if
NewAmount !
=
0
or
NewAmountShouXuFei !
=
0
or
OrdAmount !
=
0
:
pass
if
SellQuantity >
=
0
or
BuyQuantity >
=
0
:
AmountHuiCheJinE
=
round
(NewAmount
-
NewAmountShouXuFei
-
OrdAmount,
0
)
HuiCheJinE
=
round
(OrdAmount
*
ZongHuiCheBiLi,
6
)
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'设定回撤比例:'
+
str
(ZongHuiCheBiLi)
+
'/设定回撤金额:'
+
str
(HuiCheJinE) )
if
AmountHuiCheJinE <
-
HuiCheJinE
or
AmountHuiCheJinE > HuiCheJinE
*
9
:
FangXiang
=
0
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'设定回撤比例:'
\
+
str
(ZongHuiCheBiLi)
+
'/账户当日回撤:'
+
str
(AmountHuiCheJinE)
+
'/大于触发阀值:'
+
str
(HuiCheJinE)
+
'执行风控退出平仓交易!'
)
else
:
if
ZongPnl
=
=
0
or
BaoZhengJinAmount
=
=
0
:
NewZongHuiCheBiLi
=
0
else
:
NewZongHuiCheBiLi
=
round
(ZongPnl
/
BaoZhengJinAmount,
6
)
if
NewZongHuiCheBiLi <
-
ZongHuiCheBiLi:
FangXiang
=
0
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'设定回撤比例:'
\
+
str
(ZongHuiCheBiLi)
+
'/账户当日盈亏率:'
+
str
(NewZongHuiCheBiLi)
+
'/大于回撤比例:'
+
str
(ZongHuiCheBiLi)\
+
'执行风控重启平仓交易!'
)
else
:
if
abs
(
int
(TimeCha)) >
10
or
NewChiCangLiang <
50000
or
NewQianChiCangLiang <
50000
or
NewBuyJia >
=
NewZhangTing
or
NewSellJia <
=
NewDieTing:
FangXiang
=
0
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'/时差:'
+
str
(TimeCha)\
+
'秒/######################报价不活跃=======平仓######################'
)
pass
else
:
if
1
!
=
0
and
( OpenCloseTime
=
=
1
):
print
(
str
(BanKuai)
+
str
(Name)
+
'=====================测试收盘平仓========================'
)
pass
if
1
!
=
0
:
if
NewJia > NewLowJia > NewQianJieSuan:
FangXiang
=
1
pass
if
NewJia < NewHighJia < NewQianJieSuan:
FangXiang
=
-
1
pass
if
NewZhenFu < ZhenFu:
FangXiang
=
0
pass
if
CloseTime1500
=
=
1
:
FangXiang
=
0
pass
if
FangXiang
=
=
0
:
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
"/"
+
"风控:0:"
+
str
(FangXiang)
+
'/动态盈亏率:'
+
str
(YingKuiLv))
pass
if
FangXiang
=
=
1
:
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
"/"
+
"趋势:1:"
+
str
(FangXiang)
+
'/动态盈亏率:'
+
str
(YingKuiLv))
pass
if
FangXiang
=
=
2
:
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
"/"
+
"震荡:2:"
+
str
(FangXiang)
+
'/动态盈亏率:'
+
str
(YingKuiLv))
pass
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'下单对接盘口之前:'
+
' BuyVol:'
+
str
(BuyVol)
+
' SellVol:'
+
str
(SellVol))
if
BuyVol > NewSellOneVol >
0
:
BuyVol
=
int
(NewSellOneVol)
else
:
BuyVol
=
int
(BuyVol)
if
SellVol > NewBuyOneVol >
0
:
SellVol
=
int
(NewBuyOneVol)
else
:
SellVol
=
int
(SellVol)
if
BuyQuantity !
=
0
:
if
BuyQuantity > NewSellOneVol >
0
:
NewBuyVol
=
int
(NewSellOneVol)
else
:
NewBuyVol
=
int
(BuyQuantity)
if
SellQuantity !
=
0
:
if
SellQuantity > NewBuyOneVol >
0
:
NewSellVol
=
int
(NewBuyOneVol)
else
:
NewSellVol
=
int
(SellQuantity)
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'下单对接盘口之后:'
+
' BuyVol:'
+
str
(BuyVol)
+
' SellVol:'
+
str
(SellVol))
if
1
!
=
0
\
and
(FangXiang
=
=
0
):
if
BuyQuantity !
=
0
:
sell_close(s1,
"ThisClose"
,volume
=
NewBuyVol,repeat
=
1
,serial_id
=
1
)
pass
if
SellQuantity !
=
0
:
buy_close(s1,
"ThisClose"
,volume
=
NewSellVol,repeat
=
1
,serial_id
=
2
)
pass
if
1
!
=
1
\
and
( (BuyMaxVol !
=
BuyVol
and
SellMaxVol !
=
SellVol)
or
BuyPnl !
=
0
or
SellPnl !
=
0
or
KaiShouXuFei !
=
0
\
or
PingShouXuFei !
=
0
or
BuyQuantity > BuyVol
or
SellQuantity > SellVol ):
ChengBen
=
(KaiShouXuFei
+
PingShouXuFei)
*
(BuyQuantity
+
SellQuantity)
*
ZongChengBenBi
BuyChengBen
=
(KaiShouXuFei
+
PingShouXuFei)
*
BuyQuantity
*
MinChenBenBi
SellChengBen
=
(KaiShouXuFei
+
PingShouXuFei)
*
SellQuantity
*
MinChenBenBi
pass
if
ZongPnl > ChengBen
and
(BuyPnl > BuyChengBen
and
SellPnl > SellChengBen):
pass
if
BuyQuantity >
0
and
SellQuantity >
0
:
if
BuyQuantity >
0
:
pass
buy_close(s1,
"ThisClose"
,volume
=
NewBuyVol,repeat
=
0
,serial_id
=
3
)
if
SellQuantity >
0
:
pass
sell_close(s1,
"ThisClose"
,volume
=
NewSellVol,repeat
=
0
,serial_id
=
4
)
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'多空 同时持仓盈利 风控趋势震荡对平持仓 结束循环重启!'
)
pass
elif
ZongPnl > ChengBen
and
(BuyPnl > BuyChengBen
or
SellPnl > SellChengBen):
pass
if
BuyQuantity >
0
or
SellQuantity >
0
:
if
BuyQuantity >
0
:
pass
buy_close(s1,
"ThisClose"
,volume
=
NewBuyVol,repeat
=
0
,serial_id
=
5
)
if
SellQuantity >
0
:
pass
sell_close(s1,
"ThisClose"
,volume
=
NewSellVol,repeat
=
0
,serial_id
=
6
)
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'多空 分别 (单向或者双向) 持仓合并盈利 风控趋势震荡对平持仓 结束循环重启!'
)
elif
ZongPnl <
-
ChengBen
and
(BuyPnl <
-
BuyChengBen
or
SellPnl <
-
SellChengBen):
pass
if
BuyQuantity >
0
or
SellQuantity >
0
:
if
BuyQuantity >
0
:
pass
buy_close(s1,
"ThisClose"
,volume
=
NewBuyVol,repeat
=
0
,serial_id
=
7
)
if
SellQuantity >
0
:
pass
sell_close(s1,
"ThisClose"
,volume
=
NewSellVol,repeat
=
0
,serial_id
=
8
)
print
(
str
(BanKuai)
+
':'
+
str
(MaxNum)
+
':'
+
str
(Num)
+
':'
+
str
(Name)
+
'多空 分别 (单向或者双向) 持仓合并亏损 风控趋势震荡对平持仓 结束循环重启!'
)
else
:
pass
pass
if
( NewZhangSu <
-
(NewZhenFu
*
ZhenFu) )
and
FangXiang
=
=
1
:
if
BuyQuantity
=
=
0
:
buy_open(s1,
"ThisClose"
,volume
=
BuyVol,repeat
=
0
,serial_id
=
9
)
pass
if
BuyQuantity !
=
0
and
BuyQuantity < BuyMaxVol:
buy_open(s1,
"ThisClose"
,volume
=
BuyVol,repeat
=
0
,serial_id
=
10
)
if
BuyQuantity >
0
and
FangXiang
=
=
1
:
if
BuyQuantity > BuyMaxVol:
sell_close(s1,
"ThisClose"
,volume
=
BuyVol,repeat
=
0
,serial_id
=
11
)
pass
pass
if
( NewZhangSu > (NewZhenFu
*
ZhenFu) )
and
FangXiang
=
=
-
1
:
if
SellQuantity
=
=
0
:
sell_open(s1,
"ThisClose"
,volume
=
SellVol,repeat
=
0
,serial_id
=
12
)
pass
if
SellQuantity !
=
0
and
SellQuantity < SellMaxVol:
sell_open(s1,
"ThisClose"
,volume
=
SellVol,repeat
=
0
,serial_id
=
13
)
if
SellQuantity >
0
and
FangXiang
=
=
-
1
:
if
SellQuantity > SellMaxVol:
buy_close(s1,
"ThisClose"
,volume
=
SellVol,repeat
=
0
,serial_id
=
14
)
pass
pass
if
BuyQuantity
=
=
0
and
FangXiang
=
=
2
:
if
NewZhangSu <
-
(NewZhenFu
*
ZhenFu):
buy_open(s1,
"ThisClose"
,volume
=
BuyVol,repeat
=
0
,serial_id
=
15
)
pass
if
BuyQuantity !
=
0
and
FangXiang
=
=
2
:
if
BuyQuantity < BuyMaxVol
and
NewZhangSu <
-
(NewZhenFu
*
ZhenFu):
buy_open(s1,
"ThisClose"
,volume
=
BuyVol,repeat
=
0
,serial_id
=
16
)
pass
if
SellQuantity
=
=
0
and
FangXiang
=
=
2
:
if
NewZhangSu > (NewZhenFu
*
ZhenFu):
sell_open(s1,
"ThisClose"
,volume
=
SellVol,repeat
=
0
,serial_id
=
17
)
pass
if
SellQuantity !
=
0
and
FangXiang
=
=
2
:
if
SellQuantity < SellMaxVol
and
NewZhangSu > (NewZhenFu
*
ZhenFu):
sell_open(s1,
"ThisClose"
,volume
=
SellVol,repeat
=
0
,serial_id
=
18
)
pass
return