brighter-trading/markdown/database.md

5.6 KiB

make_query(item, table, columns)

    @startuml
    start
        :format string "SELECT item FROM table WHERE column = ?";
        repeat
        if (more columns?) then (yes) 
            :add to string "AND column = ?";
        else (no)
        endif
        repeat while (more columns?) is (yes)
        ->no;
        :Return query string;
    stop
    @enduml

make_insert(table, values)

    @startuml
    start
        :format string "INSERT INTO {table} (key";
        repeat
        if (another key?) then (yes)
            :append string with key;
        endif
        repeat while (another key?)
        :append string  "VALUES(;
        repeat
        : append string ?,;
        repeat while (another key)
        :finalise string );
        :return string;
        
    stop
    @enduml

get_item_where(item_name, table_name, filter_vals)

    @startuml
    start
    :query database;
    if (result) then (yes)
    :return result;
    stop
    else (no)
    :raise error;
    stop
    @enduml

get_rows_where(table, filter_vals)

    @startuml
    start
    :Connect to database;
    :Assemble sql select statement;
    :execute query;
    if (Recieved result?) then (yes)
        :Return: result;
        stop
    else (no)
        :Return: None;
    stop

    @enduml

insert_row(table, columns, values)

    @startuml
        start
        :Connect to database;
        :Assemble sql insert statement;
        :execute sql;
        :return;
        stop
    @enduml

_table_exists(table_name)

    @startuml
        start
        :Connect to database;
        :Assemble sql insert statement;
        :execute sql;
        if (result?) then (yes)
            :return true;
            stop
        else (no)
            :return false;
            stop
    @enduml

_populate_table(table_name, start_time, ex_details, end_time)

    @startuml
    start
    if (end_time?) then (no)
        :Set the default end_time to UTC now;
    endif
    :fetch_candles_from_exchange;
    if (results empty?) then (yes)
        :Insert candles into database;
    else (no)
        :Log error;
    endif
    :Return: results|empty results ;
    @enduml

get_from_static_table(item, table, indexes, create_id = False)

    @startuml
    start
    :Connect to database;
    :Assemble sql request for data;
    :Execute query;
    :Assign result;
    if (result is None && create_id = True) then (yes)
        note left
            Create_id allows for a "create if 
            field doesnt exist" condition.
        end note
        :Create a row;
        :Retrieve the auto-incremented id from db;
        :Assign result;
    endif
    if (result?) then (yes)
        :return result;
        stop
    else (no)
        :Return: None;
        stop
    @enduml

_fetch_exchange_id(exchange_name)

    @startuml
    start
        :Fetch exchange id;
        :Return: id;
    stop
    @enduml

_fetch_name_id(exchange_name)

    @startuml
    start
        :Fetch exchange id;
        :Return: id;
    stop
    @enduml

_insert_candles_into_db(candlesticks, table_name, symbol, exchange_name)

@startuml
    start
    :Retrieve the market id for the symbol;
    :Insert the market id into the dataframe;
    :Get a list of all the columns in the dataframe;
    :Assemble sql for table creation;
    :Connect to the database.;
    :Create the table if it doesn't exist;
    :insert the candles;
    :Return: None;
    stop
@enduml

get_records_since(table_name, st, et, rl, ex_details)

@startuml
    start
    if (table exists?) then (yes)
        :Request records from st to et from db;
    else (no)
        :Request records from st to et from exchange;
    endif
    if (query satisfied?) then (no)
        :request missing historical records;
    endif
    if (records up-to-date?) then (no)
        :get new records;
    endif
    :Return results;
    stop
@enduml

_get_records(table_name, st, et)

@startuml
    start
    :Connect to db;
    :Assemble sql;
    :execute sql;
    :drop the database id from records;
    :return records;
    stop
@enduml

_fetch_candles_from_exchange(symbol, interval, exchange_name, user_name, start_datetime, end_datetime)

    @startuml
        start
        
        if (start date?) then (no)
            :Set default start date;
        endif
        
        if (end date?) then (no)
            :Set end date to now;
        endif
        :Validate input;
        if (input valid?) then (no)
            :Raise Error;
            end
        endif
        :Get a reference to the exchange;
        :get historical data from exchange;
        :Isolate the open_times from the records received;
        :Calculate the number of records that would fit;
        if (records missing?) then (yes)
            :fill data holes;
        endif
        :Return: data;
        stop
    @enduml

fill_data_holes(records)

@startuml
    start
    :Receives a number of records;
    :Convert the timespan of each record to minutes;
    repeat
    repeat
        :Get one record;
        backward :Record last record.;
        repeat while (last record?)
        :Get the difference between the timestamps of this and last iteration;
        if (diff > timespan) then (yes)
            :calculate how many records should exits;
            :Insert copies of the last record in place of missing records;
        endif
        :record last record;
    repeat while (More records?)
    :Return: Records;
    stop
@enduml