Showing posts with label Concurrency and Parallelism. Show all posts
Showing posts with label Concurrency and Parallelism. Show all posts

27 March 2021

Create a Watchdog in Python

 What is Watchdog in Python

Watchdog is a python module that can be used to monitor file system changes. As the name suggests this module observes the given directory and can notify if a file is created or changed.

Step 1: Import some Stuff

import os
import time
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler

Step 2: Create the event handler


if __name__ == "__main__":
    # patterns = "*" # the file patterns we want to handle
    patterns = "*.csv" # choose only csv files
    ignore_patterns = ""
    ignore_directories = True
    case_sensitive = True
    # Create the event handler
    my_event_handler = PatternMatchingEventHandler(patterns, ignore_patterns, ignore_directories, case_sensitive)

03 February 2021

Handling Callback data between threads in TWS API - Interactive Brokers Part 5

Scenario:

After I place a market order using the TWS API, sometimes I do not receive the orderStatus callback for a very unusually long time. How then can I find out the status of the placed order ?

Important notes concerning IBApi.EWrapper.orderStatus :

  • Typically there are duplicate orderStatus messages with the same information that will be received by a client. This corresponds to messages sent back from TWS, the IB server, or the exchange.
  • There is no guarantee that an orderStatus callbacks will be triggered for every change in order status. For example with market orders when the order is accepted and executes immediately, there commonly will not be any corresponding orderStatus callbacks. For that reason it is recommended to monitor the IBApi.EWrapper.execDetails function in addition to IBApi.EWrapper.orderStatus.

22 September 2020

Thread Synchronization using Event Object in Python - Interactive Brokers Part 4

This post uses the Interactive Brokers Python TWS API to explain Thread Synchronization using Event Object.

In many applications, sometimes, we need to pause the running of the program until some external condition occurs. You may need to wait until another thread finishes, or another callback is processed. In these situations and and many other similar situations you will need to figure out a way to make your script wait. Three common ways to achieve this are mentioned: 
  1. This is sometimes achieved by using a "trial and error approach using the time.sleep() function in Python". While this may work, it is always a problematic issue in determining the amount of time we need to wait. And using the time.sleep() in such cases is not the right way of achieving the result. 
  2. Another approach is to use flags to arrive at a better approximation in using the time.sleep() function.
  3. Use the Event Object to achieve thread synchronization

03 June 2017

Get Real Time Data from Google Finance for NSE - Multi Threaded

This program is very similar to Get Real Time Data from Google for NSE . The difference is that this program uses concurrent.futures module in Python 3.5 to launch parallel tasks
  • The purpose of this program is to retrieve  Google Intraday Data for a list of symbols from the NSE stock exchange . Designed to run until 15:30 hrs Indian Standard Time(IST)
  • The Input file for this program is an Excel Spreadsheet. Name it as "stock_symbols". Enter the list of stocks in the  first column of the sheet for which you would like to retrieve the Google Intraday Data. 
  • This is a multi threaded program that uses the  concurrent.futures module (only from version Python 3.2) for asynchronously executing callables (Synchronous tasks are essentially Sequential in nature ie; when you do something synchronously, you wait for it to finish before moving on to another task. When you do something asynchronously, you can move on to next task before it finishes.)
  • The following arguments are required for the program :  sheetname time_in_minutes
  • Write's the output in OHLCV format in a CSV file
            When tested on a Intel I3-2367M processor for a list of 200 stocks:
            • For sequential processing , the time taken was about 30 seconds.
            • For asynchronous processing, the time taken was about 16 seconds.



            Test it yourself and verify the results for yourself. The results will vary based upon the machine that you use and your net speed.

            Program on GitHub  

            Cheers Maadi !!!