Search This Blog

Tuesday, September 24, 2019

Python - Map Nested Map

Input

[{
"groupId": "a1",
"orderId": "b1"
}, {
"groupId": "a2",
"orderId": "b2"
}, {
"groupId": "a2",
"orderId": "b3"
}]

Output 
{
'a1': {
'b1': {
'groupId': 'a1',
'orderId': 'b1'
}
},
'a2': {
'b2': {
'groupId': 'a2',
'orderId': 'b2'
},
'b3': {
'groupId': 'a2',
'orderId': 'b3'
}
}
}


Code

data = [{"groupId":"a1","orderId":"b1"},{"groupId":"a2","orderId":"b2"}, {"groupId":"a2","orderId":"b3"}]

groupMap = {}


for x in data:
    if ( helpers.isKeyExists (groupMap, x['groupId']) == False ):
        orderMap = {}
        orderMap[x['orderId']] = x;
        groupMap[x['groupId']] = orderMap;
    else:
        tempOrderMap = {}
        #if ( helpers.isKeyExists (orderMap, x['groupId']) == False ):
        tempOrderMap = groupMap[x['groupId']]
        tempOrderMap[x['orderId']] = x;
        groupMap[x['groupId']] = tempOrderMap;
       

print (groupMap);


Tuesday, September 17, 2019

Excel VBA - Loading POST REST Webservice JSON

1. Open excel -> developer -> visual basic
2. Tools -> reference (enabled : Microsoft win http services , version 5.1 xx )
3. paste the below code on click sheet1.
4. Click run.
5. Save As excel macro enabled from file type . else your VBA code will not be save.


Dim strResult As String
Dim objHTTP As Object
Dim URL As String
  
Private Sub Worksheet_Activate()
    Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    
    json_body = "{'Commodity':'CRUDEOIL','Expiry':'17SEP2019'}"
    URL = "https://www.mcxindia.com/backpage.aspx/GetOptionChain"
    
    objHTTP.Open "POST", URL, False
    objHTTP.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
    objHTTP.SetRequestHeader "Content-type", "application/json"
    objHTTP.Send (json_body)
    strResult = objHTTP.ResponseText
    Worksheets("Sheet1").Range("A10:A10") = strResult
    
End Sub




Friday, August 30, 2019

Python Schedule at every specific Time Interval

1. Install schedule plugin
2. use the below sample

schedulers.py

# Schedule Library imported
import schedule
import time
import datetime

def doScheduler ():
    print ( "Schedule starts @ ", datetime.datetime.now() );

# Task scheduling
# After every 10mins doScheduler () is called. 
schedule.every(10).minutes.do(doScheduler )
  
# After every hour doScheduler () is called.
schedule.every().hour.do(doScheduler )
  
# Every day at 12am or 00:00 time doScheduler () is called.
schedule.every().day.at("00:00").do(doScheduler )
  
# After every 5 to 10mins in between run doScheduler ()
schedule.every(5).to(10).minutes.do(doScheduler )
  
# Every monday doScheduler () is called
schedule.every().monday.do(doScheduler )
  
# Every tuesday at 18:00 doScheduler () is called
schedule.every().tuesday.at("18:00").do(doScheduler )

if __name__ == "__main__": schedule.every( 3 ).minutes.do( doScheduler )   
# Loop so that the scheduling task
# keeps on running all time.
while True:
  
     # Checks whether a scheduled task 
     # is pending to run or not
     schedule.run_pending()
     time.sleep(1)



#Enjoy !

Python Code to pull NSE Option Derivative HTML to JSON on Every N minutes

1. Install Python, PIP
2. type the below command from your python sample folder [ by pip or pip3 cmd ].
     pip install beautifulsoup
     OR
     pip install BeautifulSoup4
     pip install requests
     pip install schedule
     python nse_options.py

-- ENJOY

nse_options.py

# author - dr. vijay
# email - drvijayy2k2@gmail.com
# date - 30-08-2019 7:30 PM

# install plugin
# pip install BeautifulSoup

import sys
import json
import requests
# Schedule Library imported
import schedule
import time
import datetime

from bs4 import BeautifulSoup

base_url = 'https://www.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?symbol=';


def doScheduler ():
    print ( "Schedule starts @ ", datetime.datetime.now() );
    result = parseNseOptions ( 'NIFTY' );
    # result = parseNseOptions ( 'INFY' );
    f = open( 'D:/nseOptionst.txt', 'a' )
    f.write( str(result) + "\n" );
    print ( "Done !" );
 

