hikyuu2/hikyuu/examples/quick_crtsg.py

41 lines
1.1 KiB
Python
Raw Normal View History

2017-04-13 02:03:02 +08:00
#!/usr/bin/python
# -*- coding: utf8 -*-
# cp936
2024-04-02 13:33:24 +08:00
# ===============================================================================
2017-04-13 02:03:02 +08:00
# Aothor: fasiondog
# History: 20160407, Added by fasiondog
2024-04-02 13:33:24 +08:00
# ===============================================================================
2017-04-13 02:03:02 +08:00
2022-01-31 16:02:27 +08:00
from hikyuu import *
2017-04-13 02:03:02 +08:00
2024-04-02 13:33:24 +08:00
def TurtleSG(self, k):
2022-01-31 16:02:27 +08:00
n = self.get_param("n")
k = self.to
c = CLOSE(k)
2024-04-02 13:33:24 +08:00
h = REF(HHV(c, n), 1) # 前n日高点
L = REF(LLV(c, n), 1) # 前n日低点
2022-01-31 16:02:27 +08:00
for i in range(h.discard, len(k)):
if (c[i] >= h[i]):
self._add_buy_signal(k[i].datetime)
elif (c[i] <= L[i]):
self._add_sell_signal(k[i].datetime)
2017-04-13 02:03:02 +08:00
if __name__ == "__main__":
from examples_init import *
2022-01-31 16:02:27 +08:00
2017-04-13 02:03:02 +08:00
sg = crtSG(TurtleSG, {'n': 20}, 'TurtleSG')
2022-01-31 16:02:27 +08:00
s = get_stock("sh000001")
k = s.get_kdata(Query(-500))
2024-04-02 13:33:24 +08:00
# 只有设置交易对象时,才会开始实际计算
2022-01-31 16:02:27 +08:00
sg.to = k
2020-07-16 00:38:06 +08:00
dates = k.get_datetime_list()
2017-04-13 02:03:02 +08:00
for d in dates:
2022-01-31 16:02:27 +08:00
if (sg.should_buy(d)):
2017-04-13 02:03:02 +08:00
print("买入:%s" % d)
2022-01-31 16:02:27 +08:00
elif (sg.should_sell(d)):
2017-04-13 02:03:02 +08:00
print("卖出: %s" % d)