Update levels.py
Browse files
levels.py
CHANGED
|
@@ -8,7 +8,7 @@ warnings.filterwarnings('ignore')
|
|
| 8 |
|
| 9 |
df_logo = pd.read_csv("https://raw.githubusercontent.com/jarvisx17/nifty500/main/Nifty500.csv")
|
| 10 |
|
| 11 |
-
|
| 12 |
tgt1 = entry + (0.02 * entry)
|
| 13 |
tgt2 = entry + (0.04 * entry)
|
| 14 |
if ltp > tgt2:
|
|
@@ -21,17 +21,17 @@ async def calculate_profit(ltp, share, entry):
|
|
| 21 |
profit = round(share * (ltp-entry), 2)
|
| 22 |
return profit
|
| 23 |
|
| 24 |
-
|
| 25 |
data = df_logo[df_logo['Symbol'] == ticker]
|
| 26 |
logo = data.logo.values[0]
|
| 27 |
Industry = data.Industry.values[0]
|
| 28 |
return logo, Industry
|
| 29 |
|
| 30 |
-
|
| 31 |
percentage_loss = ((ltp - buying_price) / buying_price) * 100
|
| 32 |
return f"{percentage_loss:.2f}%"
|
| 33 |
|
| 34 |
-
|
| 35 |
ticker = ticker.split(".")[0]
|
| 36 |
url = f"https://stock-market-lo24myw5sq-el.a.run.app/currentprice?ticker={ticker}"
|
| 37 |
response = requests.get(url)
|
|
@@ -41,7 +41,7 @@ async def latestprice(ticker):
|
|
| 41 |
else:
|
| 42 |
return "N/A"
|
| 43 |
|
| 44 |
-
|
| 45 |
|
| 46 |
def get_rsi(close, lookback):
|
| 47 |
ret = close.diff()
|
|
@@ -71,11 +71,11 @@ async def process_dataframe(df):
|
|
| 71 |
|
| 72 |
return df
|
| 73 |
|
| 74 |
-
|
| 75 |
|
| 76 |
-
ltp =
|
| 77 |
df=yf.download(ticker, period="36mo", progress=False)
|
| 78 |
-
df =
|
| 79 |
df.reset_index(inplace=True)
|
| 80 |
df['Prev_RSI'] = df['RSI'].shift(1).round(2)
|
| 81 |
df = df.dropna()
|
|
@@ -98,8 +98,8 @@ async def fin_data(ticker, startdate):
|
|
| 98 |
|
| 99 |
def eqt(ticker, startdate, share_qty = 90):
|
| 100 |
|
| 101 |
-
df =
|
| 102 |
-
logo, Industry =
|
| 103 |
entry = False
|
| 104 |
trading = False
|
| 105 |
shares_held = 0
|
|
@@ -116,7 +116,7 @@ def eqt(ticker, startdate, share_qty = 90):
|
|
| 116 |
capital_list = []
|
| 117 |
data = {}
|
| 118 |
totalshares = share_qty
|
| 119 |
-
ltp =
|
| 120 |
|
| 121 |
for i in range(1, len(df)-1):
|
| 122 |
try:
|
|
@@ -142,8 +142,8 @@ def eqt(ticker, startdate, share_qty = 90):
|
|
| 142 |
ltp = entryStock_info['LTP']
|
| 143 |
data['StockInfo']['Stock']['Values']['Share QTY'] = share_qty
|
| 144 |
data['StockInfo']['Stock']['Values']['Invested Amount'] = (share_qty * buy_price).round(2)
|
| 145 |
-
data['StockInfo']['Stock']['Values']['Percentage'] =
|
| 146 |
-
data['StockInfo']['Stock']['Values']['Total P/L'] =
|
| 147 |
data['StockInfo']['Entry'] = entry_info
|
| 148 |
entry = True
|
| 149 |
trading = True
|
|
@@ -159,7 +159,7 @@ def eqt(ticker, startdate, share_qty = 90):
|
|
| 159 |
data['StockInfo']['TGT1'] = target1_info
|
| 160 |
data['StockInfo']['Stock']['Values']['SL'] = stop_loss
|
| 161 |
data['StockInfo']['Stock']['Levels'] = data['StockInfo']['Stock']['Levels'] + " TGT1"
|
| 162 |
-
data['StockInfo']['Stock']['Values']['Total P/L'] =
|
| 163 |
data['StockInfo']['Entry']['Trade Status'] = "Trading is ongoing...."
|
| 164 |
|
| 165 |
if trading and target1 and not target2:
|
|
@@ -169,7 +169,7 @@ def eqt(ticker, startdate, share_qty = 90):
|
|
| 169 |
total_profit += round(tgt2,2)
|
| 170 |
shares_held -= (share_qty / 3)
|
| 171 |
data['StockInfo']['Stock']['Levels'] = data['StockInfo']['Stock']['Levels'] + " TGT2"
|
| 172 |
-
data['StockInfo']['Stock']['Values']['Total P/L'] =
|
| 173 |
target2_info = {"Date" : pd.to_datetime(df.at[i+1, 'Date']).strftime('%d-%m-%Y'), "Profit" : round(tgt2,2), "Remaining Shares": shares_held,"Note" : "TGT2 Achieved Successfully", "SL" : stop_loss}
|
| 174 |
data['StockInfo']['TGT2'] = target2_info
|
| 175 |
data['StockInfo']['Entry']['Trade Status'] = "Trading is ongoing...."
|
|
@@ -187,7 +187,7 @@ def eqt(ticker, startdate, share_qty = 90):
|
|
| 187 |
data['StockInfo']['Stock']['Values']['Target3'] = tgt3
|
| 188 |
data['StockInfo']['TGT3'] = target3_info
|
| 189 |
data['StockInfo']['Stock']['Levels'] = data['StockInfo']['Stock']['Levels'] +" TGT3"
|
| 190 |
-
data['StockInfo']['Stock']['Values']['Total P/L'] =
|
| 191 |
data['StockInfo']['TotalProfit'] = {}
|
| 192 |
data['StockInfo']['TotalProfit']['Profit'] = total_profit
|
| 193 |
data['StockInfo']['Entry']['Trade Status'] = "Trade closed successfully...."
|
|
|
|
| 8 |
|
| 9 |
df_logo = pd.read_csv("https://raw.githubusercontent.com/jarvisx17/nifty500/main/Nifty500.csv")
|
| 10 |
|
| 11 |
+
def calculate_profit(ltp, share, entry):
|
| 12 |
tgt1 = entry + (0.02 * entry)
|
| 13 |
tgt2 = entry + (0.04 * entry)
|
| 14 |
if ltp > tgt2:
|
|
|
|
| 21 |
profit = round(share * (ltp-entry), 2)
|
| 22 |
return profit
|
| 23 |
|
| 24 |
+
def info(ticker):
|
| 25 |
data = df_logo[df_logo['Symbol'] == ticker]
|
| 26 |
logo = data.logo.values[0]
|
| 27 |
Industry = data.Industry.values[0]
|
| 28 |
return logo, Industry
|
| 29 |
|
| 30 |
+
def calculate_percentage_loss(buying_price, ltp):
|
| 31 |
percentage_loss = ((ltp - buying_price) / buying_price) * 100
|
| 32 |
return f"{percentage_loss:.2f}%"
|
| 33 |
|
| 34 |
+
def latestprice(ticker):
|
| 35 |
ticker = ticker.split(".")[0]
|
| 36 |
url = f"https://stock-market-lo24myw5sq-el.a.run.app/currentprice?ticker={ticker}"
|
| 37 |
response = requests.get(url)
|
|
|
|
| 41 |
else:
|
| 42 |
return "N/A"
|
| 43 |
|
| 44 |
+
def process_dataframe(df):
|
| 45 |
|
| 46 |
def get_rsi(close, lookback):
|
| 47 |
ret = close.diff()
|
|
|
|
| 71 |
|
| 72 |
return df
|
| 73 |
|
| 74 |
+
def fin_data(ticker, startdate):
|
| 75 |
|
| 76 |
+
ltp = latestprice(ticker)
|
| 77 |
df=yf.download(ticker, period="36mo", progress=False)
|
| 78 |
+
df = process_dataframe(df)
|
| 79 |
df.reset_index(inplace=True)
|
| 80 |
df['Prev_RSI'] = df['RSI'].shift(1).round(2)
|
| 81 |
df = df.dropna()
|
|
|
|
| 98 |
|
| 99 |
def eqt(ticker, startdate, share_qty = 90):
|
| 100 |
|
| 101 |
+
df = fin_data(ticker, startdate)
|
| 102 |
+
logo, Industry = info(ticker)
|
| 103 |
entry = False
|
| 104 |
trading = False
|
| 105 |
shares_held = 0
|
|
|
|
| 116 |
capital_list = []
|
| 117 |
data = {}
|
| 118 |
totalshares = share_qty
|
| 119 |
+
ltp = latestprice(ticker)
|
| 120 |
|
| 121 |
for i in range(1, len(df)-1):
|
| 122 |
try:
|
|
|
|
| 142 |
ltp = entryStock_info['LTP']
|
| 143 |
data['StockInfo']['Stock']['Values']['Share QTY'] = share_qty
|
| 144 |
data['StockInfo']['Stock']['Values']['Invested Amount'] = (share_qty * buy_price).round(2)
|
| 145 |
+
data['StockInfo']['Stock']['Values']['Percentage'] = calculate_percentage_loss(buying_price, ltp)
|
| 146 |
+
data['StockInfo']['Stock']['Values']['Total P/L'] = calculate_profit(ltp, totalshares, buy_price)
|
| 147 |
data['StockInfo']['Entry'] = entry_info
|
| 148 |
entry = True
|
| 149 |
trading = True
|
|
|
|
| 159 |
data['StockInfo']['TGT1'] = target1_info
|
| 160 |
data['StockInfo']['Stock']['Values']['SL'] = stop_loss
|
| 161 |
data['StockInfo']['Stock']['Levels'] = data['StockInfo']['Stock']['Levels'] + " TGT1"
|
| 162 |
+
data['StockInfo']['Stock']['Values']['Total P/L'] = calculate_profit(ltp, totalshares, buy_price)
|
| 163 |
data['StockInfo']['Entry']['Trade Status'] = "Trading is ongoing...."
|
| 164 |
|
| 165 |
if trading and target1 and not target2:
|
|
|
|
| 169 |
total_profit += round(tgt2,2)
|
| 170 |
shares_held -= (share_qty / 3)
|
| 171 |
data['StockInfo']['Stock']['Levels'] = data['StockInfo']['Stock']['Levels'] + " TGT2"
|
| 172 |
+
data['StockInfo']['Stock']['Values']['Total P/L'] = calculate_profit(ltp, totalshares, buy_price)
|
| 173 |
target2_info = {"Date" : pd.to_datetime(df.at[i+1, 'Date']).strftime('%d-%m-%Y'), "Profit" : round(tgt2,2), "Remaining Shares": shares_held,"Note" : "TGT2 Achieved Successfully", "SL" : stop_loss}
|
| 174 |
data['StockInfo']['TGT2'] = target2_info
|
| 175 |
data['StockInfo']['Entry']['Trade Status'] = "Trading is ongoing...."
|
|
|
|
| 187 |
data['StockInfo']['Stock']['Values']['Target3'] = tgt3
|
| 188 |
data['StockInfo']['TGT3'] = target3_info
|
| 189 |
data['StockInfo']['Stock']['Levels'] = data['StockInfo']['Stock']['Levels'] +" TGT3"
|
| 190 |
+
data['StockInfo']['Stock']['Values']['Total P/L'] = calculate_profit(ltp, totalshares, buy_price)
|
| 191 |
data['StockInfo']['TotalProfit'] = {}
|
| 192 |
data['StockInfo']['TotalProfit']['Profit'] = total_profit
|
| 193 |
data['StockInfo']['Entry']['Trade Status'] = "Trade closed successfully...."
|