1

[已解決]期貨當沖資料資料寫入疑問

https://ithelp.ithome.com.tw/questions/10199175

``````import pandas as pd
import numpy as np

df = pd.DataFrame(df)

A = np.array(df[:300]['Date'])
B = np.array(df[:300]['Time'])
C = np.array(df[:300]['Open'])
D = np.array(df[:300]['High'])
E = np.array(df[:300]['Low'])
F = np.array(df[:300]['Close'])
G = np.array(df[:300]['Volume'])

H1,H2= [] ,[]

for i in range(0, 300) :
for x in range(i , 300) :
if df.at[x, 'High'] - df.at[i, 'Open'] >=20 :
H1.append(B[x])
H2.append(20)
break

elif df.at[i, 'Open'] - df.at[x, 'Low'] >=20 :
H1.append(B[x])
H2.append(-20)
break

data_dist = {
"Date":A,
"Time":B,
"Open":C,
"High":D, "Low":E,"Close":F,"Volume":G,
"損益時間20 20":H1,"損益20 20":H2}

data_df = pd.DataFrame.from_dict(data_dist, orient='index')

data_df1 = data_df.T

print(data_df1)

#data_df1.to_csv('20200102+-全.csv', encoding='utf_8_sig')
``````

H1.append(B[x])
H2.append(20)

``````import pandas as pd
import numpy as np

df = pd.DataFrame(df)

A = np.array(df[:70500]['Date'])
B = np.array(df[:70500]['Time'])
C = np.array(df[:70500]['Open'])
D = np.array(df[:70500]['High'])
E = np.array(df[:70500]['Low'])
F = np.array(df[:70500]['Close'])
G = np.array(df[:70500]['Volume'])

H1,H2= [] ,[]

for d in range (0,235):
for i in range(0+d*300, 300+d*300) :
for x in range(i , 300+d*300 ) :
if df.at[x, 'High'] - df.at[i, 'Open'] >=20 :
H1.append(B[x])
H2.append(20)
break

elif df.at[i, 'Open'] - df.at[x, 'Low'] >=20 :
H1.append(B[x])
H2.append(-20)
break

data_dist = {
"Date":A,
"Time":B,
"Open":C,
"High":D, "Low":E,"Close":F,"Volume":G,
"損益時間20 20":H1,"損益20 20":H2}

data_df = pd.DataFrame.from_dict(data_dist, orient='index')

data_df1 = data_df.T

print(data_df1)

#data_df1.to_csv('2018+-全.csv', encoding='utf_8_sig')
``````

``````import pandas as pd
import numpy as np

df = pd.DataFrame(df)

A = np.array(df[:70500]['Date'])
B = np.array(df[:70500]['Time'])
C = np.array(df[:70500]['Open'])
D = np.array(df[:70500]['High'])
E = np.array(df[:70500]['Low'])
F = np.array(df[:70500]['Close'])
G = np.array(df[:70500]['Volume'])

H1,H2= [] ,[]
data_dist = {
"Date":A,
"Time":B,
"Open":C,
"High":D, "Low":E,"Close":F,"Volume":G,
"損益時間20 20":H1,"損益20 20":H2}

data_df = pd.DataFrame.from_dict(data_dist, orient='index')

data_df1 = data_df.T
print(data_df1)

rowmax = 300
for d in range(0,235):
for i in range(0+d*300, 300+d*300):
for x in range(i , 300+d*300 ):
if data_df1.at[x, 'High'] - data_df1.at[i, 'Open'] >=20 :
data_df1.at[i, '損益時間20 20'] = B[x]
data_df1.at[i,'損益20 20']  =  20
break

elif data_df1.at[i, 'Open'] - data_df1.at[x, 'Low'] >=20 :
data_df1.at[i, '損益時間20 20'] = B[x]
data_df1.at[i,'損益20 20']  =  -20
break
print(data_df1)
data_df1.to_csv('2018+-全12.csv', encoding='utf_8_sig')
``````

1 個回答

1
rogeryao
iT邦高手 1 級 ‧ 2020-07-30 21:38:21

``````rowmax = 300
for d in range(0,235):
for i in range(d*rowmax,(d+1)*rowmax-1):
for x in range(i+1,(d+1)*rowmax):
``````

``````import pandas as pd
import numpy as np

df = pd.DataFrame(df)

