def main(): try: Logger.open() Logger.sendTelegramMessage("Bot started...") exchange = ExchangeWrapperForBacktesting() analyst = Analyst(exchange) marketRepos = dict( ) # TODO: Markets can appear and disappear, with time... for marketName in [ x["MarketName"] for x in exchange.getMarketSummary() if x["BaseVolume"] > 3000 ]: marketRepos[marketName] = MarketStatusRepository(marketName) Logger.log("Markets: " + " ".join(marketRepos.keys())) while True: for market in marketRepos.keys(): # TODO: the iterations of this loop don't depend on each other, so we can parallelize marketRepos[market].addTick(exchange.getCurrentCandle(market)) analyst.doTrading(marketRepos.values()) exchange.wait() Logger.close() except: exceptionInfo = traceback.format_exc() Logger.log(exceptionInfo) Logger.close()
def test_buy_sell_bad(self): a = Analyst(100) buy = a.buyLong(50, 3) self.assertFalse(buy) self.assertAnalyst(a, 100, 0, 0) sell = a.sellLong(55, 3) self.assertFalse(sell) self.assertAnalyst(a, 100, 0, 0)
def study_strategies(presets): analyst = Analyst() for preset in presets.index: intern = Intern(strategy, preset) prices, signals, labels = intern.get_all_data() intern.create_samples(prices, signals, labels) scientist_deligate(8) analyst.get_parcel() return analyst.get_parcel()
def __init__(self): params = Config.params # Setups the logger self.logger = logging.getLogger(__name__) # Setups the clock self.clock = Clock() # Sets up the airport airport_name = params["airport"] self.airport = Airport.create(airport_name) # Sets up the scenario self.scenario = Scenario.create( airport_name, self.airport.surface) # Sets up the routing expert monitoring the airport surface self.routing_expert = RoutingExpert(self.airport.surface.links, self.airport.surface.nodes, params["simulation"]["cache"]) # Sets up the uncertainty module self.uncertainty = (Uncertainty(params["uncertainty"]["prob_hold"], params["uncertainty"]["speed_bias_sigma"], params["uncertainty"]["speed_bias_mu"]) if params["uncertainty"]["enabled"] else None) # Loads the requested scheduler self.scheduler = get_scheduler() if not params["simulator"]["test_mode"]: # Sets up the analyst self.analyst = Analyst(self) # Sets up the state logger self.state_logger = StateLogger() # Initializes the previous schedule time self.last_schedule_time = None # Initializes the last execution time for rescheduling to None self.last_schedule_exec_time = None self.__print_stats()
def test_rebalance(self): a = Analyst(1000) a.rebalancePortfolio(0.2, 0.4, 10, 20) portfolio = a.getPortfolio(10, 20) self.assertPortfolio(portfolio, .4, .2, .4) a.rebalancePortfolio(.4, .2, 10, 20) portfolio = a.getPortfolio(10, 20) self.assertPortfolio(portfolio, .4, .4, .2)
def test_buy_long_good(self): a = Analyst(10000) a.buyLong(50, 3) cash = 10000 - 3*50 self.assertAnalyst(a, cash, 3, 0) a.buyLong(55, 10) cash -= 55*10 self.assertAnalyst(a, cash, 13, 0)
class Simulation: """Simulation, representing a simulation day, holds both static and dynamic states of the current airport, and implements `tick()` functions for the caller to simulation to the next state. """ def __init__(self): params = Config.params # Setups the logger self.logger = logging.getLogger(__name__) # Setups the clock self.clock = Clock() # Sets up the airport airport_name = params["airport"] self.airport = Airport.create(airport_name) # Sets up the scenario self.scenario = Scenario.create(airport_name, self.airport.surface) # Sets up the routing expert monitoring the airport surface self.routing_expert = RoutingExpert(self.airport.surface.links, self.airport.surface.nodes, params["simulation"]["cache"]) # Sets up the uncertainty module self.uncertainty = (Uncertainty(params["uncertainty"]["prob_hold"]) if params["uncertainty"]["enabled"] else (None)) # Loads the requested scheduler self.scheduler = get_scheduler() if not params["simulator"]["test_mode"]: # Sets up the analyst self.analyst = Analyst(self) # Sets up the state logger self.state_logger = StateLogger() # Initializes the previous schedule time self.last_schedule_time = None # Initializes the last execution time for rescheduling to None self.last_schedule_exec_time = None self.__print_stats() def tick(self): """Moves the states of this simulation to the next state.""" self.logger.debug("\nCurrent Time: %s", self.now) try: # Reschedule happens before the tick if self.__is_time_to_reschedule(): self.logger.info("Time to reschedule") start = time.time() self.__reschedule() self.last_schedule_exec_time = time.time() - start # seconds self.last_schedule_time = self.now self.logger.info("Last schedule time is updated to %s", self.last_schedule_time) # Adds aircrafts self.airport.add_aircrafts(self.scenario, self.now, self.clock.sim_time) # Injects uncertainties if self.uncertainty: self.uncertainty.inject(self) # Ticks self.airport.tick() if not Config.params["simulator"]["test_mode"]: self.state_logger.log_on_tick(self) self.clock.tick() # Removes aircrafts self.airport.remove_aircrafts(self.scenario) # Abort on conflict conflicts = self.airport.conflicts if conflicts: for conflict in conflicts: self.logger.error("Found %s", conflict) raise SimulationException("Conflict found") # Observe if not Config.params["simulator"]["test_mode"]: self.analyst.observe_on_tick(self) except ClockException as error: # Finishes if not Config.params["simulator"]["test_mode"]: self.analyst.save() raise error except SimulationException as error: raise error except Exception as error: self.logger.error(traceback.format_exc()) raise error def __is_time_to_reschedule(self): reschedule_cycle = Config.params["simulation"]["reschedule_cycle"] last_time = self.last_schedule_time next_time = (get_seconds_after(last_time, reschedule_cycle) if last_time is not None else None) return last_time is None or next_time <= self.now def __reschedule(self): schedule = self.scheduler.schedule(self) self.airport.apply_schedule(schedule) if not Config.params["simulator"]["test_mode"]: self.analyst.observe_on_reschedule(self) @property def now(self): """Return the current time of the simulation.""" return self.clock.now def __print_stats(self): self.scenario.print_stats() self.airport.print_stats() def __getstate__(self): __dict = dict(self.__dict__) del __dict["logger"] __dict["uncertainty"] = None __dict["routing_expert"] = None return __dict def __setstate__(self, new_dict): self.__dict__.update(new_dict) def set_quiet(self, logger): """Sets the simulation and its subclass to quiet mode where the logger doesn't print that many stuff. """ self.logger = logger self.airport.set_quiet(logger) self.scenario.set_quiet(logger) self.routing_expert.set_quiet(logger) @property def copy(self): """Obtains a immutable copy of this simulation.""" # NOTE: If uncertainty is not None, call inject() in tick(). return ClonedSimulation(self)
class Simulation: """Simulation, representing a simulation day, holds both static and dynamic states of the current airport, and implements `tick()` functions for the caller to simulation to the next state. """ def __init__(self): params = Config.params # Setups the logger self.logger = logging.getLogger(__name__) # Setups the clock self.clock = Clock() # Sets up the airport airport_name = params["airport"] self.airport = Airport.create(airport_name) # Sets up the scenario self.scenario = Scenario.create( airport_name, self.airport.surface) # Sets up the routing expert monitoring the airport surface self.routing_expert = RoutingExpert(self.airport.surface.links, self.airport.surface.nodes, params["simulation"]["cache"]) # Sets up the uncertainty module self.uncertainty = (Uncertainty(params["uncertainty"]["prob_hold"], params["uncertainty"]["speed_bias_sigma"], params["uncertainty"]["speed_bias_mu"]) if params["uncertainty"]["enabled"] else None) # Loads the requested scheduler self.scheduler = get_scheduler() if not params["simulator"]["test_mode"]: # Sets up the analyst self.analyst = Analyst(self) # Sets up the state logger self.state_logger = StateLogger() # Initializes the previous schedule time self.last_schedule_time = None # Initializes the last execution time for rescheduling to None self.last_schedule_exec_time = None self.__print_stats() def tick(self): """Moves the states of this simulation to the next state.""" self.logger.debug("\nCurrent Time: %s", self.now) try: # # Reschedule happens before the tick. It will resolve conflict here # if self.__is_time_to_reschedule(): # self.logger.info("Time to reschedule") # start = time.time() # self.__reschedule() # it will try to resolve conflict # self.last_schedule_exec_time = time.time() - start # seconds # self.last_schedule_time = self.now # self.logger.info("Last schedule time is updated to %s", # self.last_schedule_time) # Add aircraft self.airport.add_aircrafts(self.scenario, self.now, self.clock.sim_time, self.scheduler) start = time.time() self.__reschedule() # it will try to resolve conflict self.last_schedule_exec_time = time.time() - start # seconds self.last_schedule_time = self.now self.logger.info("Last schedule time is updated to %s", self.last_schedule_time) # # Inject uncertainties # if self.uncertainty: # self.uncertainty.inject(self) # Tick self.airport.tick() state = None aircrafts = self.airport.control_takeoff() if not Config.params["simulator"]["test_mode"]: state = self.state_logger.log_on_tick(self) self.clock.tick() # print(len(state["aircrafts"])) # if len(state["aircrafts"]) >= 1: # print(state["aircrafts"][0]["callsign"] + " "+state["aircrafts"][0]["state"]) # Remove aircraft close to the runway self.airport.remove_aircrafts(self.scenario) self.airport.remove_departure_aircrafts(aircrafts) # Abort on conflict conflicts, conflicts_dist = self.airport.conflicts if conflicts: for idx, conflict in enumerate(conflicts): dist = conflicts_dist[idx] print("Found %s", conflict) print("Conflict distance: %d", dist) print(conflict.detailed_description) self.logger.error("Found %s", conflict) self.logger.error("Conflict distance: %d", dist) self.logger.error(conflict.detailed_description) for aircraft in self.airport.aircrafts: self.logger.error(aircraft) print(aircraft.model) aircraft.has_conflict = True # for conflict in conflicts: # self.logger.error("Found %s", conflict) # self.logger.error("Conflict distance: ", dist) # self.logger.error(conflict.detailed_description) # for aircraft in self.airport.aircrafts: # self.logger.error(aircraft) # change color for crashing airplanes # print(pair[0].model, pair[1].model) # pair[0].has_conflict = True # pair[1].has_conflict = True # raise SimulationException("Conflict found") # Observe if not Config.params["simulator"]["test_mode"]: self.analyst.observe_on_tick(self) # return current state for streaming visualization return state except ClockException as error: # Finishes if not Config.params["simulator"]["test_mode"]: self.analyst.save() raise error except SimulationException as error: raise error except Exception as error: self.logger.error(traceback.format_exc()) raise error def __is_time_to_reschedule(self): reschedule_cycle = Config.params["simulation"]["reschedule_cycle"] last_time = self.last_schedule_time next_time = (get_seconds_after(last_time, reschedule_cycle) if last_time is not None else None) return last_time is None or next_time <= self.now def __reschedule(self): schedule, priority = self.scheduler.schedule(self) self.airport.apply_schedule(schedule) self.airport.apply_priority(priority) if not Config.params["simulator"]["test_mode"]: self.analyst.observe_on_reschedule(self) @property def now(self): """Return the current time of the simulation.""" return self.clock.now def __print_stats(self): self.scenario.print_stats() self.airport.print_stats() def __getstate__(self): __dict = dict(self.__dict__) del __dict["logger"] __dict["uncertainty"] = None __dict["routing_expert"] = None return __dict def __setstate__(self, new_dict): self.__dict__.update(new_dict) def set_quiet(self, logger): """Sets the simulation and its subclass to quiet mode where the logger doesn't print that many stuff. """ self.logger = logger self.airport.set_quiet(logger) self.scenario.set_quiet(logger) self.routing_expert.set_quiet(logger) @property def copy(self): """Obtains a immutable copy of this simulation.""" # NOTE: If uncertainty is not None, call inject() in tick(). return ClonedSimulation(self)
else: p.set_title('Fixed Cluster-like 2D Test Set') p.grid(True) p.set_aspect('equal', 'datalim') for i, tup in enumerate(self.data): p.annotate(str(i), tup) plt.show() # Brief script-like behavior for development, debugging, and testing purposes: if __name__ == "__main__": from analyst import Analyst from analyst.evaluators import NucleusClusterizer s = TestSet2D(random=False) a = Analyst( embeddings=s, metric="euclidean", encoder=s.encode, decoder=s.decode, desc="2D Debug Test", evaluators=[ #"Nodal 3-Hubs", NucleusClusterizer(hub_category="Nodal 3-Hubs"), "Hubs", "Nuclei", "Nodes" ], make_distance_matrix=True) Analyst.save(a, "an_2d_debug_test")
#p.set_ylabel('y') if self.random: p.set_title('Random 2D Test Set') else: p.set_title('Fixed Cluster-like 2D Test Set') p.grid(True) p.set_aspect('equal', 'datalim') for i, tup in enumerate(self.data): p.annotate(str(i), tup) plt.show() # Brief script-like behavior for development, debugging, and testing purposes: if __name__ == "__main__": from analyst import Analyst from analyst.evaluators import NucleusClusterizer s = TestSet2D(random=False) a = Analyst(embeddings=s, metric="euclidean", encoder=s.encode, decoder=s.decode, desc="2D Debug Test", evaluators=[ "Nodal 3-Hubs", "Nuclei", "Nodes", "Supernodes", "Extremities", "7-KMeans", "Spatial" ], make_distance_matrix=True, over_write=True) Analyst.save(a, "an_2d_debug_test")
def test_analyst(self): file_path = './test/sample.jpg' analyst = Analyst() analyst(file_path)
root_dir = "images/{0:%Y}/{0:%m}/{0:%d}".format(now) os.makedirs(root_dir, exist_ok=True) return root_dir def get_save_dir(ip, token): root_dir = get_root_dir() save_dir = os.path.join(root_dir, token) os.makedirs(save_dir, exist_ok=True) return save_dir analyst = Analyst() @post('/analyst/upload') def upload(): image = request.files.get('image') ip = request.environ.get('REMOTE_ADDR') token = get_token(ip) save_dir = get_save_dir(ip, token) image = Image.open(image.file) image_path = '{}/orig.jpg'.format(save_dir) image.save(image_path) ids = analyst(image_path)
if self.random: p.set_title('Random 2D Test Set') else: p.set_title('Fixed Cluster-like 2D Test Set') p.grid(True) p.set_aspect('equal', 'datalim') for i, tup in enumerate(self.data): p.annotate(str(i), tup) plt.show() # Brief script-like behavior for development, debugging, and testing purposes: if __name__ == "__main__": from analyst import Analyst t = TestSet2D() r = TestSet2D(random=True, seed=19680801) at = Analyst(t.data, "euclidean", t.encode, t.decode, desc="Contrived 2D Test Set") ar = Analyst(r.data, "euclidean", r.encode, r.decode, desc="Random 2D Test Set") at.compare_difference(ar) Analyst.save(at, "an_2d_contrived") Analyst.save(ar, "an_2d_random")
url + "Norge/S%C3%B8r-Tr%C3%B8ndelag/Orkdal/S%C3%B8vatnet/varsel.xml"), "Åre": WeatherMonitor("Åre", url + "Sverige/J%C3%A4mtland/%C3%85re/varsel.xml"), "Stranda": WeatherMonitor("Stranda", url + "Norge/Møre_og_Romsdal/Stranda/Stranda/varsel.xml"), "Overøye": WeatherMonitor("Overøye", url + "Norge/Møre_og_Romsdal/Stranda/Stranda/varsel.xml"), "Vassfjellet": WeatherMonitor( "Vassfjellet", url + "Norge/Sør-Trøndelag/Klæbu/Vassfjellet_skisenter/varsel.xml"), } analyst = Analyst(temperature_upper_limit=1.0, precipitation_lower_limit=5.0) limit = 3 while True: try: print "Fetching data, making rec. and sending data... (" + strftime( "%Y-%m-%d %H:%M", gmtime()) + ")" start_time = time.time() recommendations = [] for elem in monitors.keys(): data = monitors[elem].get_daily_data(limit) local_recommendations = analyst.get_recommendation(data, elem) recommendations.extend(local_recommendations)
def test_init(self): a = Analyst(10000) self.assertAnalyst(a, 10000, 0, 0)
def main(): app = QApplication(sys.argv) chat = Analyst() sys.exit(app.exec_())
import numpy as np from analyst import Analyst, Command, DataSource, DataWorker, Pointer from frameworks import framework frameworks = framework() pandas = frameworks.pandas identity = Analyst("Alice", port=65442, host="127.0.0.1") worker = DataWorker(port=65441, host="127.0.0.1") dataset = DataSource(identity, worker, "Sample Data").init_pointer() dataset["Number of times pregnant"].mean() dataset["Number of times pregnant"].mean() # dataset.fillna(0) # print(dataset[dataset.isnull()].sum().get()) """ print(dataset.columns) print(dataset["Number of times pregnant"].sum().get()) print( dataset["Plasma glucose concentration a 2 hours in an oral glucose tolerance test"] .sum() .get() ) dataset = dataset[ [ "Diastolic blood pressure (mm Hg)", "2-Hour serum insulin (mu U/ml)",
def test_get_portfolio(self): a = Analyst(1000) a.buyLong(10.2, 2) a.buyShort(22, 3) portfolio = a.getPortfolio(10.2, 22) self.assertPortfolio(portfolio, 0.9136, 10.2 * 2 / 1000, 22 * 3 / 1000)
def test_average_cost(self): a = Analyst(1000) a.buyLong(10.2,2) a.buyLong(20.66,5) a.buyLong(30.66, 17) avg = (30.66*17 + 20.66 * 5 + 10.2 * 2) / (2+5+17) self.assertAlmostEqual(a.longs.avgCost, avg) a.sellLong(30, 15) a.buyLong(40.1, 8) self.assertAlmostEqual(a.longs.avgCost, 33.09676470588235)
def test_buy_sell_long(self): a = Analyst(150) a.buyLong(50, 3) a.sellLong(55, 3) cash = 150 - (3 * 50) + (3 * 55) self.assertAnalyst(a, cash, 0, 0)