AlgoBulls Python Trading Strategy Task

0% found this document useful (0 votes)
95 views7 pages
This document outlines a coding assignment to design a simple algorithmic trading strategy. The requirements include: 1. Creating a ScriptData class to fetch and format intraday stock data f…

Uploaded by

Palak
  • Task 1: Create ScriptData Class
  • Coding Assignment Overview
  • Task 2: Define Indicator Function
  • Task 3: Create Strategy Function
  • Task 4: Plot Results
  • Tools and Objectives
  • Submission Guidelines

AlgoBulls Python Developer

Coding Assignment
December 2022

Project: Design a simple Algorithmic Trading Strategy

Description: You need to code a simple trading strategy in a Jupyter Notebook as per the
given requirements:

1. Define a Class ScriptData which can fetch US Stock data using Alpha Vantage.
[Use this link to get your FREE API Key].
The class should implement the following methods:
a. fetch_intraday_data: (method arguments: script)
Fetches intraday data for given “script” (Example for script: “GOOGL”,
“AAPL”) and stores as it is.
b. convert_intraday_data: (method arguments: script)
Converts fetched intraday data (in point a.) as a pandas DataFrame
(hereafter referred as “df”) with the following columns:
i. timestamp (data type: [Link])
ii. open (data type: float)
iii. high (data type: float)
iv. low(data type: float)
v. close (data type: float)
vi. volume (data type: int)
c. Additional methods for overloading the following operations:
i. getitem
ii. setitem
iii. contains

Sample code showing how the above class will be used:


(The output data may differ for you based on which date you run this code, but the format should be the same)

(The output data may differ for you based on which date you run this code, but the format should be the same)
2. Define a function called indicator1. It should take “df” and ‘timeperiod’ (integer) as
inputs and give another pandas DataFrame as an output with two columns:
a. timestamp: Same as ‘timestamp’ column in ‘df’
b. indicator: Moving Average of the ‘close’ column in ‘df’. The number of
elements to be taken for a moving average is defined by ‘timeperiod’. For
example, if ‘timeperiod’ is 5, then each row in this column will be an average
of total 5 previous values (including current value) of the ‘close’ column.

Some sample code has been given below which shows how the above function will be used:
3. Define a class Strategy, which can do the following, given a script name:
a. Fetch intraday historical day (‘df’) using ScriptData class.
We’ll refer to the ‘close’ column of ‘df’ as close_data.
b. Compute indicator data on ‘close’ of ‘df’ using indicator1 function.
We’ll refer to the ‘indicator’ column of this data as indicator_data.
c. Generate a pandas DataFrame called ‘signals’ with 2 columns:
i. ‘timestamp’: Same as ‘timestamp’ column in ‘df’
ii. ‘signal’: This column can have the following values:
1. BUY (When: If indicator_data cuts close_data upwards)
2. SELL (When: If indicator_data cuts close_data downwards)
3. NO_SIGNAL (When: If indicator_data and close_data don’t cut
each other)

Example of ‘Cut Upwards’, ‘Cut Downwards’, ‘Do not cut each


other’:
As an example, for the below graph, if the RED line is
close_data and GREY line is indicator_data, then:
1. The BLUE points represent the instances when
indicator_data has cut close_data ‘downwards’
2. The PINK points represent the instances when
indicator_data has cut close_data ‘upwards
3. The YELLOW points represent when indicator_data
and close_data don’t cut each other.
So, there will be SELL signal for BLUE timestamps, ‘BUY’
signal for PINK timestamp and ‘NO_SIGNAL’ for yellow
timestamps.
d. Print the ‘signals’ DataFrame with only those rows where the signal is either
‘BUY’ or ‘SELL’.

Sample code showing how the above class will be used:


4. [OPTIONAL] Plot a candlestick chart of ‘df and ‘indicator’. You can use
‘pyalgotrading’ to do so. The chart will look like this.

Technology to be used for creating this application:

1. Python 3.8+
2. Jupyter Notebook (latest)
3. 3rd party Python modules:
a. alpha_vantage
b. pandas
c. numpy
d. [OPTIONAL] pyalgotrading

Objective:
1. Please come up with a git repo containing a Jupyter Notebook that can
accommodate all the requirements.
2. The Jupyter Notebook should run seamlessly. Just calling the Kernel -> Restart &
Run All option to do all that is necessary. There should be no errors and no
unnecessary code in the notebook.
3. The dependent Python packages must be captured in a [Link] file which
can be installed inside a virtualenv easily. The Jupyter Notebook should also be run
after sourcing the virtualenv.

Duration:
The ideal time is 3 days. If you need extra time, please request the same with appropriate
reasoning.

