File size: 2,362 Bytes
64d42fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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)