Beispiel #1
0
def strategy_combination_a(start_time, end_time, init_balance):
    # 写入strategy_log表
    new_log = Log(strategy_id=1, start_date=start_time, end_date=end_time, init_balance=init_balance,
                  coin_category='btc', creator=1)
    log_id = insert_2_strategy_log(new_log)
    position = Position(start_date=start_time, end_date=end_time, balance=init_balance, strategy_type=1)
    dates = position.datas['id']
    dates = dates[dates >= start_time]
    print(dates.size)
    sell_a = SellA(start_time=start_time, end_time=end_time, position=position)
    buy_a = BuyA(start_time=start_time, end_time=end_time, position=position)
    for t in dates:
        status = 0
        sell_flag = sell_a.strategy(t, position)
        position = sell_a.position
        if sell_flag.flag is not 0:
            status = 1
            # todo signal 策略中的信号放到一个对象返回到这里
        account_id_sell = account_insert(position=position, t=t, strategy_log_id=log_id, signal=int(sell_flag.signal),
                                         transaction_status=status)
        buy_flag = buy_a.strategy(t, position)
        position = buy_a.position
        if buy_flag is not 0:
            status = 2
            # todo signal 策略中的信号放到一个对象返回到这里
        account_id_buy = account_insert(position=position, t=t, strategy_log_id=log_id, signal=int(buy_flag.signal),
                                        transaction_status=status)

    #  回写策略执行后总资产到strategy_log表
    write_back2log(position, log_id)
Beispiel #2
0
def place_enemies_in_battlefield(battlefield, enemies):
    for i in range(len(enemies)):
        battlefield.add_new_character(enemies[i], Position(y=1, x=1 + i))
Beispiel #3
0
def place_characters_in_battlefield(battlefield, characters):
    for i in range(len(characters)):
        battlefield.add_new_character(characters[i], Position(y=3, x=1 + i))
