2

I'm having a fairly tedious problem with my python project. I'm trying to get stock data using yfinance and plotting the data using Plotly to get a candlestick chart with Moving averages and Volume indicators. The problem is that now I'm checking for intersections in the Moving averages iterating over the data frame columns:MA3, MA6, and MA9, then, adding each element to a list and then checking if that list has doubles. I know it has doubles but when I check for them the output is: None. I'm pretty sure it has something to do with python reading float64 or something like that so I tried to convert the number to float 32 or 16 with numpy but doing so I lose decimal digits and I don't think that's the right approach, to be honest, I don't think a data frame iteration is a good approach either

here is the code sample not including the Plotly part:

import pandas as pd


def intersections(list1: list) ->list:

  not_double = []
  double = []

  for i in list1:
    if i in not_double:
      double.append(i)
    else:
      not_double.append(i)
    
    return double


def pair_list_check(list_a: list, list_b: list) ->list:
  
  # list to store doubles
  doubles = []

  for i in list_a:
    if i in list_b:
      doubles.append(i)
    
  return doubles


# Rading the csv
df = pd.read_csv('MA-intersections.csv')


ma3 = df['1dma3']
ma6 = df['1dma6']
ma9 = df['1dma9']

all_ma = []
ma3_list = []
ma6_list = []
ma9_list = []


# Sorting numbers to the appropriate lists 
for i in ma3:
  ma3_list.append(i)
  all_ma.append(i)
for i in ma6:
  ma6_list.append(i)
  all_ma.append(i)
for i in ma9:
  ma9_list.append(i)
  all_ma.append(i)
  # print(type(i))

# Checking for intersections 

ma3_ma6 = print(f"MA3 AND MA6 INTERSECTIONS ARE: {pair_list_check(ma3_list, ma6_list)}")
ma6_ma6 = print(f"MA3 AND MA9 INTERSECTIONS ARE: {pair_list_check(ma3_list, ma9_list)}")
ma3_ma9 = print(f"MA6 AND MA9 INTERSECTIONS ARE: {pair_list_check(ma6_list, ma9_list)}")
all_ma_inter = print(f"THE COMMON INTERSECTIONS BETWEEN ALL MAs  ARE: {intersections(all_ma)}")

output:

MA3 AND MA6 INTERSECTIONS ARE: []
MA3 AND MA9 INTERSECTIONS ARE: []
MA6 AND MA9 INTERSECTIONS ARE: []
THE COMMON INTERSECTIONS BETWEEN ALL MAs  ARE: []

And this is the data frame I am currently working with:

