Home > database >  I got my data to be more often, every 30 minutes but now my data is different since it has the times
I got my data to be more often, every 30 minutes but now my data is different since it has the times

Time:04-12

Graph Broken

This is my code

from time import sleep
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import requests
plt.style.use("fivethirtyeight")
import alpaca_trade_api as tradeapi
import threading
from bs4 import BeautifulSoup
import datetime
import logging
import json

df = pd.read_csv("CSVDATA.csv")
df = df.set_index(pd.DatetimeIndex(df["Date"].values))


ShortEMA = df.Close.ewm(span=5, adjust = False).mean()
MiddleEMA = df.Close.ewm(span = 21, adjust = False).mean()
LongEMA = df.Close.ewm(span = 53, adjust = False).mean()

df['Short'] = ShortEMA
df['Middle'] = MiddleEMA
df['Long'] = LongEMA

def MyStrat(data):
    bought_list = []
    sold_list = []
    In = False
    Out = True
    for i in range(0, len(data)):
        if  data["Close"][i] > data["Short"][i] and In == False and Out == True:
            bought_list.append(data["Close"][i])
            sold_list.append(np.nan)
            In = True
            Out = False

        elif data["Close"][i] < data["Short"][i] and In == True and Out == False:
            sold_list.append(data["Close"][i])
            bought_list.append(np.nan)
            In = False
            Out = True

        else:
            bought_list.append(np.nan)
            sold_list.append(np.nan)
            

    return(bought_list,sold_list)
   
df["Bought"] = MyStrat(df)[0]
df["Sold"] = MyStrat(df)[1]
#print(df)


plt.figure(figsize=(16, 5))
plt.title('Buy and Sell', fontsize = 18)
plt.plot(df['Close'], label = 'Close Price', color = 'blue', alpha = 0.35)
plt.plot(ShortEMA, label = 'Short', color = 'red', alpha = 0.35)
plt.scatter(df.index, df["Bought"], color = "purple", marker = "^", alpha = 1)
plt.scatter(df.index, df["Sold"], color = "blue", marker = "v", alpha = 1)
plt.xlabel("Date", fontsize = 18)
plt.ylabel("Close", fontsize = 18)
plt.show()

And my data is

