Pages - Menu

標籤

AWS (1) bash (1) Boost (2) C (2) CMake (2) Concurrency_Programming (3) CPP (37) Database (2) DNS (1) Docker (4) Docker-Compose (1) ELK (1) emacs (4) gcp (1) gdrive (1) git (1) gitbash (2) gitlab (1) kvm (4) Linux (5) MT4 (4) MT5 (4) Multicast (2) MySQL (2) Nijatrader8 (1) OpenCV (1) Python (4) QT5 (1) R (1) rdp (3) screenshot (1) ssh (3) Tabnine (1) TCP (1) TensorFlow (1) Tools (12) Ubuntu_1904 (11) Ubuntu_20_04 (5) UDP (1) VS2010 (1) VS2015 (1) VS2019 (1) WebServer (1) Win10 (1) winmerge (1) WSL (1) xrdp (1)

搜尋此網誌

2019年8月15日星期四

gdrive cheat sheet

Aim

  • We want to upload file to google drive by command line on ubuntu 18.04 server

Steps

  • Install gdrive
    • # Install gdrive
      $ wget https://docs.google.com/uc?id=0B3X9GlR6EmbnWksyTEtCM0VfaFE&export=download
      $ mv uc\?id\=0B3X9GlR6EmbnWksyTEtCM0VfaFE gdrive
      $ chmod +x gdrive
      $ sudo install gdrive /usr/local/bin/gdrive
      
  • Connect gdrive to your google drive
    • # Connect gdrive with google drive, just use any command, for example
      $ gdrive list
      # Follow the steps suggested by gdrive
      # You can find the token file from
      $ ls ~/.gdrive
      
  • Normal use case
    • # After that you can upload file to target folder, you can list and see the location
      $ gdrive list
      $ gdrive upload --parent die231 backup.txt
      

Reference

2019年8月14日星期三

Increase disk space in google vps gcp

Aim

  • The default disk space is 10 GB. /dev/sda1
  • Filesystem      Size  Used Avail Use% Mounted on
    udev            1.8G     0  1.8G   0% /dev
    tmpfs           370M  876K  369M   1% /run
    /dev/sda1       9.6G  3.6G   6.0G   38% /
    tmpfs           1.9G     0  1.9G   0% /dev/shm
    tmpfs           5.0M     0  5.0M   0% /run/lock
    tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
    /dev/loop0       89M   89M     0 100% /snap/core/7270
    /dev/loop1       89M   89M     0 100% /snap/core/7396
    /dev/loop2       61M   61M     0 100% /snap/google-cloud-sdk/93
    /dev/loop3       61M   61M     0 100% /snap/google-cloud-sdk/94
    /dev/sda15      105M  3.6M  101M   4% /boot/efi
    tmpfs           370M     0  370M   0% /run/user/1001
    
  • We want to increase the disk space

Learning meterial

  • You may need to get the auth login for the first time to use gcloud
    • $ gcloud auth login
      
    • Follow the steps provided by gcloud
  • Get the DISK_NAME
    • $ gcloud compute instances describe garch-trader | grep disk
      Did you mean zone [us-central1-a] for instance: [garch-trader] (Y/n)? Y
      disks:
        source: https://www.googleapis.com/compute/v1/projects/idyllic-silo-248808/zones/us-central1-a/disks/garch-trader
      
  • Resize the disk
    • $ gcloud compute disks resize garch-trader --zone us-central1-a --size 50GB
      $ sudo growpart /dev/sda 1
      $ sudo resize2fs /dev/sda1
      
  • DONE

2019年8月12日星期一

Share Folder Remmina RDP remote desktop client

Learn From

Steps

  1. Create a Remmina RDP connection to the windows machine
  2. Choose a share folder
  3. In Advanced tab, change sound to local. Save it and connect

2019年8月11日星期日

Database for trading data. The first step.

Aim

  • This document shows how to repeat the result from quantstart
  • We will:
    • Set up a database to store OHLC trading data by using MySQL server.
    • Create an account for management.
    • Install vstudio for GUI management.
    • Using python scripts to show how to:
      • Download data to database
      • Get data from database

Learning meterial

