File size: 2,124 Bytes
1e1b538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64


#  data = data[['market', 'time', 'open', 'high', 'low', 'close', 'volume']]
# transform this dataset so to 'market', 'start', 'column', 'value1', 'value2', 'value3', 'value(n)'
import pandas as pd
import numpy as np
from tqdm import tqdm

columns = ['open','close','volume', 'rsi', 'sma']
window_size = 10

def create_sequences(df, columns=columns, window_size=window_size):
    # group by market
    grouped = df.groupby('market')

    # create a list of dataframes
    dfs = []

    for name, group in tqdm(grouped):
        # create a new dataframe
        new_df = pd.DataFrame()
        new_df['market'] = name
        # create a list of lists
        sequences = []
        # only include the close column
        # iterate over the rows of the dataframe
        for i in range(len(group) - window_size):
            # create a sequence
            sequence = group.iloc[i:i+window_size][columns].values
            # transpose the sequence so that it is a column
            sequence = sequence.T

            # create a dataframe from the sequence
            sequence = pd.DataFrame(sequence)

            # add the market, time, column_name to the sequence
            sequence['market'] = name
            sequence['time'] = group.iloc[i+window_size]['time']
            sequence['column'] = columns

            # set market, time as the first columns and index
            sequence = sequence.set_index(['market', 'time', 'column'])

            # add the sequence to the list of sequences
            sequences.append(sequence)
        if len(sequences) == 0:
            continue
        # create a dataframe from the list of lists
        new_df = pd.concat(sequences)
        # add the dataframe to the list of dataframes
        dfs.append(new_df)
    
    # concatenate the list of dataframes
    final_df = pd.concat(dfs)

    return final_df

df = pd.read_csv('indicators.csv')

# create the sequences
sequences = create_sequences(df, columns=columns, window_size=15)

# save the sequences to a new file
sequences.to_csv('sequences.csv')