Ciao a tutti, sono nuovo del forum e non so assolutamente niente di Python, ma devo creare un codice per la dichiarazione dei redditi Crypto.
Non sapendo niente , ho chiesto a chat gpt di crearmi un codice per tale scopo, ma non so come farlo girare, tra l' altro mi da degli errori.
So che devo inserire le mie chiavi al posto di your api key. e nella prova l ho fatto.
incollo qui gli errori ed il codice.
io sto provando a lanciarlo dal prompt dei comandi.
errori:
C:\Temp>python crypto4.py
Traceback (most recent call last):
File "C:\Temp\crypto4.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
CODICE:
import requests
import hmac
import hashlib
import time
from urllib.parse import urlencode
class CryptoTransaction:
def __init__(self, date, type, amount, price):
self.date = date
self.type = type
self.amount = amount
self.price = price
def sign(params, secret):
query_string = urlencode(params)
m = hmac.new(secret.encode('utf-8'), query_string.encode('utf-8'), hashlib.sha256)
return m.hexdigest()
def get_crypto_com_transactions(api_key, api_secret):
base_url = 'https://api.crypto.com/v2/private/get-trades'
timestamp = int(time.time() * 1000) # Corretto il calcolo del timestamp
params = {
'api_key': api_key,
'id': 1,
'method': 'private/get-trades',
'nonce': timestamp
}
params['sig'] = sign(params, api_secret)
headers = {
'Content-Type': 'application/json'
}
response = requests.post(base_url, json=params, headers=headers)
return response.json()['result']['data']
def calculate_fifo(transactions):
holdings = []
total_profit = 0.0
report = []
for tx in transactions:
if tx.type == 'BUY':
holdings.append(tx)
elif tx.type == 'SELL':
amount_to_sell = tx.amount
while amount_to_sell > 0:
buy_tx = holdings.pop(0)
if buy_tx.amount <= amount_to_sell:
profit = (tx.price - buy_tx.price) * buy_tx.amount
total_profit += profit
report.append((tx.date, profit))
amount_to_sell -= buy_tx.amount
else:
profit = (tx.price - buy_tx.price) * amount_to_sell
total_profit += profit
report.append((tx.date, profit))
buy_tx.amount -= amount_to_sell
holdings.insert(0, buy_tx)
amount_to_sell = 0
return total_profit, report
def generate_report(report):
print("Date\t\tProfit")
for entry in report:
print(f"{entry[0]}\t{entry[1]:.2f}")
print("\nTotal Profit: {:.2f}".format(sum([entry[1] for entry in report])))
if __name__ == "__main__":
api_key = 'YOUR_API_KEY'
api_secret = 'YOUR_API_SECRET'
raw_transactions = get_crypto_com_transactions(api_key, api_secret)
transactions = []
for tx in raw_transactions:
date = tx['create_time']
type = 'BUY' if tx['side'] == 'BUY' else 'SELL'
amount = float(tx['quantity'])
price = float(tx['price'])
transactions.append(CryptoTransaction(date, type, amount, price))
total_profit, report = calculate_fifo(transactions)
generate_report(report)