File size: 9,556 Bytes
8a420b0
 
 
 
1c5e919
8e56885
8a420b0
 
c1f9f48
8a420b0
 
 
 
 
 
 
 
 
 
 
1c5e919
c1f9f48
8a420b0
 
c1f9f48
8a420b0
 
1c5e919
8a420b0
 
1c5e919
8a420b0
 
 
1c5e919
8a420b0
 
 
 
 
 
 
 
8e56885
8a420b0
 
 
 
 
 
 
1c5e919
8a420b0
1c5e919
8a420b0
 
 
 
1c5e919
8a420b0
 
1c5e919
8a420b0
 
 
 
 
1c5e919
8a420b0
 
1c5e919
8a420b0
 
8e56885
8a420b0
 
 
 
8e56885
8a420b0
8e56885
8a420b0
 
 
 
 
 
 
8e56885
8a420b0
 
 
8e56885
8a420b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c5e919
8a420b0
 
1c5e919
8a420b0
 
 
 
1c5e919
8a420b0
 
 
 
 
1c5e919
8a420b0
 
1c5e919
8a420b0
 
 
1c5e919
8a420b0
 
1c5e919
8a420b0
8e56885
8a420b0
 
 
 
 
8e56885
8a420b0
 
 
 
 
 
 
 
 
 
 
8e56885
1c5e919
8a420b0
 
 
8e56885
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
from flask import Flask, render_template, request, redirect, url_for, jsonify, flash
from db import signals as signals_module
from db import deeper as deeper_module
from db import paires as paires_module
import json

app = Flask(__name__)
app.secret_key = 'trading_signals_secret_key'

@app.route('/')
def index():
    # Fetch signals from GitHub
    signals_response = signals_module.fetch_json_from_github()
    deeper_response = deeper_module.fetch_json_from_github()
    paires_response = paires_module.fetch_json_from_github()

    signals_data = []
    system_status = {
        'deeper': False,
        'paires': False
    }

    if signals_response['success']:
        signals_data = signals_response['data']

    if deeper_response['success']:
        system_status['deeper'] = deeper_response['data'].get('status', False)

    if paires_response['success']:
        system_status['paires'] = paires_response['data'].get('status', False)

    return render_template('index.html',
                          signals=signals_data,
                          system_status=system_status)

@app.route('/signals', methods=['GET'])
def get_signals():
    # Fetch signals from GitHub
    response = signals_module.fetch_json_from_github()
    if response['success']:
        return jsonify(response['data'])
    else:
        return jsonify({"error": response['message']}), 500

@app.route('/signals/delete/<int:signal_id>', methods=['POST'])
def delete_signal(signal_id):
    # Get current signals
    response = signals_module.fetch_json_from_github()
    if not response['success']:
        flash('Failed to fetch signals data', 'error')
        return redirect(url_for('index'))

    signals_data = response['data']

    # Check if signal_id is valid
    if signal_id < 0 or signal_id >= len(signals_data):
        flash('Invalid signal ID', 'error')
        return redirect(url_for('index'))

    # Remove the signal
    signals_data.pop(signal_id)

    # Update the signals.json file
    auth_token, commit_oid = signals_module.fetch_authenticity_token_and_commit_oid()
    if not auth_token or not commit_oid:
        flash('Failed to get authentication tokens', 'error')
        return redirect(url_for('index'))

    # Convert the updated signals data to JSON string
    new_content = json.dumps(signals_data, ensure_ascii=False)

    # Update the signals.json file
    update_response = signals_module.update_user_json_file(auth_token, commit_oid, new_content)

    if update_response['success']:
        flash('Signal deleted successfully', 'success')
    else:
        flash(f'Failed to delete signal: {update_response["message"]}', 'error')

    return redirect(url_for('index'))

@app.route('/signals/delete_all', methods=['POST'])
def delete_all_signals():
    # Get authentication tokens
    auth_token, commit_oid = signals_module.fetch_authenticity_token_and_commit_oid()
    if not auth_token or not commit_oid:
        flash('Failed to get authentication tokens', 'error')
        return redirect(url_for('index'))

    # Create an empty array for signals
    empty_signals = []
    new_content = json.dumps(empty_signals, ensure_ascii=False)

    # Update the signals.json file
    update_response = signals_module.update_user_json_file(auth_token, commit_oid, new_content)

    if update_response['success']:
        flash('All signals deleted successfully', 'success')
    else:
        flash(f'Failed to delete all signals: {update_response["message"]}', 'error')

    return redirect(url_for('index'))

