list_spread() calculates the spread between the max and min in a list of numbers.
Code:
def list_spread(ts):
min = maxint
max = -maxint
range = 0.0
if len(ts) > 1:
for x in ts:
if x < min:
min = x
if x > max:
max = x
return max - min
else:
return 0.0
Name | Type | Default | Information |
---|---|---|---|
time series data | list | required | list of numbers |
Type | Special | Notes |
---|---|---|
float | [0] | will return [0] if empty list is passed in |
Get the spread between the high and low in list. 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'
# 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=-5)
print bar.high
print ('test time: %s\t%s') % (t, ktgfunc.list_spread(bar.high ) )
service.terminate()
Console
[ 105.18000031 104.37000275 101.77999878 100.4536972 98.98999786] test time: 2016-06-15 6.19000244141