Im relatively new to programming and Im trying to develop an algotrading system with python. Now I have a list which contains all the support levels. printing that list returns :
[417.0, 434.0, 432.9, 432.1, 428.4, 425.3, 400.2, 404.7, 400.8,
384.7, 384.5, 393.7, 383.9, 370.9, 384.2, 383.2, 342.9, 371.3,
372.0, 386.1, 377.3, 378.3, 411.2, 413.2, 404.1, 374.7, 380.3,
382.8, 384.4, 387.3, 387.3, 384.9, 382.2, 378.2, 402.7, 397.6,
373.1, 380.0, 377.4, 379.2, 373.6, 371.5, 374.7, 383.3, 379.5]
Then I draw lines on these levels using Plotly. My question is, How can I get the elements with the least difference so that I can draw support clusters. I have done the following:
def get_diff(t):
for i in range(len(t)):
return abs(t[i]-t[i 1])
comb = combinations(levelssupp, 2)
print('this', min(comb, key=lambda a: get_diff(a)))
but it returns only one tuple:
(375.0, 375.0)
CodePudding user response:
I hope I've understood your question right. This example will print largest list where the values are within a threshold (1% in this example):
lst = [... your data from the question...]
threshold = 0.01 # 1.0 = 100%, 0.01 = 1%
def get_lists(data):
for v in data:
t = v * threshold
yield [j for j in data if abs(v - j) <= t]
largest_list = max(get_lists(lst), key=len)
print(largest_list)
Prints:
[384.7, 384.5, 383.9, 384.2, 383.2, 386.1, 380.3, 382.8, 384.4, 384.9, 382.2, 380.0, 379.2, 383.3, 379.5]