spread_size_strategy.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:cryptocurrency-trader 作者: TobCar 项目源码 文件源码
def process_order_book(self, highest_bid, lowest_ask):
        '''
        Pre: Traders keep track of their balance / assets and do not attempt a trade when they do not
             have the balance to buy with or the assets to sell.
        '''

        # Financial calculations need accurate decimals
        with localcontext() as context:
            context.prec = 8

            # The spread has to be calculated using the values that the strategy will try to use, not what
            # is already being used.
            highest_bid = Decimal(highest_bid)+self.undercut_market_by
            lowest_ask = Decimal(lowest_ask)-self.undercut_market_by

            if self._spread_size_indicator.is_profitable(highest_bid, lowest_ask):

                # Changing current_position causes the trader to vacillate between buying and selling.
                # Traders will not enter a position when the trader does not have the balance/assets
                # so this causes the Trader to wait until their position is exited before they enter the
                # market again - with the opposite position.

                if self.current_position:
                    self.notify_observers(False, lowest_ask)
                    self.current_position = False
                else:
                    self.notify_observers(True, highest_bid)
                    self.current_position = True
                self._first_time_unprofitable = True

            else:

                # Not profitable = hold default position.
                if self.default_position == DefaultPosition.HOLD:
                    if self._first_time_unprofitable:
                        print("Spread is not profitable. Holding.")
                    self.notify_observers(None, -1) # market_value is irrelevant so it will be set to -1
                else:
                    if self.default_position == DefaultPosition.BUY:
                        if self._first_time_unprofitable:
                            print("Spread is not profitable. Holding major currency.")
                        self.notify_observers(None, lowest_ask)
                    elif self.default_position == DefaultPosition.SELL:
                        if self._first_time_unprofitable:
                            print("Spread is not profitable. Holding minor currency.")
                        self.notify_observers(None, highest_bid)
                    self.current_position = self.default_position
                self._first_time_unprofitable = False
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号