根據上篇實際跑出來的結果,發現有些明明已經獲利但獲利回吐,所以適時的停損停利才能保持獲利相對最大化
position = 0
position_price = 0
reward = 0
reward_list = []
sig = []
total_reward = 0
hold_reward = []
ROI = []
for i in range(1,300):
price = df['Close'][i]
if position == 1:
reward = price - position_price
elif position == -1:
reward = position_price - price
else:
reward = 0
reward_list.append(reward)
print("ts =", df['ts'][i], "position_price =", position_price, "now_price =", price, "reward =", reward, "total_reward =", total_reward)
# 5MA往上穿越10MA call
if ma_dif[i-1] < 0 and ma_dif[i] > 0:
if position == 0: # call
position += 1
sig.append(1)
position_price = price
hold_reward.append(reward)
print("ts =", df['ts'][i], "buy =", price)
elif position == -1: # close
position += 1
sig.append(1)
reward = position_price - price
total_reward += reward
reward_list.clear()
ROI.append(reward/position_price)
position_price = 0
print("ts =", df['ts'][i], "buy =", price, "reward =", reward)
# 5MA往下穿越10MA put
elif ma_dif[i-1] > 0 and ma_dif[i] < 0:
if position == 1: # close
position -= 1
sig.append(-1)
reward = price - position_price
total_reward += reward
reward_list.clear()
ROI.append(reward/position_price)
position_price = 0
print("ts =", df['ts'][i], "sell =", price, "reward =", reward)
elif position == 0: # put
position -= 1
sig.append(-1)
position_price = price
print("ts =", df['ts'][i], "sell =", price)
else:
for j in range(1,len(reward_list)):
if (reward_list[j] < reward_list[j-1]) and (reward_list[j] < max(reward_list)):
if position == 1: # close
position -= 1
sig.append(-1)
reward = price - position_price
total_reward += reward
reward_list.clear()
ROI.append(reward/position_price)
position_price = 0
print("ts =", df['ts'][i], "sell =", price, "reward =", reward)
elif position == -1: # close
position += 1
sig.append(1)
reward = position_price - price
total_reward += reward
reward_list.clear()
ROI.append(reward/position_price)
position_price = 0
print("ts =", df['ts'][i], "buy =", price, "reward =", reward)
sig.append(0)
成果似乎還可以在精進
ts = 2021-09-01 02:48:00 position_price = 0 now_price = 17325.0 reward = 0 total_reward = 0
ts = 2021-09-01 02:54:00 position_price = 0 now_price = 17325.0 reward = 0 total_reward = 0
ts = 2021-09-01 03:11:00 position_price = 0 now_price = 17321.0 reward = 0 total_reward = 0
ts = 2021-09-01 03:23:00 position_price = 0 now_price = 17320.0 reward = 0 total_reward = 0
ts = 2021-09-01 03:54:00 position_price = 0 now_price = 17341.0 reward = 0 total_reward = 0
ts = 2021-09-01 03:57:00 position_price = 0 now_price = 17345.0 reward = 0 total_reward = 0
ts = 2021-09-01 03:58:00 position_price = 0 now_price = 17352.0 reward = 0 total_reward = 0
ts = 2021-09-01 04:22:00 position_price = 0 now_price = 17343.0 reward = 0 total_reward = 0
ts = 2021-09-01 04:36:00 position_price = 0 now_price = 17342.0 reward = 0 total_reward = 0
ts = 2021-09-01 04:58:00 position_price = 0 now_price = 17333.0 reward = 0 total_reward = 0
ts = 2021-09-01 05:00:00 position_price = 0 now_price = 17332.0 reward = 0 total_reward = 0
ts = 2021-09-01 08:46:00 position_price = 0 now_price = 17342.0 reward = 0 total_reward = 0
ts = 2021-09-01 08:47:00 position_price = 0 now_price = 17346.0 reward = 0 total_reward = 0
ts = 2021-09-01 08:47:00 sell = 17346.0
ts = 2021-09-01 08:48:00 position_price = 17346.0 now_price = 17342.0 reward = 4.0 total_reward = 0
ts = 2021-09-01 08:49:00 position_price = 17346.0 now_price = 17341.0 reward = 5.0 total_reward = 0
ts = 2021-09-01 08:50:00 position_price = 17346.0 now_price = 17337.0 reward = 9.0 total_reward = 0
ts = 2021-09-01 08:50:00 buy = 17337.0 reward = 9.0
ts = 2021-09-01 08:51:00 position_price = 0 now_price = 17340.0 reward = 0 total_reward = 9.0
ts = 2021-09-01 08:52:00 position_price = 0 now_price = 17339.0 reward = 0 total_reward = 9.0
ts = 2021-09-01 08:53:00 position_price = 0 now_price = 17342.0 reward = 0 total_reward = 9.0
ts = 2021-09-01 08:54:00 position_price = 0 now_price = 17333.0 reward = 0 total_reward = 9.0
ts = 2021-09-01 08:54:00 sell = 17333.0
ts = 2021-09-01 08:55:00 position_price = 17333.0 now_price = 17323.0 reward = 10.0 total_reward = 9.0
ts = 2021-09-01 08:56:00 position_price = 17333.0 now_price = 17309.0 reward = 24.0 total_reward = 9.0
ts = 2021-09-01 08:57:00 position_price = 17333.0 now_price = 17311.0 reward = 22.0 total_reward = 9.0
ts = 2021-09-01 08:57:00 buy = 17311.0 reward = 22.0
ts = 2021-09-01 08:58:00 position_price = 0 now_price = 17317.0 reward = 0 total_reward = 31.0
ts = 2021-09-01 08:59:00 position_price = 0 now_price = 17327.0 reward = 0 total_reward = 31.0
ts = 2021-09-01 09:00:00 position_price = 0 now_price = 17320.0 reward = 0 total_reward = 31.0
ts = 2021-09-01 09:01:00 position_price = 0 now_price = 17324.0 reward = 0 total_reward = 31.0
ts = 2021-09-01 09:02:00 position_price = 0 now_price = 17333.0 reward = 0 total_reward = 31.0
ts = 2021-09-01 09:02:00 buy = 17333.0
ts = 2021-09-01 09:03:00 position_price = 17333.0 now_price = 17342.0 reward = 9.0 total_reward = 31.0
ts = 2021-09-01 09:04:00 position_price = 17333.0 now_price = 17355.0 reward = 22.0 total_reward = 31.0
ts = 2021-09-01 09:05:00 position_price = 17333.0 now_price = 17356.0 reward = 23.0 total_reward = 31.0
ts = 2021-09-01 09:07:00 position_price = 17333.0 now_price = 17350.0 reward = 17.0 total_reward = 31.0
ts = 2021-09-01 09:07:00 sell = 17350.0 reward = 17.0
ts = 2021-09-01 09:08:00 position_price = 0 now_price = 17372.0 reward = 0 total_reward = 48.0
ts = 2021-09-01 09:09:00 position_price = 0 now_price = 17373.0 reward = 0 total_reward = 48.0
ts = 2021-09-01 09:10:00 position_price = 0 now_price = 17375.0 reward = 0 total_reward = 48.0
ts = 2021-09-01 09:11:00 position_price = 0 now_price = 17380.0 reward = 0 total_reward = 48.0
ts = 2021-09-01 09:12:00 position_price = 0 now_price = 17389.0 reward = 0 total_reward = 48.0
ts = 2021-09-01 09:13:00 position_price = 0 now_price = 17392.0 reward = 0 total_reward = 48.0
ts = 2021-09-01 09:14:00 position_price = 0 now_price = 17390.0 reward = 0 total_reward = 48.0
ts = 2021-09-01 09:15:00 position_price = 0 now_price = 17376.0 reward = 0 total_reward = 48.0
ts = 2021-09-01 09:16:00 position_price = 0 now_price = 17364.0 reward = 0 total_reward = 48.0
ts = 2021-09-01 09:17:00 position_price = 0 now_price = 17367.0 reward = 0 total_reward = 48.0
ts = 2021-09-01 09:18:00 position_price = 0 now_price = 17382.0 reward = 0 total_reward = 48.0
ts = 2021-09-01 09:19:00 position_price = 0 now_price = 17401.0 reward = 0 total_reward = 48.0
ts = 2021-09-01 09:20:00 position_price = 0 now_price = 17400.0 reward = 0 total_reward = 48.0
ts = 2021-09-01 09:21:00 position_price = 0 now_price = 17384.0 reward = 0 total_reward = 48.0
接下來串個 backtrader 跑個長時間資料看看這個策略是否能長期穩定獲利