Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Load the data
|
7 |
+
@st.cache_data
|
8 |
+
def load_data():
|
9 |
+
return pd.read_csv('BANKNIFTY_OPTION_CHAIN_data.csv', parse_dates=['date'])
|
10 |
+
|
11 |
+
# Load Llama model and tokenizer
|
12 |
+
@st.cache_resource
|
13 |
+
def load_llama_model():
|
14 |
+
model_name = "meta-llama/Meta-Llama-3.1-405B" # You may need to adjust this based on the specific Llama model you want to use
|
15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
16 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
|
17 |
+
return tokenizer, model
|
18 |
+
|
19 |
+
# Function to generate response from Llama
|
20 |
+
def generate_llama_response(prompt, max_length=7000):
|
21 |
+
inputs = tokenizer.encode(prompt, return_tensors="pt").to(model.device)
|
22 |
+
outputs = model.generate(inputs, max_length=max_length, num_return_sequences=1, temperature=0.7)
|
23 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
24 |
+
return response
|
25 |
+
|
26 |
+
# Load data and model
|
27 |
+
data = load_data()
|
28 |
+
tokenizer, model = load_llama_model()
|
29 |
+
|
30 |
+
st.title('BANKNIFTY Option Chain Analysis with Llama Model')
|
31 |
+
|
32 |
+
# Date range selector
|
33 |
+
start_date = st.date_input('Start Date', min(data['date']))
|
34 |
+
end_date = st.date_input('End Date', max(data['date']))
|
35 |
+
|
36 |
+
# Filter data based on selected date range
|
37 |
+
filtered_data = data[(data['date'].dt.date >= start_date) & (data['date'].dt.date <= end_date)]
|
38 |
+
|
39 |
+
# Display basic statistics
|
40 |
+
st.subheader('Basic Statistics')
|
41 |
+
st.write(filtered_data.describe())
|
42 |
+
|
43 |
+
# Prepare data summary for Llama
|
44 |
+
data_summary = f"""
|
45 |
+
Date Range: {start_date} to {end_date}
|
46 |
+
Total Rows: {len(filtered_data)}
|
47 |
+
Unique Strike Prices: {filtered_data['Strike'].nunique()}
|
48 |
+
Call Options: {len(filtered_data[filtered_data['OptionType'] == 'CE'])}
|
49 |
+
Put Options: {len(filtered_data[filtered_data['OptionType'] == 'PE'])}
|
50 |
+
Average Open Interest: {filtered_data['oi'].mean():.2f}
|
51 |
+
Average Volume: {filtered_data['volume'].mean():.2f}
|
52 |
+
"""
|
53 |
+
|
54 |
+
# Generate Llama analysis
|
55 |
+
st.subheader('Llama Model Analysis')
|
56 |
+
|
57 |
+
prompt = f"""
|
58 |
+
You are a financial expert specializing in options trading. Analyze the following BANKNIFTY option chain data summary and provide insights on market sentiment, potential trading strategies, and key observations. Be specific and provide actionable advice.
|
59 |
+
|
60 |
+
Data Summary:
|
61 |
+
{data_summary}
|
62 |
+
|
63 |
+
Based on this data, provide your analysis and recommendations.
|
64 |
+
"""
|
65 |
+
|
66 |
+
if st.button('Generate Llama Analysis'):
|
67 |
+
with st.spinner('Generating analysis...'):
|
68 |
+
llama_response = generate_llama_response(prompt)
|
69 |
+
st.write(llama_response)
|
70 |
+
|
71 |
+
# Generate trading strategy
|
72 |
+
st.subheader('Generate Trading Strategy')
|
73 |
+
|
74 |
+
strategy_prompt = f"""
|
75 |
+
Based on the BANKNIFTY option chain data summary below, create a detailed trading strategy. Include entry and exit points, risk management techniques, and explain the rationale behind the strategy. Consider factors like market sentiment, volatility, and option Greeks if applicable.
|
76 |
+
|
77 |
+
Data Summary:
|
78 |
+
{data_summary}
|
79 |
+
|
80 |
+
Provide a step-by-step trading strategy based on this data.
|
81 |
+
"""
|
82 |
+
|
83 |
+
if st.button('Generate Trading Strategy'):
|
84 |
+
with st.spinner('Generating strategy...'):
|
85 |
+
strategy_response = generate_llama_response(strategy_prompt, max_length=1000)
|
86 |
+
st.write(strategy_response)
|
87 |
+
|
88 |
+
# Option to download the Llama-generated analysis and strategy
|
89 |
+
if st.button('Download Llama Analysis and Strategy'):
|
90 |
+
combined_analysis = f"Llama Analysis:\n\n{llama_response}\n\nTrading Strategy:\n\n{strategy_response}"
|
91 |
+
st.download_button(
|
92 |
+
label="Download Analysis",
|
93 |
+
data=combined_analysis,
|
94 |
+
file_name="llama_banknifty_analysis.txt",
|
95 |
+
mime="text/plain"
|
96 |
+
)
|