def parseNseOptions ( scrip='NIFTY' ):
    url = base_url + scrip;
    response = response = requests.get( url ) 
    if( response != None and response.ok ):
        optionScripListHtml = response.content
        # print ( optionScripListHtml );
        soup = BeautifulSoup( optionScripListHtml, "html.parser" )
        tableHtml = soup.find( "table", id="octable" )
        f1 = tableHtml.findAll( "thead" )[0].findAll( 'tr' )
     
        callDataColumnEndsAt = 11
        optionStrikePriceColumnAt = ( 12 - 1 )
        putDataColumnEndsAt = 23
        headers = {}
        headersOnIndex = {}
        h1 = f1[1].findAll( "th" )
        for index, h in enumerate( h1, start=0 ):
            if ( index < callDataColumnEndsAt ):
                headers["call-" + h.text] = h.get( "title", "" )
                headersOnIndex[index] = "call-" + h.text 
            elif ( index == optionStrikePriceColumnAt ):
                 headers[h.text] = h.get( "title", "" )
                 headersOnIndex[index] = h.text 
            elif ( index < putDataColumnEndsAt ):
                headers["put-" + h.text] = h.get( "title", "" )
                headersOnIndex[index] = "put-" + h.text 
        # print(headersOnIndex)
     
        fnoDataRows = tableHtml.findAll( "tr" )
        tempMap = {}
        totalOptionPCOI = {}
        tempMapWithStrikePriceAsKey = {}
        startingRow = 3
        endingRow = len( fnoDataRows )
        callOIChangeCol = 2
        callTotalChangeInOIValue = 0
        putOIChangeCol = 20
        putTotalChangeInOIValue = 0
        index = 1
        colIndex = 1
        recordFound = False
     
        for rowIndex, x in enumerate( fnoDataRows, start=0 ):
            if ( rowIndex == endingRow - 1 ):
                xx = x.findAll( "td" )
                totalOptionPCOI = {"callTotalOI": xx[1].text, "callTotalChangeInOI": xx[2].text, "callTotalVolume": xx[3].text, "putTotalVolume": xx[5].text, "putTotalChangeInOI": xx[6].text, "putTotalOI": xx[7].text }
                # break the entire loop as we got all the information
                break;
            for colIndex, c in enumerate( x.findAll( "td" ), start=0 ):
                value = c.text.strip()
                if value == "" or value == "-":
                    value = "0"
                else:
                    value = value.replace( ",", "" )
             
                if ( colIndex == callOIChangeCol ):
                    callTotalChangeInOIValue = callTotalChangeInOIValue + int( value )
                if ( colIndex == putOIChangeCol ):
                    putTotalChangeInOIValue = putTotalChangeInOIValue + int( value )
                     
                tempMap[ headersOnIndex[colIndex]] = value
                recordFound = True
             
            if recordFound == True:
                tempMapWithStrikePriceAsKey[tempMap[headersOnIndex[optionStrikePriceColumnAt]]] = tempMap.copy()
                strikePrice = tempMap["Strike Price"]
                tempMap.clear()   

            recordFound = False
     
        return dict( { "resultFetchTime": datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "totalOptionPCOI":totalOptionPCOI, "mapWithStrikePriceAsKey":tempMapWithStrikePriceAsKey} )


if __name__ == "__main__":
 
    schedule.every( 3 ).minutes.do( doScheduler )
    while True:
        schedule.run_pending()
        time.sleep( 1 )


OUTPUT

