def update_other_trades(transaction_id, user_id, selected_level, parent_trade):
    cursor = connection.cursor()
    level_based_by_user_id = f"SELECT * FROM level_based_by_user_id WHERE transaction_id = {transaction_id}"
    existing_trades = cursor.execute(level_based_by_user_id)

    current_user_trade = f"SELECT * FROM user_transactions WHERE transaction_id = {transaction_id} AND user_id = {user_id}"
    current_user_trade = cursor.execute(current_user_trade)
    current_user_trade = current_user_trade[0]

    for trade in existing_trades:
        if trade['user_id'] != user_id:
            update = f"UPDATE user_transactions SET available_levels = {current_user_trade['available_levels']}" \
                     f",level_owners = '{current_user_trade['level_owners']}' " \
                     f"WHERE transaction_id = {transaction_id} and user_id = {trade['user_id']}"
            cursor.execute(update)
    #   update transactions_levels_status available_levels
    end_time = Helper.get_time_formatted(current_user_trade['end_time'])
    update_transactions_levels_status = f"UPDATE transactions_levels_status SET available_levels = " \
                                        f"{current_user_trade['available_levels']} " \
                                        f"WHERE status = '{parent_trade['status']}' " \
                                        f"AND currency = '{parent_trade['currency']}'" \
                                        f"AND end_time = '{end_time}' " \
                                        f"AND purchase_type = '{parent_trade['purchase_type']}' " \
                                        f"AND amount = {parent_trade['amount']} " \
                                        f"AND transaction_id = {parent_trade['transaction_id']} " \
                                        f"AND user_id = {parent_trade['user_id']}"

    cursor.execute(update_transactions_levels_status)
def persist_transactions_by_end_time(transaction_id, user_id, parent_trade):
    end_time = Helper.get_time_formatted(parent_trade['end_time'])
    fields = "(transaction_id, user_id,status,trade_type,end_time)"
    values = f"({transaction_id},{user_id},'{parent_trade['status']}','{parent_trade['trade_type']}','{end_time}')"

    transactions_by_end_time = f"INSERT INTO transactions_by_end_time {fields} VALUES {values}"
    cursor = connection.cursor()
    cursor.execute(transactions_by_end_time)
def persist_transactions_changes_allowed_time(transaction_id, user_id,
                                              parent_trade):
    changes_allowed_time = Helper.get_time_formatted(
        parent_trade['changes_allowed_time'])

    fields = "(transaction_id,user_id,status,changes_allowed_time)"
    values = f"({transaction_id},{user_id},'{parent_trade['status']}','{changes_allowed_time}')"

    transactions_changes_allowed_time = f"INSERT INTO transactions_changes_allowed_time {fields} VALUES {values}"
    cursor = connection.cursor()
    cursor.execute(transactions_changes_allowed_time)
def persist_user_transactions(user_id, transaction_id, parent_trade,
                              selected_level):
    current_user = Helper.get_user_by_id(user_id)

    converted_amount = Helper.convert_currency(parent_trade['amount'],
                                               parent_trade['amount_currency'],
                                               current_user['currency'])

    price_range = get_selected_level_price(parent_trade['levels_price'],
                                           selected_level)

    available_levels = get_available_levels(parent_trade['available_levels'],
                                            selected_level)

    level_owners = add_level_owners(parent_trade['level_owners'], user_id,
                                    selected_level)
    start_time = Helper.get_time_formatted(parent_trade['start_time'])
    end_time = Helper.get_time_formatted(parent_trade['end_time'])
    changes_allowed_time = Helper.get_time_formatted(
        parent_trade['changes_allowed_time'])

    fields = "(transaction_id,user_id,created_date,trade_type,purchase_type,currency,staring_price," \
             "amount,amount_currency,start_time,end_time,changes_allowed_time,outcome,status,level_pips,levels_price,level_owners," \
             "join_date,level_start_price,level_end_price,level_selected,created_by,parent_id,child,available_levels)"

    values = f"({transaction_id},{user_id},'{created_date}','{parent_trade['trade_type']}'," \
             f"'{parent_trade['purchase_type']}','{parent_trade['currency']}',{parent_trade['staring_price']}," \
             f"{converted_amount},'{current_user['currency']}','{start_time}','{end_time}'," \
             f"'{changes_allowed_time}','{parent_trade['outcome']}','{parent_trade['status']}'," \
             f"{parent_trade['level_pips']},'{parent_trade['levels_price']}'," \
             f"'{level_owners}'," \
             f"'{Helper.get_current_time_formatted()}',{price_range['range'][0]},{price_range['range'][1]}," \
             f"{selected_level},{parent_trade['created_by']},{parent_trade['user_id']},{True}, {available_levels})"

    user_transactions = f"INSERT INTO user_transactions {fields} VALUES {values}"
    cursor = connection.cursor()
    cursor.execute(user_transactions)
    Trading.save_levels_general_stats(user_id, selected_level,
                                      converted_amount,
                                      parent_trade['purchase_type'])