Beispiel #4
0
def strategy_poc(strategy_id, start_time, end_time, init_balance, create_time, creator):
    print('strategy_id' + str(strategy_id))
    print('creator' + str(creator))
    new_log = Log(strategy_id=strategy_id, start_date=start_time, end_date=end_time, create_time=create_time,
                  init_balance=init_balance,
                  coin_category='btc', creator=creator)
    log_id = insert_2_strategy_log(new_log)
    balance = init_balance
    data = read_datas_1day_test(start_time - 777600, end_time + 86400)

    if data.empty:
        print('load data error!')
        return False
    df = get_strategy_conf_list(strategy_id)
    if df.empty:
        return False

    pos = Position(start_date=start_time, end_date=end_time, balance=init_balance, strategy_type=0)
    pos.datas = data
    df_sell = df[df['5'] == 2]
    df_buy = df[df['5'] == 1]
    # 转换构造sell和buy的所有条件
    sell_dict = create_conditions_dictionary(df_sell)
    buy_dict = create_conditions_dictionary(df_buy)
    position = 0.000000
    # todo 初始化持仓

    end_time_close = data[data['id'] == end_time].iat[0, 2]
    id_list = data[data['id'] >= start_time]
    id_list = id_list[id_list['id'] <= end_time]['id']
    strategy_profit = 0.00
    benchmark_profit = 0.00
    for t in id_list:

        data_t = data[data['id'] == t]
        next_data_t = data[data['id'] == t + 86400]
        close_t = data_t.iat[0, 2]
        next_close_t = next_data_t.iat[0, 2]
        # 返回价钱和数量signal
        signal = sell_signal(t, sell_dict, data)
        # 插入account表
        pos.current_position = position
        # print(str(position))
        pos.balance = balance

        account_id = account_insert(position=pos, t=t, strategy_log_id=log_id,
                                    signal=int(signal.signal),
                                    transaction_status=signal.signal)
        # 最后一次t卖出
        if t == end_time:
            signal = Poc_signal(signal=2, price=Decimal(end_time_close).quantize(Decimal('0.000000')))

        if signal.signal == 2 and position != 0:
            # 卖出并返回余额
            amount = position
            pre_balance = balance
            balance = Decimal(str(balance)).quantize(Decimal('0.00'))
            balance += Decimal(str(position)) * Decimal(str(close_t))
            position = 0
            balance = Decimal(str(balance)).quantize(Decimal('0.00'))

            # position对象更新
            pos.balance = balance
            pos.current_position = position
            # todo 初始化运算器
            calculator = Calculator.Calculator(pos, t, signal=0, strategy_id=strategy_id,
                                               strategy_account_id=account_id)
            calculator.transaction(flag=2, cost=close_t, amount=amount, pre_current_position=amount,
                                   pre_balance=pre_balance)

        # print('********sell*********')
        # print('balance: ' + str(balance))
        # print('position: ' + str(position))
        # print('current profit: ' + str(
        #     ((balance + Decimal(str(position)) * Decimal(str(close_t))) - Decimal(str(init_balance))) / Decimal(str(
        #         init_balance))))
        # print('\n')

        signal = buy_signal(t, buy_dict, data)
        # 最后一t不买
        if t == end_time:
            signal.signal = 0
        if signal.signal == 1 and balance != 0:
            # 买入并返回余额,买入数量
            amount = (Decimal(str(balance)) / Decimal(str(close_t)))
            amount *= 100000000
            amount = floor(amount) / 100000000
            # .quantize(Decimal('0.00000000'))
            position = Decimal(str(amount))
            pre_balance = balance
            balance = Decimal(str(balance)).quantize(Decimal('0.00'))
            balance -= Decimal(str(amount)) * Decimal(str(close_t))
            balance = Decimal(str(balance)).quantize(Decimal('0.00'))
            # balance = 0

            # position对象更新
            pos.balance = balance
            pos.current_position = position
            # todo 初始化运算器
            calculator = Calculator.Calculator(pos, t, signal=0, strategy_id=strategy_id,
                                               strategy_account_id=account_id)
            calculator.transaction(flag=1, cost=close_t, amount=amount, pre_current_position=0,
                                   pre_balance=pre_balance)

        # print('********buy********')
        # print('balance: ' + str(balance))
        # print('position: ' + str(position))
        # print('current profit: ' + str(((balance + position * Decimal(close_t)) - Decimal(init_balance)) / Decimal(
        #     init_balance)))
        # print('\n')
        cal_balance = Decimal(str(balance)).quantize(Decimal('0.00'))
        cal_balance += Decimal(position) * Decimal(close_t)
        # 策略收益率计算
        strategy_profit = (cal_balance - Decimal(init_balance)) / Decimal(init_balance)
        # print('strategy_profit='+str(strategy_profit))
        # 基准收益率
        df_start = data[data['id'] == start_time]
        df_end = data[data['id'] == end_time]
        close_start_t = Decimal(df_start.iat[0, 2])
        benchmark_profit = (Decimal(str(close_t)) - close_start_t) / close_start_t
        # print('benchmark_profit=' + str(benchmark_profit))
        # pos.rate_of_return = strategy_profit

        account_update_total_margin(strategy_profit, account_id)

    # 回写策略执行后 收益率 到strategy_log表
    write_back2log(margin=strategy_profit, benchmark=benchmark_profit, log_id=log_id)
    # 计算并回写 最大回撤率 到strategy_log表
    write_max_drawdown_back2log(log_id=log_id)

    return Poc_response(strategy_profit=Decimal(str(strategy_profit)).quantize(Decimal('0.0000')),
                        benchmark_profit=Decimal(str(benchmark_profit)).quantize(Decimal('0.0000')),
                        log_id=log_id)
Beispiel #5
0
    def update_MA(self, T):
        start_timestamp_5T = T - 5 * 86400
        start_timestamp_10T = T - 10 * 86400
        pre_T = T - 86400
        df_5 = self.datas[(self.idt >= start_timestamp_5T)]
        df1_5 = df_5[df_5['id'] < pre_T]
        df_10 = self.datas[(self.idt >= start_timestamp_10T)]
        df1_10 = df_10[df_10['id'] < pre_T]

        self.MA5 = df1_5['close'].mean()
        self.MA10 = df1_10['close'].mean()

    # get MA5(T-1) MA10(T-1)
    def update_pre_MA(self, T):
        start_timestamp_5T = T - 6 * 86400
        start_timestamp_10T = T - 11 * 86400
        pre_T = T - 2 * 86400
        df_5 = self.datas[(self.idt >= start_timestamp_5T)]
        df1_5 = df_5[df_5['id'] < pre_T]
        df_10 = self.datas[(self.idt >= start_timestamp_10T)]
        df1_10 = df_10[df_10['id'] < pre_T]

        self.pre_MA5 = df1_5['close'].mean()
        self.pre_MA10 = df1_10['close'].mean()


if __name__ == '__main__':
    pos = Position(1509984000, 1515600000, 200, 1)
    buy_a = BuyA(1509984000, 1515600000, pos)
    buy_a.update_MA(1514995200)