Home > Blockchain >  related to pandas data frame
related to pandas data frame

Time:08-09

CODE :-

from datetime import date
from datetime import timedelta
from nsepy import get_history
import pandas as pd

end1 =date.today()
start1 = end1 - timedelta(days=10)
exp_date1 = date(2022,8,25)
exp_date2 = date(2022,9,29)

stock = ['RELIANCE','HDFCBANK','INFY','ICICIBANK','HDFC','TCS','KOTAKBANK','LT','SBIN','HINDUNILVR','AXISBANK',
     'ITC','BAJFINANCE','BHARTIARTL','ASIANPAINT','HCLTECH','MARUTI','TITAN','BAJAJFINSV','TATAMOTORS',
     'TECHM','SUNPHARMA','TATASTEEL','M&M','WIPRO','ULTRACEMCO','POWERGRID','HINDALCO','NTPC','NESTLEIND',
     'GRASIM','ONGC','JSWSTEEL','HDFCLIFE','INDUSINDBK','SBILIFE','DRREDDY','ADANIPORTS','DIVISLAB','CIPLA',
     'BAJAJ-AUTO','TATACONSUM','UPL','BRITANNIA','BPCL','EICHERMOT','HEROMOTOCO','COALINDIA','SHREECEM','IOC']
for stock in stock:
    stock_jan = get_history(symbol=stock,
                        start=start1,
                        end=end1,
                        futures=True,
                        expiry_date=exp_date1)
    stock_feb = get_history(symbol=stock,
                        start=start1,
                        end=end1,
                        futures=True,
                        expiry_date=exp_date2)
    delivery_per_age = get_history(symbol=stock,
                               start=start1,
                               end=end1)
    symbol_s = get_history(symbol=stock,
                       start=start1,
                       end=end1)
    oi_combined = pd.concat([stock_jan['Change in OI']   stock_feb['Change in OI']])
    total_oi = pd.concat([stock_jan['Open Interest'] stock_feb['Open Interest']])
    delivery_vol = pd.concat([delivery_per_age['Deliverable Volume']])
    na_me = pd.concat([symbol_s['Symbol']])
    close = pd.concat([delivery_per_age['Close']])
    df = pd.DataFrame(na_me)
    df['TOTAL_OPN_INT'] = total_oi
    df['OI_COMBINED'] = oi_combined
    df['%_CHANGE'] = ((df['OI_COMBINED'] / df['TOTAL_OPN_INT']) * 100).__round__(0)
   
    
    pd.set_option('display.max_columns',8)
    pd.set_option('display.width',200)
    print(df)

PRODUCT:-

              Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                      
2022-07-26  RELIANCE       24406250      6664500      27.0
2022-07-27  RELIANCE       30434500      6028250      20.0
2022-07-28  RELIANCE       36177500      5743000      16.0
2022-07-29  RELIANCE       35629250      -548250      -2.0
2022-08-01  RELIANCE       33920750     -1708500      -5.0
2022-08-02  RELIANCE       32738250     -1182500      -4.0
2022-08-03  RELIANCE       32026500      -711750      -2.0
2022-08-04  RELIANCE       32886500       860000       3.0
              Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                      
2022-07-26  HDFCBANK       44094050     12837550      29.0
2022-07-27  HDFCBANK       53098100      9004050      17.0
2022-07-28  HDFCBANK       58785650      5687550      10.0
2022-07-29  HDFCBANK       59424200       638550       1.0
2022-08-01  HDFCBANK       60106200       682000       1.0
2022-08-02  HDFCBANK       60987300       881100       1.0
2022-08-03  HDFCBANK       60483500      -503800      -1.0
2022-08-04  HDFCBANK       60819550       336050       1.0
           Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                   
2022-07-26   INFY       27968100     10026900      36.0
2022-07-27   INFY       32902800      4934700      15.0
2022-07-28   INFY       36741900      3839100      10.0
2022-07-29   INFY       36555000      -186900      -1.0
2022-08-01   INFY       36683100       128100       0.0
2022-08-02   INFY       36653700       -29400      -0.0
2022-08-03   INFY       36848700       195000       1.0
2022-08-04   INFY       36459900      -388800      -1.0
               Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                       
