Esempio n. 1
0
 def calculate_positions_value(self):
     multipliers = [
         get_multiplier(symbol) for symbol in self._position_amounts.keys()
     ]
     result = 0
     for amount, price, multiplier in zip(self._position_amounts,
                                          self._position_last_sale_prices,
                                          multipliers):
         result += amount * price * multiplier
     return result
Esempio n. 2
0
    def execute_transaction(self, txn):
        # Update Position
        # ----------------
        if 'contract' in txn.__dict__:
            sid = (txn.sid, txn.contract)
        else:
            sid = txn.sid

        position = self.positions[sid]

        position.update(txn)
        self.ensure_position_index(sid)
        self._position_amounts[sid] = position.amount

        # Max Leverage
        # ---------------
        # Calculate the maximum capital used and maximum leverage
        transaction_cost = txn.price * txn.amount
        self.cumulative_capital_used += transaction_cost

        # * #
        # now we update ending_mav and ending_total_value such that the performance tracker doesn't think we
        # profited when in fact we just entered another position.
        # how? just put a negative balance into cash_adjustment equal to the value of the position entered
        self.cash_adjustment -= txn.price * txn.amount * get_multiplier(sid)

        if math.fabs(self.cumulative_capital_used) > self.max_capital_used:
            self.max_capital_used = math.fabs(self.cumulative_capital_used)

            # We want to conveye a level, rather than a precise figure.
            # round to the nearest 5,000 to keep the number easy on the eyes
            self.max_capital_used = self.round_to_nearest(
                self.max_capital_used,
                base=5000
            )

            # we're adding a 10% cushion to the capital used.
            self.max_leverage = 1.1 * \
                                self.max_capital_used / self.starting_mav

        # add transaction to the list of processed transactions
        if self.keep_transactions:
            self.processed_transactions[txn.dt].append(txn)
Esempio n. 3
0
    def execute_transaction(self, txn):
        # Update Position
        # ----------------
        if 'contract' in txn.__dict__:
            sid = (txn.sid, txn.contract)
        else:
            sid = txn.sid

        position = self.positions[sid]

        position.update(txn)
        self.ensure_position_index(sid)
        self._position_amounts[sid] = position.amount

        # Max Leverage
        # ---------------
        # Calculate the maximum capital used and maximum leverage
        transaction_cost = txn.price * txn.amount
        self.cumulative_capital_used += transaction_cost

        # * #
        # now we update ending_mav and ending_total_value such that the performance tracker doesn't think we
        # profited when in fact we just entered another position.
        # how? just put a negative balance into cash_adjustment equal to the value of the position entered
        self.cash_adjustment -= txn.price * txn.amount * get_multiplier(sid)

        if math.fabs(self.cumulative_capital_used) > self.max_capital_used:
            self.max_capital_used = math.fabs(self.cumulative_capital_used)

            # We want to conveye a level, rather than a precise figure.
            # round to the nearest 5,000 to keep the number easy on the eyes
            self.max_capital_used = self.round_to_nearest(
                self.max_capital_used, base=5000)

            # we're adding a 10% cushion to the capital used.
            self.max_leverage = 1.1 * \
                                self.max_capital_used / self.starting_mav

        # add transaction to the list of processed transactions
        if self.keep_transactions:
            self.processed_transactions[txn.dt].append(txn)
Esempio n. 4
0
 def calculate_positions_value(self):
     multipliers = [get_multiplier(symbol) for symbol in self._position_amounts.keys()]
     result = 0
     for amount, price, multiplier in zip(self._position_amounts, self._position_last_sale_prices, multipliers):
         result += amount * price * multiplier
     return result