예제 #1
0
    def calculate_profit(activities, test_only=False, year=None, splits=None):
        buy_action = defaultdict(list)
        if year is None:
            if len(activities) > 0:
                last_year = activities[-1].settle_date.year
            else:
                last_year = 2020
        else:
            last_year = year

        income = Decimal(0)
        cost = Decimal(0)
        context = Context(prec=12, rounding=ROUND_DOWN)
        for activity in activities:
            if activity.type == 'BUY':
                if test_only:
                    rate = 1
                else:
                    rate = exchange_rate(activity.currency.lower(),
                                         activity.settle_date)
                buy_action[activity.symbol].append(
                    (activity.quantity, activity.price * rate))
            else:
                actions = buy_action[activity.symbol]
                actions_q = [q for q, _ in actions]
                sum_quantity = Decimal(0)
                for q in actions_q:
                    sum_quantity = context.add(sum_quantity, q)

                if sum_quantity < activity.quantity:
                    raise AccountException(
                        f"Brakuje cen akcji {activity.symbol} w ilości "
                        f"{activity.quantity - sum_quantity}. Dodaj pliki z brakującymi danymi"
                    )
                else:
                    avg_price = sum([q * p for q, p in actions]) / sum_quantity
                    # calculate profit only for the last year
                    if activity.settle_date.year == last_year:
                        # get exchange rate from nbp
                        if test_only:
                            rate = 1
                        else:
                            rate = exchange_rate(activity.currency.lower(),
                                                 activity.settle_date)
                        income += activity.quantity * activity.price * rate
                        cost += avg_price * activity.quantity

                    current_quantity = activity.quantity
                    for i, item in enumerate(actions):
                        q, p = item
                        if q < current_quantity:
                            current_quantity = current_quantity - q
                            actions[i] = (0, p)
                        else:
                            q -= current_quantity
                            actions[i] = (q, p)
                            current_quantity = 0
        return income, cost, last_year
예제 #2
0
def task_info_page(request):
	context = {}
	task_info = ts.objects.all()[0]
	# task_info = get_list_or_404(ts, theExaminer = request.user).lastest('submitTime')
	today = timezone.now()
	if task_info : 
		last_update = task_info.submitTime
		if ((last_update.year == today.year) and (last_update.month == today.month)) :
			# get and calc task info
			decimalContext = Context(prec = 4)	# set Decimal precision
			# yi tong
			context['yiTongRenWuLiang'] = mt(theExaminer=request.user, year = today.year, month = today.month).objects.monthlyTask
			context['yiChuAn'] = task_info.yiChuAn
			context['xyJian'] = task_info.xyJian
			context['xyLv'] = task_info.xyJian / task_info.yiChuAn
			context['yiTongLv'] = decimalContext.divide(task_info.yiChuAn, Decimal(8.05)) * 100
			#jie an
			context['jieAnRenWuLiang'] = 2
			context['shouQuan'] = task_info.shouQuan
			context['boHui'] = task_info.boHui
			context['shiChe'] = task_info.shiChe
			context['jieAnLiang'] = decimalContext.add( \
						decimalContext.add(context['shouQuan'], context['boHui']), \
						context['shiChe'])
			context['jieAnLv'] = decimalContext.divide(context['jieAnLiang'], Decimal(context['jieAnRenWuLiang'])) * 100
		else:
			# get and calc task info
			decimalContext = Context(prec = 4)	# set Decimal precision
			# yi tong
			context['yiTongRenWuLiang'] = 8.05
			context['yiChuAn'] = 0
			context['xyJian'] = 0
			context['xyLv'] = 0
			context['yiTongLv'] = decimalContext.divide(0, Decimal(8.05)) * 100
			#jie an
			context['jieAnRenWuLiang'] = 2
			context['shouQuan'] = 0
			context['boHui'] = 0
			context['shiChe'] = 0
			context['jieAnLiang'] = 0
			context['jieAnLv'] = 0 * 100
	

	return render(request, 'task_submit/task_info.html', context)
예제 #3
0
class _Amount:
    def __init__(self, fraction_digits: int, precision: int):
        self._ctx = Context(prec=precision, rounding=ROUND_HALF_EVEN)

        self._fraction_digits = fraction_digits
        self._precision = precision
        self._quantizer = self._ctx.create_decimal(1).scaleb(-fraction_digits)

        self._value = self._ctx.create_decimal(0)

    def deserialize(self, serialized_value):
        self._value = (
            self._ctx.create_decimal(serialized_value)
            .scaleb(-self._fraction_digits)
            .quantize(self._quantizer)
        )
        return self

    def serialize(self) -> int:
        return int(self._value.scaleb(self._fraction_digits))

    def set(self, value: Decimal):
        self._value = self._ctx.create_decimal(value).quantize(self._quantizer)
        return self

    def clone(self):
        raise NotImplementedError

    def __str__(self):
        return str(self._value)

    def __imul__(self, other):
        operand = other._value if isinstance(other, Amount) else other
        value = self._ctx.multiply(self._value, operand)
        return self.set(value)

    def __itruediv__(self, other):
        operand = other._value if isinstance(other, Amount) else other
        value = self._ctx.divide(self._value, operand)
        return self.set(value)

    def __mul__(self, other):
        return self.clone().__imul__(other)

    def __truediv__(self, other):
        return self.clone().__itruediv__(other)

    def __iadd__(self, other):
        operand = other._value if isinstance(other, Amount) else other
        value = self._ctx.add(self._value, operand)
        return self.set(value)

    def __add__(self, other):
        return self.clone().__iadd__(other)