Upload 2 files
Browse files- app.py +69 -0
- index.html +61 -0
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from flask import Flask, request, jsonify, render_template
|
3 |
+
import pandas as pd
|
4 |
+
|
5 |
+
app = Flask(__name__)
|
6 |
+
UPLOAD_FOLDER = 'uploads'
|
7 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
8 |
+
|
9 |
+
# Ensure the upload folder exists
|
10 |
+
if not os.path.exists(UPLOAD_FOLDER):
|
11 |
+
os.makedirs(UPLOAD_FOLDER)
|
12 |
+
|
13 |
+
@app.route('/')
|
14 |
+
def index():
|
15 |
+
return render_template('index.html')
|
16 |
+
|
17 |
+
@app.route('/calculate_profit_csv', methods=['POST'])
|
18 |
+
def calculate_profit_csv():
|
19 |
+
if 'file' not in request.files:
|
20 |
+
return jsonify({'error': 'No file part in the request'}), 400
|
21 |
+
|
22 |
+
file = request.files['file']
|
23 |
+
if file.filename == '':
|
24 |
+
return jsonify({'error': 'No selected file'}), 400
|
25 |
+
|
26 |
+
# Save the file to the upload folder
|
27 |
+
file_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
|
28 |
+
file.save(file_path)
|
29 |
+
|
30 |
+
# Load and process the CSV file
|
31 |
+
try:
|
32 |
+
data = pd.read_csv(file_path, delimiter=',')
|
33 |
+
data.columns = [
|
34 |
+
'Time', 'Deal', 'Symbol', 'Type', 'Direction', 'Volume', 'Price', 'Order',
|
35 |
+
'Commission', 'Fee', 'Swap', 'Profit', 'Balance', 'Comment'
|
36 |
+
]
|
37 |
+
|
38 |
+
result = calculate_profit_from_csv(data)
|
39 |
+
return jsonify(result)
|
40 |
+
except Exception as e:
|
41 |
+
return jsonify({'error': str(e)}), 500
|
42 |
+
|
43 |
+
def calculate_profit_from_csv(data):
|
44 |
+
# Extract deposits and withdrawals based on 'Comment' column
|
45 |
+
deposits = data[(data['Type'] == 'balance') & (data['Comment'].str.contains('Deposit', na=False))]
|
46 |
+
withdrawals = data[(data['Type'] == 'balance') & (data['Comment'].str.contains('Withdrawal', na=False))]
|
47 |
+
|
48 |
+
if deposits.empty or withdrawals.empty:
|
49 |
+
return {'error': 'No deposits or withdrawals found in the data'}
|
50 |
+
|
51 |
+
# Summing up the 'Profit' column for deposits and withdrawals
|
52 |
+
jumlah_awal_deposit = deposits['Profit'].str.replace(',', '').astype(float).sum()
|
53 |
+
total_withdraw = withdrawals['Profit'].str.replace(',', '').astype(float).sum()
|
54 |
+
|
55 |
+
# Calculate the profit
|
56 |
+
profit = total_withdraw - jumlah_awal_deposit
|
57 |
+
|
58 |
+
# Calculate the profit percentage
|
59 |
+
profit_percentage = (profit / jumlah_awal_deposit) * 100
|
60 |
+
|
61 |
+
return {
|
62 |
+
'jumlah_awal_deposit': jumlah_awal_deposit,
|
63 |
+
'total_withdraw': total_withdraw,
|
64 |
+
'profit': profit,
|
65 |
+
'profit_percentage': profit_percentage
|
66 |
+
}
|
67 |
+
|
68 |
+
if __name__ == '__main__':
|
69 |
+
app.run(debug=True)
|
index.html
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Calculate Profit from CSV</title>
|
7 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/milligram/1.4.1/milligram.min.css">
|
8 |
+
<style>
|
9 |
+
body {
|
10 |
+
padding: 2rem;
|
11 |
+
}
|
12 |
+
.container {
|
13 |
+
max-width: 600px;
|
14 |
+
margin: auto;
|
15 |
+
}
|
16 |
+
.output {
|
17 |
+
margin-top: 2rem;
|
18 |
+
}
|
19 |
+
</style>
|
20 |
+
</head>
|
21 |
+
<body>
|
22 |
+
<div class="container">
|
23 |
+
<h1>Calculate Profit from CSV</h1>
|
24 |
+
<form id="upload-form">
|
25 |
+
<label for="file">Choose CSV file:</label>
|
26 |
+
<input type="file" id="file" name="file" accept=".csv" required>
|
27 |
+
<button type="submit" class="button-primary">Upload</button>
|
28 |
+
</form>
|
29 |
+
<div class="output" id="output"></div>
|
30 |
+
</div>
|
31 |
+
|
32 |
+
<script>
|
33 |
+
document.getElementById('upload-form').addEventListener('submit', function(event) {
|
34 |
+
event.preventDefault();
|
35 |
+
var formData = new FormData();
|
36 |
+
var fileField = document.querySelector("input[type='file']");
|
37 |
+
formData.append('file', fileField.files[0]);
|
38 |
+
|
39 |
+
fetch('/calculate_profit_csv', {
|
40 |
+
method: 'POST',
|
41 |
+
body: formData
|
42 |
+
})
|
43 |
+
.then(response => response.json())
|
44 |
+
.then(data => {
|
45 |
+
const outputDiv = document.getElementById('output');
|
46 |
+
outputDiv.innerHTML = `
|
47 |
+
<h2>Profit Calculation Result</h2>
|
48 |
+
<p><strong>Initial Deposit Amount:</strong> ${data.jumlah_awal_deposit}</p>
|
49 |
+
<p><strong>Total Withdrawals:</strong> ${data.total_withdraw}</p>
|
50 |
+
<p><strong>Profit:</strong> ${data.profit}</p>
|
51 |
+
<p><strong>Profit Percentage:</strong> ${data.profit_percentage.toFixed(2)}%</p>
|
52 |
+
`;
|
53 |
+
})
|
54 |
+
.catch(error => {
|
55 |
+
const outputDiv = document.getElementById('output');
|
56 |
+
outputDiv.innerHTML = `<p class="error">Error: ${error}</p>`;
|
57 |
+
});
|
58 |
+
});
|
59 |
+
</script>
|
60 |
+
</body>
|
61 |
+
</html>
|