Ejemplo n.º 1
0
class SimDetect(Detect):
    """
        - net_desc: description of the network
        - norm_desc: description of the normal case
        - ano_list: list of anomalies.
    """
    def __init__(self, argv):
        Detect.__init__(self, argv)
        self.sim = Sim(argv)

        self.output_flow_file = self.ROOT + \
                '/Simulator/n%i_flow.txt'%(self.args.detect_node_id)
        self.args.data = self.output_flow_file

        self.export_abnormal_flow_file = self.ROOT + \
                '/Simulator/abnormal_n%i_flow.txt'%(self.args.detect_node_id)

    def init_parser(self, parser):
        Detect.init_parser(self, parser)
        parser.add_argument('--detect_node_id', default=0, type=int,
                help = """ specify the node id you want to monitor,
                default value is 0""")
        parser.add_argument('--no_sim', default=False, action='store_true',
                help = """turn on this switch to disable the fs simulaiton""")

    def run(self):
        if not self.args.no_sim:
            self.sim.run()
        Detect.run(self)
        return self.detector
Ejemplo n.º 2
0
def main():

    first_seen = []
    random = []
    sim_time = 100

    # N, P, R

    for i in range(sim_time):

        sr = Sim(100, 1, 100, "r")
        sf = Sim(100, 1, 100, "f")

        sr_res = sr.run()
        sf_res = sf.run()

        # print("random: ", sr_res)
        # print("first_seen: ", sf_res)
        first_seen.append(sf_res)
        random.append(sr_res)

        print("round: ", i)
        # print("first seen: ", sum(first_seen) / len(first_seen))
        # print(first_seen)
        # print("random: ", sum(random) / len(random))
        # print(random)

    print(first_seen)
    print(random)

    plt.scatter(range(sim_time), first_seen, color='red')
    plt.hlines(sum(first_seen) / len(first_seen), 0, sim_time, colors='red')
    plt.scatter(range(sim_time), random, color='blue')
    plt.hlines(sum(random) / len(random), 0, sim_time, colors='blue')
    plt.show()
Ejemplo n.º 3
0
class SimDetect(Detect):
    """
        - net_desc: description of the network
        - norm_desc: description of the normal case
        - ano_list: list of anomalies.
    """
    def __init__(self, argv):
        Detect.__init__(self, argv)
        self.sim = Sim(argv)

        self.output_flow_file = self.ROOT + \
                '/Simulator/n%i_flow.txt'%(self.args.detect_node_id)
        self.args.data = self.output_flow_file

        self.export_abnormal_flow_file = self.ROOT + \
                '/Simulator/abnormal_n%i_flow.txt'%(self.args.detect_node_id)

    def init_parser(self, parser):
        Detect.init_parser(self, parser)
        parser.add_argument('--detect_node_id',
                            default=0,
                            type=int,
                            help=""" specify the node id you want to monitor,
                default value is 0""")
        parser.add_argument(
            '--no_sim',
            default=False,
            action='store_true',
            help="""turn on this switch to disable the fs simulaiton""")

    def run(self):
        if not self.args.no_sim:
            self.sim.run()
        Detect.run(self)
        return self.detector
Ejemplo n.º 4
0
def train_evaluate(search_params):

    hyperparameters = {}
    pick_kwargs = {}
    for k in list(search_params.keys()):
        if k in ['w_dfh', 'w_sharpe', 'w_100d', 'v_100d', 'v_dfh', 'v_rfl']:
            pick_kwargs[k] = search_params[k]
        else:
            hyperparameters[k] = search_params[k]

    hyperparameters['pick_kwargs'] = pick_kwargs
    print('------------')
    print(json.dumps(hyperparameters, indent=2, sort_keys=True))

    sim = Sim(neptune=neptune,
              period='2y',
              timedelay=100,
              window=100,
              timestep=1,
              budget=5000,
              stockPicks=5,
              avoidDowntrends=True,
              sellAllOnCrash=False,
              **hyperparameters)
    stats = sim.run()

    analysis = Analysis(neptune=neptune,
                        stats=stats,
                        positions=sim.portfolio.holdings,
                        prices=sim.downloader.prices)
    #analysis.chart()
    output, advanced_stats, obj_stats = analysis.positionStats()

    for k in list(obj_stats.keys()):
        neptune.log_metric(k, obj_stats[k])

    print(output)

    #neptune.log_artifact('data/output_1y.pkl')
    sharpe = analysis.sharpe()
    stats = sim.portfolio.summary()

    if math.isnan(sharpe) or math.isinf(sharpe) or sharpe <= -2 or sharpe >= 5:
        sharpe = -5

    #neptune.log_metric('sharpe', sharpe)
    #neptune.log_metric('start_value', 5000)
    #neptune.log_metric('end_value', stats['total_value'])

    report = {
        'hyperparameters': hyperparameters,
        'sharpe': sharpe,
        'end_value': stats['total_value'],
        'gains': (stats['total_value'] - 5000.0) / 5000.0
    }

    neptune.log_text('report', json.dumps(report, indent=2, sort_keys=True))

    return sharpe
