hikyuu2/docs/examples/Turtle_SG.py

48 lines
1.3 KiB
Python
Raw Normal View History

2019-02-17 16:10:03 +08:00
#!/usr/bin/python
# -*- coding: utf8 -*-
# cp936
2024-04-02 21:56:25 +08:00
# ===============================================================================
2019-02-17 16:10:03 +08:00
# Aothor: fasiondog
# History: 20160407, Added by fasiondog
2024-04-02 21:56:25 +08:00
# ===============================================================================
2019-02-17 16:10:03 +08:00
2022-02-27 10:11:43 +08:00
from hikyuu import *
2019-02-17 16:10:03 +08:00
2024-04-02 21:56:25 +08:00
class TurtleSignal(SignalBase, k):
2022-02-27 10:11:43 +08:00
def __init__(self, n=20):
2019-02-17 16:10:03 +08:00
super(TurtleSignal, self).__init__("TurtleSignal")
2022-02-27 10:11:43 +08:00
self.set_param("n", 20)
2019-02-17 16:10:03 +08:00
def _clone(self):
return TurtleSignal()
def _calculate(self):
2022-02-27 10:11:43 +08:00
n = self.get_param("n")
2019-02-17 16:10:03 +08:00
c = CLOSE(k)
2024-04-02 21:56:25 +08:00
h = REF(HHV(c, n), 1) # 前n日高点
L = REF(LLV(c, n), 1) # 前n日低点
2019-02-17 16:10:03 +08:00
for i in range(h.discard, len(k)):
if (c[i] >= h[i]):
2022-02-27 10:11:43 +08:00
self._add_buy_signal(k[i].datetime)
2019-02-17 16:10:03 +08:00
elif (c[i] <= L[i]):
2022-02-27 10:11:43 +08:00
self._add_sell_signal(k[i].datetime)
2019-02-17 16:10:03 +08:00
if __name__ == "__main__":
from examples_init import *
2022-02-27 10:11:43 +08:00
2019-02-17 16:10:03 +08:00
sg = TurtleSignal()
2022-02-27 10:11:43 +08:00
s = get_stock("sh000001")
k = s.get_kdata(Query(-500))
2024-04-02 21:56:25 +08:00
# 只有设置交易对象时,才会开始实际计算
2022-02-27 10:11:43 +08:00
sg.to = k
dates = k.get_datetime_list()
2019-02-17 16:10:03 +08:00
for d in dates:
2022-02-27 10:11:43 +08:00
if (sg.should_buy(d)):
2019-02-17 16:10:03 +08:00
print("买入:%s" % d)
2022-02-27 10:11:43 +08:00
elif (sg.should_sell(d)):
2019-02-17 16:10:03 +08:00
print("卖出: %s" % d)