Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import plotly.graph_objects as go
|
| 5 |
+
import io
|
| 6 |
+
import google.generativeai as genai
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
# Configure Gemini AI
|
| 10 |
+
API_KEY = "AIzaSyBOOuFWwlywK77pLGg82li1m1mkNgeyQz4"
|
| 11 |
+
genai.configure(api_key=API_KEY)
|
| 12 |
+
model = genai.GenerativeModel("gemini-2.0-flash-exp")
|
| 13 |
+
|
| 14 |
+
# Function to fetch data from Gemini API
|
| 15 |
+
def get_gemini_data(symbol, timeframe):
|
| 16 |
+
# Mapping timeframe to Gemini API argument
|
| 17 |
+
timeframe_mapping = {
|
| 18 |
+
'1m': '1m',
|
| 19 |
+
'5m': '5m',
|
| 20 |
+
'15m': '15m',
|
| 21 |
+
'30m': '30m',
|
| 22 |
+
'1h': '1h',
|
| 23 |
+
'6h': '6h',
|
| 24 |
+
'1d': '1d'
|
| 25 |
+
}
|
| 26 |
+
url = f"https://api.gemini.com/v2/candles/{symbol}/{timeframe_mapping[timeframe]}"
|
| 27 |
+
response = requests.get(url)
|
| 28 |
+
data = response.json()
|
| 29 |
+
df = pd.DataFrame(data, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
|
| 30 |
+
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='s')
|
| 31 |
+
return df
|
| 32 |
+
|
| 33 |
+
# Function to plot candlestick chart
|
| 34 |
+
def plot_candlestick(data):
|
| 35 |
+
fig = go.Figure(data=[
|
| 36 |
+
go.Candlestick(
|
| 37 |
+
x=data['timestamp'],
|
| 38 |
+
open=data['open'],
|
| 39 |
+
high=data['high'],
|
| 40 |
+
low=data['low'],
|
| 41 |
+
close=data['close']
|
| 42 |
+
)
|
| 43 |
+
])
|
| 44 |
+
fig.update_xaxes(title_text='Time')
|
| 45 |
+
fig.update_yaxes(title_text='Price')
|
| 46 |
+
fig.update_layout(title_text='Candlestick Chart')
|
| 47 |
+
return fig
|
| 48 |
+
|
| 49 |
+
# Function to analyze candlestick image with Gemini AI
|
| 50 |
+
def analyze_candlestick(model, chart_image):
|
| 51 |
+
image_bytes = io.BytesIO()
|
| 52 |
+
chart_image.save(image_bytes, format='PNG')
|
| 53 |
+
image_bytes = image_bytes.getvalue()
|
| 54 |
+
content = f"خودت رو بعنوان یک متخصص کارگزاری تصمیم بگیر و طبق نمودار شمعی که بهت داده شده، یک توصیه معاملاتی برای خرید یا فروش ارائه کن و توضیحات دقیق ارائه ده: {image_bytes}"
|
| 55 |
+
response = model.generate_content([content, "خودت رو بعنوان یک متخصص کارگزاری تصمور کن و طبق این نمودار شمعی که بهت داده شده یک توصیه معاملاتی برای خرید یا فروش ارائه کن و توضیحات دقیق ارائه ده"])
|
| 56 |
+
return response.text
|
| 57 |
+
|
| 58 |
+
# Main function
|
| 59 |
+
def main():
|
| 60 |
+
st.title("Gemini Trading Analysis with AI")
|
| 61 |
+
|
| 62 |
+
# User input for symbol and timeframe
|
| 63 |
+
symbol = st.text_input("Enter trading symbol (e.g., BTCUSD):", value="BTCUSD")
|
| 64 |
+
timeframe = st.selectbox("Select timeframe:", ['1m', '5m', '15m', '30m', '1h', '6h', '1d'])
|
| 65 |
+
|
| 66 |
+
# Fetch and display candlestick chart
|
| 67 |
+
if st.button("Analyze"):
|
| 68 |
+
df = get_gemini_data(symbol, timeframe)
|
| 69 |
+
fig = plot_candlestick(df)
|
| 70 |
+
st.plotly_chart(fig)
|
| 71 |
+
|
| 72 |
+
# Save the plot as an image
|
| 73 |
+
img_bytes = io.BytesIO()
|
| 74 |
+
fig.write_image(img_bytes, format='png')
|
| 75 |
+
img = Image.open(img_bytes)
|
| 76 |
+
|
| 77 |
+
# Run AI analysis
|
| 78 |
+
suggestion = analyze_candlestick(model, img)
|
| 79 |
+
st.markdown(suggestion)
|
| 80 |
+
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
main()
|