Algorithmic Stock Trading Strategy Using Python

stock_trading_strategy

Stock Price Prediction Using Python & Machine Learning

stock_prediction

create Solr server with ansible roles from Galaxy

Apache Solr is a fast and scalable search server optimized for full-text search, word highlighting, faceted search, fast indexing, and more. It’s a very popular search server, and it’s pretty easy to install and configure using Ansible.

Step 1. Getting roles from Galaxy
https://galaxy.ansible.com/

ansible-galaxy install geerlingguy.java geerlingguy.solr

Step 2. create solr.yml

---
- hosts: all
  become: yes
  
  roles:
    - geerlingguy.java
    - geerlingguy.solr

Step 3. run it with ansible-playbook solr.yml, then you can open http://ubuntu2004:8983/solr

if you didn’t install roles, the playbook.yml could be:

---
- hosts: all
  become: true

  vars_files:
    - vars.yml

  pre_tasks:
    - name: Update apt cache if needed.
      apt: update_cache=true cache_valid_time=3600

  tasks:
    - name: Install Java.
      apt: name=openjdk-11-jdk state=present

    - name: Download Solr.
      get_url:
        url: "https://archive.apache.org/dist/lucene/solr/{{ solr_version }}/solr-{{ solr_version }}.tgz"
        dest: "{{ download_dir }}/solr-{{ solr_version }}.tgz"
        checksum: "{{ solr_checksum }}"

    - name: Expand Solr.
      unarchive:
        src: "{{ download_dir }}/solr-{{ solr_version }}.tgz"
        dest: "{{ download_dir }}"
        remote_src: true
        creates: "{{ download_dir }}/solr-{{ solr_version }}/README.txt"

    - name: Run Solr installation script.
      command: >
        {{ download_dir }}/solr-{{ solr_version }}/bin/install_solr_service.sh
        {{ download_dir }}/solr-{{ solr_version }}.tgz
        -i /opt
        -d /var/solr
        -u solr
        -s solr
        -p 8983
        creates={{ solr_dir }}/bin/solr

    - name: Ensure solr is started and enabled on boot.
      service: name=solr state=started enabled=yes

Helpful Galaxy commands:
ansible-galaxy list displays a list of installed roles, with version numbers
ansible-galaxy remove [role] removes an installed role

Using ansible-vault to store ssh and sudo password

  1. edit /etc/ansible/ansible.cfg
    host_key_checking = False
  2. edit /etc/ansible/hosts
    [all:vars]
    ansible_connection=ssh
    ansible_ssh_user=your_sshid
    ansible_ssh_pass=your_sshpassword
    ansible_sudo_pass=your_sudopassword
    [appservers]
    192.168.0.[1-3]
  3. edit playbook.yml
---
-hosts: all
 vars_files:
 - vars.yml
 tasks:
  -name: download file from link
   get_url:
    url:http://github.com/test.txt
    dest: /tmp/test.txt
    mode: 0755
  -name: ensure directory exists
   file:
     path: "{{ jboss_conf_location }}"
     state: directory
     owner: jboss
     group: jboss
  -name: copy file
   command: >
     cp /tmp/test.txt {{ jboss_conf_location }}
   become: yes
   become_user: jboss
  -name: remove file
   file:
     path: "{{ jboss_conf_location }}/test.txt"
     state: absent
   become: yes
   become_user: jboss   

then you can run it with ansible-playbook playbook.yml

there is another way to store password, ansible-vault create passwd.yml

ansible_ssh_user: your_sshid
ansible_ssh_pass: your_sshpassword
ansible_sudo_pass: your_sudopassword

then you can run it with command:

ansible-playbook --ask-vault-pass --extra-vars '@passwd.yml' playbook.yml

you can run Shell command with sudo password input:

ansible appservers -m shell -a "source /home/pm2/.bashrc && pm2 status" -b --become-user=pm2 -K

scp files:

- name: Copy configuration files.
  copy:
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
    owner: root
    group: root
    mode: 0644
  with_items:
    - src: httpd.conf
      dest: /etc/httpd/conf/httpd.conf
    - src: httpd-vhosts.conf
      dest: /etc/httpd/conf/httpd-vhosts.conf

