def create_moving_avg_simple(self, num_history_pts): self.output_table_name = self.output_table_name.replace("TREND", "POINT") self.output_table_name = self.output_table_name.replace("CANDLE", "POINT") self.output_table_name = self.output_table_name + self.SIMPLE_AVG + "_" + str(num_history_pts) if self.to_save: ##if already exists, drop it first and then recreate if DBManager.exists_table(self.output_table_name): DBManager.drop_table(self.output_table_name) pt = PointTable(self.output_table_name) pt.save() points = self.input_points mv = MovingAverage(self.output_table_name, points) pt_array = mv.simple(num_history_pts) if self.to_save: self.save_pts(pt_array) ## possible delete the temporary point table created from candle ##if CandleTable.TEMP in self.input_point_table_name: ## DBManager.drop_table(self.input_point_table_name) return pt_array
class LIDARAltitudeMA: # Node initialization def __init__(self): # Create the publisher and subscriber self.moving_average = MovingAverage(5) self.pub = rospy.Publisher('/uav/sensors/lidar_altitude_ma', AltitudeStamped, queue_size=1) self.sub = rospy.Subscriber('/uav/sensors/lidar_position', PointStamped, self.process_altitude, queue_size=1) self.lidar_error = float( rospy.get_param('/uav/sensors/lidar_position_noise', str(0))) self.altitude = AltitudeStamped() self.lidar_point = PointStamped() rospy.spin() def process_altitude(self, msg): self.lidar_point = msg self.moving_average.add(self.lidar_point.point.z) self.altitude.value = self.moving_average.get_average() self.altitude.stamp = self.lidar_point.header.stamp self.altitude.error = self.lidar_error / math.sqrt( self.moving_average.window_size) self.pub.publish(self.altitude)
class GPSAltitudeMovingAverage(): # Node initialization def __init__(self): window_size = rospy.get_param("/gps_altitude_ma/gps_window_size", 4) self.ma = MovingAverage(window_size) # Create the publisher and subscriber self.pub = rospy.Publisher('/uav/sensors/gps_altitude_ma', AltitudeStamped, queue_size=1) self.sub = rospy.Subscriber('/uav/sensors/gps', PoseStamped, self.process_altitude, queue_size=1) rospy.spin() def process_altitude(self, msg): # add new value to the moving average list # use MovingAverage to calculate the new moving average # use the same timestamp as in msg # publish self.ma.add(msg.pose.position.z) avg = self.ma.get_average() altStamped = AltitudeStamped() altStamped.value = avg altStamped.stamp = msg.header.stamp self.pub.publish(altStamped)
def test_window(self): mw = MovingAverage(5) values = [random() for i in range(10)] for v in values: mw.add(v) self.assertTrue( abs(mw.get_average() - np.average(values[5:]) <= 0.0001))
def __init__(self): self.main_iteration = 0 #iteration in the main file self.SAMPLE_TIME = 10 #record a sample every 20 iterations self.MAX_SAMPLES = 6 #the number of samples in the array self.MIN_TEMP = 30 #minimum temperature to consider burner on #create a moving average list for each burner self.moving_average_ur = MovingAverage(self.MAX_SAMPLES) self.moving_average_ul = MovingAverage(self.MAX_SAMPLES) self.moving_average_ll = MovingAverage(self.MAX_SAMPLES) self.moving_average_lr = MovingAverage(self.MAX_SAMPLES)
def __init__(self): window_size = rospy.get_param("/gps_altitude_ma/gps_window_size", 4) self.ma = MovingAverage(window_size) # Create the publisher and subscriber self.pub = rospy.Publisher('/uav/sensors/gps_altitude_ma', AltitudeStamped, queue_size=1) self.sub = rospy.Subscriber('/uav/sensors/gps', PoseStamped, self.process_altitude, queue_size=1) rospy.spin()
def __init__(self): # Create the publisher and subscriber self.moving_average = MovingAverage(5) self.pub = rospy.Publisher('/uav/sensors/altitude_ma', AltitudeStamped, queue_size=1) self.sub = rospy.Subscriber('/uav/sensors/gps_noisy', PoseStamped, self.process_altitude, queue_size=1) self.gps_error = float(rospy.get_param('/uav/sensors/position_noise_lvl', str(0))) self.altitude = AltitudeStamped() self.gps_pose = PoseStamped() rospy.spin()
class TimerFps(Timer): def __init__(self, name='', average_width=10, is_verbose=True): super().__init__(name, is_verbose) self.moving_average = MovingAverage(average_width) def refresh(self): elapsed = self.elapsed() self.moving_average.getAverage(elapsed) self.start() if self._is_verbose is True: dT = self.moving_average.getAverage() name = self._name if self._is_paused: name += ' [paused]' message = 'Timer::' + name + ' - fps: ' + str( 1. / dT) + ', T: ' + str(dT) timer_print(message)
def __init__(self, filename): # initialize relay controller self.relay = Relay() # initialize voltage reader self.vreader = VReader() # initialize voltage-to-temp converter self.v_to_t = VtoT() # initialize moving average with an appropriate number of samples self.ma = MovingAverage(20) # initialize the target temperature vs time self.t_profile = TProfile(filename) # initialize data logger self.logger = Logger(chop_off_end(chop_off_folder(filename), ".csv"))
def test_window_changing(self): mw = MovingAverage(5) zero_values = [random() for i in range(5)] for v in zero_values: mw.add(v) self.assertTrue( abs(mw.get_average() - np.average(zero_values) <= 0.0001)) five_values = [5 + random() for i in range(5)] for v in five_values: mw.add(v) self.assertTrue( abs(mw.get_average() - np.average(five_values) <= 5.0001))
def create_moving_avg_exp(self): self.output_table_name += self.EXP_AVG pt_name = self.table_name if DBManager.exists_table(self.output_table_name): DBManager.drop_table(self.output_table_name) pt = PointTable(self.output_table_name) pt.save() points = self.input_points mv = MovingAverage(self.output_table_name, points) pt_array = mv.exponential() self.save_pts(pt_array) ## possible delete the temporary point table created from candle ##if CandleTable.TEMP in self.input_point_table_name: ## DBManager.drop_table(self.input_point_table_name) return self.output_table_name
def __init__(self): self.TEMPON = 45 #low range of the sensor (this will be blue on the screen) self.MINTEMP = 18 #high range of the sensor (this will be red on the screen) self.MAXTEMP = 100 #initialize the sensor self.sensor = Adafruit_AMG88xx() self.points = [(math.floor(ix / 8), (ix % 8)) for ix in range(0, 64)] self.grid_x, self.grid_y = np.mgrid[0:7:32j, 0:7:32j] #height and width of processed image self.HEIGHT = 32 self.WIDTH = 32 #minimum contour area self.MIN_AREA = 50 #minimum temperature self.MIN_TEMP = 30 #cap on maximum temperature for input picture self.TEMP_CAP = 80 #maximum color mapping self.MAX_COLOR = 240 #the number of samples in the temperature average array self.MAX_SAMPLES = 20 #create a moving average list for each burner self.moving_average_ur = MovingAverage(self.MAX_SAMPLES) self.moving_average_ul = MovingAverage(self.MAX_SAMPLES) self.moving_average_ll = MovingAverage(self.MAX_SAMPLES) self.moving_average_lr = MovingAverage(self.MAX_SAMPLES)
def begin(self, range, config_fname): self.range = range self.config_fname = config_fname options = self.read_shape_config() for option in options: if option == 'gains': self.gains = [float(i) for i in options['gains']] self.gains = np.array(self.gains) # print gains # gains = options['gains'] elif option == 'master_gain': self.master_gain = float(options['master_gain'][0]) # print master_gain elif option == 'washouts': washout_time = [int(i) for i in options['washouts']] for idx, t in enumerate(washout_time): self.set_washout(idx, washout_time[idx]) # print washout_time elif option == 'moving_averages': self.ma_samples = [int(i) for i in options['moving_averages']] for count in self.ma_samples: self.moving_average.append(MovingAverage(count))
def calc_vol_tables(self): if self.calc_stats: self.strat_stat_array = [] self.ss_tn = StratStatTable.calc_name(self.table_name, self.NAME) if DBManager.exists_table(self.ss_tn): DBManager.drop_table(self.ss_tn) sst = StratStatTable(self.ss_tn) sst.save() pt_name = CandleTable.to_point_table(self.table_name, "volume") pt_close = CandleTable.to_point_table(self.table_name, "close") self.points = PointTable.get_point_array(pt_name) self.points_close = PointTable.get_point_array(pt_close) pt_name_1 = "TEMP1" mv1 = MovingAverage(pt_name_1, self.points) pt_name_2 = "TEMP2" mv2 = MovingAverage(pt_name_2, self.points) self.vol_pts_short = mv1.simple(self.VOL_PERIOD_SHORT) self.vol_pts_long = mv2.simple(self.VOL_PERIOD_LONG) ##pp = PointPopulator(self.table_name) ##self.stddev_pts_short = pp.create_stddev(self.STDDEV_PERIOD_SHORT) ##self.stddev_pts_long = pp.create_stddev(self.STDDEV_PERIOD_LONG) DBManager.drop_matching_tables("TEMP")
class TestMovingAverage(unittest.TestCase): def setUp(self): self.moving_average = MovingAverage() def test_compute_moving_average_example_1(self): result = self.moving_average.compute(3, [0, 1, 2, 3]) output = [0, 0.5, 1, 2] self.assertEqual(result, output) def test_compute_moving_average_example_2(self): result = self.moving_average.compute( 5, [0, 1, -2, 3, -4, 5, -6, 7, -8, 9]) output = [ 0, 0.5, -0.3333333333333333, 0.5, -0.4, 0.6, -0.8, 1, -1.2, 1.4 ] self.assertEqual(result, output) def test_compute_fails_with_non_int_window_size(self): err_msg = 'window_size must be an int' with self.assertRaises(TypeError) as err: self.moving_average.compute('3', [0, 1, 2, 3]) self.assertEqual(str(err.exception), err_msg) def test_compute_fails_with_non_list_values(self): err_msg = 'values nust be an array' with self.assertRaises(TypeError) as err: self.moving_average.compute(3, '[0, 1, 2, 3]') self.assertEqual(str(err.exception), err_msg) def test_compute_fails_with_an_empty_list_values(self): err_msg = 'values cannot be an empty array' with self.assertRaises(ValueError) as err: self.moving_average.compute(3, []) self.assertEqual(str(err.exception), err_msg) def test_compute_fails_with_window_size_smaller_than_2(self): err_msg = 'window_size cannot be less than 2' with self.assertRaises(ValueError) as err: self.moving_average.compute(1, [0, 1, 2, 3]) self.assertEqual(str(err.exception), err_msg) def test_compute_fails_with_values_smaller_than_2(self): err_msg = 'values cannot have less than 2 items' with self.assertRaises(ValueError) as err: self.moving_average.compute(3, [0]) self.assertEqual(str(err.exception), err_msg) def test_compute_fails_with_window_size_greater_than_values_list(self): err_msg = 'window_size cannot be greater than the values array size' with self.assertRaises(ValueError) as err: self.moving_average.compute(5, [0, 1, 2, 3]) self.assertEqual(str(err.exception), err_msg) def test_compute_fails_with_values_list_is_not_float(self): err_msg = 'found a non-float value. values must be a list of floating point numbers' with self.assertRaises(TypeError) as err: self.moving_average.compute(3, [0, '1', 2, 3]) self.assertEqual(str(err.exception), err_msg)
def test_empty_window(self): mw = MovingAverage(10) self.assertEqual(mw.get_average(), 0.0)
from zipline.utils.factory import load_from_yahoo from pandas import DataFrame from moving_average import MovingAverage import datetime import logging import os logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) start = datetime.datetime.today() - datetime.timedelta(days=500) end = datetime.datetime.today() stocks = [row.strip() for row in open('indexes.csv') if row.strip() != ""] data_cache_path = 'data_cache.csv' if not os.path.exists(data_cache_path): logger.info("No Data Cache found, loading from Yahoo") data = load_from_yahoo(indexes={}, stocks=[stocks[0]], start=start, end=end, adjusted=False) data.to_csv(data_cache_path) else: logger.info("Loading data from cache") data = DataFrame.from_csv(data_cache_path) mavg = MovingAverage() perf = mavg.run(data)
parser.add_argument("-d", "--device", dest=DEVICE, required=True, help="Device ('left' or 'right'") cli.verbose(parser) args = vars(parser.parse_args()) # Setup logging setup_logging(level=args[LOG_LEVEL]) userdata = { TOPIC: "lidar/{0}/mm".format(args[DEVICE]), COMMAND: "lidar/{0}/command".format(args[DEVICE]), ENABLED: True, MOVING_AVERAGE: MovingAverage(size=3), OOR_VALUES: OutOfRangeValues(size=args[OOR_SIZE]), OOR_TIME: args[OOR_TIME], OOR_UPPER: args[OOR_UPPER] } with SerialReader(func=fetch_data, userdata=userdata, port=SerialReader.lookup_port(args[DEVICE_ID]) if args.get(DEVICE_ID) else args[SERIAL_PORT], baudrate=args[BAUD_RATE], debug=True): with MqttConnection(hostname=args[MQTT_HOST], userdata=userdata, on_connect=on_connect, on_message=frc_utils.on_message):
def __init__(self, name='', average_width=10, is_verbose=True): super().__init__(name, is_verbose) self.moving_average = MovingAverage(average_width)
def compute(window_size, values): moving_average = MovingAverage() result = moving_average.compute(window_size, values) print("window size: %s\nvalues: %s\nmoving avg: %s" % (window_size, values, result))
def setUp(self): self.moving_average = MovingAverage()
import logging from optibook.synchronous_client import Exchange from strategy import should_kill_attempt, arbitrage, stoikov_mm from utils import balance_positions from moving_average import MovingAverage logging.getLogger('client').setLevel('ERROR') exchange = Exchange() exchange.connect() START_PNL = exchange.get_pnl() ma_A = MovingAverage(exchange, "PHILIPS_A") tick = 1 while not should_kill_attempt(exchange, START_PNL): time.sleep(0.11) print(f"tick {tick}") tick += 1 ma_A.update() # Don't want to balance our trades from our MM positions exchange.delete_orders("PHILIPS_A") if balance_positions(exchange, total_threshold=40):
class PController: # initializes with the filename of the temperature profile csv def __init__(self, filename): # initialize relay controller self.relay = Relay() # initialize voltage reader self.vreader = VReader() # initialize voltage-to-temp converter self.v_to_t = VtoT() # initialize moving average with an appropriate number of samples self.ma = MovingAverage(20) # initialize the target temperature vs time self.t_profile = TProfile(filename) # initialize data logger self.logger = Logger(chop_off_end(chop_off_folder(filename), ".csv")) # update the relay state # based on the given target temperature # and the read temperature # arguments are: # target temp def update(self, target): # determine the temperature in the kiln right now v = self.vreader.get() T = self.v_to_t.get(v) T_avg = self.ma.append(T) # determine what the temperature in the kiln should be right now T_target = self.t_profile.get_target() # determine if the coil state needs to be switched # if it switches, log it switched = False if not self.relay.is_on() and T_avg < T_target - T_tolerance: self.relay.turn_on() switched = True elif self.relay.is_on() and T_avg > T_target + T_tolerance: self.relay.turn_off() switched = True # log the data self.logger.log(time.time(), T_avg, self.relay.is_on(), switched) # output to the console print("target temp = " + str(int(T_target)) + " C (" + str(int(C_to_F(T_target))) + " F) ") print("actual temp = " + str(int(T_avg)) + " C (" + str(int(C_to_F(T_avg))) + " F) [V = " + str(v) + " V, T = " + str(int(T)) + " C (" + str(int(C_to_F(T))) + " F)] ") if self.relay.is_on(): print("relay: " + Back.GREEN + Fore.BLACK + "ON" + Style.RESET_ALL + " ") else: print("relay: " + Back.RED + Fore.BLACK + "OFF" + Style.RESET_ALL + " ") print("\033[A\033[A\033[A\033[A\r" ) # reset print location to beginning of the above text block
parser.add_argument("--avg_size", dest=AVG_SIZE, default=10, type=int, help="Moving average size [10]") cli.verbose(parser) args = vars(parser.parse_args()) # Setup logging setup_logging(level=args[LOG_LEVEL]) userdata = { TOPIC: "lidar/{0}/cm".format(args[DEVICE]), COMMAND: "lidar/{0}/command".format(args[DEVICE]), ENABLED: True, MOVING_AVERAGE: MovingAverage(args[AVG_SIZE]), OOR_VALUES: OutOfRangeValues(size=args[OOR_SIZE]), OOR_TIME: args[OOR_TIME] } with SerialReader(func=fetch_data, userdata=userdata, port=SerialReader.lookup_port(args[DEVICE_ID]) if args.get(DEVICE_ID) else args[SERIAL_PORT], baudrate=args[BAUD_RATE]): with MqttConnection(hostname=args[MQTT_HOST], userdata=userdata, on_connect=on_connect, on_message=frc_utils.on_message): waitForKeyboardInterrupt()