How to submit:
1. Please upload your code on a private GitHub repo and share it with our GitHub
user-id - algobulls-dev, hruturaj-nikam-algobulls. Include the Postman Collection
as well in the repo.
2. Make sure your code is cleaned up as per PEP8 standards, before you do the final
submission. It is ok to keep pushing any number of intermediate code commits.
3. Add sufficient comments for complex logic, before you do the final submission.
4. Include a README that includes basic documentation on how to run the code.
5. Once done, please send a mail to developers@[Link] &
[Link]@[Link], mentioning your name and GitHub ID, requesting a
review. Attach this coding assignment pdf to the mail as well. We may request you to
set up a screen sharing session for discussion purposes.

Asking for clarification/hints:


Please send an email to developers@[Link] & [Link]@[Link] with
your query and we will get back to you.

Common questions

Powered by AI

Designing an algorithmic trading strategy involves ensuring methodical data fetching, processing, and strategy formulation within the limitations of a Jupyter Notebook. Key challenges include managing real-time data processing, implementing reliable and efficient algorithms, meeting technical requirements, and maintaining code readability and reproducibility. Furthermore, ensuring accurate signal generation through precise indicator calculations and handling intrinsic financial data complexities are significant considerations .

The decision-making process involves comparing 'indicator_data' with 'close_data'. A 'BUY' signal is generated when 'indicator_data' crosses 'close_data' upwards, a 'SELL' signal is issued when it crosses downwards, and 'NO_SIGNAL' is noted when no crossover occurs. Each type of crossover corresponds to distinct market movement predictions, crucial for trading strategy formulation .

The 'indicator1' function computes the Moving Average on the 'close' column of a provided DataFrame ('df'). It takes 'df' and an integer 'timeperiod' as inputs, and returns a DataFrame with two columns: the original 'timestamp' and the Moving Average of 'close' over the specified time period. This involves averaging a specified number of preceding 'close' values, including the current value, for each point in the data .

The 'requirements.txt' file lists all third-party dependencies necessary for the project, including alpha_vantage, pandas, numpy, and potentially pyalgotrading. This file enables users to install these dependencies easily within a virtual environment, ensuring that the Jupyter Notebook runs smoothly without missing packages, which is crucial for the deployment and execution of the trading strategy .

The 'Strategy' class comprises the following critical components: a) It uses the 'ScriptData' class to fetch intraday historical data ('df'), referring to the 'close' column as 'close_data'. b) It computes indicator data on 'close' using the 'indicator1' function and refers to the result as 'indicator_data'. c) It generates a 'signals' DataFrame with timestamps and signals, which can be 'BUY', 'SELL', or 'NO_SIGNAL', based on the intersection of 'indicator_data' and 'close_data' .

API handling involves using the Alpha Vantage API, where data fetching is encapsulated in the 'ScriptData' class with methods specifically designed for intraday data retrieval and conversion into a pandas DataFrame. Data handling further includes managing API keys, ensuring data accuracy, and implementing overloading operations. These components are architected to integrate seamlessly within the Jupyter Notebook, emphasizing reproducibility and clarity .

To define the 'ScriptData' class, you need to include methods for fetching and processing data: a) 'fetch_intraday_data' takes a 'script' as an argument to download intraday stock data using Alpha Vantage. b) 'convert_intraday_data' converts this data into a pandas DataFrame with specified columns: timestamp, open, high, low, close, and volume. c) Implement method overloading for operations such as getitem, setitem, and contains. Possible solutions must adhere to the requirements, allowing for interaction through method calls .

Submission guidelines involve uploading the code onto a private GitHub repository, ensuring compliance with PEP8 standards, and including detailed comments for complex logic. The submission should also contain a comprehensive README and a Postman Collection. Upon completion, participants must notify the designated contacts via email with the GitHub repository link and coding assignment details, following which a potential screen sharing session may be scheduled for discussions .

The recommended technology stack includes Python 3.8+, Jupyter Notebook for development, and third-party Python modules such as alpha_vantage, pandas, numpy, and optionally pyalgotrading for visualization. This setup facilitates efficient development and testing of algorithmic trading strategies outlined in the requirements .

The 'Strategy' class should output a 'signals' DataFrame containing timestamps and corresponding signals, filtered to include only 'BUY' or 'SELL' entries for clarity in decision-making. These outputs should be part of a clear and concise DataFrame, adhering to the formats specified in the outline, allowing easy interpretation and use in trading operations .

AlgoBulls Python Developer
Coding Assignment
December 2022
Project: Design a simple Algorithmic Trading Strategy
Description:
(The output data may differ for you based on which date you run this code, but the format should be the same)
(The output dat
2.
Define a function called indicator1. It should take “df” and ‘timeperiod’ (integer) as
inputs and give another pandas Data
3.
Define a class Strategy, which can do the following, given a script name:
a.
Fetch intraday historical day (‘df’) using Sc
d.
Print the ‘signals’ DataFrame with only those rows where the signal is either
‘BUY’ or ‘SELL’.
Sample code showing how the
4.
[OPTIONAL] Plot a candlestick chart of ‘df and ‘indicator’. You can use
‘pyalgotrading’ to do so. The chart will look like
2.
Make sure your code is cleaned up as per PEP8 standards, before you do the final
submission. It is ok to keep pushing any

You might also like