forex repeat.
CHAPTER 5
Let’s leave the deep learning models for a while and try some simply statistics to create our strategy.
Is there any time during the week that the next candle will be most likely bullish or bearish? Let’s find out
Import Libraries
import matplotlib
import numpy as np
import pandas as pd
import time
import datetime
from matplotlib import pyplot as plt
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
from mpl_finance import candlestick_ohlc
import seaborn as sns
print('Numpy version: ' + np.__version__)
print('Pandas version: ' + pd.__version__)
print('Matplotlib version: ' + matplotlib.__version__)
Numpy version: 1.16.4
Pandas version: 0.24.2
Matplotlib version: 3.1.0
float_data = pd.read_csv('Hour/EURUSD.csv')
print(len(float_data))
101160
float_data.head(10)
Gmt time | Open | High | Low | Close | Volume | |
---|---|---|---|---|---|---|
0 | 04.05.2003 21:00:00.000 | 1.12284 | 1.12338 | 1.12242 | 1.12305 | 29059.0996 |
1 | 04.05.2003 22:00:00.000 | 1.12274 | 1.12302 | 1.12226 | 1.12241 | 26091.8008 |
2 | 04.05.2003 23:00:00.000 | 1.12235 | 1.12235 | 1.12160 | 1.12169 | 29240.9004 |
3 | 05.05.2003 00:00:00.000 | 1.12161 | 1.12314 | 1.12154 | 1.12258 | 29914.8008 |
4 | 05.05.2003 01:00:00.000 | 1.12232 | 1.12262 | 1.12099 | 1.12140 | 28370.6992 |
5 | 05.05.2003 02:00:00.000 | 1.12141 | 1.12211 | 1.12085 | 1.12152 | 29867.6992 |
6 | 05.05.2003 03:00:00.000 | 1.12123 | 1.12179 | 1.12049 | 1.12162 | 27256.8008 |
7 | 05.05.2003 04:00:00.000 | 1.12098 | 1.12176 | 1.12079 | 1.12122 | 28278.9004 |
8 | 05.05.2003 05:00:00.000 | 1.12129 | 1.12222 | 1.12091 | 1.12143 | 26764.9004 |
9 | 05.05.2003 06:00:00.000 | 1.12128 | 1.12241 | 1.12066 | 1.12088 | 29956.5996 |
def transform_to_heatmap(dataframe):
dataframe.loc[dataframe['Close']>dataframe['Open'], 'color'] = 1
dataframe.loc[dataframe['Close']<dataframe['Open'], 'color'] = -1
del dataframe['Volume']
del dataframe['Open']
del dataframe['High']
del dataframe['Low']
del dataframe['Close']
return dataframe
float_data=transform_to_heatmap(float_data)
float_data.head(10)
Gmt time | color | |
---|---|---|
0 | 04.05.2003 21:00:00.000 | 1.0 |
1 | 04.05.2003 22:00:00.000 | -1.0 |
2 | 04.05.2003 23:00:00.000 | -1.0 |
3 | 05.05.2003 00:00:00.000 | 1.0 |
4 | 05.05.2003 01:00:00.000 | -1.0 |
5 | 05.05.2003 02:00:00.000 | 1.0 |
6 | 05.05.2003 03:00:00.000 | 1.0 |
7 | 05.05.2003 04:00:00.000 | 1.0 |
8 | 05.05.2003 05:00:00.000 | 1.0 |
9 | 05.05.2003 06:00:00.000 | -1.0 |
def get_week_activity(raw_predictions,date_values,Filter=False):
Monday_ls = [0] * 24
Tuesday_ls = [0] * 24
Wendsday_ls = [0] * 24
Thursday_ls = [0] * 24
Friday_ls = [0] * 24
counter = 0
for a in raw_predictions:
raw_date_converted = datetime.datetime.strptime(date_values[counter], '%d.%m.%Y %H:%M:%S.000')
week_day = raw_date_converted.weekday()
hour = raw_date_converted.hour
candle_check =[0,0]
if a == 1: candle_check =[1,0]
if a == -1: candle_check =[0,1]
if week_day==0: Monday_ls[hour-1]=Monday_ls[hour-1]+np.array(candle_check)
if week_day==1: Tuesday_ls[hour-1]=Tuesday_ls[hour-1]+np.array(candle_check)
if week_day==2: Wendsday_ls[hour-1]=Wendsday_ls[hour-1]+np.array(candle_check)
if week_day==3: Thursday_ls[hour-1]=Thursday_ls[hour-1]+np.array(candle_check)
if week_day==4: Friday_ls[hour-1]=Friday_ls[hour-1]+np.array(candle_check)
counter=counter+1
final_list = [Monday_ls,Tuesday_ls,Wendsday_ls,Thursday_ls,Friday_ls]
weekdays_count = 0
for weekdays in final_list:
hours_count = 0
for hours in weekdays:
if type(hours) != int:
stats = round((hours[0]*100)/(hours[1]+hours[0]),0)
if Filter==True:
if stats <= 55 and stats >= 45: stats=0
if stats < 45: stats= -stats
final_list[weekdays_count][hours_count]=stats
hours_count=hours_count+1
weekdays_count=weekdays_count+1
return [Monday_ls,Tuesday_ls,Wendsday_ls,Thursday_ls,Friday_ls]
test=get_week_activity(float_data['color'].values,float_data['Gmt time'].values)
def make_heat_map(dataset):
weekdays=['Monday','Tuesday','Wendsday','Thursday','Friday']
plt.figure(figsize=(18,5))
ax = sns.heatmap(dataset,annot=True,annot_kws={"size": 10},
linewidth=0.5, yticklabels=weekdays,cmap="Blues")
plt.title('Trade activity during the week')
ax.set_ylabel('Week Days')
ax.set_xlabel('Hours')
plt.show
EUR/USD 2003-2019 1h candles data
make_heat_map(test)
GBP/USD 2003-2019 1h candles data
GBPUSD_dataset = pd.read_csv('Hour/GBPUSD.csv')
float_data=transform_to_heatmap(GBPUSD_dataset)
stats_list=get_week_activity(float_data['color'].values,float_data['Gmt time'].values)
make_heat_map(stats_list)
stats_list=get_week_activity(float_data['color'].values,float_data['Gmt time'].values,Filter=True)
make_heat_map(stats_list)
USD/CAD 2003-2019 1h candles data
USDCAD_dataset = pd.read_csv('Hour/USDCAD.csv')
float_data=transform_to_heatmap(USDCAD_dataset)
stats_list=get_week_activity(float_data['color'].values,float_data['Gmt time'].values)
make_heat_map(stats_list)
stats_list=get_week_activity(float_data['color'].values,float_data['Gmt time'].values,Filter=True)
make_heat_map(stats_list)
NZD/USD 2003-2019 1h candles data
NZDUSD_dataset = pd.read_csv('Hour/NZDUSD.csv')
float_data=transform_to_heatmap(NZDUSD_dataset)
stats_list=get_week_activity(float_data['color'].values,float_data['Gmt time'].values)
make_heat_map(stats_list)
stats_list=get_week_activity(float_data['color'].values,float_data['Gmt time'].values,Filter=True)
make_heat_map(stats_list)
USD/JPY 2003-2019 1h candles data
USDJPY_dataset = pd.read_csv('Hour/USDJPY.csv')
float_data=transform_to_heatmap(USDJPY_dataset)
stats_list=get_week_activity(float_data['color'].values,float_data['Gmt time'].values)
make_heat_map(stats_list)
stats_list=get_week_activity(float_data['color'].values,float_data['Gmt time'].values,Filter=True)
make_heat_map(stats_list)
AUD/USD 2003-2019 1h candles data
AUDUSD_dataset = pd.read_csv('Hour/AUDUSD.csv')
float_data=transform_to_heatmap(AUDUSD_dataset)
stats_list=get_week_activity(float_data['color'].values,float_data['Gmt time'].values)
make_heat_map(stats_list)
stats_list=get_week_activity(float_data['color'].values,float_data['Gmt time'].values,Filter=True)
make_heat_map(stats_list)
USD/CHF 2003-2019 1h candles data
USDCHF_dataset = pd.read_csv('Hour/USDCHF.csv')
float_data=transform_to_heatmap(USDCHF_dataset)
stats_list=get_week_activity(float_data['color'].values,float_data['Gmt time'].values)
make_heat_map(stats_list)
stats_list=get_week_activity(float_data['color'].values,float_data['Gmt time'].values,Filter=True)
make_heat_map(stats_list)