Python ------ 一起來對網格做下回測

專注的阿熊發表於2021-06-07

max_consume_money = consume_money

for day_up_down in data.index:

     open = data.loc[day_up_down].values[0]

     high = data.loc[day_up_down].values[1]

     low = data.loc[day_up_down].values[2]

     # close = data.loc[day_up_down].values[3]

     # 盤前

     # 如果 opt 為空,沒有任何操作, 基準價 > 開盤價,觸發買入

     # if len(opt) == 0 and benchmark > open:

     if benchmark * (1 - grid) > open:

         # 一手買入 或者 倍數買入

         # 買入

         # 基準價變更

         benchmark = open

         print(day_up_down, " 開盤買入 ", benchmark)

         # 計算的操作

         consume_money += Decimal(benchmark) * Decimal(count)

         if consume_money.compare(max_consume_money) > 0:

             max_consume_money = consume_money

         # 新增記錄

         h = history.History(stock_code, 1, open, count)

         opt.append(h)

         opt_b.append([day_up_down, benchmark, 1])

     elif benchmark * (1 + grid) <= open:

         if len(opt) > 0:

             # 賣出

             # 基準價變更

             benchmark = open

             # 計算的操作

             # 利潤

             temp = float(

                 ((Decimal(benchmark) - Decimal(opt[len(opt) - 1].price)) * count).quantize(Decimal('0.00')))

             profit += temp

             consume_money -= Decimal(benchmark) * Decimal(count)

             print(day_up_down, " 開盤賣出 ", benchmark, opt[len(opt) - 1].price, " 收益 ", temp)

             # 修改記錄

             h = history.History(stock_code, -1, benchmark, count)

             opt.pop()

             opt_s.append([day_up_down, benchmark, -1])

     while benchmark * (1 - grid) >= low:

         # 盤中

         # 一手買入 或者 倍數買入

         # 買入

         # 基準價變更

         benchmark = float(Decimal(benchmark * (1 - grid)).quantize(Decimal('0.000')))

         print(day_up_down, " 盤中買入 ", benchmark)

         # 計算的操作

         consume_money += Decimal(benchmark) * Decimal(count)

         if consume_money.compare(max_consume_money) > 0:

             max_consume_money = consume_money

         # 新增記錄

         h = history.History(stock_code, 1, benchmark, count)

         opt.append(h)

         opt_b.append([day_up_down, benchmark, 1])

     # open = high 開盤價就是最高價的情況 外匯跟單gendan5.com到時候再觸發 low 會多買,這是一個假收益

     # 不會那麼巧吧開盤價跟最高價一樣

     if len(opt) > 0 and open != high:

         while len(opt) > 0 and benchmark * (1 + grid) <= high:

             # 賣出

             # 基準價變更

             benchmark = float(Decimal(benchmark * (1 + grid)).quantize(Decimal('0.000')))

             # 計算的操作

             temp = float(

                 ((Decimal(benchmark) - Decimal(opt[len(opt) - 1].price)) * count)

                     .quantize(Decimal('0.00')))

             profit += temp

             consume_money -= Decimal(benchmark) * Decimal(count)

             print(day_up_down, " 盤中賣出 ", benchmark, opt[len(opt) - 1].price, " 收益 ", temp)

             # 修改記錄

             h = history.History(stock_code, -1, benchmark, count)

             opt.pop()

             opt_s.append([day_up_down, benchmark, -1])


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2775836/,如需轉載,請註明出處,否則將追究法律責任。

相關文章