For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

There are many trades in the log, but it occuers keyError in the analyzer



  • There are many trades in the log, but it occuers keyError.
    analysis["total"]["total"] is 1.
    How can I fix it?
    ![alt text](スクリーンショット 2020-05-15 17.28.13.png image url)



  • without seeing your code, it is hard to tell. ( it is better to paste a text instead of an image).



  • Thank you for your advice. This is my strategy. The analysis does not work, it couldn't count the amount of trades.
    I run get_analysis() and then,
    winrate = analysis.won.total / analysis.total.closed
    It occuers keyError.

    def next(self): # 行ごとに呼び出される

        #print(self.position)
        if self.judge_order_buy():
            trade_amount= int(self.cash/20/self.dataclose)
            self.count+=1
            #if self.position: # ポジションを持っている場合
             #   self.close() #
            self.order=self.buy(size=trade_amount)
            self.sell_unwind.append([len(self),trade_amount,0])
    
        elif self.judge_order_sell():
            trade_amount = int(self.cash/ 20 / self.dataclose)
            self.order = self.sell(trade_amount)
            self.buy_unwind.append([len(self), trade_amount, 0])
        #if self.order.status in [self.order.Canceled, self.order.Margin, self.order.Rejected]:
            #self.broker.cancel(self.order)
        self.unwind_buy_operate()
        self.unwind_sell_operate()
        
    
    
        if Close_all_positions:
            # i = list(range(0, len(self.datas)))
            # for (d, j) in zip(self.datas, i):
            for j, d in enumerate(self.datas):
                if len(d) == d.buflen() - 1:
                    close = self.close(d, exectype=bt.Order.Market)
    

    class strategy_my(myStrategy):
    unwind_days = 20 # 1M
    count=0

    def judge_order_buy(self):
        return self.signal == 1
    
    def judge_order_sell(self):
        return False
    
    def unwind_buy_operate(self, expected_rate=None, loss_cut_rate=None):
        for i, buy in enumerate(self.buy_unwind):
            if buy[0] + self.unwind_days == len(self) and buy[1] > 0:
                # Unwind at the last day
                self.buy(size=buy[1])
                self.buy_unwind[i][1] = 0
    
    def unwind_sell_operate(self, expected_rate=None, loss_cut_rate=None):
        for i, sell in enumerate(self.sell_unwind):
            if sell[0] + self.unwind_days == len(self) and sell[1] > 0:
                # Unwind at the last day
                print(len(self))
                
                if self.position: # ポジションを持っている場合
                
                  self.order=self.close(size=sell[1])
                  self.sell_unwind[i][1] = 0


  • @kumagai said in There are many trades in the log, but it occuers keyError in the analyzer:

    I run get_analysis() and then,
    winrate = analysis.won.total / analysis.total.closed
    It occuers keyError.

    Could you show the code that contains this?



  • Thank you for your replay.
    I found the reason is I didn't use tradeid.
    In my trade, there are buy,buy,sell,sell.
    After I rewrite multitrade version by using tradeid, the bag fixed.
    Thank you


Log in to reply
 

});