Ejemplo n.º 5
0
def Main():
    if len(sys.argv) <= 1:
        print("Argument required: create, watch, buy, sell, stats")
    else:
        if sys.argv[1] == 'create':
            if len(sys.argv) < 3:
                print("Invalid arguments. `python main.py create 5000`")
            else:
                watcher = Watcher(filename='data/portfolio',
                                  hyperparameters=hyperparameters)
                watcher.create(balance=float(sys.argv[2]))

        elif sys.argv[1] == 'watch':
            watcher = Watcher(filename='data/portfolio',
                              hyperparameters=hyperparameters)
            watcher.start()

        elif sys.argv[1] == 'buy':
            # main.py buy 20 AMD 81.26
            if len(sys.argv) < 5:
                print("Invalid arguments. `python main.py buy 20 AMD 81.26`")
            else:
                executor = Executor(filename='data/portfolio',
                                    hyperparameters=hyperparameters)
                print(
                    executor.buy(symbol=sys.argv[3],
                                 count=int(sys.argv[2]),
                                 value=float(sys.argv[4])))

        elif sys.argv[1] == 'sell':
            # main.py sell 20 AMD 81.26
            if len(sys.argv) < 5:
                print("Invalid arguments. `python main.py sell 20 AMD 81.26`")
            else:
                executor = Executor(filename='data/portfolio',
                                    hyperparameters=hyperparameters)
                print(
                    executor.sell(symbol=sys.argv[3],
                                  count=int(sys.argv[2]),
                                  value=float(sys.argv[4])))

        elif sys.argv[1] == 'stats':
            watcher = Watcher(filename='data/portfolio',
                              hyperparameters=hyperparameters)
            watcher.stats()

        elif sys.argv[1] == 'sim':
            from Sim import Sim
            from Analysis import Analysis

            sim = Sim(period='3y',
                      timedelay=100,
                      window=100,
                      timestep=1,
                      budget=5000,
                      stockPicks=5,
                      avoidDowntrends=True,
                      sellAllOnCrash=False,
                      **hyperparameters)
            stats = sim.run()

            analysis = Analysis(stats=stats,
                                positions=sim.portfolio.holdings,
                                prices=sim.downloader.prices)
            analysis.chart('data/best_optimized_3y.png')
            output, advanced_stats = analysis.positionStats()
            print(output)

            g = sim.portfolio.holdings.copy().groupby('label').sum()
            g['profits_pct'] = (g['current_price'] - g['purchase_price']
                                ) / g['purchase_price'] * 100
            print(g)

        elif sys.argv[1] == 'check':
            watcher = Watcher(filename='data/portfolio',
                              hyperparameters=hyperparameters)
            watcher.check(sys.argv[2])

        else:
            print("Unknown command")
Ejemplo n.º 6
0
pd.set_option('display.expand_frame_repr', False)
pd.set_option('max_colwidth', None)

from Sim import Sim
from Analysis import Analysis

sim = Sim(period='1y',
          timedelay=100,
          window=100,
          timestep=1,
          budget=10000,
          stockPicks=5,
          sl=-0.04,
          tp=3.0,
          ts=0.05,
          ts_threshold=0.05,
          avoidDowntrends=True,
          sellAllOnCrash=False)
stats = sim.run()

analysis = Analysis(stats=stats,
                    positions=sim.portfolio.holdings,
                    prices=sim.downloader.prices)
analysis.chart('data/output_1y.png')
output, advanced_stats = analysis.positionStats()
print(output)

g = sim.portfolio.holdings.copy().groupby('label').sum()
g['profits_pct'] = (g['current_price'] -
                    g['purchase_price']) / g['purchase_price'] * 100
print(g)