cross_below() determines if one timeseries (testing) is crossing over another timeseries (control). The testing timeseries one starts above and ends below. Length of each list has to be greater than 1. Only evaluates the last two items of each list.
Code:
def cross_below(ts1, ts2):
retval = False
if len(ts1) > 1 and len(ts2) > 1:
if ts1[-2] > ts2[-2] and ts1[-1] < ts2[-1]:
retval = True
return retval
Name | Type | Default | Information |
---|---|---|---|
list of numbers | list | required | timeseries that is crossing below the second timeseries |
list of numbers | list | required | control timeseries |
Type | Notes |
---|---|
boolean | True if timeseries 1 is cross below timeseries 2, else False |
from cloudquant.interfaces import Strategy
import ktgfunc
class TestKTGFunc(Strategy):
@classmethod
def is_symbol_qualified(cls, symbol, md, service, account):
return symbol == 'TQQQ' and service.time_to_string(service.system_time, '%Y-%m-%d') == '2016-06-15'
# called at the beginning of each instance
def on_start(self, md, order, service, account):
t = service.time_to_string(service.system_time, '%Y-%m-%d')
bar = md.bar.daily(start=-13)
print bar.high
#[ 105.20999908 105.19000244 104.72100067 105.62999725 105.30000305 105.18000031 104.37000275 101.77999878 100.4536972 98.98999786]
ts_up = bar.high[2:4]
ts_down = bar.high[4:6]
ts_control = bar.high[0:2]
ts_not_close = bar.high[-2:]
print '\ntimeseries up', ts_up
print '\ntimeseries down', ts_down
print '\ntimeseries control', ts_control
print '\nktgfunc.cross_below( ts_down, ts_control )'
print ('test time: %s\t%s') % (t, ktgfunc.cross_below( ts_down, ts_control ) ) # True
print '\nktgfunc.cross_below( ts_up, ts_control )'
print ('test time: %s\t%s') % (t, ktgfunc.cross_below( ts_up, ts_control ) ) # False
print '\nktgfunc.cross_below( ts_not_close, ts_control )'
print ('test time: %s\t%s') % (t, ktgfunc.cross_below( ts_not_close, ts_control ) ) # False
service.terminate()
Console
[ 105.20999908 105.19000244 104.72100067 105.62999725 105.30000305 105.18000031 104.37000275 101.77999878 100.4536972 98.98999786] timeseries up [ 104.72100067 105.62999725] timeseries down [ 105.30000305 105.18000031] timeseries control [ 105.20999908 105.19000244] ktgfunc.cross_below( ts_down, ts_control ) test time: 2016-06-15 True ktgfunc.cross_below( ts_up, ts_control ) test time: 2016-06-15 False ktgfunc.cross_below( ts_not_close, ts_control ) test time: 2016-06-15 False