seq_down() tests a list or array of numbers are sequentially decreasing.
Code:
def seq_down(ts):
for x in xrange(1, len(ts)):
if ts[x - 1] < ts[x]:
return False
return True
Name | Type | Default | Information |
---|---|---|---|
time series data | list | required | list of numbers |
Type | Notes |
---|---|
boolean | True if numbers are decreasing, else False |
Check if the daily bar highs are sequentially decreasing. Backtest for TQQQ on 2016/06/15 from 9:30-16:00.
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]
print '\ntimeseries up', ts_up
print '\ntimeseries down', ts_down
print '\nktgfunc.seq_down( ts_down )'
print ('test time: %s\t%s') % (t, ktgfunc.seq_down( ts_down ) ) # True
print '\nktgfunc.seq_down( ts_up )'
print ('test time: %s\t%s') % (t, ktgfunc.seq_down( ts_up ) ) # 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] ktgfunc.seq_down( ts_down ) test time: 2016-06-15 True ktgfunc.seq_down( ts_up ) test time: 2016-06-15 False