2022-07-26  ICICIBANK       50990500     10811625      21.0
2022-07-27  ICICIBANK       59917000      8926500      15.0
2022-07-28  ICICIBANK       65434875      5517875       8.0
2022-07-29  ICICIBANK       64421500     -1013375      -2.0
2022-08-01  ICICIBANK       63976000      -445500      -1.0
2022-08-02  ICICIBANK       64975625       999625       2.0
2022-08-03  ICICIBANK       64824375      -151250      -0.0
2022-08-04  ICICIBANK       64097000      -727375      -1.0
           Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                   
2022-07-26   HDFC       14214900      4004400      28.0
2022-07-27   HDFC       16781100      2566200      15.0
2022-07-28   HDFC       21082800      4301700      20.0
2022-07-29   HDFC       21459600       376800       2.0
2022-08-01   HDFC       21417300       -42300      -0.0
2022-08-02   HDFC       21621300       204000       1.0
2022-08-03   HDFC       21690900        69600       0.0
2022-08-04   HDFC       21563100      -127800      -1.0
           Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                   
2022-07-26    TCS        8746050      2746050      31.0
2022-07-27    TCS       10440150      1694100      16.0
2022-07-28    TCS       12167850      1727700      14.0
2022-07-29    TCS       11899800      -268050      -2.0
2022-08-01    TCS       11961300        61500       1.0
2022-08-02    TCS       12141900       180600       1.0
2022-08-03    TCS       12310350       168450       1.0
2022-08-04    TCS       12492900       182550       1.0
               Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                       
2022-07-26  KOTAKBANK       10294000      4272400      42.0
2022-07-27  KOTAKBANK       13121600      2827600      22.0
2022-07-28  KOTAKBANK       14876800      1755200      12.0
2022-07-29  KOTAKBANK       14772000      -104800      -1.0
2022-08-01  KOTAKBANK       15180000       408000       3.0
2022-08-02  KOTAKBANK       15558000       378000       2.0
2022-08-03  KOTAKBANK       15645200        87200       1.0
2022-08-04  KOTAKBANK       15792800       147600       1.0
           Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                   
2022-07-26     LT        6851700      1901100      28.0
2022-07-27     LT        8606700      1755000      20.0
2022-07-28     LT        9540300       933600      10.0
2022-07-29     LT        9676200       135900       1.0
2022-08-01     LT        9579600       -96600      -1.0
2022-08-02     LT        9432300      -147300      -2.0
2022-08-03     LT        9510600        78300       1.0
2022-08-04     LT        9499200       -11400      -0.0
           Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                   
2022-07-26   SBIN       30426000     10119000      33.0
2022-07-27   SBIN       43723500     13297500      30.0
2022-07-28   SBIN       48078000      4354500       9.0
2022-07-29   SBIN       45868500     -2209500      -5.0
2022-08-01   SBIN       47425500      1557000       3.0
2022-08-02   SBIN       50124000      2698500       5.0
2022-08-03   SBIN       52092000      1968000       4.0
2022-08-04   SBIN       51882000      -210000      -0.0
                Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                        
2022-07-26  HINDUNILVR        6886200      1464900      21.0
2022-07-27  HINDUNILVR        8522700      1636500      19.0
2022-07-28  HINDUNILVR       10300200      1777500      17.0
2022-07-29  HINDUNILVR       10250100       -50100      -0.0
2022-08-01  HINDUNILVR       10237200       -12900      -0.0
2022-08-02  HINDUNILVR       10178700       -58500      -1.0
2022-08-03  HINDUNILVR       10208400        29700       0.0
2022-08-04  HINDUNILVR       10289700        81300       1.0
              Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                      
2022-07-26  AXISBANK       38370000     13545600      35.0
2022-07-27  AXISBANK       44377200      6007200      14.0
2022-07-28  AXISBANK       48842400      4465200       9.0
2022-07-29  AXISBANK       48660000      -182400      -0.0
2022-08-01  AXISBANK       48901200       241200       0.0
2022-08-02  AXISBANK       50166000      1264800       3.0
2022-08-03  AXISBANK       50004000      -162000      -0.0
2022-08-04  AXISBANK       50222400       218400       0.0
           Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                   
2022-07-26    ITC       52278400     13782400      26.0
2022-07-27    ITC       66179200     13900800      21.0
2022-07-28    ITC       78844800     12665600      16.0
2022-07-29    ITC       83827200      4982400       6.0
2022-08-01    ITC       85734400      1907200       2.0
2022-08-02    ITC       86812800      1078400       1.0
2022-08-03    ITC       83555200     -3257600      -4.0
2022-08-04    ITC       80704000     -2851200      -4.0
                Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                        