Date,Open,High,Low,Close,Adj Close,Volume
Date,Open,High,Low,Close,Adj Close,Volume
2021-01-04 09:00:00 00:00,709.0,725.0,709.0,724.0,129845,4762,720.654099
2021-01-04 09:30:00 00:00,724.0,726.75,722.8,726.75,40136,1256,724.7681
2021-01-04 10:00:00 00:00,726.85,729.77,725.0,725.32,42825,1606,727.418178
2021-01-04 10:30:00 00:00,725.25,726.51,725.0,725.12,23813,774,725.473752
2021-01-04 11:00:00 00:00,725.6,726.98,725.0,725.5,14357,498,725.435472
2021-01-04 11:30:00 00:00,725.99,728.0,725.42,726.14,20353,665,726.891527
2021-01-04 12:00:00 00:00,725.51,725.98,718.23,720.5,346249,8535,722.103485
2021-01-04 12:30:00 00:00,720.46,722.5,720.0,720.5,174076,4165,721.133323
2021-01-04 13:00:00 00:00,726.0,726.0,718.2,720.33,254528,6054,721.224213
2021-01-04 13:30:00 00:00,720.6,722.77,719.21,719.6,170366,5282,721.233608
2021-01-04 14:00:00 00:00,719.9,720.9,715.5,719.77,409856,12035,718.359061
2021-01-04 14:30:00 00:00,719.5,743.7599,717.1895,738.55,9971570,197284,732.306153
2021-01-04 15:00:00 00:00,738.95,743.0,733.5,740.03,4562344,99317,738.878094
2021-01-04 15:30:00 00:00,740.0,744.4899,730.52,731.2001,4511525,92511,738.520546
2021-01-04 16:00:00 00:00,731.335,740.18,728.26,735.62,3995753,79800,731.826834
2021-01-04 16:30:00 00:00,735.45,735.75,729.2527,730.975,1895759,42343,732.773805
2021-01-04 17:00:00 00:00,730.8544,732.6343,718.03,723.61,5138212,86736,724.354432
2021-01-04 17:30:00 00:00,723.76,725.71,719.9,724.055,2099705,45174,722.749092
2021-01-04 18:00:00 00:00,723.83,733.0,723.31,731.165,2384220,44495,728.321716
2021-01-04 18:30:00 00:00,731.11,736.4905,719.24,733.13,2150704,43342,733.394764
2021-01-04 19:00:00 00:00,733.283,733.9896,726.2501,727.47,1756963,36477,730.059996
2021-01-04 19:30:00 00:00,727.28,730.69,724.28,730.38,1964226,37763,727.486091
2021-01-04 20:00:00 00:00,730.37,732.68,726.8001,730.7913,1781455,35104,730.114156
2021-01-04 20:30:00 00:00,730.855,732.78,728.25,729.75,2460216,51705,730.17104
2021-01-04 21:00:00 00:00,729.77,731.5251,729.3328,730.29,2135771,2699,729.791166
2021-01-04 21:30:00 00:00,730.4,731.0,729.77,730.5102,41181,4855,730.4039
2021-01-04 22:00:00 00:00,730.5202,731.8,730.33,730.6,29005,1175,730.951285
2021-01-04 22:30:00 00:00,730.95,731.88,730.6,731.0,24402,952,731.046817
2021-01-04 23:00:00 00:00,730.8,730.8,730.0,730.3,27769,639,730.238444
2021-01-04 23:30:00 00:00,730.17,730.39,729.5,729.6,21186,451,729.890703
2021-01-05 00:00:00 00:00,729.21,730.8499,729.05,730.2,19822,453,729.822813
2021-01-05 00:30:00 00:00,729.9,733.66,729.9,733.6,42026,1043,731.635581
2021-01-05 09:00:00 00:00,730.13,737.89,730.13,736.59,23960,864,734.352225
2021-01-05 09:30:00 00:00,735.0,735.68,731.0,732.0,13782,442,733.273587
2021-01-05 10:00:00 00:00,731.95,732.6,729.84,731.06,17980,608,730.994818
2021-01-05 10:30:00 00:00,731.75,732.01,730.99,731.2,4738,132,731.56741
2021-01-05 11:00:00 00:00,732.85,734.14,732.85,733.69,1222,43,733.227414
2021-01-05 11:30:00 00:00,733.5,734.0,730.0,730.81,7986,242,731.767621
2021-01-05 12:00:00 00:00,731.0,734.3,729.0,729.01,194526,3961,731.242816
2021-01-05 12:30:00 00:00,728.9,729.48,725.66,726.46,123885,3415,727.924189
2021-01-05 13:00:00 00:00,732.9801,733.99,721.35,722.6,262780,6302,724.44749
2021-01-05 13:30:00 00:00,722.59,729.77,721.55,723.82,154333,5134,723.936729
2021-01-05 14:00:00 00:00,723.2,724.44,721.01,724.0,178496,7704,722.560421
2021-01-05 14:30:00 00:00,723.66,728.43,719.2,721.9,4980614,107184,724.458486
2021-01-05 15:00:00 00:00,721.94,735.68,720.3,732.003,4371697,84433,729.263509
2021-01-05 15:30:00 00:00,731.91,738.5,730.55,734.6638,2873353,59747,734.576983
2021-01-05 16:00:00 00:00,734.76,738.8983,733.37,738.59,2120578,44396,736.360104
2021-01-05 16:30:00 00:00,738.7799,740.84,736.2701,737.4,2172756,46648,738.690673
2021-01-05 17:00:00 00:00,737.41,737.79,733.53,737.67,1632424,34483,736.018615
2021-01-05 17:30:00 00:00,737.67,738.33,735.1501,738.32,1038302,24358,736.690593
2021-01-05 18:00:00 00:00,738.2025,740.6,737.04,738.737,1698257,35432,738.889342
2021-01-05 18:30:00 00:00,738.73,740.53,738.01,738.67,946580,23721,739.280519
2021-01-05 19:00:00 00:00,738.7,739.7,737.0,738.3288,973908,24697,738.349992
2021-01-05 19:30:00 00:00,738.4,739.4,735.2,736.035,1256872,28996,737.202965
2021-01-05 20:00:00 00:00,736.025,736.69,733.6,735.9,1299491,30587,735.263778
2021-01-05 20:30:00 00:00,735.9,736.8,731.12,735.11,2583717,56108,733.619869
2021-01-05 21:00:00 00:00,735.11,744.51,733.04,744.21,1108191,8266,737.053041
2021-01-05 21:30:00 00:00,744.17,750.0,733.2396,749.36,873674,18161,745.229113
2021-01-05 22:00:00 00:00,749.65,751.75,735.11,750.5,541392,8998,745.464868
2021-01-05 22:30:00 00:00,750.5,754.4,749.61,753.21,311759,6866,752.260985
2021-01-05 23:00:00 00:00,753.39,753.5,751.6,753.44,107713,2446,752.39002
2021-01-05 23:30:00 00:00,753.47,754.18,753.15,753.34,74609,1822,753.674155
2021-01-06 00:00:00 00:00,753.32,753.32,750.5,752.69,88126,1899,752.04168
2021-01-06 00:30:00 00:00,752.9999,753.19,752.0,752.9001,97061,2062,752.724362
2021-01-06 09:00:00 00:00,748.0,752.55,746.66,752.49,54602,1607,749.133066
2021-01-06 09:30:00 00:00,752.21,752.21,750.21,751.88,11146,294,751.36678
2021-01-06 10:00:00 00:00,751.6,752.75,750.55,750.55,12005,329,751.540315
2021-01-06 10:30:00 00:00,750.06,753.0,749.26,750.5,15706,534,750.992452
2021-01-06 11:00:00 00:00,750.96,751.68,749.0,749.0,24949,654,750.138775
2021-01-06 11:30:00 00:00,748.61,751.85,747.5,751.85,27775,610,749.479488
2021-01-06 12:00:00 00:00,751.65,757.47,751.33,756.0,255439,5987,755.244314
2021-01-06 12:30:00 00:00,755.93,759.9,755.93,757.88,236707,6040,758.332992
2021-01-06 13:00:00 00:00,757.12,762.67,751.32,762.0,379940,8560,759.258449
2021-01-06 13:30:00 00:00,762.08,764.0,760.26,760.6,403679,9667,762.597508
2021-01-06 14:00:00 00:00,760.74,763.74,757.0,758.0,362365,13095,761.058574
2021-01-06 14:30:00 00:00,757.99,761.4999,750.12,751.33,6772377,146465,756.511534
2021-01-06 15:00:00 00:00,751.52,760.5,749.1,757.3092,4003540,85864,756.564791
2021-01-06 15:30:00 00:00,757.2504,763.8,755.5101,762.8784,3811848,83132,760.543447
2021-01-06 16:00:00 00:00,762.89,769.98,762.87,769.42,3821155,81082,767.188179
2021-01-06 16:30:00 00:00,769.615,772.7,768.4,771.1255,3095444,67550,770.711601
2021-01-06 17:00:00 00:00,771.18,773.7499,770.3301,771.1446,2136256,47282,772.097878
2021-01-06 17:30:00 00:00,771.18,772.5,768.3418,771.0326,1451575,35587,770.834208
2021-01-06 18:00:00 00:00,771.24,771.35,768.58,769.6904,1190453,29472,769.894005
2021-01-06 18:30:00 00:00,769.615,772.15,769.2572,771.655,1157568,26625,770.795744
2021-01-06 19:00:00 00:00,771.7999,774.0,761.875,764.5,2488141,53652,769.880887
2021-01-06 19:30:00 00:00,764.51,767.93,750.0,761.6767,5263494,111719,759.659177
2021-01-06 20:00:00 00:00,761.8445,762.5,754.77,758.68,3022468,59874,759.168884
2021-01-06 20:30:00 00:00,758.53,761.95,755.0,756.46,3161486,64218,757.97222
2021-01-06 21:00:00 00:00,755.98,763.12,755.0,762.91,828485,6248,756.638929
2021-01-06 21:30:00 00:00,763.0,765.0,755.98,762.75,253261,7906,763.010735
2021-01-06 22:00:00 00:00,762.64,762.75,760.05,761.4,142431,1909,757.793609
2021-01-06 22:30:00 00:00,761.5,762.49,760.39,760.5,49669,1600,761.284492
2021-01-06 23:00:00 00:00,760.8,762.0,760.8,762.0,33352,697,761.590854
2021-01-06 23:30:00 00:00,762.35,762.8,761.6,762.01,33489,627,762.099817
2021-01-07 00:00:00 00:00,762.07,763.25,761.88,763.25,44086,807,762.317904
2021-01-07 00:30:00 00:00,762.94,764.45,762.3,763.6,138125,1597,763.715477
2021-01-07 09:00:00 00:00,769.0,779.0,767.2,777.85,53428,1700,774.828142
2021-01-07 09:30:00 00:00,776.62,778.79,776.3,778.0,14858,425,777.223632
2021-01-07 10:00:00 00:00,777.77,781.3,777.77,780.2,22883,842,779.804607
2021-01-07 10:30:00 00:00,780.2,780.21,778.3,778.61,9575,365,779.322838
2021-01-07 11:00:00 00:00,778.78,779.0,776.88,778.0,13618,443,778.096065
2021-01-07 11:30:00 00:00,777.8,778.0,777.07,777.7,20420,527,777.714189
2021-01-07 12:00:00 00:00,777.14,778.48,775.0,777.46,238986,5247,776.961178
2021-01-07 12:30:00 00:00,777.4,779.0,776.13,778.5,127240,3218,777.95877
2021-01-07 13:00:00 00:00,776.0358,778.88,775.0,776.5,223147,5333,777.379651
2021-01-07 13:30:00 00:00,776.52,777.5,774.22,774.53,124965,4792,775.890287

