Skip to content

Commit

Permalink
use Decimal to solve 0.0024 issue
Browse files Browse the repository at this point in the history
  • Loading branch information
msaltnet committed Sep 17, 2023
1 parent 052339e commit 2fff47b
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions smtm/strategy_sma_ml.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import copy
import math
from datetime import datetime
from decimal import Decimal, ROUND_DOWN
from sklearn.linear_model import LinearRegression
import pandas as pd
import numpy as np
Expand Down Expand Up @@ -315,10 +316,10 @@ def __create_buy(self):
budget -= budget * self.COMMISSION_RATIO
price = float(self.data[-1]["closing_price"])
amount = budget / price
final_value = amount * price

# 소숫점 4자리 아래 버림
amount = math.floor(amount * 10000) / 10000
final_value = amount * price
amount = Decimal(str(amount)).quantize(Decimal("0.0001"), rounding=ROUND_DOWN)

if self.min_price > budget or self.process_unit[0] <= 0 or final_value > self.balance:
self.logger.info(f"target_budget is too small or invalid unit {self.process_unit}")
Expand All @@ -334,8 +335,8 @@ def __create_buy(self):
return {
"id": DateConverter.timestamp_id(),
"type": "buy",
"price": price,
"amount": amount,
"price": str(price),
"amount": str(amount.normalize()),
}

def __create_sell(self):
Expand All @@ -344,9 +345,9 @@ def __create_sell(self):
amount = self.asset_amount

# 소숫점 4자리 아래 버림
amount = math.floor(amount * 10000) / 10000
amount = Decimal(str(amount)).quantize(Decimal("0.0001"), rounding=ROUND_DOWN)

price = float(self.data[-1]["closing_price"])
price = Decimal(self.data[-1]["closing_price"])
total_value = price * amount

if amount <= 0 or total_value < self.min_price:
Expand All @@ -363,6 +364,6 @@ def __create_sell(self):
return {
"id": DateConverter.timestamp_id(),
"type": "sell",
"price": price,
"amount": amount,
"price": str(price.normalize()),
"amount": str(amount.normalize()),
}

0 comments on commit 2fff47b

Please sign in to comment.