,Datetime,1dma3,1dma6,1dma9
0,2021-09-15 16:00:00-04:00,,,
1,2021-09-15 15:55:00-04:00,,,
2,2021-09-15 15:50:00-04:00,149.07333374023438,,
3,2021-09-15 15:45:00-04:00,149.15333557128906,,
4,2021-09-15 15:40:00-04:00,149.17833455403647,,
5,2021-09-15 15:35:00-04:00,149.25833638509116,149.16583506266275,
6,2021-09-15 15:30:00-04:00,149.1816660563151,149.1675008138021,
7,2021-09-15 15:25:00-04:00,149.14336649576822,149.16085052490234,
8,2021-09-15 15:20:00-04:00,148.9929656982422,149.12565104166666,149.10821194118924
9,2021-09-15 15:15:00-04:00,148.96963500976562,149.07565053304037,149.10154554578992
10,2021-09-15 15:10:00-04:00,148.8896026611328,149.01648457845053,149.0704345703125
11,2021-09-15 15:05:00-04:00,148.8300018310547,148.91148376464844,149.02710130479602
12,2021-09-15 15:00:00-04:00,148.72040303548178,148.8450190226237,148.95723470052084
13,2021-09-15 14:55:00-04:00,148.5970662434896,148.7433344523112,148.87667846679688
14,2021-09-15 14:50:00-04:00,148.52540079752603,148.67770131429037,148.7827894422743
15,2021-09-15 14:45:00-04:00,148.48666381835938,148.60353342692056,148.72556728786893
16,2021-09-15 14:40:00-04:00,148.4499969482422,148.52353159586588,148.6455552842882
17,2021-09-15 14:35:00-04:00,148.4016316731771,148.46351623535156,148.58567810058594
18,2021-09-15 14:30:00-04:00,148.3218994140625,148.40428161621094,148.50965542263455
19,2021-09-15 14:25:00-04:00,148.31856791178384,148.38428243001303,148.45521036783853
20,2021-09-15 14:20:00-04:00,148.28693135579428,148.3442815144857,148.40465460883246
21,2021-09-15 14:15:00-04:00,148.24169921875,148.28179931640625,148.35008748372397
22,2021-09-15 14:10:00-04:00,148.20836385091147,148.26346588134766,148.32564290364584
23,2021-09-15 14:05:00-04:00,148.23169962565103,148.25931549072266,148.30675421820746
24,2021-09-15 14:00:00-04:00,148.24496459960938,148.2433319091797,148.26952107747397
25,2021-09-15 13:55:00-04:00,148.21263122558594,148.2104975382487,148.24652099609375
26,2021-09-15 13:50:00-04:00,148.2342987060547,148.23299916585287,148.2509765625
27,2021-09-15 13:45:00-04:00,148.31096903483072,148.27796681722006,148.26587761773004
28,2021-09-15 13:40:00-04:00,148.35177103678384,148.2822011311849,148.2575887044271
29,2021-09-15 13:35:00-04:00,148.2917683919271,148.26303354899088,148.2525889078776
30,2021-09-15 13:30:00-04:00,148.25566609700522,148.28331756591797,148.2705332438151
31,2021-09-15 13:25:00-04:00,148.23719787597656,148.29448445638022,148.26720004611545
32,2021-09-15 13:20:00-04:00,148.19219970703125,148.24198404947916,148.23942226833768
33,2021-09-15 13:15:00-04:00,148.09830220540366,148.17698415120444,148.22164577907986
34,2021-09-15 13:10:00-04:00,147.99493408203125,148.1160659790039,148.19463433159723
35,2021-09-15 13:05:00-04:00,147.9499308268229,148.0710652669271,148.1446329752604
36,2021-09-15 13:00:00-04:00,148.0017293294271,148.05001576741537,148.11856587727866
37,2021-09-15 12:55:00-04:00,148.05343119303384,148.02418263753256,148.09518771701389
38,2021-09-15 12:50:00-04:00,148.07343546549478,148.01168314615884,148.07185533311633
39,2021-09-15 12:45:00-04:00,148.0433349609375,148.02253214518228,148.04778883192273
40,2021-09-15 12:40:00-04:00,148.0066680908203,148.0300496419271,148.01834445529514
41,2021-09-15 12:35:00-04:00,148.02333068847656,148.0483830769857,148.01556566026477
42,2021-09-15 12:30:00-04:00,147.92332967122397,147.98333231608072,147.98946465386285
43,2021-09-15 12:25:00-04:00,147.80499776204428,147.90583292643228,147.9550323486328
44,2021-09-15 12:20:00-04:00,147.55333455403647,147.7883326212565,147.88336690266928
45,2021-09-15 12:15:00-04:00,147.35333760579428,147.63833363850912,147.77333407931857
46,2021-09-15 12:10:00-04:00,147.203369140625,147.50418345133463,147.6716783311632
47,2021-09-15 12:05:00-04:00,147.20213317871094,147.3777338663737,147.59293280707465
48,2021-09-15 12:00:00-04:00,147.23379516601562,147.29356638590494,147.5034874810113
49,2021-09-15 11:55:00-04:00,147.27709452311197,147.2402318318685,147.4284871419271
50,2021-09-15 11:50:00-04:00,147.15333048502603,147.1777318318685,147.3029327392578
51,2021-09-15 11:45:00-04:00,147.03999837239584,147.13689676920572,147.20904371473523
52,2021-09-15 11:40:00-04:00,146.97666931152344,147.12688191731772,147.15237765842014
53,2021-09-15 11:35:00-04:00,147.00833638509116,147.0808334350586,147.12126668294272
54,2021-09-15 11:30:00-04:00,147.03667195638022,147.03833516438803,147.10348849826389
55,2021-09-15 11:25:00-04:00,147.02033487955728,146.99850209554037,147.09136623806424
56,2021-09-15 11:20:00-04:00,147.0720011393229,147.04016876220703,147.07788933648004
57,2021-09-15 11:15:00-04:00,147.05676778157553,147.04671986897787,147.0444793701172
58,2021-09-15 11:10:00-04:00,147.096435546875,147.05838521321616,147.03114657931857
59,2021-09-15 11:05:00-04:00,147.13810221354166,147.10505167643228,147.07281324598523
60,2021-09-15 11:00:00-04:00,147.25166829427084,147.1542180379232,147.1150360107422
61,2021-09-15 10:55:00-04:00,147.27833557128906,147.18738555908203,147.13170199924045
62,2021-09-15 10:50:00-04:00,147.15833536783853,147.1482187906901,147.12281290690103
63,2021-09-15 10:45:00-04:00,147.04833475748697,147.1500015258789,147.11892361111111
64,2021-09-15 10:40:00-04:00,146.90166727701822,147.09000142415366,147.09214613172742
65,2021-09-15 10:35:00-04:00,146.89176432291666,147.0250498453776,147.06273396809897
66,2021-09-15 10:30:00-04:00,146.77176411946616,146.91004943847656,147.023922390408
67,2021-09-15 10:25:00-04:00,146.7117665608724,146.8067169189453,146.96392313639322
68,2021-09-15 10:20:00-04:00,146.6433359781901,146.76755015055338,146.89781188964844
69,2021-09-15 10:15:00-04:00,146.66333516438803,146.7175496419271,146.8278113471137
70,2021-09-15 10:10:00-04:00,146.70000203450522,146.7058842976888,146.7711452907986
71,2021-09-15 10:05:00-04:00,146.89510091145834,146.76921844482422,146.81006707085504
72,2021-09-15 10:00:00-04:00,147.07510375976562,146.8692194620768,146.83673434787326
73,2021-09-15 09:55:00-04:00,147.3184356689453,147.00921885172525,146.91006808810764
74,2021-09-15 09:50:00-04:00,147.2483367919922,147.07171885172525,146.92892456054688
75,2021-09-15 09:45:00-04:00,147.4250030517578,147.25005340576172,147.05448065863715
76,2021-09-15 09:40:00-04:00,147.4683380126953,147.3933868408203,147.1622585720486
77,2021-09-15 09:35:00-04:00,147.78667195638022,147.5175043741862,147.31003655327692
78,2021-09-15 09:30:00-04:00,148.00000508626303,147.7125040690104,147.50003729926215

