Step 1 : At 9:31, Check Top 5 Gainer and Loser.

Let’s assume We’re using the broker module only to fire orders and taking the other data from NSE’s website directly. In this case, with slight browsing around, We can see the “Top Gainers” and “Top Losers” can be seen distinctively in NSE India website.

If we dig further with “Chrome Developer Tools” and their “Network Console”, and see the “XHR” requests, We found the URL https://www.nseindia.com/api/live-analysis-variations?index=gainers there. Let’s apply our nsefetch() function from NSEPython Module.
from nsepython import *
gainers = nsefetch("<https://www.nseindia.com/api/live-analysis-variations?
index=gainers>")
print(gainers)

It shows us the JSON Structure. Let’s copy that and put it to http://jsonformatter.org/json-viewer%7Cjsonformatter.org/json-viewer for understanding the schema.
It is showing data from all the sectors. Lets check the Top 5 gainers and losers from NIFTY50 only.

The next step will be pulling the data inside that list to a Pandas database.It is showing data from all the sectors. Lets check the Top 5 gainers and losers from NIFTY50 only.
import pandas as pd
gainers=pd.DataFrame.from_records(gainers["NIFTY"]["data"])
print(gainers)
We have the pandas database sorted right now and with lots of extra information with it. The bigger question is what information you need and what you don’t. To check that, let’s revisit our initial agenda !

Step 2 : Buy 5 stocks who are in top 5 gainers if the range breaks with SL at day's low

So, from the above image of “Top gainers”,

  1. The “High Price” taken at 9:31 AM is hence considered as day high.
  2. We need to keep checking the current price i.e “LTP” with that value.
  3. If (“LTP” at any time> “Day High” of 9:31 AM): Buy with Stop Loss at “Day Low”.

Also, Let’s first shorten the list to Top 5. (Let’s do the Top Part first.)
Also, Remove the columns We do not need.

Now Let’s wrap it under a function. Now Let’s wrap it under a function.

from nsepython import *

def get_gainers():
    gainers = nsefetch("<https://www.nseindia.com/api/live-analysis-variations?index=gainers>")
    gainers=pd.DataFrame.from_records(gainers["NIFTY"]["data"])
    gainers=gainers.head(5)
    gainers.drop(gainers.columns.difference(["symbol","high_price","low_price","ltp"]), 1, inplace=True)
    return gainers

gainers = get_gainers()
Another thing is the NSEPython Module shows the debug logging enabled by default. That is harmless and You can turn off using this piece of code –
logger = logging.getLogger()
logger.setLevel(<http://logging.INFO|logging.INFO>)
Now, We will check the LTP. The easiest way to do is by using the nse_quote_ltp() function.
nse_quote_ltp("TCS")
And, With slight help of for loop we can get all the LTP of the Pandas’ frame updated.
for i in range(0,5):
    print(nse_quote_ltp(gainers.symbol.iloc[i]))
The output of this code will come like this. It serves our purpose beautifully.
But, We do not need the LTP in this case too. Let’s make it more minimal by removing LTP.
from nsepython import *

def get_gainers():
    gainers = nsefetch("<https://www.nseindia.com/api/live-analysis-variations?index=gainers>")
    gainers=pd.DataFrame.from_records(gainers["NIFTY"]["data"])
    gainers=gainers.head(5)
    gainers.drop(gainers.columns.difference(["symbol","high_price","low_price"]), 1, inplace=True)
    return gainers

gainers = get_gainers()
print(gainers)
And, with minor modifications, here goes the Paper Buy Trade Module -
for i in range(0,5):
    symbol = gainers.symbol.iloc[i]
    current_ltp = nse_quote_ltp(symbol)
    
    day_high = gainers.high_price.iloc[i]
    day_low = gainers.low_price.iloc[i]
    
    if(current_ltp&gt;day_high): print(symbol+" triggered Buy at "+str(current_ltp)+" with a stop loss at "+ str(day_low))    
   
This is checking very basic stuff which is already written as You can see. One can also fire Cover order from Broker API at the place of print() function.

Heatmap ORB Strategy

Strategy

At 9:31, Check Top 5 Gainer and Loser.

  • Buy 5 stocks who are in top5 gainers if the range breaks with SL at day’s low
  • Sell 5 stocks who are in top5 sellers if the range breaks with SL at day’s high

The post Quant ORB appeared first on Unofficed.