def numberFormat(val, dec_places=2, error=(None, None)): error_obj, error_key = error[0], error[1] ERRORMSG_RA_NUMFORMAT='test' if validations.isNumeric(val) and validations.isNumeric(dec_places): factor = pow(10, dec_places) return float(int(val * factor)) / factor else: error_obj.Add(error_key, ERRORMSG_RA_NUMFORMAT.format(val))
def Add(self, data): """Receives singular value or list/tuple of values to add to raw_data.""" if validations.isList(data) and validations.isNumeric(data): for x in data: self._Add(x) elif validations.isNumeric(data): self._Add(data) else: self.error.Add(self.base_error_key, ERRORMSG_RA_ADDFAIL.format(data))
def _Add(self, val): if (self._only_numerical and validations.isNumeric(val)) or not self._only_numerical : self._rollinglist.append(val) self.periods += 1 if self._only_numerical and validations.isNumeric(val): self._high = val if val > self._high or self._high is None else self._high self._low = val if val < self._low or self._low is None else self._low self._range = self.high - self.low if self.high and self.low else None if len(self) > self._listlimit: self._Delete(0) else: # Add Error handling here pass
def equalizeTimestamps(ts1, ts2): """ Ensures that timestamp2 (ts2) is of the same form as ts1 """ if not (validations.isNumeric(ts1) or validations.isTimestamp(ts1)): return None if not (validations.isNumeric(ts2) or validations.isTimestamp(ts2)): return None if validations.isTimestamp(ts1) and not validations.isTimestamp(ts2): ts2 = convertTimeStamp(ts2) elif validations.isNumeric(ts1) and not validations.isNumeric(ts2): ts2 = convertTimeStamp(ts2) return ts2
def AllowOnlyNum(self, cleanse = True): self._only_numerical = True if validations.isNumeric(self._rollinglist): if cleanse: for i, val in enumerate(self._rollinglist): if not validations.isNumeric(val): self._rollinglist.pop(i) self._high = np.max(self._rollinglist) self._low = np.max(self._rollinglist) self._range = self.high - self.low else: self._high = None self._low = None self._range = None
def DelScope(self, val): if validations.isNumeric(val): for k, v in self._scopes.iteritems(): if val == v: self._scopes.pop(k, None) elif isinstance(val, str): self._scopes.pop(val, None)
def NormalizeDataset_RL(self, num_list, sliding=True, forced_high = None, forced_low = None): """Normalize data in RollingList object. Sliding: True - High's/Low's re-defined during iteration of list. False - High/Low defined once in the context of the entire list. """ normalized_list = [] if validations.isList(num_list) and validations.isNumeric(num_list): if sliding: low, high = None, None for val in num_list: low = val if val < low or low is None else low high = val if val > high or high is None else high low = forced_low if forced_low is not None else low high = forced_high if forced_high is not None else high normalized_list.append(self.Normalize(low, high, val)) else: low = np.min(num_list) if forced_low is not None else forced_low high = np.max(num_list) if forced_high is not None else forced_high for val in num_list: normalized_list.append(self.Normalize(low, high, val)) return normalized_list
def ReStDev(new_data, old_stdev, old_mean, old_periods, error_obj=None): """Calculate St. Deviation using existing stdev/mean/period values. Without having to calculate ENTIRE raw_data. (faster than StDev) """ err_is_object = not isinstance(error_obj, type(None)) def Calc(val, old_stdev, old_mean, old_periods): sumsq = SumSq(old_stdev, old_mean, old_periods) new_periods = old_periods + 1 new_mean = ((old_mean * old_periods) + val) / new_periods new_sumsq = sumsq + pow(val, 2) new_stdev = StDevSumSq(new_sumsq, new_mean, new_periods) return new_stdev, new_mean, new_periods if validations.isNumeric(new_data): if validations.isList(new_data): for data in new_data: old_stdev, old_mean, old_periods = Calc(data, old_stdev, old_mean, old_periods) new_stdev, new_mean, new_periods = old_stdev, old_mean, old_periods else: new_stdev, new_mean, new_periods = Calc(new_data, old_stdev, old_mean, old_periods) elif err_is_object: error_obj.Add(STDEV_ERRORKEY, 'Input(s) must be numeric.') return error_obj else: return False return new_stdev, new_periods, new_mean
def SumSq_array(data_array): """Calculate Sum of Squares given an array of numerical values.""" sumsq = 0.0 if validations.isNumeric(data_array): for val in data_array: sumsq += pow(val, 2) return sumsq
def StDev(data_array, error_obj=None): """Calculate Standard Deviation from array of numeric datapoints.""" #Validate array. sum_variance = 0.0 validated = True err_is_object = not isinstance(error_obj, type(None)) if validations.isList(data_array): if not validations.isNumeric(data_array): validated = False if err_is_object: error_obj.Add(STDEV_ERRORKEY, 'Array contents must be numeric.') elif err_is_object: error_obj.Add(STDEV_ERRORKEY, 'Input must be a list or tuple.') validated = False else: validated = False # Calculate Standard Deviation. if validated: periods = len(data_array) if periods > 1: mean = np.mean(data_array) for val in data_array: sum_variance += pow(val - mean, 2) stdev = pow(pow(periods-1, -1) * sum_variance, .5) else: mean = float(data_array[0]) stdev = 0.0 return stdev, periods, mean elif error_obj: return error_obj else: return False
def Add(self, val): if validations.isNumeric(val): if validations.isList(val): for x in val: self.Add(x) else: self.data.add(val) self._calcAverages()
def Add(self, val): if self._only_numerical and validations.isNumeric(val): if validations.isList(val): for x in val: self._Add(x) else: self._Add(val) elif not self._only_numerical: self._Add(val)
def __init__(self, input_val): self.iterations = 0 self.pivotpoint = 50 self.pivotrange = 10 self.AddValueCounter = 0 self.SigObj = sig.RangeAnalysis([]) self.SigObj.Reset() if validations.isNumeric(input_val): self.iterations = input_val
def convertTimeStamp(timestamp): if validations.isTimestamp(timestamp): part1 = int(timestamp.strftime('%s')) part2 = int(timestamp.strftime('%f')) return float('{0}.{1}'.format(part1, part2)) if validations.isNumeric(timestamp): return dt.datetime.fromtimestamp(timestamp) return None
def Add(self, val): if validations.isNumeric(val) and self._only_numerical_values: if validations.isList(val): for x in val: self.Add(x) else: self._rollinglist.append(val) if len(self) > self.limit: self.Delete() elif not self._only_numerical_values: self._rollinglist.append(val)
def ParseInput(self, inp, from_tuple=False): # Parse input as datetime object if isinstance(inp, dt.datetime): self._parseInpDT(inp) # Parse input as tuple elif isinstance(inp, tuple) and not from_tuple: for val in inp: self.ParseInput(val, True) # Parse input as int/float -- Assume UTC input elif validations.isNumeric(inp): self.ParseInput(self._epochToDateConvert(inp)) # Parse input as date or time object elif isinstance(inp, dt.date) or isinstance(inp, dt.time): input_tz = self._validateTZinp(self._inputTz) self._tz = input_tz if (input_tz and not self._tz) else self._tz if self._tz else self.local_tz if isinstance(inp, dt.date): # Assign date value if not self._date: self._date = inp # Parse input as Time object else: # Assign time value if not self._time: self._time = inp # Attempt to parse string elif isinstance(inp, str): try: self.ParseInput(parse(inp)) except: self.ParseInput(None, False) # No input (or bad input). Create new Timestamp elif inp is None: input_tz = self._validateTZinp(self._inputTz) self._tz = input_tz if (input_tz and not self._tz) else self._tz if self._tz else self.local_tz # Create new DT new_dt = self._tz.localize(dt.datetime.now()) self._date = new_dt.date() self._time = new_dt.time() else: # Raise error pass
def AddData(self, RL_obj): """Receives RollingList object to calculate trailing MA values as per scope definitions. """ if issubclass(type(RL_obj), RollingList): data_set = RL_obj.list new_data = [] for label, scope_factor in self._scopes.iteritems(): if len(data_set) >= scope_factor and validations.isNumeric(data_set): avg = utils.numberFormat(np.mean(data_set[-1 * scope_factor:]), 4) else: avg = 'NA' new_data.append((label, scope_factor, avg)) self.Add(tuple(new_data))
def _epochToDateConvert(self, val): yr_sec = 31556926 mt_sec = 2629743 dy_sec = 86400 hr_sec = 3600 Rdelta = rd.relativedelta if validations.isNumeric(val): #print ('Start: {0}'.format(val)) years = int(math.floor(val/yr_sec)) val -= years * yr_sec #print ('Years: {0}, New Val{1}'.format(years, val)) months = int(math.floor(val/mt_sec)) val -= months * mt_sec #print ('Months: {0}, New Val{1}'.format(months, val)) days = int(math.floor(val/dy_sec)) val -= days * dy_sec #print ('Days: {0}, New Val{1}'.format(days, val)) hours = int(math.floor(val/hr_sec)) val -= hours * hr_sec #print ('Hours: {0}, New Val{1}'.format(hours, val)) minutes = int(math.floor(val/60)) val -= minutes * 60 #print ('Minutes: {0}, New Val{1}'.format(minutes, val)) seconds = int(math.floor(val)) micro = int((val - seconds)*100000) #print ('Seconds: {0}, New Val{1}'.format(seconds, val)) #print ('Micro: {0}'.format(micro)) return self.epoch_utc_time + Rdelta(years=years, months=months, days=days, hours=hours, minutes=minutes, seconds=seconds, microseconds=micro) return None
def _setValSpecialCase(self, prop_name, val): err_msg = None if PDMu.PROPERTY_TYPES[prop_name] == float and validations.isNumeric( val): setattr(self, '_{0}'.format(prop_name), float(val)) return None if isinstance(val, str) and (prop_name == 'market' or prop_name == 'ticker'): if prop_name == 'market': market = PDMu.MarketSearch(val) if market: setattr(self, '_{0}'.format(prop_name), market) else: # Error - No Market found err_msg = 'No market found: {0}'.format(val) if prop_name == 'ticker': if self.market: ticker = None for tick in self.market.tickers: if val.lower() == tick.lower(): ticker = tick.upper() if ticker: setattr(self, '_{0}'.format(prop_name), self.market.value[ticker]) else: # Error - Ticker not found in Market err_msg = 'Ticker ({0}) not found in Market ({1}). Ignoring.'.format( val, self.market.name) else: # Error - Must add market first err_msg = 'Cannot set ticker ({0}) without market.'.format( val) else: err_msg = -1 return err_msg
def __init__(self, dim=None): self._dim = dim if validations.isNumeric(dim) else 1 self._coords_labels = [] self._coords = []
def numberFormat_list(val, dec_places=None, error=(None, None)): if validations.isList(val) and validations.isNumeric(val): return [x for numberFormat(x, dec_places, error)]
def valueCheck(self): return not((self.NUMERIC_ONLY and not validations.isNumeric(self.value)) or self.value is None)
def AllowOnlyNumbers(self, cleanse = False): self._only_numerical_values = True if cleanse: for i, val in enumerate(self._rollinglist): if not validations.isNumeric(val): self._rollinglist.pop(i)
def AddScope(self, label=None, value=None): if label and validations.isNumeric(value): self._scopes[label] = value else: pass # Error exception handling here
def SetLimit(self, val): if validations.isNumeric(val): self._list_limit = val while len(self) > val: self._rollinglist.pop(0)
def SetLimit(self, val): if validations.isNumeric(val): self._listlimit = val
def ConvertEpoch(epoch): if validations.isNumeric(epoch): return pytz.utc.localize(parse(time.strftime(DATETIME_FORMAT, time.gmtime(epoch)))) return None
def _Delete(self, idx): if validations.isNumeric(idx): if idx < len(self): self._rollinglist.pop(idx)