Exemple #1
0
def start():
    """Main method for start."""
    _prepare_logging()
    args = _parse_args()
    config = parse_config(args.config_path)
    starter = _create_starter(config)
    analyzer = starter.analyzer()
Exemple #2
0
def start():
    """Main method for start."""
    _prepare_logging()
    args = _parse_args()
    config = parse_config(args.config_path)
    starter = _create_starter(config)
    # Установка реального рублевого баланса
    # starter.api.changebalance(5955150)
    # Установка тестового долларового баланса
    starter.api.changebalance(12074331)
    collector = starter.start_collector()
    trader = create_trader(starter.api, starter.active)
    trader.start()
    print vars(starter.api.timesync)
    predictor = Predictor(trader, starter.redis, starter.db, starter.active)
    last_quotation = None
    # trader.trade(Signal("put", 10, 10, "turbo"))
    while True:
        starter.api.timesync.expiration_time = 1
        starter.api.buy(
            10,
            10,
            "turbo",
            "call")
        quotation = collector.put_quotations()
        if quotation:
            last_quotation = quotation
            predictor_thread = threading.Thread(target=predictor.get_candles(quotation, ), args=(), kwargs={})
            predictor_thread.start()

        if last_quotation:
            predictor.check_predictions(last_quotation)
        time.sleep(1)
Exemple #3
0
def start():
    """Main method for start."""
    _prepare_logging()
    args = _parse_args()
    config = parse_config(args.config_path)
    starter = _create_starter(config, args.setting_id)
    trader = create_trader(None, starter.settings, starter.db, True)
    predictor = Analyzer(trader, starter.redis, starter.db, starter.settings,
                         True)
    begin = int(args.from_ts)
    end = int(time.time())
    if args.to_ts:
        end = args.to_ts

    cursor = starter.db.cursor()
    cursor.execute(
        "SELECT * FROM quotations WHERE ts>=%s AND ts<=%s AND active_id=%s ORDER BY ts;",
        (begin, end, starter.settings.active["db_id"]))
    rows = cursor.fetchall()
    if len(rows) > 0:
        """Запускаем демона для проверки кеша и получения результата торгов"""
        check_thread = threading.Thread(target=checker_daemon,
                                        args=(predictor, ))
        check_thread.setDaemon(True)
        check_thread.start()

        i = 0
        thread_limit = 10
        threads_count = 0
        total_threads = []
        for row in rows:
            i += 1
            if i >= starter.settings.collector_working_interval_sec:
                # response_threads.append(pool.apply_async(run_analysis, (row, predictor)))
                """Проверка на количество работающих тредов и блокировка"""
                while threads_count >= thread_limit:
                    threads_count = len(get_alive_threads(total_threads))

                analysis_thread = threading.Thread(target=run_analysis,
                                                   args=(row, predictor))
                analysis_thread.setDaemon(True)
                analysis_thread.start()

                total_threads.append(analysis_thread)
                threads_count += 1
                # print "Run analysis thread. Total:", len(total_threads)
                i = 0

        for th in total_threads:
            th.join()

        print "All threads closed"
Exemple #4
0
def start():
    """Main method for start."""
    _prepare_logging()
    args = _parse_args()
    config = parse_config(args.config_path)

    check_prediction_last_time = None
    threshold_repeats = 10
    repeats_counter = 0
    first_iter = True
    while True:
        if first_iter or starter.api.websocket_client.is_closed:
            first_iter = False
            starter = _create_starter(config, args.setting_id)
            # Установка реального рублевого баланса
            # starter.api.changebalance(5955150)
            # Установка тестового долларового баланса
            starter.api.changebalance(12074331)
            collector = starter.start_collector()
            trader = create_trader(starter.api, starter.settings, starter.db)
            predictor = Analyzer(trader, starter.redis, starter.db, starter.settings)
            continue

        now = time.time()
        quotation = collector.put_quotations()
        if collector.last_quotation:
            """Запускаем тред на проверку прогноза"""
            check_thread = threading.Thread(target=predictor.check_predictions, args=(collector.last_quotation,))
            check_thread.daemon = True
            check_thread.start()

            if collector.last_quotation.time > check_prediction_last_time:
                check_prediction_last_time = quotation.time
                """Запускаем тред на запись котировки и ее свечей"""
                analysis_thread = threading.Thread(target=run_analysis, args=(quotation, collector, predictor))
                analysis_thread.daemon = True
                analysis_thread.start()
                repeats_counter = 0
            else:
                repeats_counter += 1
                if repeats_counter > threshold_repeats:
                    print "threshold_repeats"

        tsleep = float(starter.settings.collector_working_interval_sec) - (
            starter.settings.collector_working_interval_sec / 10.0)
        tend = time.time() - now
        if tend > 0:
            tsleep -= tend
        if tsleep > 0:
            time.sleep(tsleep)
Exemple #5
0
def start():
    """Main method for start."""
    _prepare_logging()
    args = _parse_args()
    history_duration = args.duration
    from_time = args.from_time
    chunk = 3000
    config = parse_config(args.config_path)
    starter = _create_starter(config)
    data_handler = DataHandler(starter.db, starter.active["db_id"])

    if not from_time:
        from_time = int(time.time())
        current_time = int(time.time())
    else:
        current_time = from_time

    old_data = None
    old_quotation_value = None
    while history_duration > 0:
        minus = chunk
        if history_duration - chunk < 0:
            minus = chunk + (history_duration - chunk)
        last_ts = current_time - history_duration
        first_ts = last_ts + chunk
        if first_ts > current_time:
            first_ts = current_time
        starter.api.getcandles(starter.active["platform_id"], 1, last_ts,
                               first_ts, chunk)

        quotations = []
        start_get_candles_time = int(time.time())
        while True:

            last_get_candles_time = int(time.time())
            last_get_time = last_get_candles_time - start_get_candles_time
            if int(last_get_time
                   ) > 3 and starter.api.websocket_client.is_closed:
                starter.create_connection()

            data = starter.api.candles.candles_data
            if (data and
                (old_data and
                 (old_data[0][0] != data[0][0]))) or (data and not old_data):
                old_data = data
                for quotation in data:
                    value = float(
                        (quotation[1] + quotation[2]) / 2) / 1000000.0
                    if not old_quotation_value or old_quotation_value != value:
                        old_quotation_value = value
                        quotations.append(
                            (quotation[0], starter.active["db_id"], 0, 0,
                             value))
                break

        history_duration -= minus
        from_time -= minus
        if len(quotations) > 0:
            print "Time:", int(time.time()), "From:", int(
                quotations[0][0]), "Inserted:", len(
                    quotations), "Remaining:", history_duration
            thread = threading.Thread(target=make_quotations_and_candles,
                                      args=(data_handler, quotations, config))
            thread.daemon = True
            thread.start()