In this post, we are going to continue to extract useful features for algoritmic trading. I really recommend you to read the previous posts. The links are;

In the previous post, we extracted only two features using Bollinger Bands, which were ‘percent b’ and ‘bandwidth’. Another kind of indicators that financial traders use is the candlestick patterns. When the closing price value of a financial instrument is larger than the opening price, then it is called ‘Bullish’ candle stick which is shown as green. In the opposite case (opening price is larger than the closing price), it is called ‘Bearish’ one which is shown as red. The lines above and below the body are called the tails or shadow of the sticks which are determined by the ‘high’ and ‘low’ price values. It is believed that the shape of the candlestick patterns tells about the particular market movements. As we stated in the previous posts, we can not directly use these type of features since they are not standardized. One way to standardize these features is to normalize them by using the maximum and minimum price values within a particular period. Let us implement this in python. Last time, we had already obtained the typical price (tp) value in our data frame (df). First, we need to obtain min and max price values in a given period (N). Afterthat we fill the ‘NA’ values with an interpolation method.

```
df['min'] = df['tp'].rolling(N).min() #N is our period
df['max'] = df['tp'].rolling(N).max()
df['min'] = df['min'].interpolate(method='linear').bfill()
df['max'] = df['max'].interpolate(method='linear').bfill()
```

Now, we can calculate the four features that represent a candlestick.

```
df['candle-o'] = df[['open','min','max']].apply(lambda x: (x[0]-x[1])/(x[2]-x[1]),axis=1)
df['candle-c'] = df[['close','min','max']].apply(lambda x: (x[0]-x[1])/(x[2]-x[1]),axis=1)
df['candle-h'] = df[['high','min','max']].apply(lambda x: (x[0]-x[1])/(x[2]-x[1]),axis=1)
df['candle-l'] = df[['low','min','max']].apply(lambda x: (x[0]-x[1])/(x[2]-x[1]),axis=1)
```

In this post, we will also show how to obtain ‘rate of change’ (ROC) features which are in the group of momentum oscillators. One can calculate the ROC using the formula below:

`ROC-N = (typical close price - typical close price N periods ago)/typical close price N periods ago`

Let us say we want to obtain 20 ROC features. The following lines show how to do this in python.

```
period_list = range(1,21)
for i in period_list:
df['diff-'+str(i)] = df[['tp']].diff(periods=i)
df['tp-'+str(i)] = df[['tp']].shift(i)
df['roc-'+str(i)] = df[['diff-'+str(i),'tp-'+str(i)]].apply(lambda x: x[0]/x[1],axis=1 )
df['roc-'+str(i)] = df['roc-'+str(i)].interpolate(method='linear').bfill()
```

Finally, we store all of our features into a new csv file.

```
feature_list = ['percent-b','bandwidth','candle-o','candle-c','candle-h','candle-l']
for i in period_list:
feature_list = feature_list + ['roc-'+str(i)]
df[feature_list].to_csv('ltcusdt-1hour-out.csv')
```

Enjoy your trading!