Make sure Apache or other service is started now and at boot:

- name: Make sure Apache is started now and at boot
  service: name=httpd state=started enabled=yes

yum install apache or other packages:

- name: Install Apache.
  yum:
    name:
      - httpd
      - httpd-devel
    state: present

lineinfile edit:

- name: Adjust OpCache memory setting.
  lineinfile:
    dest: "/etc/php/7.1/apache2/conf.d/10-opcache.ini"
    regexp: "^opcache.memory_consumption"
    line: "opcache.memory_consumption = 96"
    state: present
  notify: restart apache

git clone:

- name: Check out drush 8.x branch.
  git:
    repo: https://github.com/drush-ops/drush.git
    version: 8.x
    dest: /opt/drush

run command in one line:

- name: Install Drush dependencies with Composer.
  command: >
    /usr/local/bin/composer install
    chdir=/opt/drush
    creates=/opt/drush/vendor/autoload.php

create file link:

- name: Create drush bin symlink.
  file:
    src: /opt/drush/drush
    dest: /usr/local/bin/drush
    state: link

list files in directory:

    - shell: 'ls -ltr /opt/jboss/log/'
      register: ps

    - debug: var=ps.stdout_lines

How to Install Anaconda and Jupyter on Ubuntu 20.04

STEP1. Commands:
sudo apt install libgl1-mesa-glx libegl1-mesa libxrandr2 libxrandr2 libxss1 libxcursor1 libxcomposite1 libasound2 libxi6 libxtst6
wget -P /tmp https://repo.anaconda.com/archive/Anaconda3-2020.02-Linux-x86_64.sh
bash /tmp/Anaconda3-2020.02-Linux-x86_64.sh
source ~/.bashrc
conda update –all

STEP2. anaconda-navigator to open the Navigator GUI in terminal:

STEP3. install jupyter notebook extension and start it

pip install jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable –user
conda install -c conda-forge jupyter_contrib_nbextensions
jupyter notebook –ip 0.0.0.0
then you can open http://192.168.226.128:8888/tree#notebooks

You can enable Table of Content to add TOC for your notebook!

Query google geocoding API with python

  1. Create your application’s API key:
  • Go to the Google API Console https://console.cloud.google.com/apis/dashboard
  • From the projects list, select a project or create a new one.
  • If the APIs & services page isn’t already open, open the left side menu and select APIs & services.
  • On the left, choose Credentials.
  • Click Create credentials and then select API key.
  1. Enable Geocoding API:
  • Log in to the Google API Manager Console here: https://console.developers.google.com/apis/library
  • Click the Library link in the left sidebar
  • Select the project you created when you created your API Key for WP Google Maps (See the top arrow in the screenshot below)
  • Click the link to the Google Maps Geocoding API
  • Click Enable on the Google Maps Geocoding API window.
  1. Verify your KEY in browser https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=your-key
  1. Create geocode.py
import json
import time
import urllib.error
import urllib.request
import urllib.parse

api_file = 'your-key'
base_url = "https://maps.googleapis.com/maps/api/geocode/json"

def geoapi(address):
    params = urllib.parse.urlencode({"address": address, "key": api_file})
    url = f"{base_url}?{params}"
    response = urllib.request.urlopen(url)
    result = json.load(response)
    return result

if __name__ == "__main__":
    goecode = geoapi('3600 Steeles Ave E, Markham, ON')
    print(goecode)
  1. run it with python geocode.py

Get started with machine learning in Databricks

  1. create Azure Databricks and launch workspace

2. import jupyter notebook into workspace

import Notebooks URL: https://docs.microsoft.com/en-us/azure/databricks/_static/notebooks/getting-started/get-started-sklearn-7.3.html

3. create cluster
we can select Databricks Runtime Version: 8.1 ML (includes Apache Spark 3.1.1, Scala 2.12)
Worker Type: Standard_D12_v2

4. Attach the cluster to notebook and click “Run All”