{
'mapWithStrikePriceAsKey': {
//..... other strike prices
'10850.00': {
'call-Chart': '0',
'call-OI': '49950',
'call-Chng in OI': '26475',
'call-Volume': '10689',
'call-IV': '14.71',
'call-LTP': '210.75',
'call-Net Chng': '29.00',
'call-BidQty': '300',
'call-BidPrice': '208.85',
'call-AskPrice': '215.95',
'call-AskQty': '75',
'Strike Price': '10850.00',
'put-BidQty': '300',
'put-BidPrice': '19.50',
'put-AskPrice': '20.45',
'put-AskQty': '600',
'put-Net Chng': '-25.55',
'put-LTP': '20.45',
'put-IV': '14.92',
'put-Volume': '134223',
'put-Chng in OI': '261600',
'put-OI': '399750',
'put-Chart': '0'
},
'10900.00': {
'call-Chart': '0',
'call-OI': '574650',
'call-Chng in OI': '198600',
'call-Volume': '157457',
'call-IV': '14.15',
'call-LTP': '169.05',
'call-Net Chng': '26.85',
'call-BidQty': '4350',
'call-BidPrice': '169.05',
'call-AskPrice': '170.85',
'call-AskQty': '75',
'Strike Price': '10900.00',
'put-BidQty': '150',
'put-BidPrice': '27.45',
'put-AskPrice': '28.20',
'put-AskQty': '525',
'put-Net Chng': '-33.60',
'put-LTP': '28.20',
'put-IV': '14.22',
'put-Volume': '396350',
'put-Chng in OI': '894450',
'put-OI': '1796100',
'put-Chart': '0'
}
  //..... other strike prices
},
'totalOptionPCOI': {
'callTotalOI': ' 12,057,225',
'callTotalChangeInOI': '',
'callTotalVolume': ' 2,231,841',
'putTotalVolume': ' 1,883,484',
'putTotalChangeInOI': '',
'putTotalOI': ' 12,479,775'
}

}

Tuesday, August 27, 2019

Telegram - Broadcast or Send Message through Programming Java Python C# VB

1. Create a bot token through @botfather.
           1.1 Install Telegram
           1.2 Login to telegram
           1.3 on Search window type @botfather and select
           1.4 type one by one until you see Done !, Congratulations , your token
                     /start
                     /newbot
                    drvautobot     (username)
                   
                   Done !, ...
                   token    123456:dsfd..

2. Copy this token and keep it.
   
3. Open telegram and create new channel.
     Ex: autobot
     3.1 open autobot channel window
     3.2 click on top to see administrators , click
     3.3 search @drvautobot (bot username), select
     3.4 assign as administrator

Thats it.
Now use browser or programing with GET method.

browser
https://api.telegram.org/bot/sendMessage?chat_id=@&text=hello

ex: https://api.telegram.org/bot123456:dsfdadsfsdfdsfahgjhgsfd/sendMessage?chat_id=@autobot&text=hello

response 
{"ok":true,"result":{"message_id":2,"chat":{"id":-1001493064021,"title":"Autobot","username":"infovijay","type":"channel"},"date":1566909775,"text":"hello"}}




open your channel window and see the text hello.


Enjoy

Thursday, August 22, 2019

Install Python 3.x 3.7 in Ubuntu 18 from Bundle

Steps

1.   Donwload the tar - Python-3.7.4.tar.xz
2.  cd /home/ubuntu/
3.  tar xf Python-3.7.4.tar.xz
4   chmod 775 Python-3.7.4
5   chown -R root Python-3.7.4
6   chown -R root Python-3.7.4
7   cd Python-3.7.4/
8   ./configure
9   ./configure --enable-optimizations
10  make
11  make test
12  sudo make install
13  python3 --version
14  pip3
15  which python3


Your Sample Rest Application
1  cd /opt/stock/stockautobot
2  pip3 install -r requirements.txt
3  python3 app.py


Any package/module missed. please use below command to add
1  pip3 install requests


RUN
1 wget http://127.0.0.1:5002/
2 curl http://localhost:5002/


Monday, August 19, 2019

Friday, August 16, 2019

SCP copy with hidden files

scp -rp /folder/.[!.]*  root@172.16.1.9:/folder

Tuesday, August 13, 2019

Python : websocket-client 0.56.0 error: [Errno 2] No such file or directory: script.tmpl

1. Create a file script.tmpl under C:\Users\xxx\AppData\Roaming\Python\Python37\site-packages

2. paste the below code

# EASY-INSTALL-SCRIPT: %(spec)r,%(script_name)r
__requires__ = %(spec)r
__import__('pkg_resources').run_script(%(spec)r, %(script_name)r)


3. run your setup again
py setup.py install


-- Enjoy



Thursday, August 8, 2019

AngularJS setup in Linux

1. Remove the older NPM / NODE / NG modules
    sudo apt purge ng-common ng-latin
    sudo apt purge node
    sudo apt purge npm
    sudo apt-get update

2. Install new version
     curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash - sudo 
OR
     curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash
      apt-get install -y nodejs
OR
     sudo apt-get update  
     apt-cache policy nodejs  
     sudo apt install nodejs
     sudo npm install -g @angular/cli

   npm --version
   node --version

3. Go to your folder
    npm install

4. type
    ng serve --host=ip

 OR to run in background
    nohup ng serve --host=ip &


RUN = http://ip:4200

Hit Counter


View My Stats