2022-07-26  BAJFINANCE        3364500      1196625      36.0
2022-07-27  BAJFINANCE        4470500      1106000      25.0
2022-07-28  BAJFINANCE        4969750       499250      10.0
2022-07-29  BAJFINANCE        4754000      -215750      -5.0
2022-08-01  BAJFINANCE        4698125       -55875      -1.0
2022-08-02  BAJFINANCE        4670750       -27375      -1.0
2022-08-03  BAJFINANCE        4645625       -25125      -1.0
2022-08-04  BAJFINANCE        4619000       -26625      -1.0
                Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                        
2022-07-26  BHARTIARTL       31892450     11726800      37.0
2022-07-27  BHARTIARTL       41211950      9319500      23.0
2022-07-28  BHARTIARTL       50717650      9505700      19.0
2022-07-29  BHARTIARTL       52344050      1626400       3.0
2022-08-01  BHARTIARTL       53248450       904400       2.0
2022-08-02  BHARTIARTL       53561950       313500       1.0
2022-08-03  BHARTIARTL       53350100      -211850      -0.0
2022-08-04  BHARTIARTL       53362450        12350       0.0
                Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                        
2022-07-26  ASIANPAINT        4498800      1840000      41.0
2022-07-27  ASIANPAINT        5360400       861600      16.0
2022-07-28  ASIANPAINT        5885400       525000       9.0
2022-07-29  ASIANPAINT        5864200       -21200      -0.0
2022-08-01  ASIANPAINT        5812200       -52000      -1.0
2022-08-02  ASIANPAINT        5809800        -2400      -0.0
2022-08-03  ASIANPAINT        5773000       -36800      -1.0
2022-08-04  ASIANPAINT        5824800        51800       1.0
             Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                     
2022-07-26  HCLTECH       16972200      4148900      24.0
2022-07-27  HCLTECH       19371800      2399600      12.0
2022-07-28  HCLTECH       21725200      2353400      11.0
2022-07-29  HCLTECH       21765100        39900       0.0
2022-08-01  HCLTECH       21652400      -112700      -1.0
2022-08-02  HCLTECH       21272300      -380100      -2.0
2022-08-03  HCLTECH       21593600       321300       1.0
2022-08-04  HCLTECH       21654500        60900       0.0
            Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                    
2022-07-26  MARUTI        2804400      1075200      38.0
2022-07-27  MARUTI        3538900       734500      21.0
2022-07-28  MARUTI        3836200       297300       8.0
2022-07-29  MARUTI        3983700       147500       4.0
2022-08-01  MARUTI        3996700        13000       0.0
2022-08-02  MARUTI        4102600       105900       3.0
2022-08-03  MARUTI        3949400      -153200      -4.0
2022-08-04  MARUTI        3952000         2600       0.0
           Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                   
2022-07-26  TITAN        3439875      1219125      35.0
2022-07-27  TITAN        4491000      1051125      23.0
2022-07-28  TITAN        5237625       746625      14.0
2022-07-29  TITAN        5311875        74250       1.0
2022-08-01  TITAN        5392875        81000       2.0
2022-08-02  TITAN        5452500        59625       1.0
2022-08-03  TITAN        5470500        18000       0.0
2022-08-04  TITAN        5572125       101625       2.0
                Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                        
2022-07-26  BAJAJFINSV         616100       193350      31.0
2022-07-27  BAJAJFINSV         776000       159900      21.0
2022-07-28  BAJAJFINSV         868250        92250      11.0
2022-07-29  BAJAJFINSV         816100       -52150      -6.0
2022-08-01  BAJAJFINSV         785600       -30500      -4.0
2022-08-02  BAJAJFINSV         788650         3050       0.0
2022-08-03  BAJAJFINSV         772850       -15800      -2.0
2022-08-04  BAJAJFINSV         746550       -26300      -4.0
                Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                        
2022-07-26  TATAMOTORS       38274075     12728100      33.0
2022-07-27  TATAMOTORS       52608150     14334075      27.0
2022-07-28  TATAMOTORS       70717050     18108900      26.0
2022-07-29  TATAMOTORS       69433125     -1283925      -2.0
2022-08-01  TATAMOTORS       70537500      1104375       2.0
2022-08-02  TATAMOTORS       69673950      -863550      -1.0
2022-08-03  TATAMOTORS       67837125     -1836825      -3.0
2022-08-04  TATAMOTORS       67834275        -2850      -0.0
           Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                   
