File size: 3,183 Bytes
45ede9f
 
 
f87bdf0
 
45ede9f
 
b338e2c
45ede9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import requests
import pandas as pd
import subprocess
subprocess.run("pip install plotly")
import plotly.graph_objects as go
import io
subprocess.run("pip install -q -U google-generativeai", shell=True)
import google.generativeai as genai
from PIL import Image

# Configure Gemini AI
API_KEY = "AIzaSyBOOuFWwlywK77pLGg82li1m1mkNgeyQz4"
genai.configure(api_key=API_KEY)
model = genai.GenerativeModel("gemini-2.0-flash-exp")

# Function to fetch data from Gemini API
def get_gemini_data(symbol, timeframe):
    # Mapping timeframe to Gemini API argument
    timeframe_mapping = {
        '1m': '1m',
        '5m': '5m',
        '15m': '15m',
        '30m': '30m',
        '1h': '1h',
        '6h': '6h',
        '1d': '1d'
    }
    url = f"https://api.gemini.com/v2/candles/{symbol}/{timeframe_mapping[timeframe]}"
    response = requests.get(url)
    data = response.json()
    df = pd.DataFrame(data, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
    df['timestamp'] = pd.to_datetime(df['timestamp'], unit='s')
    return df

# Function to plot candlestick chart
def plot_candlestick(data):
    fig = go.Figure(data=[
        go.Candlestick(
            x=data['timestamp'],
            open=data['open'],
            high=data['high'],
            low=data['low'],
            close=data['close']
        )
    ])
    fig.update_xaxes(title_text='Time')
    fig.update_yaxes(title_text='Price')
    fig.update_layout(title_text='Candlestick Chart')
    return fig

# Function to analyze candlestick image with Gemini AI
def analyze_candlestick(model, chart_image):
    image_bytes = io.BytesIO()
    chart_image.save(image_bytes, format='PNG')
    image_bytes = image_bytes.getvalue()
    content = f"خودت رو بعنوان یک متخصص کارگزاری تصمیم بگیر و طبق نمودار شمعی که بهت داده شده، یک توصیه معاملاتی برای خرید یا فروش ارائه کن و توضیحات دقیق ارائه ده: {image_bytes}"
    response = model.generate_content([content, "خودت رو بعنوان یک متخصص کارگزاری تصمور کن و طبق این نمودار شمعی که بهت داده شده یک توصیه معاملاتی برای خرید یا فروش ارائه کن و توضیحات دقیق ارائه ده"])
    return response.text

# Main function
def main():
    st.title("Gemini Trading Analysis with AI")
    
    # User input for symbol and timeframe
    symbol = st.text_input("Enter trading symbol (e.g., BTCUSD):", value="BTCUSD")
    timeframe = st.selectbox("Select timeframe:", ['1m', '5m', '15m', '30m', '1h', '6h', '1d'])
    
    # Fetch and display candlestick chart
    if st.button("Analyze"):
        df = get_gemini_data(symbol, timeframe)
        fig = plot_candlestick(df)
        st.plotly_chart(fig)
        
        # Save the plot as an image
        img_bytes = io.BytesIO()
        fig.write_image(img_bytes, format='png')
        img = Image.open(img_bytes)
        
        # Run AI analysis
        suggestion = analyze_candlestick(model, img)
        st.markdown(suggestion)

if __name__ == "__main__":
    main()