0

I was trying to draw the color of the candles of OHLC. That was accomplished using the code available at: https://stackoverflow.com/a/51417197/4948889

But there is issue while I am trying it in my code. I will let know what situation is here.

dataset_train = pd.read_csv('EURUSD_M1TechnicalIndicators.csv',usecols=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15])
dataset_train.head(10)
dataset_test = pd.read_csv('Test_EURUSD_M1TechnicalIndicators.csv',usecols=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],nrows=50)
dataset_test.head(10)

train  = dataset_train.filter(['open','high','low','close','closeTarget',"k","d","atr","macdmain","macdsgnal","bbup","bbmid","bblow"], axis=1)
x = train.values #returns a numpy array
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
datasetTrain = pd.DataFrame(x_scaled)
datasetTrain.columns = ['open','high','low','close','closeTarget',"k","d","atr","macdmain","macdsgnal","bbup","bbmid","bblow"]
datasetTrain.head(10) 

test = dataset_test.filter(['open','high','low','close','closeTarget',"k","d","atr","macdmain","macdsgnal","bbup","bbmid","bblow"], axis=1)
x = test.values #returns a numpy array
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
datasetTest = pd.DataFrame(x_scaled)
datasetTest.columns = ['open','high','low','close','closeTarget',"k","d","atr","macdmain","macdsgnal","bbup","bbmid","bblow"]
datasetTest.head(10)

plt.figure(figsize=(25,5))
plt.plot(xTrain[:,3])
plt.title('Train (' +str(len(xTrain))+' data points)')
plt.show()
plt.figure(figsize=(10,3))
plt.plot(xTest[:,0])
plt.title('Test (' +str(len(xTest))+' data points)')
plt.show()

The output till here was:

train

test

Then I tried this:

def draw_rects(ax, quotes, width=5., height=1., yloc=1., colorup='g', 
               colordown='r', edgecolor='k', alpha=1.0):

    OFFSET = width / 2.0
    patches = []
    for q in quotes:
        t, open, close, high, low = q[:5]
        if close > open:
            color = colorup
        else:
            color = colordown

        rect = Rectangle(
            xy=(t - OFFSET, yloc),
            width=width,
            height=height,
            facecolor=color,
            edgecolor=edgecolor,
        )
        rect.set_alpha(alpha)
        patches.append(rect)
        ax.add_patch(rect)

    ax.autoscale_view()

    return patches

fig, ax = plt.subplots(1,1)
quotes = xTest
p1 = draw_rects(ax, xTrain, yloc=1)
p2 = draw_rects(ax, xTest, yloc=4)
labels = [item.get_text() for item in ax.get_yticklabels()]
labels[2] = 'Train'
labels[8] = 'Test'
ax.set_yticklabels(labels)
plt.show()

Output:

outptu

I was expecting the candles color to be shown. So that above process didn't worked for me. So I created another column in the dataset as color and shiftedcolor

Now I am thinking to just showcase the color data from the datasets in the candle fashion using the rectangles. Please help me in doing that.

Here are the datasets used by me. EURUSD_M1TechnicalIndicators And Test_EURUSD_M1TechnicalIndicators

Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139
  • By looking at the plot now knowing that your Files have 30202 lines - couldn't this plot be ok but just hard to read because of thousands of (possibly overlapping) rectangles? – SpghttCd Jul 19 '18 at 21:38

1 Answers1

1

I don't really get the complexity of your problem plotting some rectangles (no offending, please correct me if I just ignored important things, in other words: tl;dr...)

But just as an example, how I would plot those two rows (let's say as a different approach to start discussing if it's worth it...):

x = np.arange(10)
y = np.ones(10)

bools = np.random.randint(0, 2, 10).astype(bool)
colors = np.array(bools, str)
colors[bools] = 'g'
colors[~bools] = 'r'

rx = 1
ry = 2
rect = [(-rx, -ry), (rx, -ry), (rx, ry), (-rx, ry)]

plt.figure()
plt.scatter(x, y, facecolor=colors, verts=rect, s=1000)
plt.scatter(x, y+3, facecolor=colors[::-1], verts=rect, s=1000)
plt.ylim(0, 5)
plt.yticks([1, 4], ['Train', 'Test'])

leads to:

enter image description here

EDIT: The same thing applied to your data files: (And I saw you want also black for equal values, so added this, too)

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

dataset_train = pd.read_csv('EURUSD_M1TechnicalIndicators.txt', usecols=['open', 'close'])
dataset_test = pd.read_csv('Test_EURUSD_M1TechnicalIndicators.csv', usecols=['open', 'close'])

gt_train = (dataset_train.open - dataset_train.close) > 0
gt_test = (dataset_test.open - dataset_test.close) > 0
eq_train = (dataset_train.open - dataset_train.close) == 0
eq_test = (dataset_test.open - dataset_test.close) == 0

y_train = np.ones(len(gt_train))
y_test = np.ones(len(gt_test))

colors_train = np.array(gt_train, str)
colors_test = np.array(gt_test, str)
colors_train[gt_train] = 'g'
colors_train[~gt_train] = 'r'
colors_train[eq_train] = 'k'
colors_test[gt_test] = 'g'
colors_test[~gt_test] = 'r'
colors_test[eq_test] = 'k'

rx = .2
ry = 2
rect = [(-rx, -ry), (rx, -ry), (rx, ry), (-rx, ry)]

plt.figure()
plt.scatter(np.arange(y_train.size), y_train, facecolor=colors_train, verts=rect, s=1000)
plt.scatter(np.arange(y_test.size), y_test+3, facecolor=colors_test, verts=rect, s=1000)
plt.ylim(0, 5)
plt.yticks([1, 4], ['Train', 'Test'])

enter image description here

SpghttCd
  • 10,510
  • 2
  • 20
  • 25
  • Wow, this one is what an excellent answer I have got till now. Can you just help me get them labelled please? – Jaffer Wilson Jul 19 '18 at 13:26
  • Which labels please? You can set 'x = np.arange(-2, 4)' if you like to get the x tick labels you posted above. Or 'plt.yticks([1, 4], ['Train', 'Test'])' for your y axis labels. Or did you want to label the markers themselves? – SpghttCd Jul 19 '18 at 19:26
  • It was obviously the axis label what I mean. I guess is done. One more thing, Can you tell me if I wanted to increase the gap between the two rectangles then what I have to do? – Jaffer Wilson Jul 20 '18 at 05:07
  • You can change the aspect ratio of the rectangle which is defined in the `verts` kwarg. I added two parameters `rx`and `ry`, so that you can easily adjust it. – SpghttCd Jul 20 '18 at 06:08
  • Thank you sir for your help. I am checking it out. – Jaffer Wilson Jul 20 '18 at 06:30
  • Sir can you just please modify the second example what you have suggested to look like the output that you have mentioned in the first example. Something separated. I am getting the damn messy output and is not understandable. Please can you just help me do that. I tried to modify the values of `rx` and `ry` to 1 and 2 but there is nothing happening. Please help me. – Jaffer Wilson Jul 20 '18 at 06:46
  • You don't see gaps in the second plot because there are thousands of rectangles plotted on your ~80dpi-screen... Try to zoom in until you have the range of some few samples on the x axis. There the plot will look like the first again. – SpghttCd Jul 20 '18 at 07:13