Пример #5
0
def get_history(currency, timeframe, price_type, chart_type):
    start_date = ""
    end_date = ""
    if timeframe == 'ticks':
        start_date = Helper.get_time_formatted(datetime.now() -
                                               timedelta(hours=3))
        end_date = Helper.get_time_formatted(datetime.now())
        table = "forex_pip_data"

    if timeframe == 'minute':
        start_date = Helper.get_time_formatted(datetime.now() -
                                               timedelta(days=3))
        end_date = Helper.get_time_formatted(datetime.now())
        table = "forex_data"

    if timeframe == 'five_min':
        start_date = Helper.get_time_formatted(datetime.now() -
                                               timedelta(days=3))
        end_date = Helper.get_time_formatted(datetime.now())
        table = "forex_data_five_min"

    if timeframe == 'fifteen_min':
        start_date = Helper.get_time_formatted(datetime.now() -
                                               timedelta(days=4))
        end_date = Helper.get_time_formatted(datetime.now())
        table = "forex_data_fifteen_min"

    if timeframe == 'thirty_min':
        start_date = Helper.get_time_formatted(datetime.now() -
                                               timedelta(days=10))
        end_date = Helper.get_time_formatted(datetime.now())
        table = "forex_data_thirty_min"

    if timeframe == 'hour':
        start_date = Helper.get_time_formatted(datetime.now() -
                                               timedelta(days=15))
        end_date = Helper.get_time_formatted(datetime.now())
        table = "forex_data_one_hour"

    if timeframe == 'four_hours':
        start_date = Helper.get_time_formatted(datetime.now() -
                                               timedelta(days=15))
        end_date = Helper.get_time_formatted(datetime.now())
        table = "forex_data_four_hours"

    if timeframe == 'one_day':
        start_date = Helper.get_time_formatted(datetime.now() -
                                               timedelta(days=100))
        end_date = Helper.get_time_formatted(datetime.now())
        table = "forex_data_one_day"

    cursor = connection.cursor()
    result = cursor.execute(
        f"SELECT * FROM {table} WHERE currency_pair = '{currency}' "
        f"and timestamp > '{start_date}' AND timestamp < '{end_date}'")

    if not result:
        return JsonResponse(
            Helper.get_json_response(False, {}, ["Data Not available"]))

    df = pd.DataFrame(result)
    df = df.sort_values(by='timestamp', ascending=True)
    timestamp = df['timestamp'].astype(str).to_json(orient='records')
    data = dict()
    if chart_type == "line":
        if price_type == "close":
            close = df['close'].to_json(orient='records')

            data["timestamp"] = timestamp
            data["close"] = close

        if price_type == "open":
            open_price = df['open'].to_json(orient='records')

            data["timestamp"] = timestamp
            data["open"] = open_price

        if price_type == "high":
            high = df['high'].to_json(orient='records')

            data["timestamp"] = timestamp
            data["high"] = high

        if price_type == "low":
            low = df['low'].to_json(orient='records')
            data["timestamp"] = timestamp
            data["low"] = low

    if chart_type == "candlestick":
        close = df['close'].to_json(orient='records')
        open_price = df['open'].to_json(orient='records')
        high = df['high'].to_json(orient='records')
        low = df['low'].to_json(orient='records')

        data["timestamp"] = timestamp
        data["close"] = close
        data["open"] = open_price
        data["high"] = high
        data["low"] = low

    return JsonResponse(Helper.get_json_response(True, data, []))