Iqbaljanitra's picture
Upload 2 files
64d42fd verified
import os
from flask import Flask, request, jsonify, render_template
import pandas as pd
app = Flask(__name__)
UPLOAD_FOLDER = 'uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# Ensure the upload folder exists
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/calculate_profit_csv', methods=['POST'])
def calculate_profit_csv():
if 'file' not in request.files:
return jsonify({'error': 'No file part in the request'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
# Save the file to the upload folder
file_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
file.save(file_path)
# Load and process the CSV file
try:
data = pd.read_csv(file_path, delimiter=',')
data.columns = [
'Time', 'Deal', 'Symbol', 'Type', 'Direction', 'Volume', 'Price', 'Order',
'Commission', 'Fee', 'Swap', 'Profit', 'Balance', 'Comment'
]
result = calculate_profit_from_csv(data)
return jsonify(result)
except Exception as e:
return jsonify({'error': str(e)}), 500
def calculate_profit_from_csv(data):
# Extract deposits and withdrawals based on 'Comment' column
deposits = data[(data['Type'] == 'balance') & (data['Comment'].str.contains('Deposit', na=False))]
withdrawals = data[(data['Type'] == 'balance') & (data['Comment'].str.contains('Withdrawal', na=False))]
if deposits.empty or withdrawals.empty:
return {'error': 'No deposits or withdrawals found in the data'}
# Summing up the 'Profit' column for deposits and withdrawals
jumlah_awal_deposit = deposits['Profit'].str.replace(',', '').astype(float).sum()
total_withdraw = withdrawals['Profit'].str.replace(',', '').astype(float).sum()
# Calculate the profit
profit = total_withdraw - jumlah_awal_deposit
# Calculate the profit percentage
profit_percentage = (profit / jumlah_awal_deposit) * 100
return {
'jumlah_awal_deposit': jumlah_awal_deposit,
'total_withdraw': total_withdraw,
'profit': profit,
'profit_percentage': profit_percentage
}
if __name__ == '__main__':
app.run(debug=True)