2022-07-26  TECHM       14919000      4667400      31.0
2022-07-27  TECHM       18929400      4010400      21.0
2022-07-28  TECHM       22117800      3188400      14.0
2022-07-29  TECHM       22616400       498600       2.0
2022-08-01  TECHM       22501800      -114600      -1.0
2022-08-02  TECHM       22698600       196800       1.0
2022-08-03  TECHM       22839600       141000       1.0
2022-08-04  TECHM       22904400        64800       0.0
               Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                       
2022-07-26  SUNPHARMA        7263200      3342500      46.0
2022-07-27  SUNPHARMA       14949900      7686700      51.0
2022-07-28  SUNPHARMA       18085200      3135300      17.0
2022-07-29  SUNPHARMA       20848100      2762900      13.0
2022-08-01  SUNPHARMA       20908300        60200       0.0
2022-08-02  SUNPHARMA       20686400      -221900      -1.0
2022-08-03  SUNPHARMA       21007000       320600       2.0
2022-08-04  SUNPHARMA       21337400       330400       2.0
               Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                       
2022-07-26  TATASTEEL       15523550      5135700      33.0
2022-07-27  TATASTEEL       20983950      5460400      26.0
2022-07-28  TATASTEEL      247078000    226094050      92.0
2022-07-29  TATASTEEL      239398250     -7679750      -3.0
2022-08-01  TATASTEEL      248765250      9367000       4.0
2022-08-02  TATASTEEL      243975500     -4789750      -2.0
2022-08-03  TATASTEEL      241000500     -2975000      -1.0
2022-08-04  TATASTEEL      240758250      -242250      -0.0
           Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                   
2022-07-26    M&M        8032500      2580200      32.0
2022-07-27    M&M       10152100      2119600      21.0
2022-07-28    M&M       10845100       693000       6.0
2022-07-29    M&M       11348400       503300       4.0
2022-08-01    M&M       11429600        81200       1.0
2022-08-02    M&M       11151000      -278600      -2.0
2022-08-03    M&M       11196500        45500       0.0
2022-08-04    M&M       11816700       620200       5.0
           Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                   
2022-07-26  WIPRO       29145000      9058000      31.0
2022-07-27  WIPRO       38028000      8883000      23.0
2022-07-28  WIPRO       44330000      6302000      14.0
2022-07-29  WIPRO       44173000      -157000      -0.0
2022-08-01  WIPRO       43964000      -209000      -0.0
2022-08-02  WIPRO       42742000     -1222000      -3.0
2022-08-03  WIPRO       41634000     -1108000      -3.0
2022-08-04  WIPRO       39661000     -1973000      -5.0
                Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                        
2022-07-26  ULTRACEMCO        1822300       469300      26.0
2022-07-27  ULTRACEMCO        2157000       334700      16.0
2022-07-28  ULTRACEMCO        2222100        65100       3.0
2022-07-29  ULTRACEMCO        2168600       -53500      -2.0
2022-08-01  ULTRACEMCO        2078700       -89900      -4.0
2022-08-02  ULTRACEMCO        2094200        15500       1.0
2022-08-03  ULTRACEMCO        2036500       -57700      -3.0
2022-08-04  ULTRACEMCO        2013200       -23300      -1.0
               Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                       
2022-07-26  POWERGRID       41042700     16669800      41.0
2022-07-27  POWERGRID       47209500      6166800      13.0
2022-07-28  POWERGRID       49596300      2386800       5.0
2022-07-29  POWERGRID       51262200      1665900       3.0
2022-08-01  POWERGRID       51135300      -126900      -0.0
2022-08-02  POWERGRID       49148100     -1987200      -4.0
2022-08-03  POWERGRID       45613800     -3534300      -8.0
2022-08-04  POWERGRID       44012700     -1601100      -4.0
              Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                      
2022-07-26  HINDALCO       19059750      6948800      36.0
2022-07-27  HINDALCO       24745425      5685675      23.0
2022-07-28  HINDALCO       30913775      6168350      20.0
2022-07-29  HINDALCO       30877225       -36550      -0.0
2022-08-01  HINDALCO       28327325     -2549900      -9.0
2022-08-02  HINDALCO       26728800     -1598525      -6.0
2022-08-03  HINDALCO       26794375        65575       0.0
2022-08-04  HINDALCO       27211475       417100       2.0
           Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                   
2022-07-26   NTPC       46358100     20565600      44.0
2022-07-27   NTPC       54326700      7968600      15.0
2022-07-28   NTPC       62933700      8607000      14.0
2022-07-29   NTPC       67641900      4708200       7.0
2022-08-01   NTPC       67225800      -416100      -1.0
2022-08-02   NTPC       67282800        57000       0.0
2022-08-03   NTPC       66353700      -929100      -1.0
2022-08-04   NTPC       62244000     -4109700      -7.0
               Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                       