What is new is that i have the 13:30:00 00:00 included with the data too which I don't think that my code can take care of. I made it in higher intervals so that I could plot the buy / sell signals more often Instead of it being daily.

Thanks in Advance!

CodePudding user response:

Here I did the following edits:

  • load the dataframe and didn't change any index.
  • I create a matplotlib figure, so that we can use the autofmt_xdate method.
  • set a custom number of xticks and xticks labels.
from time import sleep
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import requests
plt.style.use("fivethirtyeight")
import alpaca_trade_api as tradeapi
import threading
from bs4 import BeautifulSoup
import datetime
import logging
import json

df = pd.read_csv("CSVDATA.csv")

ShortEMA = df.Close.ewm(span=5, adjust = False).mean()
MiddleEMA = df.Close.ewm(span = 21, adjust = False).mean()
LongEMA = df.Close.ewm(span = 53, adjust = False).mean()

df['Short'] = ShortEMA
df['Middle'] = MiddleEMA
df['Long'] = LongEMA

def MyStrat(data):
    bought_list = []
    sold_list = []
    In = False
    Out = True
    for i in range(0, len(data)):
        if  data["Close"][i] > data["Short"][i] and In == False and Out == True:
            bought_list.append(data["Close"][i])
            sold_list.append(np.nan)
            In = True
            Out = False

        elif data["Close"][i] < data["Short"][i] and In == True and Out == False:
            sold_list.append(data["Close"][i])
            bought_list.append(np.nan)
            In = False
            Out = True

        else:
            bought_list.append(np.nan)
            sold_list.append(np.nan)
            

    return(bought_list,sold_list)
   
df["Bought"] = MyStrat(df)[0]
df["Sold"] = MyStrat(df)[1]

f, ax = plt.subplots(figsize=(16, 5))
ax.set_title('Buy and Sell', fontsize = 18)
ax.plot(df['Close'], label = 'Close Price', color = 'blue', alpha = 0.35)
ax.plot(ShortEMA, label = 'Short', color = 'red', alpha = 0.35)
ax.scatter(df.index, df["Bought"], color = "purple", marker = "^", alpha = 1)
ax.scatter(df.index, df["Sold"], color = "blue", marker = "v", alpha = 1)
ax.set_xlabel("Date", fontsize = 18)
ax.set_ylabel("Close", fontsize = 18)

# show one label every n
n = 5
xx = [i for i in range(len(df.index)) if i % n == 0]
labels = [l.replace(":00 00:00", "") for i, l in enumerate(df.index) if i % n == 0]
ax.set_xticks(xx)
ax.set_xticklabels(labels)
f.autofmt_xdate(rotation=80, ha="right")
plt.show()

enter image description here

  • Related