@app.route('/signals/edit/<int:signal_id>', methods=['GET', 'POST'])
def edit_signal(signal_id):
    # Get current signals
    response = signals_module.fetch_json_from_github()
    if not response['success']:
        flash('Failed to fetch signals data', 'error')
        return redirect(url_for('index'))

    signals_data = response['data']

    # Check if signal_id is valid
    if signal_id < 0 or signal_id >= len(signals_data):
        flash('Invalid signal ID', 'error')
        return redirect(url_for('index'))

    if request.method == 'POST':
        # Update signal with form data
        signals_data[signal_id]['pair'] = request.form['pair']
        signals_data[signal_id]['timeframe'] = request.form['timeframe']
        signals_data[signal_id]['type'] = request.form['type']
        signals_data[signal_id]['entry'] = request.form['entry']
        signals_data[signal_id]['stop_loss'] = request.form['stop_loss']
        signals_data[signal_id]['take_profit'] = request.form['take_profit']
        signals_data[signal_id]['duration'] = request.form['duration']
        signals_data[signal_id]['reason'] = request.form['reason']
        signals_data[signal_id]['status'] = request.form['status']

        # Get authentication tokens
        auth_token, commit_oid = signals_module.fetch_authenticity_token_and_commit_oid()
        if not auth_token or not commit_oid:
            flash('Failed to get authentication tokens', 'error')
            return redirect(url_for('index'))

        # Convert the updated signals data to JSON string
        new_content = json.dumps(signals_data, ensure_ascii=False)

        # Update the signals.json file
        update_response = signals_module.update_user_json_file(auth_token, commit_oid, new_content)

        if update_response['success']:
            flash('Signal updated successfully', 'success')
            return redirect(url_for('index'))
        else:
            flash(f'Failed to update signal: {update_response["message"]}', 'error')
            return redirect(url_for('index'))

    # GET request - display edit form with current signal data
    return render_template('edit_signal.html', signal=signals_data[signal_id], signal_id=signal_id)

@app.route('/signals/add', methods=['GET', 'POST'])
def add_signal():
    if request.method == 'POST':
        # Get current signals
        response = signals_module.fetch_json_from_github()
        if not response['success']:
            flash('Failed to fetch signals data', 'error')
            return redirect(url_for('index'))

        signals_data = response['data']

        # Create new signal from form data
        new_signal = {
            'pair': request.form['pair'],
            'timeframe': request.form['timeframe'],
            'type': request.form['type'],
            'entry': request.form['entry'],
            'stop_loss': request.form['stop_loss'],
            'take_profit': request.form['take_profit'],
            'duration': request.form['duration'],
            'reason': request.form['reason'],
            'status': request.form['status']
        }

        # Add new signal to the beginning of the list
        signals_data.insert(0, new_signal)

        # Get authentication tokens
        auth_token, commit_oid = signals_module.fetch_authenticity_token_and_commit_oid()
        if not auth_token or not commit_oid:
            flash('Failed to get authentication tokens', 'error')
            return redirect(url_for('index'))

        # Convert the updated signals data to JSON string
        new_content = json.dumps(signals_data, ensure_ascii=False)

        # Update the signals.json file
        update_response = signals_module.update_user_json_file(auth_token, commit_oid, new_content)

        if update_response['success']:
            flash('Signal added successfully', 'success')
            return redirect(url_for('index'))
        else:
            flash(f'Failed to add signal: {update_response["message"]}', 'error')
            return redirect(url_for('index'))

    # GET request - display add form
    return render_template('add_signal.html')

@app.route('/system/toggle', methods=['POST'])
def toggle_system():
    system_type = request.form.get('system_type')
    new_status = request.form.get('status') == 'true'

    if system_type == 'deeper':
        # Update deeper.json
        response = deeper_module.fetch_json_from_github()
        if not response['success']:
            return jsonify({"success": False, "message": "Failed to fetch deeper.json data"}), 500

        deeper_data = response['data']
        deeper_data['status'] = new_status

        auth_token, commit_oid = deeper_module.fetch_authenticity_token_and_commit_oid()
        if not auth_token or not commit_oid:
            return jsonify({"success": False, "message": "Failed to get authentication tokens"}), 500

        new_content = json.dumps(deeper_data, ensure_ascii=False)
        update_response = deeper_module.update_user_json_file(auth_token, commit_oid, new_content)

        return jsonify(update_response)

    elif system_type == 'paires':
        # Update paires.json
        response = paires_module.fetch_json_from_github()
        if not response['success']:
            return jsonify({"success": False, "message": "Failed to fetch paires.json data"}), 500

        paires_data = response['data']
        paires_data['status'] = new_status

        auth_token, commit_oid = paires_module.fetch_authenticity_token_and_commit_oid()
        if not auth_token or not commit_oid:
            return jsonify({"success": False, "message": "Failed to get authentication tokens"}), 500

        new_content = json.dumps(paires_data, ensure_ascii=False)
        update_response = paires_module.update_user_json_file(auth_token, commit_oid, new_content)

        return jsonify(update_response)

    else:
        return jsonify({"success": False, "message": "Invalid system type"}), 400

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860, debug=False)