cross_below() determines if one timeseries (testing) is crossing over another timeseries (control). The testing timeseries one starts below and ends above. Length of each list has to be greater than 1. Only evaluates the last two items of each list.
Code:
def cross_above(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 above the second timeseries |
list of numbers | list | required | control timeseries |
Type | Notes |
---|---|
boolean | True if timeseries 1 is cross above 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_above( ts_up, ts_control )'
print ('test time: %s\t%s') % (t, ktgfunc.cross_above( ts_up, ts_control ) ) # True
print '\nktgfunc.cross_above( ts_down, ts_control )'
print ('test time: %s\t%s') % (t, ktgfunc.cross_above( ts_down, ts_control ) ) # False
print '\nktgfunc.cross_above( ts_not_close, ts_control ) '
print ('test time: %s\t%s') % (t, ktgfunc.cross_above( 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_above( ts_up, ts_control ) test time: 2016-06-15 True ktgfunc.cross_above( ts_down, ts_control ) test time: 2016-06-15 False ktgfunc.cross_above( ts_not_close, ts_control ) test time: 2016-06-15 False