Sample of the chart

Note that the purple line is a line going through the intersection between all MAs values (considering only the first 3 decimal digits)

JustAG33K
  • 1,403
  • 3
  • 13
  • 28

1 Answers1

2
  • using this technique for finding rows where all values are equal Pandas Dataframe Find Rows Where all Columns Equal
  • I don't find any intersections when using 3DP, there are a number if 1DP is used
  • bringing it back together as a plotly express figure
import io
import numpy as np
import pandas as pd
import plotly.express as px

df = pd.read_csv(io.StringIO(""",Datetime,1dma3,1dma6,1dma9
0,2021-09-15 16:00:00-04:00,,,
1,2021-09-15 15:55:00-04:00,,,
2,2021-09-15 15:50:00-04:00,149.07333374023438,,
3,2021-09-15 15:45:00-04:00,149.15333557128906,,
4,2021-09-15 15:40:00-04:00,149.17833455403647,,
5,2021-09-15 15:35:00-04:00,149.25833638509116,149.16583506266275,
6,2021-09-15 15:30:00-04:00,149.1816660563151,149.1675008138021,
7,2021-09-15 15:25:00-04:00,149.14336649576822,149.16085052490234,
8,2021-09-15 15:20:00-04:00,148.9929656982422,149.12565104166666,149.10821194118924
9,2021-09-15 15:15:00-04:00,148.96963500976562,149.07565053304037,149.10154554578992
10,2021-09-15 15:10:00-04:00,148.8896026611328,149.01648457845053,149.0704345703125
11,2021-09-15 15:05:00-04:00,148.8300018310547,148.91148376464844,149.02710130479602
12,2021-09-15 15:00:00-04:00,148.72040303548178,148.8450190226237,148.95723470052084
13,2021-09-15 14:55:00-04:00,148.5970662434896,148.7433344523112,148.87667846679688
14,2021-09-15 14:50:00-04:00,148.52540079752603,148.67770131429037,148.7827894422743
15,2021-09-15 14:45:00-04:00,148.48666381835938,148.60353342692056,148.72556728786893
16,2021-09-15 14:40:00-04:00,148.4499969482422,148.52353159586588,148.6455552842882
17,2021-09-15 14:35:00-04:00,148.4016316731771,148.46351623535156,148.58567810058594
18,2021-09-15 14:30:00-04:00,148.3218994140625,148.40428161621094,148.50965542263455
19,2021-09-15 14:25:00-04:00,148.31856791178384,148.38428243001303,148.45521036783853
20,2021-09-15 14:20:00-04:00,148.28693135579428,148.3442815144857,148.40465460883246
21,2021-09-15 14:15:00-04:00,148.24169921875,148.28179931640625,148.35008748372397
22,2021-09-15 14:10:00-04:00,148.20836385091147,148.26346588134766,148.32564290364584
23,2021-09-15 14:05:00-04:00,148.23169962565103,148.25931549072266,148.30675421820746
24,2021-09-15 14:00:00-04:00,148.24496459960938,148.2433319091797,148.26952107747397
25,2021-09-15 13:55:00-04:00,148.21263122558594,148.2104975382487,148.24652099609375
26,2021-09-15 13:50:00-04:00,148.2342987060547,148.23299916585287,148.2509765625
27,2021-09-15 13:45:00-04:00,148.31096903483072,148.27796681722006,148.26587761773004
28,2021-09-15 13:40:00-04:00,148.35177103678384,148.2822011311849,148.2575887044271
29,2021-09-15 13:35:00-04:00,148.2917683919271,148.26303354899088,148.2525889078776
30,2021-09-15 13:30:00-04:00,148.25566609700522,148.28331756591797,148.2705332438151
31,2021-09-15 13:25:00-04:00,148.23719787597656,148.29448445638022,148.26720004611545
32,2021-09-15 13:20:00-04:00,148.19219970703125,148.24198404947916,148.23942226833768
33,2021-09-15 13:15:00-04:00,148.09830220540366,148.17698415120444,148.22164577907986
34,2021-09-15 13:10:00-04:00,147.99493408203125,148.1160659790039,148.19463433159723
35,2021-09-15 13:05:00-04:00,147.9499308268229,148.0710652669271,148.1446329752604
36,2021-09-15 13:00:00-04:00,148.0017293294271,148.05001576741537,148.11856587727866
37,2021-09-15 12:55:00-04:00,148.05343119303384,148.02418263753256,148.09518771701389
38,2021-09-15 12:50:00-04:00,148.07343546549478,148.01168314615884,148.07185533311633
39,2021-09-15 12:45:00-04:00,148.0433349609375,148.02253214518228,148.04778883192273
40,2021-09-15 12:40:00-04:00,148.0066680908203,148.0300496419271,148.01834445529514
41,2021-09-15 12:35:00-04:00,148.02333068847656,148.0483830769857,148.01556566026477
42,2021-09-15 12:30:00-04:00,147.92332967122397,147.98333231608072,147.98946465386285
43,2021-09-15 12:25:00-04:00,147.80499776204428,147.90583292643228,147.9550323486328
44,2021-09-15 12:20:00-04:00,147.55333455403647,147.7883326212565,147.88336690266928
45,2021-09-15 12:15:00-04:00,147.35333760579428,147.63833363850912,147.77333407931857
46,2021-09-15 12:10:00-04:00,147.203369140625,147.50418345133463,147.6716783311632
47,2021-09-15 12:05:00-04:00,147.20213317871094,147.3777338663737,147.59293280707465
48,2021-09-15 12:00:00-04:00,147.23379516601562,147.29356638590494,147.5034874810113
49,2021-09-15 11:55:00-04:00,147.27709452311197,147.2402318318685,147.4284871419271
50,2021-09-15 11:50:00-04:00,147.15333048502603,147.1777318318685,147.3029327392578
51,2021-09-15 11:45:00-04:00,147.03999837239584,147.13689676920572,147.20904371473523
52,2021-09-15 11:40:00-04:00,146.97666931152344,147.12688191731772,147.15237765842014
53,2021-09-15 11:35:00-04:00,147.00833638509116,147.0808334350586,147.12126668294272
54,2021-09-15 11:30:00-04:00,147.03667195638022,147.03833516438803,147.10348849826389
55,2021-09-15 11:25:00-04:00,147.02033487955728,146.99850209554037,147.09136623806424
56,2021-09-15 11:20:00-04:00,147.0720011393229,147.04016876220703,147.07788933648004
57,2021-09-15 11:15:00-04:00,147.05676778157553,147.04671986897787,147.0444793701172
58,2021-09-15 11:10:00-04:00,147.096435546875,147.05838521321616,147.03114657931857
59,2021-09-15 11:05:00-04:00,147.13810221354166,147.10505167643228,147.07281324598523
60,2021-09-15 11:00:00-04:00,147.25166829427084,147.1542180379232,147.1150360107422
61,2021-09-15 10:55:00-04:00,147.27833557128906,147.18738555908203,147.13170199924045
62,2021-09-15 10:50:00-04:00,147.15833536783853,147.1482187906901,147.12281290690103
63,2021-09-15 10:45:00-04:00,147.04833475748697,147.1500015258789,147.11892361111111
64,2021-09-15 10:40:00-04:00,146.90166727701822,147.09000142415366,147.09214613172742
65,2021-09-15 10:35:00-04:00,146.89176432291666,147.0250498453776,147.06273396809897
66,2021-09-15 10:30:00-04:00,146.77176411946616,146.91004943847656,147.023922390408
67,2021-09-15 10:25:00-04:00,146.7117665608724,146.8067169189453,146.96392313639322
68,2021-09-15 10:20:00-04:00,146.6433359781901,146.76755015055338,146.89781188964844
69,2021-09-15 10:15:00-04:00,146.66333516438803,146.7175496419271,146.8278113471137
70,2021-09-15 10:10:00-04:00,146.70000203450522,146.7058842976888,146.7711452907986
71,2021-09-15 10:05:00-04:00,146.89510091145834,146.76921844482422,146.81006707085504
72,2021-09-15 10:00:00-04:00,147.07510375976562,146.8692194620768,146.83673434787326
73,2021-09-15 09:55:00-04:00,147.3184356689453,147.00921885172525,146.91006808810764
74,2021-09-15 09:50:00-04:00,147.2483367919922,147.07171885172525,146.92892456054688
75,2021-09-15 09:45:00-04:00,147.4250030517578,147.25005340576172,147.05448065863715
76,2021-09-15 09:40:00-04:00,147.4683380126953,147.3933868408203,147.1622585720486
77,2021-09-15 09:35:00-04:00,147.78667195638022,147.5175043741862,147.31003655327692
78,2021-09-15 09:30:00-04:00,148.00000508626303,147.7125040690104,147.50003729926215"""))

df = df.drop(columns=["Unnamed: 0"])
df["Datetime"] = pd.to_datetime(df["Datetime"])


DP = 1
a = df.loc[:,[c for c in df.columns if "1dma" in c]].round(DP).values
df["intersect"] = np.where((a == a[:, [0]]).all(axis=1), df["1dma3"], np.nan)
df["intersect"] = df["intersect"].fillna(method="bfill").fillna(method="ffill")

px.line(df, x="Datetime", y=[c for c in df.columns if c!="Datetime"])


enter image description here

Rob Raymond
  • 29,118
  • 3
  • 14
  • 30