A = np.array(df[:70500]['Date'])
B = np.array(df[:70500]['Time'])
C = np.array(df[:70500]['Open'])
D = np.array(df[:70500]['High'])
E = np.array(df[:70500]['Low'])
F = np.array(df[:70500]['Close'])
G = np.array(df[:70500]['Volume'])
H = np.array(df[:70500]['ActDate'])
I = np.array(df[:70500]['ActTime'])
J = np.array(df[:70500]['Value'])

rowmax = 30
for d in range(0,2):
for i in range(d*rowmax,(d+1)*rowmax-1):
for x in range(i+1,(d+1)*rowmax):
if df.at[x, 'High'] - df.at[i, 'Open'] >=20 :
H[i]=df.at[x, 'Date']
I[i]=df.at[x, 'Time']
J[i]=20
break
elif df.at[i, 'Open'] - df.at[x, 'Low'] >=20 :
H[i]=df.at[x, 'Date']
I[i]=df.at[x, 'Time']
J[i]=-20
break

data_dist = {
"Date":A,
"Time":B,
"Open":C,
"High":D,
"Low":E,
"Close":F,
"Volume":G,
"ActDate":H,
"ActTime":I,
"Value":J
}

data_df = pd.DataFrame.from_dict(data_dist, orient='index')

data_df1 = data_df.T

print(data_df1)

#data_df1.to_csv('20200102+-全.csv', encoding='utf_8_sig')
``````

Demo

``````File "<ipython-input-5-4691a8a2fb5e>", line 19
for i in range(d*rowmax, (d+1)*rowmax - 2) :
^
IndentationError: expected an indented block
``````
hokou iT邦新手 5 級 ‧ 2020-07-31 09:23:40 檢舉

https://repl.it/repls/MisguidedDimgreyMemwatch#main.py

H1.append(B[x])
H2.append(20)

listennn08

(學習了許多感謝rogeryao大大)

``````import pandas as pd
import numpy as np

df = pd.DataFrame(df)

A = np.array(df[:70500]['Date'])
B = np.array(df[:70500]['Time'])
C = np.array(df[:70500]['Open'])
D = np.array(df[:70500]['High'])
E = np.array(df[:70500]['Low'])
F = np.array(df[:70500]['Close'])
G = np.array(df[:70500]['Volume'])

H1,H2= [] ,[]
data_dist = {
"Date":A,
"Time":B,
"Open":C,
"High":D, "Low":E,"Close":F,"Volume":G,
"損益時間20 20":H1,"損益20 20":H2}

data_df = pd.DataFrame.from_dict(data_dist, orient='index')

data_df1 = data_df.T
print(data_df1)

rowmax = 300
for d in range(0,235):
for i in range(0+d*300, 300+d*300):
for x in range(i , 300+d*300 ):
if data_df1.at[x, 'High'] - data_df1.at[i, 'Open'] >=20 :
data_df1.at[i, '損益時間20 20'] = B[x]
data_df1.at[i,'損益20 20']  =  20
break

elif data_df1.at[i, 'Open'] - data_df1.at[x, 'Low'] >=20 :
data_df1.at[i, '損益時間20 20'] = B[x]
data_df1.at[i,'損益20 20']  =  -20
break
print(data_df1)
data_df1.to_csv('2018+-全12.csv', encoding='utf_8_sig')
``````

rogeryao

``````rowmax = 5
for d in range(0,2):
for i in range(d*rowmax,(d+1)*rowmax-1):
for x in range(i+1,(d+1)*rowmax):
print(d,i,x)
``````

``````0 0 1
0 0 2
0 0 3
0 0 4
0 1 2
0 1 3
0 1 4
0 2 3
0 2 4
0 3 4
1 5 6
1 5 7
1 5 8
1 5 9
1 6 7
1 6 8
1 6 9
1 7 8
1 7 9
1 8 9
``````

``````for d in range(0,2):
for i in range(d*rowmax,(d+1)*rowmax):
for x in range(i,(d+1)*rowmax):
print(d,i,x)
``````

``````0 0 0
0 0 1
0 0 2
0 0 3
0 0 4
0 1 1
0 1 2
0 1 3
0 1 4
0 2 2
0 2 3
0 2 4
0 3 3
0 3 4
0 4 4
1 5 5
1 5 6
1 5 7
1 5 8
1 5 9
1 6 6
1 6 7
1 6 8
1 6 9
1 7 7
1 7 8
1 7 9
1 8 8
1 8 9
1 9 9
``````

rogeryao iT邦高手 1 級 ‧ 2020-07-31 21:44:57 檢舉

https://ithelp.ithome.com.tw/questions/10199175

A[0]-20 >= C

high low是期間
close 是收盤最尾巴紀錄

rogeryao