如何解决为什么我的函数返回一个空的数据框
嘿,我正在尝试对存储在数据库中的时间序列数据运行我的函数。
从数据库中获取数据后,我运行我的图表功能,它运行没有任何错误。
但是当我尝试调用它时,它返回一个空的数据框。
这是代码
import math
def Rangebaroperation(ticks,rangesize):
#declare rangesize nad all important variables like hightarget and lowtarget & etc.
highTarget = None
lowTarget = None
lastClose = None
rangesize = None
tempHighTarget = None
def createBar(close):
newTick = {
['Open']: int(float(round(lastClose),2)),['Close']: int(float(round(close),['High']: int(round(highTarget),2),['Low']: int(round(lowTarget),}
newTick[float['Close'][-1]] = newTick[float['Open']]
def processMove(ticks,b):
#declare the continuous direction of a trend like upwards or downwards.
global rangesize
global lowTarget
global highTarget
while (1):
if (price < b):
#direction is upward
price = min(b,highTarget)
lowTarget = max(lowTarget,(price - rangesize))
if (b < highTarget): break
elif (price >= b):
#direction is downward
price = max(b,lowTarget)
highTarget = min(highTarget,(price + rangesize))
if (b > lowTarget): break
createBar(price)
resetTargets()
def resetTargets():
for tick in ticks:
global tempHighTarget
global highTarget
global lowTarget
global lastClose
global rangesize
highTarget = price + rangesize
lowTarget = price - rangesize
lastClose = price
for i in rangesize(0,len(tick)):
i=i+1
q = tick[i]
if not q: continue
q1 = tick[i - 1]
if not i:
if q1:
price = q1.newTick['Close']
if (price or price == 0): resetTargets()
if not q1: continue
C = q.newTick['Close']
O = q.newTick['Open']
H = q.newTick['High']
L = q.newTick['Low']
if (not C and C != 0): continue
O = O if O or O == 0 else C
H = H if H or H == 0 else C
L = L if L or L == 0 else C
if (not price and price!= 0):
start = math.floor(O / rangesize) * rangesize
price = O if start.isnumeric() else start
resetTargets()
processMove(q1,O)
if (i): processMove(q,O)
#SHORTEST disTANCE BETWEEN OPEN & EITHER HIGH OR LOW DETERmineS INITIAL DIRECTION.
if (H - O < O - L):
if (H): processMove(q,H)
if (L): processMove(q,L)
else:
if (L): processMove(q,L)
if (H): processMove(q,H)
processMove(q,C)
if (i == len(tick) - 1 and C != lastClose):
tempHighTarget = highTarget
highTarget = lowTarget + rangesize
lowTarget = tempHighTarget - rangesize
createBar(C)
这是我用来从数据库中获取数据并将其转换为我需要的图表模式的代码。
db = sqlite3.connect('Z:/II.IV.XVI/GOALS/INTradAY TradING/TONY_STARK/ALGORITHM FILES/ticks.db')
def get_hist(ticker,db):
token = instrumentLookup(instrument_df,ticker)
data = pd.read_sql('''SELECT * FROM TOKEN%s WHERE ts >= date() - '12 day';''' %token,con=db)
data = data.set_index(['ts'])
data.index = pd.to_datetime(data.index)
ticks = data.loc[:,['price']]
df=ticks['price'].resample('5min').apply(Rangebaroperation,0.4).dropna()
return df
candle_df = pd.DataFrame(get_hist("RELIANCE",db))
这是我在数据框形式的代码中应用的示例输入
ts price
2020-12-09 13:39:12 2021.95
2020-12-09 13:39:13 2021.35
2020-12-09 13:39:14 2021.35
2020-12-09 13:39:15 2021.35
2020-12-09 13:39:16 2022.00
...
2020-12-15 14:15:29 1975.75
2020-12-15 14:15:30 1975.95
2020-12-15 14:15:31 1975.90
2020-12-15 14:15:32 1975.90
2020-12-15 14:15:33 1975.95
这是我希望收到的示例输出(也是数据帧的形式)。
ts open high low close
2020-12-09 13:35:00 2021.95 2022.10 2021.10 2021.30
2020-12-09 13:40:00 2021.30 2024.45 2021.05 2024.00
2020-12-09 13:45:00 2024.00 2024.25 2022.65 2023.95
2020-12-09 13:50:00 2024.00 2024.00 2022.50 2023.80
2020-12-09 13:55:00 2023.85 2025.00 2023.30 2024.90
2020-12-09 14:00:00 2024.85 2027.40 2024.80 2025.80
2020-12-09 14:05:00 2025.85 2029.00 2025.40 2027.00
2020-12-09 14:10:00 2026.65 2033.60 2026.25 2033.25
2020-12-09 14:15:00 2033.05 2033.80 2028.00 2030.70
2020-12-09 14:20:00 2030.35 2031.00 2026.10 2027.00
在这个庞大的 stackoverflow 家族中,谁能帮我找到错误。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。