Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please help me.
#9
(May-06-2023, 10:27 AM)SuchUmami Wrote: . I definitely could do this if I break the code up to only look for one currency pair on one timeframe but I thought this code will make it a lot easier and condensed for me (as there's 133 different modules this code would break down into).

The trouble I am having is that now that I want to write this new module to rate the data as individual pieces (for instance, rate btc/usd moving averages on the 15 minute time frame), that I am not sure how to isolate these pieces of data in order to rate them.
You should break it up to the basic,the code you have written could be one function and new stuff in another function.
It's much harder when you have written code that do a lot stuff in a big loop,to add test test stuff out.
So as example.
# kr4.py
import krakenex
import pandas as pd

# create a Kraken API client
api = krakenex.API()

# set the currency pair and interval
pair = 'BTC/USD'
interval = 15

# make the API request for OHLC data
ohlc_data = api.query_public('OHLC', {'pair': pair, 'interval': interval})
# Convert the data to a pandas dataframe
df = pd.DataFrame(ohlc_data['result'][pair], columns=['time', 'open', 'high', 'low', 'close', 'vwap', 'volume', 'count'])
df['time'] = pd.to_datetime(df['time'], unit='s')
df.set_index('time', inplace=True)

# Calculate the moving averages
last_20_candles = df.iloc[-21:-1]
last_20_close_prices = last_20_candles['close'].astype(float).tolist()
avg_last_20_close_price = sum(last_20_close_prices) / len(last_20_close_prices)
print(f"20 period m/a: {avg_last_20_close_price}")
What i often to now is to run this code interactive when testing,if not have Editor that do this can use -i flagg.
(dl_env) G:\div_code\dl_env
λ python -i kr4.py
20 period m/a: 28792.299999999996

# The original DataFrame
>>> df
                        open     high      low    close     vwap       volume  count
time
2023-04-29 06:00:00  29383.4  29405.9  29383.3  29383.3  29387.6   8.66207226    136
2023-04-29 06:15:00  29383.4  29394.9  29341.9  29342.1  29381.4  14.18612946    172
2023-04-29 06:30:00  29342.0  29353.2  29341.2  29342.3  29345.5   2.62617868    104
2023-04-29 06:45:00  29342.3  29360.4  29322.0  29343.5  29337.8  13.15491601    182
2023-04-29 07:00:00  29343.5  29390.4  29343.4  29369.7  29372.0   8.08884765    155
...                      ...      ...      ...      ...      ...          ...    ...
2023-05-06 16:45:00  28710.1  28767.1  28710.0  28760.1  28743.2   7.60574838    257
2023-05-06 17:00:00  28760.1  28760.1  28733.0  28733.1  28742.0  10.26238430    210
2023-05-06 17:15:00  28733.1  28890.0  28733.0  28878.4  28819.0  79.55429626    573
2023-05-06 17:30:00  28875.2  28883.2  28789.2  28803.8  28817.6  16.42856836    238
2023-05-06 17:45:00  28803.8  28810.9  28800.0  28807.4  28803.7   0.22000144     52

[720 rows x 7 columns]

>>> last_20_close_prices
[29141.2, 29104.5, 29069.1, 28982.2, 28979.2, 28740.1, 28670.1, 28662.8, 28647.7, 28590.1, 28626.6, 28666.5, 28630.3, 28710.0, 28740.1, 28710.1, 28760.1, 28733.1, 28878.4, 28803.8]

>>> last_20_candles
                        open     high      low    close     vwap        volume  count
time
2023-05-06 12:45:00  29109.2  29142.5  29051.6  29141.2  29084.2   39.59896757    326
2023-05-06 13:00:00  29139.6  29139.6  29046.2  29104.5  29083.6  141.26224316    665
2023-05-06 13:15:00  29104.5  29104.5  29015.0  29069.1  29047.3  196.75223375    881
2023-05-06 13:30:00  29062.1  29062.1  28949.6  28982.2  28983.4  112.89161354    632
2023-05-06 13:45:00  28982.0  29043.0  28919.6  28979.2  28979.1   40.68199757    451
2023-05-06 14:00:00  28976.2  28976.2  28602.0  28740.1  28709.3  303.95728877   2030
2023-05-06 14:15:00  28730.7  28733.6  28628.1  28670.1  28673.5   79.46272297    617
2023-05-06 14:30:00  28669.4  28734.1  28610.0  28662.8  28656.8   92.00957394    729
2023-05-06 14:45:00  28656.4  28693.8  28629.6  28647.7  28658.5   16.64881712    319
2023-05-06 15:00:00  28643.2  28643.2  28338.0  28590.1  28469.0  233.08117238   1223
2023-05-06 15:15:00  28590.1  28640.6  28540.6  28626.6  28583.3   54.44542700    442
2023-05-06 15:30:00  28629.4  28675.0  28590.0  28666.5  28629.3   12.56857437    373
2023-05-06 15:45:00  28664.4  28670.5  28629.0  28630.3  28650.6   29.61014138    361
2023-05-06 16:00:00  28630.3  28712.0  28597.7  28710.0  28651.1   28.06378914    340
2023-05-06 16:15:00  28710.1  28740.1  28661.7  28740.1  28698.2   34.03577445    354
2023-05-06 16:30:00  28740.1  28740.1  28700.0  28710.1  28714.5   34.78157198    351
2023-05-06 16:45:00  28710.1  28767.1  28710.0  28760.1  28743.2    7.60574838    257
2023-05-06 17:00:00  28760.1  28760.1  28733.0  28733.1  28742.0   10.26238430    210
2023-05-06 17:15:00  28733.1  28890.0  28733.0  28878.4  28819.0   79.55429626    573
2023-05-06 17:30:00  28875.2  28883.2  28789.2  28803.8  28817.6   16.42856836    238
 
As see can look at all data and is not in loop,now easier to start testing out stuff.
Reply


Messages In This Thread
Please help me. - by SuchUmami - May-05-2023, 05:18 PM
RE: Please help me. - by deanhystad - May-05-2023, 05:29 PM
RE: Please help me. - by SuchUmami - May-05-2023, 05:32 PM
RE: Please help me. - by deanhystad - May-05-2023, 07:11 PM
RE: Please help me. - by SuchUmami - May-05-2023, 07:22 PM
RE: Please help me. - by SuchUmami - May-05-2023, 07:56 PM
RE: Please help me. - by deanhystad - May-05-2023, 08:34 PM
RE: Please help me. - by SuchUmami - May-06-2023, 10:27 AM
RE: Please help me. - by snippsat - May-06-2023, 05:53 PM
RE: Please help me. - by SuchUmami - May-07-2023, 01:44 PM
RE: Please help me. - by snippsat - May-07-2023, 04:24 PM
RE: Please help me. - by SuchUmami - May-08-2023, 09:19 AM
RE: Please help me. - by snippsat - May-08-2023, 01:58 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020