208 lines
6.0 KiB
Python
208 lines
6.0 KiB
Python
from django.shortcuts import render
|
|
from django.http import JsonResponse
|
|
from .models import BitcoinPrice
|
|
from datetime import datetime, timezone
|
|
import requests
|
|
|
|
|
|
|
|
|
|
def fetch_bitcoin_price(request):
|
|
"""Fetch current Bitcoin price from CoinGecko API and save it."""
|
|
try:
|
|
# Simple API call to CoinGecko
|
|
response = requests.get(
|
|
'https://api.coingecko.com/api/v3/simple/price',
|
|
params={'ids': 'bitcoin', 'vs_currencies': 'usd'}
|
|
)
|
|
data = response.json()
|
|
|
|
if 'bitcoin' in data:
|
|
price = data['bitcoin']['usd']
|
|
|
|
# Save to database
|
|
BitcoinPrice.objects.create(
|
|
price_usd=price,
|
|
timestamp=datetime.now(timezone.utc)
|
|
)
|
|
analyzer = MarketAnalyzer()
|
|
analyzer.analyze_market('hourly')
|
|
return JsonResponse({
|
|
'success': True,
|
|
'price': price,
|
|
'message': f'Price saved: ${price}'
|
|
})
|
|
else:
|
|
return JsonResponse({
|
|
'success': False,
|
|
'message': 'Failed to fetch price'
|
|
})
|
|
|
|
except Exception as e:
|
|
return JsonResponse({
|
|
'success': False,
|
|
'message': f'Error: {str(e)}'
|
|
})
|
|
|
|
from monitor.services.analyzer import MarketAnalyzer
|
|
from monitor.models import MarketAnalysis # Add this import
|
|
|
|
|
|
# Add new view functions
|
|
def run_analysis(request):
|
|
"""Run market analysis."""
|
|
analyzer = MarketAnalyzer(threshold_percent=15.0)
|
|
|
|
# Run analysis for different periods
|
|
analyses = []
|
|
for period in ['hourly', 'daily', 'yearly']:
|
|
analysis = analyzer.analyze_market(period)
|
|
if analysis:
|
|
analyses.append(analysis)
|
|
|
|
context = {
|
|
'analyses': analyses,
|
|
'message': f'Ran {len(analyses)} analyses successfully',
|
|
}
|
|
|
|
return render(request, 'monitor/analysis_result.html', context)
|
|
|
|
|
|
def view_analysis(request):
|
|
"""View latest market analysis."""
|
|
analyzer = MarketAnalyzer(threshold_percent=15.0)
|
|
|
|
# Get latest analyses
|
|
hourly = analyzer.get_latest_analysis('hourly')
|
|
daily = analyzer.get_latest_analysis('daily')
|
|
yearly = analyzer.get_latest_analysis('yearly')
|
|
|
|
# Get analysis summary
|
|
summary = analyzer.get_analysis_summary()
|
|
|
|
# Get all analyses for the table
|
|
all_analyses = MarketAnalysis.objects.all().order_by('-timestamp')[:20]
|
|
|
|
context = {
|
|
'hourly_analysis': hourly,
|
|
'daily_analysis': daily,
|
|
'yearly_analysis': yearly,
|
|
'summary': summary,
|
|
'all_analyses': all_analyses,
|
|
}
|
|
|
|
return render(request, 'monitor/view_analysis.html', context)
|
|
|
|
|
|
# Update bitcoin_data view to include analysis
|
|
def bitcoin_data(request):
|
|
"""Display Bitcoin data from database."""
|
|
# Get latest 10 prices
|
|
latest_prices = BitcoinPrice.objects.all()[:10]
|
|
|
|
# Calculate basic stats if we have data
|
|
if latest_prices:
|
|
latest_price = latest_prices[0]
|
|
stats = {
|
|
'latest_price': latest_price.price_usd,
|
|
'latest_time': latest_price.timestamp,
|
|
'total_records': BitcoinPrice.objects.count(),
|
|
}
|
|
|
|
# Get latest hourly analysis
|
|
analyzer = MarketAnalyzer()
|
|
hourly_analysis = analyzer.get_latest_analysis('hourly')
|
|
else:
|
|
stats = {
|
|
'latest_price': 'No data',
|
|
'latest_time': 'No data',
|
|
'total_records': 0,
|
|
}
|
|
hourly_analysis = None
|
|
|
|
return render(request, 'monitor/bitcoin_data.html', {
|
|
'prices': latest_prices,
|
|
'stats': stats,
|
|
'analysis': hourly_analysis, # Add analysis to context
|
|
})
|
|
# Add these imports at the top
|
|
|
|
|
|
# Add new view functions
|
|
def run_analysis(request):
|
|
"""Run market analysis."""
|
|
analyzer = MarketAnalyzer(threshold_percent=15.0)
|
|
|
|
# Run analysis for different periods
|
|
analyses = []
|
|
for period in ['hourly', 'daily', 'yearly']:
|
|
analysis = analyzer.analyze_market(period)
|
|
if analysis:
|
|
analyses.append(analysis)
|
|
|
|
context = {
|
|
'analyses': analyses,
|
|
'message': f'Ran {len(analyses)} analyses successfully',
|
|
}
|
|
|
|
return render(request, 'monitor/analysis_result.html', context)
|
|
|
|
|
|
def view_analysis(request):
|
|
"""View latest market analysis."""
|
|
analyzer = MarketAnalyzer(threshold_percent=15.0)
|
|
|
|
# Get latest analyses
|
|
hourly = analyzer.get_latest_analysis('hourly')
|
|
daily = analyzer.get_latest_analysis('daily')
|
|
yearly = analyzer.get_latest_analysis('yearly')
|
|
|
|
# Get analysis summary
|
|
summary = analyzer.get_analysis_summary()
|
|
|
|
# Get all analyses for the table
|
|
all_analyses = MarketAnalysis.objects.all().order_by('-timestamp')[:20]
|
|
|
|
context = {
|
|
'hourly_analysis': hourly,
|
|
'daily_analysis': daily,
|
|
'yearly_analysis': yearly,
|
|
'summary': summary,
|
|
'all_analyses': all_analyses,
|
|
}
|
|
|
|
return render(request, 'monitor/view_analysis.html', context)
|
|
|
|
|
|
# Update bitcoin_data view to include analysis
|
|
def bitcoin_data(request):
|
|
"""Display Bitcoin data from database."""
|
|
# Get latest 10 prices
|
|
latest_prices = BitcoinPrice.objects.all()[:10]
|
|
|
|
# Calculate basic stats if we have data
|
|
if latest_prices:
|
|
latest_price = latest_prices[0]
|
|
stats = {
|
|
'latest_price': latest_price.price_usd,
|
|
'latest_time': latest_price.timestamp,
|
|
'total_records': BitcoinPrice.objects.count(),
|
|
}
|
|
|
|
# Get latest hourly analysis
|
|
analyzer = MarketAnalyzer()
|
|
hourly_analysis = analyzer.get_latest_analysis('hourly')
|
|
else:
|
|
stats = {
|
|
'latest_price': 'No data',
|
|
'latest_time': 'No data',
|
|
'total_records': 0,
|
|
}
|
|
hourly_analysis = None
|
|
|
|
return render(request, 'monitor/bitcoin_data.html', {
|
|
'prices': latest_prices,
|
|
'stats': stats,
|
|
'analysis': hourly_analysis, # Add analysis to context
|
|
})
|