Sure! Below is the revised version of your code, incorporating all the features you've requested. I've broken down the changes into sections for clarity.
1. Updated URLs
# urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('buy_signals/', views.buy_signals, name='buy_signals'),
path('sell_signals/', views.sell_signals, name='sell_signals'),
path('sideways_signals/', views.sideways_signals, name='sideways_signals'),
path('export/', views.export_signals, name='export_signals'),
]
2. Views Adjustments
# views.py
import csv
from django.shortcuts import render
from django.http import HttpResponse
from django.core.paginator import Paginator
from .models import Signal
def home(request):
# Get unique symbols
symbols = Signal.objects.values_list('symbol', flat=True).distinct()
# Get the selected symbol or default to BTCUSDT
default_symbol = request.GET.get('symbol', 'BTCUSDT')
# Get top gainers and losers
top_gainers = Signal.objects.all().order_by('-price_change_24h')[:5]
top_losers = Signal.objects.all().order_by('price_change_24h')[:5]
context = {
'symbols': symbols,
'default_symbol': default_symbol,
'top_gainers': top_gainers,
'top_losers': top_losers,
}
return render(request, 'home.html', context)
def buy_signals(request):
signals = Signal.objects.filter(signal_type='BUY')
# Define criteria for 'very bullish' and 'bullish' signals
very_bullish_signals = signals.filter(price_change_24h__gte=5)
bullish_signals = signals.filter(price_change_24h__lt=5)
context = {
'very_bullish_signals': very_bullish_signals,
'bullish_signals': bullish_signals,
}
return render(request, 'buy_signals.html', context)
def sell_signals(request):
signals = Signal.objects.filter(signal_type='SELL')
# Define criteria for 'very bearish' and 'bearish' signals
very_bearish_signals = signals.filter(price_change_24h__lte=-5)
bearish_signals = signals.filter(price_change_24h__gt=-5)
context = {
'very_bearish_signals': very_bearish_signals,
'bearish_signals': bearish_signals,
}
return render(request, 'sell_signals.html', context)
def sideways_signals(request):
signals = Signal.objects.filter(signal_type='SIDEWAYS')
context = {
'sideways_signals': signals,
}
return render(request, 'sideways_signals.html', context)
def export_signals(request):
# Same as before
# ... (Your existing export_signals function)
return response
3. Home Page Template
<!-- templates/home.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Trading Signals - Home</title>
<style>
body {
background-color: black;
color: white;
}
.buttons {
display: flex;
justify-content: center;
margin-top: 20px;
}
.buttons a {
background-color: blue;
color: white;
padding: 20px;
margin: 0 20px;
text-decoration: none;
font-size: 20px;
border-radius: 5px;
}
.chart-container {
width: 80%;
margin: 40px auto;
}
.top-movers {
display: flex;
justify-content: space-around;
margin-top: 40px;
}
.top-movers .column {
width: 45%;
}
.top-movers table {
width: 100%;
border-collapse: collapse;
}
.top-movers th, .top-movers td {
border: 1px solid white;
padding: 8px;
text-align: center;
}
.top-movers th {
background-color: blue;
}
/* Additional styles for better aesthetics */
select {
background-color: black;
color: white;
border: 1px solid white;
padding: 5px;
margin-left: 10px;
}
label {
font-size: 18px;
}
</style>
<!-- Include TradingView library -->
<script type="text/javascript" src="https://s3.tradingview.com/tv.js"></script>
</head>
<body>
<div class="buttons">
<a href="{% url 'buy_signals' %}">Buy Signals</a>
<a href="{% url 'sell_signals' %}">Sell Signals</a>
<a href="{% url 'sideways_signals' %}">Sideways</a>
</div>
<!-- Chart -->
<div class="chart-container">
<h2 style="text-align:center;">Price Chart</h2>
<!-- Symbol selection -->
<form method="get" style="text-align:center;">
<label for="symbol">Select Symbol:</label>
<select name="symbol" id="symbol" onchange="this.form.submit()">
{% for symbol in symbols %}
<option value="{{ symbol }}" {% if symbol == default_symbol %}selected{% endif %}>{{ symbol }}</option>
{% endfor %}
</select>
</form>
<!-- TradingView Advanced Chart Widget -->
<div class="tradingview-widget-container">
<div id="tradingview_chart"></div>
<script type="text/javascript">
new TradingView.widget(
{
"width": "100%",
"height": 500,
"symbol": "{{ default_symbol }}",
"interval": "D",
"timezone": "Etc/UTC",
"theme": "dark",
"style": "1",
"locale": "en",
"toolbar_bg": "#f1f3f6",
"enable_publishing": false,
"hide_side_toolbar": false,
"allow_symbol_change": true,
"container_id": "tradingview_chart"
}
);
</script>
</div>
</div>
<!-- Top Movers -->
<div class="top-movers">
<div class="column">
<h3>Top Gainers (24h)</h3>
<table>
<thead>
<tr>
<th>Symbol</th>
<th>24h % Change</th>
</tr>
</thead>
<tbody>
{% for signal in top_gainers %}
<tr>
<td>{{ signal.symbol }}</td>
<td>{{ signal.price_change_24h }}%</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="column">
<h3>Top Losers (24h)</h3>
<table>
<thead>
<tr>
<th>Symbol</th>
<th>24h % Change</th>
</tr>
</thead>
<tbody>
{% for signal in top_losers %}
<tr>
<td>{{ signal.symbol }}</td>
<td>{{ signal.price_change_24h }}%</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</body>
</html>
4. Buy Signals Page
<!-- templates/buy_signals.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Buy Signals</title>
<style>
body {
background-color: black;
color: white;
}
.back-button {
margin: 20px;
}
.back-button a {
background-color: blue;
color: white;
padding: 10px;
text-decoration: none;
border-radius: 5px;
}
.tab {
overflow: hidden;
background-color: black;
color: white;
display: flex;
justify-content: center;
margin: 20px auto;
width: 80%;
}
.tab button {
background-color: inherit;
flex: 1;
border: 1px solid white;
outline: none;
cursor: pointer;
padding: 14px 16px;
text-align:center;
font-size: 17px;
color:white;
}
.tab button.active {
background-color: blue;
}
.tabcontent {
display: none;
padding: 6px 12px;
width: 80%;
margin: 20px auto;
}
table {
width: 100%;
border-collapse: collapse;
color:white;
}
th, td {
border: 1px solid white;
padding: 8px;
text-align: center;
}
th {
background-color: blue;
}
a.chart-link {
color: pink;
text-decoration: underline;
}
</style>
</head>
<body>
<div class="back-button">
<a href="{% url 'home' %}">Back to Home</a>
</div>
<div class="tab">
<button class="tablinks" onclick="openTab(event, 'VeryBullish')">Very Bullish</button>
<button class="tablinks" onclick="openTab(event, 'Bullish')">Bullish</button>
</div>
<div id="VeryBullish" class="tabcontent">
<h2>Very Bullish Signals</h2>
{% include 'signal_table.html' with signals=very_bullish_signals %}
</div>
<div id="Bullish" class="tabcontent">
<h2>Bullish Signals</h2>
{% include 'signal_table.html' with signals=bullish_signals %}
</div>
<script>
function openTab(evt, tabName) {
var i, tabcontent, tablinks;
// Hide all tabcontents
tabcontent = document.getElementsByClassName("tabcontent");
for (i=0; i<tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
// Remove 'active' class from all tablinks
tablinks = document.getElementsByClassName("tablinks");
for (i=0; i<tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active","");
}
// Show the clicked tabcontent and set the button as active
document.getElementById(tabName).style.display = "block";
evt.currentTarget.className += " active";
}
// Set default open tab
document.addEventitor('DOMContentLoaded', function(){
document.querySelector('.tablinks').click();
});
</script>
</body>
</html>
5. Sell Signals Page
<!-- templates/sell_signals.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sell Signals</title>
<style>
/* Similar styles as buy_signals.html */
/* ... */
</style>
</head>
<body>
<div class="back-button">
<a href="{% url 'home' %}">Back to Home</a>
</div>
<div class="tab">
<button class="tablinks" onclick="openTab(event, 'VeryBearish')">Very Bearish</button>
<button class="tablinks" onclick="openTab(event, 'Bearish')">Bearish</button>
</div>
<div id="VeryBearish" class="tabcontent">
<h2>Very Bearish Signals</h2>
{% include 'signal_table.html' with signals=very_bearish_signals %}
</div>
<div id="Bearish" class="tabcontent">
<h2>Bearish Signals</h2>
{% include 'signal_table.html' with signals=bearish_signals %}
</div>
<script>
// Similar script as buy_signals.html
</script>
</body>
</html>
6. Sideways Signals Page
<!-- templates/sideways_signals.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sideways Signals</title>
<style>
/* Similar styles */
</style>
</head>
<body>
<div class="back-button">
<a href="{% url 'home' %}">Back to Home</a>
</div>
<h2 style="text-align:center;">Sideways Signals</h2>
{% include 'signal_table.html' with signals=sideways_signals %}
</body>
</html>
7. Signal Table Template with Chart Link
<!-- templates/signal_table.html -->
<table>
<thead>
<tr>
<th>#</th>
<th>Run Date</th>
<th>Timestamp</th>
<th>Symbol</th>
<th>Price</th>
<th>24h Price Change</th>
<th>Signal Type</th>
<th>Stop Loss</th>
<th>Take Profit</th>
<th>Support</th>
<th>Resistance</th>
<th>Chart</th>
</tr>
</thead>
<tbody>
{% for signal in signals %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ signal.run_date|date:"Y-m-d H:i:s" }}</td>
<td>{{ signal.timestamp|date:"Y-m-d H:i:s" }}</td>
<td>{{ signal.symbol }}</td>
<td>{{ signal.price }}</td>
<td>{{ signal.price_change_24h|default:"N/A" }}</td>
<td>{{ signal.signal_type }}</td>
<td>{{ signal.stop_loss|default:"-" }}</td>
<td>{{ signal.take_profit|default:"-" }}</td>
<td>{{ signal.support|default:"-" }}</td>
<td>{{ signal.resistance|default:"-" }}</td>
<td><a href="{% url 'home' %}?symbol={{ signal.symbol }}" class="chart-link">View Chart</a></td>
</tr>
{% empty %}
<tr>
<td colspan="12">No Signals Available</td>
</tr>
{% endfor %}
</tbody>
</table>
8. Additional Improvements
- Technical Indicators on Chart: By using the TradingView Advanced Chart Widget, users can add indicators manually.
- Responsive Design: The CSS uses percentages for widths to ensure responsiveness.
- Consistent Theme: The colors black, pink, blue, and white are used throughout the site to create a visually appealing theme.
- User Experience: Navigation buttons and links are added to enhance the user experience.
9. Final Notes
- Testing: Ensure that all pages are tested thoroughly to confirm that the links, tabs, and charts work as expected.
- Dynamic Criteria: The criteria for "very bullish" and "bullish" can be adjusted based on your specific logic.
- Styling: Feel free to adjust the CSS to fine-tune the appearance according to your preferences.
I hope this meets all your requirements! Let me know if you need further adjustments.