2022-07-26  NESTLEIND         256560       139920      55.0
2022-07-27  NESTLEIND         345520        88960      26.0
2022-07-28  NESTLEIND         395240        49720      13.0
2022-07-29  NESTLEIND         396520         1280       0.0
2022-08-01  NESTLEIND         388440        -8080      -2.0
2022-08-02  NESTLEIND         389280          840       0.0
2022-08-03  NESTLEIND         390400         1120       0.0
2022-08-04  NESTLEIND         392600         2200       1.0
            Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                    
2022-07-26  GRASIM       10256200      2748350      27.0
2022-07-27  GRASIM       11433725      1177525      10.0
2022-07-28  GRASIM       11896850       463125       4.0
2022-07-29  GRASIM       11830350       -66500      -1.0
2022-08-01  GRASIM       11571000      -259350      -2.0
2022-08-02  GRASIM       11541075       -29925      -0.0
2022-08-03  GRASIM       11362475      -178600      -2.0
2022-08-04  GRASIM       11183400      -179075      -2.0
           Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                   
2022-07-26   ONGC       39350850     11992750      30.0
2022-07-27   ONGC       46153800      6802950      15.0
2022-07-28   ONGC       45957450      -196350      -0.0
2022-07-29   ONGC       45052700      -904750      -2.0
2022-08-01   ONGC       45480050       427350       1.0
2022-08-02   ONGC       46061400       581350       1.0
2022-08-03   ONGC       48263600      2202200       5.0
2022-08-04   ONGC       47682250      -581350      -1.0
              Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                      
2022-07-26  JSWSTEEL       41593500      9583650      23.0
2022-07-27  JSWSTEEL       45940500      4347000       9.0
2022-07-28  JSWSTEEL       46996200      1055700       2.0
2022-07-29  JSWSTEEL       45624600     -1371600      -3.0
2022-08-01  JSWSTEEL       45524700       -99900      -0.0
2022-08-02  JSWSTEEL       45318150      -206550      -0.0
2022-08-03  JSWSTEEL       45300600       -17550      -0.0
2022-08-04  JSWSTEEL       45576000       275400       1.0
              Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                      
2022-07-26  HDFCLIFE       18042200      4721200      26.0
2022-07-27  HDFCLIFE       24107600      6065400      25.0
2022-07-28  HDFCLIFE       30991400      6883800      22.0
2022-07-29  HDFCLIFE       32433500      1442100       4.0
2022-08-01  HDFCLIFE       32698600       265100       1.0
2022-08-02  HDFCLIFE       32905400       206800       1.0
2022-08-03  HDFCLIFE       33080300       174900       1.0
2022-08-04  HDFCLIFE       33061600       -18700      -0.0
                Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                        
2022-07-26  INDUSINDBK       22698000      4559400      20.0
2022-07-27  INDUSINDBK       25984800      3286800      13.0
2022-07-28  INDUSINDBK       27224100      1239300       5.0
2022-07-29  INDUSINDBK       26694000      -530100      -2.0
2022-08-01  INDUSINDBK       25869600      -824400      -3.0
2022-08-02  INDUSINDBK       26315100       445500       2.0
2022-08-03  INDUSINDBK       26329500        14400       0.0
2022-08-04  INDUSINDBK       26091000      -238500      -1.0
             Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                     
2022-07-26  SBILIFE        3927000      1581000      40.0
2022-07-27  SBILIFE        5461500      1534500      28.0
2022-07-28  SBILIFE        6110250       648750      11.0
2022-07-29  SBILIFE        7089000       978750      14.0
2022-08-01  SBILIFE        6964500      -124500      -2.0
2022-08-02  SBILIFE        6978750        14250       0.0
2022-08-03  SBILIFE        6897750       -81000      -1.0
2022-08-04  SBILIFE        6791250      -106500      -2.0
             Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                     
2022-07-26  DRREDDY        1435000       704375      49.0
2022-07-27  DRREDDY        1753125       318125      18.0
2022-07-28  DRREDDY        2011375       258250      13.0
2022-07-29  DRREDDY        2667250       655875      25.0
2022-08-01  DRREDDY        2681625        14375       1.0
2022-08-02  DRREDDY        2735625        54000       2.0
2022-08-03  DRREDDY        2735000         -625      -0.0
2022-08-04  DRREDDY        2580000      -155000      -6.0
                Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                        
