Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import warnings
|
| 4 |
+
from stratagy.rsi_stratagy import eqt
|
| 5 |
+
|
| 6 |
+
warnings.simplefilter(action='ignore', category=FutureWarning)
|
| 7 |
+
|
| 8 |
+
def backtest(ticker, startdate, enddate, share_qty):
|
| 9 |
+
try:
|
| 10 |
+
df = eqt(ticker, startdate, enddate, share_qty)
|
| 11 |
+
return df
|
| 12 |
+
except Exception as e:
|
| 13 |
+
st.error(f"Please check input stock name of date.")
|
| 14 |
+
|
| 15 |
+
def main():
|
| 16 |
+
st.title("RSI Backtesting App")
|
| 17 |
+
st.sidebar.title("Input Parameters")
|
| 18 |
+
|
| 19 |
+
default_ticker = 'MRF'
|
| 20 |
+
default_startdate = '2019-02-01'
|
| 21 |
+
default_enddate = '2023-11-28'
|
| 22 |
+
default_share_qty = 90
|
| 23 |
+
|
| 24 |
+
ticker = st.sidebar.text_input("Enter Ticker", default_ticker)
|
| 25 |
+
ticker = ticker.upper() + ".NS"
|
| 26 |
+
|
| 27 |
+
# Date picker for start date
|
| 28 |
+
startdate = st.sidebar.date_input("Select Start Date", pd.to_datetime(default_startdate))
|
| 29 |
+
|
| 30 |
+
# Date picker for end date
|
| 31 |
+
enddate = st.sidebar.date_input("Select End Date", pd.to_datetime(default_enddate))
|
| 32 |
+
|
| 33 |
+
share_qty = st.sidebar.number_input("Enter Share Quantity", value=default_share_qty)
|
| 34 |
+
|
| 35 |
+
if st.sidebar.button("Backtest"):
|
| 36 |
+
if startdate > enddate:
|
| 37 |
+
st.error("End Date must be after Start Date.")
|
| 38 |
+
else:
|
| 39 |
+
st.write("Backtesting with the following parameters:")
|
| 40 |
+
st.write(f"- Ticker: {ticker}")
|
| 41 |
+
st.write(f"- Start Date: {startdate.strftime('%Y-%m-%d')}")
|
| 42 |
+
st.write(f"- End Date: {enddate.strftime('%Y-%m-%d')}")
|
| 43 |
+
st.write(f"- Share Quantity: {share_qty}")
|
| 44 |
+
|
| 45 |
+
result_df = backtest(ticker, startdate.strftime('%Y-%m-%d'), enddate.strftime('%Y-%m-%d'), share_qty)
|
| 46 |
+
if result_df is not None:
|
| 47 |
+
# Calculate total profit and trading duration
|
| 48 |
+
total_profit = round(result_df['profit'].sum(), 2)
|
| 49 |
+
trading_duration = f"{result_df.iloc[0]['Start'].strftime('%Y-%m-%d')} to {result_df.iloc[-1]['End'].strftime('%Y-%m-%d')}"
|
| 50 |
+
|
| 51 |
+
# Display profit in green if positive, else in red
|
| 52 |
+
profit_color = "green" if total_profit >= 0 else "red"
|
| 53 |
+
st.write(f"Total Profit: <span style='color:{profit_color}'>{total_profit}₹</span>", unsafe_allow_html=True)
|
| 54 |
+
st.write(f"Trading Duration: {trading_duration}")
|
| 55 |
+
|
| 56 |
+
# Display backtesting results table
|
| 57 |
+
st.write("Backtesting Results:")
|
| 58 |
+
st.write(result_df)
|
| 59 |
+
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
main()
|