基本原理

  • 1、在当天收盘,计算两个值:最高价 - 收盘价,和收盘价 - 最低价。然后取这两个值较大的那个,乘以k值。结果成为触发值。

  • 2、在第二天开盘,记录开盘价,然后在价格超过(开盘+触发值)时马上买多,或者价格低于(开盘-触发值)时马上卖空。

  • 3、这个系统是反转系统,没有单独止损。也就是说,反向信号也同时就是平仓信号。

策略代码

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
var STATE_IDLE = 0
var STATE_LONG = 1
var STATE_SHORT = 2
var State = STATE_IDLE
var LastBarTime = 0
var UpTrack = 0
var DownTrack = 0
var InitAccount = null
function GetPosition(posType) {
var positions = exchange.GetPosition()
for (var i = 0; i < positions.length; i++) {
if (positions[i].Type === posType) {
return [positions[i].Price, positions[i].Amount];
}
}
return [0, 0]
}
function CancelPendingOrders() {
while (true) {
var orders = exchange.GetOrders()
for (var i = 0; i < orders.length; i++) {
exchange.CancelOrder(orders[i].Id)
Sleep(500)
}
if (orders.length === 0) {
break
}
}
}
function Trade(currentState, nextState) {
var pfn = nextState === STATE_LONG ? exchange.Buy : exchange.Sell
if (currentState !== STATE_IDLE) {
exchange.SetDirection(currentState === STATE_LONG ? "closebuy" : "closesell")
while (true) {
var amount = GetPosition(currentState === STATE_LONG ? PD_LONG : PD_SHORT)[1]
if (amount === 0) {
break
}
pfn(nextState === STATE_LONG ? _C(exchange.GetTicker).Sell * 1.001 : _C(exchange.GetTicker).Buy * 0.999, amount)
Sleep(500)
CancelPendingOrders()
}
var account = exchange.GetAccount()
LogProfit(_N(account.Stocks - InitAccount.Stocks, 3), "收益率:", _N((account.Stocks - InitAccount.Stocks) * 100 / InitAccount.Stocks, 3) + '%')
}
exchange.SetDirection(nextState === STATE_LONG ? "buy" : "sell")
while (true) {
var pos = GetPosition(nextState === STATE_LONG ? PD_LONG : PD_SHORT)
if (pos[1] >= AmountOP) {
Log("持仓均价", pos[0], "数量:", pos[1])
break
}
pfn(nextState === STATE_LONG ? _C(exchange.GetTicker).Sell * 1.001 : _C(exchange.GetTicker).Buy * 0.999, AmountOP-pos[1])
Sleep(500)
CancelPendingOrders()
}
}
function onTick() {
var records = exchange.GetRecords()
if (!records || records.length <= NPeriod) {
return
}
var Bar = records[records.length - 1]
$.PlotRecords(records, 'K线')
if (LastBarTime !== Bar.Time) {
var HH = TA.Highest(records, NPeriod, 'High')
var HC = TA.Highest(records, NPeriod, 'Close')
var LL = TA.Lowest(records, NPeriod, 'Low')
var LC = TA.Lowest(records, NPeriod, 'Close')
var Range = Math.max(HH - LC, HC - LL)
UpTrack = _N(Bar.Open + (Ks * Range), 3)
DownTrack = _N(Bar.Open - (Kx * Range), 3)
$.PlotHLine(UpTrack, 'UpTrack')
$.PlotHLine(DownTrack, 'DownTrack')
LastBarTime = Bar.Time
}
LogStatus("Price:", Bar.Close, "Up:", UpTrack, "Down:", DownTrack, "Date:", new Date())
var msg
if (State === STATE_IDLE || State === STATE_SHORT) {
if (Bar.Close >= UpTrack) {
msg = '做多 触发价: ' + Bar.Close + ' 上轨:' + UpTrack
Log(msg)
Trade(State, STATE_LONG)
State = STATE_LONG
$.PlotFlag(Bar.Time, msg, '多', 'flag', 'red')
}
}
if (State === STATE_IDLE || State === STATE_LONG) {
if (Bar.Close <= DownTrack) {
msg = '做空 触发价: ' + Bar.Close + ' 下轨:' + DownTrack
Log(msg)
Trade(State, STATE_SHORT)
$.PlotFlag(Bar.Time, msg, '空', 'circlepin', 'green')
State = STATE_SHORT
}
}
}
function main() {
exchange.SetContractType("quarter")
exchange.SetMarginLevel(10)
if (exchange.GetPosition().length > 0) {
throw "策略启动前不能有持仓."
}
CancelPendingOrders()
InitAccount = exchange.GetAccount()
while (true) {
onTick()
Sleep(500)
}
}