2022-07-26  ADANIPORTS       66928750      7380000      11.0
2022-07-27  ADANIPORTS       73210000      6281250       9.0
2022-07-28  ADANIPORTS       76423750      3213750       4.0
2022-07-29  ADANIPORTS       76556250       132500       0.0
2022-08-01  ADANIPORTS       76650000        93750       0.0
2022-08-02  ADANIPORTS       76297500      -352500      -0.0
2022-08-03  ADANIPORTS       75886250      -411250      -1.0
2022-08-04  ADANIPORTS       75835000       -51250      -0.0
              Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                      
2022-07-26  DIVISLAB        1836000       766200      42.0
2022-07-27  DIVISLAB        2267700       431700      19.0
2022-07-28  DIVISLAB        2399550       131850       5.0
2022-07-29  DIVISLAB        2419350        19800       1.0
2022-08-01  DIVISLAB        2467800        48450       2.0
2022-08-02  DIVISLAB        2503950        36150       1.0
2022-08-03  DIVISLAB        2511900         7950       0.0
2022-08-04  DIVISLAB        2540700        28800       1.0
           Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                   
2022-07-26  CIPLA        4988750      3086850      62.0
2022-07-27  CIPLA        6264050      1275300      20.0
2022-07-28  CIPLA        8102900      1838850      23.0
2022-07-29  CIPLA        9441900      1339000      14.0
2022-08-01  CIPLA        9381450       -60450      -1.0
2022-08-02  CIPLA        9222850      -158600      -2.0
2022-08-03  CIPLA        8828300      -394550      -4.0
2022-08-04  CIPLA        8856250        27950       0.0
                Symbol  TOTAL_OPN_INT  OI_COMBINED  %_CHANGE
Date                                                        
2022-07-26  BAJAJ-AUTO        1353000       413500      31.0
2022-07-27  BAJAJ-AUTO        1749000       396000      23.0
2022-07-28  BAJAJ-AUTO        1951500       202500      10.0
2022-07-29  BAJAJ-AUTO        1852250       -99250      -5.0
2022-08-01  BAJAJ-AUTO        1924000        71750       4.0
2022-08-02  BAJAJ-AUTO        1961500        37500       2.0
2022-08-03  BAJAJ-AUTO        1940000       -21500      -1.0
2022-08-04  BAJAJ-AUTO        1964250        24250       1.0

               

I have created a screener which shows stocks data of Indian markets. after running the code the product which getting to me is shown above. in this product how can I add a condition that is [ the stocks should have positive %_CHANGE(column name) for at least previous 4 to 5 days in a row and suddenly if product shows negative %_CHANGE then only that stock name should occur after running the code.] In my code all positive and negative data is showing so how can I eliminate this and get the stocks which follows my criteria.
So please tell me the code to solve the problem. I have shared my code and product. Thank you.

CodePudding user response:

A few lines of code are sufficient to check the condition and add the stock's symbol.

# ... your code before the loop

target_stocks = []

for stock in stock:
    
    # ... your code in the loop

    print(df)
    
    # check condition
    cond_loc = ((df.loc[df.index[-5:-1], '%_CHANGE'].agg(min) > 0) |(df.loc[df.index[-6:-1], '%_CHANGE'].agg(min) > 0)) & (df.loc[df.index[-1], '%_CHANGE'] < 0) 
    if(cond_loc):
        target_stocks.append(df['Symbol'][0])

However, please note that your condition is rather restrictive. Often, the list of target_stocks will remain empty (such as today). An elementary explanation based on some probability theory provides an intuition why that is.

Assume that a stock moves up or down with ~50% probability (this assumption is fairly realistic, btw.) Then, for example, 4 consecutive up-movements followed by a single down-movement has a chance of 0.5^(4 1)=0.03125=3.125% assuming stochastic independence between the movements (another very realistic assumption w.r.t. stock price movements). That probability is about a mere 3%. The chance that not a single of your 13 (initially provided) stocks displays this behavior is, therefore (0.0312)^13, approximately 66% and quite likely. Lowering the number of days with up-movement to 2 provides only a single stock for today's data, namely INFY.

For the bigger dataset of 50 stocks, this chance shrinks to around 20% though - in theory. Since stock price movements usually display a positive correlation (due to an overlap in industries/markets etc.) it will be more likely to observe comovement - if the overall market conditions are conducive.

  • Related