Steps

  • Set up MySQL server on ubuntu 19.04 through docker
    • Install the latest docker. One may need to search the method by Google.
    • Set up docker, vstudio, and login to the server
    • Create database
      • mysql> CREATE DATABASE securities_master;
        mysql> USE securities_master;
        
    • Create user
      • # Create a new user and let remote access possible
        mysql> CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
        mysql> GRANT ALL PRIVILEGES ON securities_master.* TO 'username'@'localhost' WITH GRANT OPTION;
        mysql> CREATE USER 'username'@'%' IDENTIFIED BY 'password';
        mysql> GRANT ALL PRIVILEGES ON securities_master.* TO 'username'@'%' WITH GRANT OPTION;
        mysql> FLUSH PRIVILEGES;
        
    • Create tables, 
      • CREATE TABLE `exchange` (
          `id` int NOT NULL AUTO_INCREMENT,
          `abbrev` varchar(32) NOT NULL,
          `name` varchar(255) NOT NULL,
          `city` varchar(255) NULL,
          `country` varchar(255) NULL,
          `currency` varchar(64) NULL,
          `timezone_offset` time NULL,
          `created_date` datetime NOT NULL,
          `last_updated_date` datetime NOT NULL,
          PRIMARY KEY (`id`)
        ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
        CREATE TABLE `data_vendor` (
          `id` int NOT NULL AUTO_INCREMENT,
          `name` varchar(64) NOT NULL,
          `website_url` varchar(255) NULL,
          `support_email` varchar(255) NULL,
          `created_date` datetime NOT NULL,
          `last_updated_date` datetime NOT NULL,
          PRIMARY KEY (`id`)
        ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
        CREATE TABLE `symbol` (
          `id` int NOT NULL AUTO_INCREMENT,
          `exchange_id` int NULL,
          `ticker` varchar(32) NOT NULL,
          `instrument` varchar(64) NOT NULL,
          `name` varchar(255) NULL,
          `sector` varchar(255) NULL,
          `currency` varchar(32) NULL,
          `created_date` datetime NOT NULL,
          `last_updated_date` datetime NOT NULL,
          PRIMARY KEY (`id`),
          KEY `index_exchange_id` (`exchange_id`)
        ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
        CREATE TABLE `daily_price` (
          `id` int NOT NULL AUTO_INCREMENT,
          `data_vendor_id` int NOT NULL,
          `symbol_id` int NOT NULL,
          `price_date` datetime NOT NULL,
          `created_date` datetime NOT NULL,
          `last_updated_date` datetime NOT NULL,
          `open_price` decimal(19,4) NULL,
          `high_price` decimal(19,4) NULL,
          `low_price` decimal(19,4) NULL,
          `close_price` decimal(19,4) NULL,
          `adj_close_price` decimal(19,4) NULL,
          `volume` bigint NULL,
          PRIMARY KEY (`id`),
          KEY `index_data_vendor_id` (`data_vendor_id`),
          KEY `index_synbol_id` (`symbol_id`)
        ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
        
    • At this moment, one should be able to view their database from GUI
  • Set up python, virtualenv for python2
    • link
    • pip install pandas
      pip install lxml
      pip install yfinance --upgrade --no-cache-dir
      sudo apt-get install libmysqlclient-dev
      pip install MySQL-python
      
  • Get symbols of S&P 500 from wiki by python and save to database, symbol table
    • #!/usr/bin/python
      # -*- coding: utf-8 -*-
      
      import datetime
      import lxml.html
      from lxml import etree
      from urllib2 import urlopen
      import MySQLdb as mdb
      
      from math import ceil
      
      def obtain_parse_wiki_snp500():
        """Download and parse the Wikipedia list of S&P500
        constituents using requests and libxml.
      
        Returns a list of tuples for to add to MySQL."""
      
        # Stores the current time, for the created_at record
        now = datetime.datetime.utcnow()
      
        # Use libxml to download the list of S&P500 companies and obtain the symbol table
        # page = lxml.html.parse("http://en.wikipedia.org/wiki/List_of_S%26P_500_companies") # this line is failed due to https problem
        page = lxml.html.parse(urlopen("http://en.wikipedia.org/wiki/List_of_S%26P_500_companies"))
        # print etree.tostring(page.getroot()) # check to see if the content has downloaded
        symbolslist = page.xpath('//table[1]/tbody/tr')[1:]
      
        # Obtain the symbol information for each row in the S&P500 constituent table
        symbols = []
        for symbol in symbolslist:
          tds = symbol.getchildren()
          sd = {'ticker': tds[0].getchildren()[0].text,
              'name': tds[1].getchildren()[0].text,
              'sector': tds[3].text}
          # Create a tuple (for the DB format) and append to the grand list
          symbols.append( (sd['ticker'], 'stock', sd['name'],
            sd['sector'], 'USD', now, now) )
        return symbols
      
      def insert_snp500_symbols(symbols):
        """Insert the S&P500 symbols into the MySQL database."""
      
        # Connect to the MySQL instance
        db_host = '127.0.0.1'
        db_user = 'sec_user'
        db_pass = '1234'
        db_name = 'securities_master'
        con = mdb.connect(host=db_host, user=db_user, passwd=db_pass, db=db_name)
      
        # Create the insert strings
        column_str = "ticker, instrument, name, sector, currency, created_date, last_updated_date"
        insert_str = ("%s, " * 7)[:-2]
        final_str = "INSERT INTO symbol (%s) VALUES (%s)" % (column_str, insert_str)
        print final_str, len(symbols)
      
        # Using the MySQL connection, carry out an INSERT INTO for every symbol
        with con:
          cur = con.cursor()
          # This line avoids the MySQL MAX_PACKET_SIZE
          # Although of course it could be set larger!
          for i in range(0, int(ceil(len(symbols) / 100.0))):
            cur.executemany(final_str, symbols[i*100:(i+1)*100-1])
      
      if __name__ == "__main__":
        symbols = obtain_parse_wiki_snp500()
        insert_snp500_symbols(symbols)
      
  • Get daily OHLC for symbols
    • #!/usr/bin/python
      # -*- coding: utf-8 -*-
      
      import datetime
      import MySQLdb as mdb
      import urllib2
      import yfinance as yf
      import math
      
      
      # Obtain a database connection to the MySQL instance
      db_host = '127.0.0.1'
      db_user = 'sec_user'
      db_pass = '1234'
      db_name = 'securities_master'
      con = mdb.connect(db_host, db_user, db_pass, db_name)
      
      def obtain_list_of_db_tickers():
        """Obtains a list of the ticker symbols in the database."""
        with con:
          cur = con.cursor()
          cur.execute("SELECT id, ticker FROM symbol")
          data = cur.fetchall()
          return [(d[0], d[1]) for d in data]
      
      def get_daily_historic_data_yahoo(ticker,
                            start_date=(2000,1,1),
                            end_date=datetime.date.today().timetuple()[0:3]):
          """Obtains data from Yahoo Finance returns and a list of tuples.
      
        ticker: Yahoo Finance ticker symbol, e.g. "GOOG" for Google, Inc.
        start_date: Start date in (YYYY, M, D) format
        end_date: End date in (YYYY, M, D) format"""
      
          # Construct the Yahoo URL with the correct integer query parameters
          # for start and end dates. Note that some parameters are zero-based!
          yahoo_url = "http://ichart.finance.yahoo.com/table.csv?s=%s&a=%s&b=%s&c=%s&d=%s&e=%s&f=%s" % \
            (ticker, start_date[1] - 1, start_date[2], start_date[0], end_date[1] - 1, end_date[2], end_date[0])
      
          startDate = str(start_date[0]) + '-' + str(start_date[1]) + '-' + str(start_date[2])
          endDate = str(end_date[0]) + '-' + str(end_date[1]) + '-' + str(end_date[2])
          # Try connecting to Yahoo Finance and obtaining the data
          # On failure, print an error message.
          try:
              # yf_data = urllib2.urlopen(yahoo_url).readlines()[1:] # Ignore the header
              yf_ticker = yf.Ticker(ticker)
              # yf_data = yf_ticker.history(period="max")
              # yf_data = yf_ticker.history(start=startDate, end=endDate)
              yf_data = yf.download(ticker, start=startDate, end=endDate)
              prices = []
              for index, row in yf_data.iterrows():
                p = str(index).strip().split(' ')
                if math.isnan(row['Open']) or math.isnan(row['High']) or math.isnan(row['Low']) or math.isnan(row['Close']) or math.isnan(row['Volume']) or math.isnan(row['Adj Close']):
                  continue
                prices.append( (datetime.datetime.strptime(p[0], '%Y-%m-%d'), row['Open'], row['High'], row['Low'], row['Close'], row['Volume'], row['Adj Close']) )
          except Exception, e:
              print "Could not download Yahoo data: %s" % e
          return prices
      
      def insert_daily_data_into_db(data_vendor_id, symbol_id, daily_data):
        """Takes a list of tuples of daily data and adds it to the
        MySQL database. Appends the vendor ID and symbol ID to the data.
      
        daily_data: List of tuples of the OHLC data (with
        adj_close and volume)"""
      
        # Create the time now
        now = datetime.datetime.utcnow()
      
        # Amend the data to include the vendor ID and symbol ID
        daily_data = [(data_vendor_id, symbol_id, d[0], now, now,
          d[1], d[2], d[3], d[4], d[5], d[6]) for d in daily_data]
      
        # Create the insert strings
        column_str = """data_vendor_id, symbol_id, price_date, created_date,
                last_updated_date, open_price, high_price, low_price,
                close_price, volume, adj_close_price"""
        insert_str = ("%s, " * 11)[:-2]
        final_str = "INSERT INTO daily_price (%s) VALUES (%s)" % (column_str, insert_str)
      
        # Using the MySQL connection, carry out an INSERT INTO for every symbol
        with con:
          cur = con.cursor()
          cur.executemany(final_str, daily_data)
      
      if __name__ == "__main__":
        # Loop over the tickers and insert the daily historical
        # data into the database
        tickers = obtain_list_of_db_tickers()
        for t in tickers:
          print "Adding data for %s" % t[1]
          yf_data = get_daily_historic_data_yahoo(t[1])
          insert_daily_data_into_db('1', t[0], yf_data)
      
  • Get the close price for a selected stock from database
    • #!/usr/bin/python
      # -*- coding: utf-8 -*-
      
      import pandas as pd
      import pandas.io.sql as psql
      import MySQLdb as mdb
      
      
      # Connect to the MySQL instance
      db_host = '127.0.0.1'
      db_user = 'sec_user'
      db_pass = '1234'
      db_name = 'securities_master'
      con = mdb.connect(db_host, db_user, db_pass, db_name)
      
      # Select all of the historic Google adjusted close data
      sql = """SELECT dp.price_date, dp.adj_close_price
               FROM symbol AS sym
               INNER JOIN daily_price AS dp
               ON dp.symbol_id = sym.id
               WHERE sym.ticker = 'GOOG'
               ORDER BY dp.price_date ASC;"""
      
      # Create a pandas dataframe from the SQL query
      # goog = psql.frame_query(sql, con=con, index_col='price_date')
      goog = psql.read_sql(sql, con=con, index_col='price_date')
      
      # Output the dataframe tail
      print goog.tail()
      

2019年8月10日星期六

Install Docker on ubuntu

Steps:

  • on Ubuntu 18.04 / 19.04

$ sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable edge"
$ sudo apt-get update
$ apt-cache policy docker-ce
$ sudo apt-get install -y docker-ce
$ sudo systemctl status docker

Learn From:

link

Steps:

  • on Ubuntu 20.04


sudo apt install docker.io
sudo systemctl enable --now docker
docker --version


Learn From

Use docker by non-root user:

To display all users run following command: 

 $ compgen -u

To display all groups run following command:
$ compgen -g
If there has no group docker, you can add it first:
$ sudo groupadd docker
Then add user to docker group:
$ sudo usermod -aG docker [non-root user] 

Restart or re logon to activate this setting.

2019年8月9日星期五

MySQL cheat sheet

Cheat Sheet


# Create a new user and let remote access possible
mysql> CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' WITH GRANT OPTION;
mysql> CREATE USER 'username'@'%' IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
#
# Show user in sql
# Show all MySQL users:
mysql> SELECT user FROM mysql.user;
# List only unique user names:
mysql> SELECT DISTINCT user FROM mysql.user;
# Show MySQL users and hosts they are allowed to connect from:
mysql> SELECT user,host FROM mysql.user;
# Show MySQL users, their passwords and hosts:
mysql> SELECT user,host,password FROM mysql.user;
# in MySQL 5.7 and higher:
mysql> SELECT host,user,authentication_string FROM mysql.user;
#
# Remove user
## Revoke all grants for a mysql user
mysql> REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'username'@'localhost';
mysql> DROP USER 'username'@'localhost';
mysql> FLUSH PRIVILEGES;
# Clean a table, delete all records
mysql> DELETE FROM tableName;
# select / use a database
mysql> USE databaseName;
# remove a table
mysql> DROP TABLE tablename;
# remove a database
mysql> DROP DATABASE dbname;
# login with other user name
mysql> mysql -h localhost -P 3306 --protocol=tcp -u userName -p
mysql> mysql -h 127.0.0.1 -P 3306 --protocol=tcp -u userName -p

Python virtualenv cheat sheet

Aim

  • Install and use python through virtualenv

Steps

  • Install python2 and python3
  • Set up python2 workspace
    • activate
    • do things
    • deactivate
  • Set up python3 workspace

$ sudo apt-get install python python3 python-dev python3-dev
$ sudo apt-get install python-virtualenv
$ sudo apt install python-pip
$ sudo apt install python3-pip
# python 2 environment
$ mkdir -p ./python-apps/env_python2
$ cd ./python-apps/env_python2
$ virtualenv .
$ source ./app/activate
#
# Do what you want
#
$ deactivate
# python 3 environment
$ mkdir -p ./python-apps/env_python3
$ cd ./python-apps/env_python3
$ virtualenv -p python3 .
$ source ./app/activate
#
# Do what you want
#
$ deactivate

Python virtualenv windows


# Install python in windows, remember to add python to Path
# open cmd.exe

$ pip install virtualenv

# Go to the working folder
$ virtualenv venv
$ C:\Your working folder\venv\Scripts\activate.bat

# Now you are in the virtualenv

 Python autocomplete in spacemacs

  • Create virtualenv in any folder
  • In spacemacs press SPC-mVa, then select the virtualenv
  • DONE

To make this work, you have to make sure the binary python is under <Your selected folder/bin> . If spacemacs cannot find the binary, you will see error message, (file-missing Searching for program No such file or directory python)

Docker MySql

Aim

Want to:

  • Run MySql through docker
  • Remote connect to MySql server
    • From host to docker container

Reference

Steps

  • Install and create new user so that we can do a remote connection
$ sudo docker pull mysql/mysql-server:latest
$ docker run -p 3306:3306 --name=mysql1 -d mysql/mysql-server:latest
# Get the auto generated password
$ docker logs mysql1 2>&1 | grep GENERATED
GENERATED ROOT PASSWORD: Axegh3kAJyDLaRuBemecis&EShOs
# Going into the docker container
$ docker exec -it mysql1 mysql -uroot -p
#
#
Server version: 8.0.17
#
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
#
# You needed to reset your password
mysql> SET PASSWORD = '12345678';
# Create a new user and let remote access possible
mysql> CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' WITH GRANT OPTION;
mysql> CREATE USER 'username'@'%' IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
  • Connect from host to docker container
$ mysql -h localhost -P 3306 --protocol=tcp -u sec_user -p

2019年8月4日星期日

Install GUI to ubuntu server on AWS Amazon

Aim


  • Install ubuntu server on AWS Amazon
  • Can use Remote desktop, RDP, to connect to that server

Reference

Steps

Install xrdp


$ sudo apt update &&  sudo apt upgrade
$ sudo sed -i 's/^PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config
$ sudo /etc/init.d/ssh restart
$ sudo passwd <userName>
$ sudo apt install xrdp xfce4 xfce4-goodies tightvncserver
$ echo xfce4-session > /home/ubuntu/.xsession
$ sudo cp /home/ubuntu/.xsession /etc/skel
$ sudo sed -i '0,/-1/s//ask-1/' /etc/xrdp/xrdp.ini
$ sudo service xrdp restart
$ netstat -antp

Make local redirect ssh tunnel


  • ssh -Llocalhost:8888:<VPS private IP>:3389 -i <private key from VPS> <VPS user>@<VPS domain>
    • VPS private IP:
    • Connection information, you can get from AWS:

Use Remmina from local ubuntu