Skip to content

Commit 1b74886

Browse files
author
Sara Galante
committed
ReadME and associated files
0 parents  commit 1b74886

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

LICENSE

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2023, The MathWorks, Inc.
2+
All rights reserved.
3+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
5+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
6+
3. In all cases, the software is, and all modifications and derivatives of the software shall be, licensed to you solely for use in conjunction with MathWorks products and service offerings.
7+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Trading Analysis in MATLAB using Python DataFrames
2+
3+
![front_1](images/front_1.png)
4+
5+
This repository demonstrates how MATLAB® and Python® users can collaborate seamlessly to implement and test algorithmic trading strategies through table-to-DataFrame conversion and custom Live Tasks. Co-execution offers a significant time-saving advantage by allowing direct execution of pre-existing Python code alongside MATLAB. In environments where financial developers work across multiple teams, it’s often necessary to interface with or inherit code from both platforms. Recoding between these environments can be time-consuming, costly, and generally impractical. Instead, developers can execute Python code directly within MATLAB and easily convert between pandas DataFrames and MATLAB Tables, simplifying collaboration. This demo showcases how to use co-execution to create a custom Live Task that generates synthetic data in MATLAB tables, converts the tables to Python DataFrames for executing pre-written Python trading strategies, and finally converts the DataFrames back into MATLAB tables for analysis.
6+
7+
# Co-execution Overview and Benefits
8+
9+
Co-execution involves using two frameworks in tandem to create a single, unified application. In this example, data is passed between MATLAB Tables and Python DataFrames using [recent functionality introduced in MATLAB version 2024a](https://www.mathworks.com/help/matlab/matlab_external/python-pandas-dataframes.html). This approach offers several benefits. By interfacing multiple frameworks, development time and costs can be reduced, as teams no longer need to code in the same language. This enables teams to work in their preferred environments, avoiding the need to learn and retool code in unfamiliar languages. Co-executing Python with MATLAB leverages MATLAB's accessible functionality, allowing users to explore the parameter space and fine-tune trading strategies more efficiently.
10+
11+
## MATLAB Tables and Python DataFrames
12+
13+
MATLAB Tables and Python DataFrames are both data structures that are used for organizing and analyzing tabular data. Interfacing between these two is essential when integrating MATLAB with Python workflows, which is now possible through MATLAB version 2024a. Conversion between the two data types is simple and fundamental to the demo in this page. A MATLAB Table can be converted to a Python DataFrame, as well as the reverse, through one line of code, shown in the red boxes in the image below.
14+
15+
![conversions](images/conversions.png)
16+
17+
# Custom Live Tasks
18+
19+
[Live Tasks](https://www.mathworks.com/help/matlab/matlab_prog/add-live-editor-tasks-to-a-live-script.html) are small graphical user interfaces (GUI) that can be added to [live scripts](https://www.mathworks.com/help/matlab/matlab_prog/what-is-a-live-script-or-function.html) to execute specific MATLAB commands. A variety of Live Tasks are built into MATLAB, ranging from simple operations like plotting and visualization to more complex tasks like optimization. Live Tasks also have the capability to generate reusable code from the parameters that the user selected. In this demo, a custom Live Task provides a GUI that allows the user to explore the free parameters of the demo in a user-friendly way. The Live Task interfaces MATLAB with Python code under the hood. The gif below demonstrates how to use the Live Task, with editable fields and a drop down menu, while viewing the results on the fly.
20+
21+
![trading_gif](images/trading_gif.gif)
22+
23+
# Getting started
24+
25+
Download or clone this repository to your machine and open it in MATLAB.
26+
27+
# Setup
28+
29+
Requires:
30+
* [MATLAB version 2024a or later](https://www.mathworks.com/products/matlab.html)
31+
* [Python 3.9+](https://www.python.org/downloads/)
32+
33+
For more information about installing Python to interface with MATLAB, see [Install Supported Python Implementation](https://www.mathworks.com/help/matlab/matlab_external/install-supported-python-implementation.html).
34+
35+
## Custom Live Task Creation
36+
37+
Creating a custom Live Task takes a [few steps](https://www.mathworks.com/help/matlab/creating_guis/create-task-from-selection.html) - the major steps are highlighted below:
38+
39+
1. Write your code in a Live Script
40+
2. [Add interactive controls](https://www.mathworks.com/help/matlab/matlab_prog/add-interactive-controls-to-a-live-script.html) for exploratory parameters in your Live Script. This includes edit fields, dropdown boxes, and other types of controls. The Live Script with interactive controls written for this repo is shown below.
41+
42+
![rawcode](images/rawcode.png)
43+
44+
3. Select your code and go to the Live Editor tab in the MATLAB task bar. Click on the "Task" dropdown menu
45+
a. Under "Create", click on the "Convert to Live Task" option, as shown below
46+
47+
![task_creation_2](images/task_creation_2.png)
48+
49+
b. Name your Live Task and click on the OK button. A new .m will appear in the Current Folder.
50+
4. You can use the new Live Task by clicking on the "Task" dropdown menu and selecting it under "My Tasks"
51+
52+
## Using the Algorithmic Trading Live Task
53+
54+
To launch the custom algo trading Live Task in this repo:
55+
* Right-click on the "CustomLiveTask" folder and select "Add to Path" before following any of the setup options below.
56+
* Click on the "CustomLiveTask" folder. You will see three files in the following formats: .m, .py, and .matlab_external
57+
* To install the Live Task to your instance of MATLAB, enter the following command into the Command Window: matlab.task.configureMetadata . Select the "Algo_Trading.m" file in the window below
58+
![setup_1](images/setup_1.png)
59+
* A new window will arrive that asks you to specify the details of the Live Task, as shown below. Click "OK" when prompted:
60+
![setup_2](images/setup_2.png)
61+
* Open the "Trading_TestScript" Live Script. You will see the algorithmic trading Live Task come up automatically, as shown below
62+
![livetask_1](images/livetask_1.png)
63+
* In total, you will see four parameters that you may alter
64+
* Under "Control Synthetic Data", you have three free parameters (Initial Price, Volatility, and Drift) with edit fields that allow you enter numerical values
65+
* "Select Trading Strategy" allows you to select one of three trading strategies, which include Mean Reversion, Momentum, and Simple Moving Average
66+
* The results will change automatically when changing the parameters. On the right hand side, you will see a graph of the PnL, as well as six associated metrics for the trading algorithm's performance.
67+
68+
# Conclusion
69+
70+
The goal of this demo was to show:
71+
72+
* How to smoothly interface MATLAB with Python by seamlessly switching between tables and DataFrames
73+
* What custom Live Tasks are and how to use them
74+
* Build a foundation for finance professionals to integrate Python models with MATLAB's unique functionality and interfacing
75+
76+
# License
77+
78+
The license is available in the License.txt file in this GitHub repository.
79+
80+
# Contact
81+
82+
Sara Galante at sgalante@mathworks.com, Application Engineering
83+
84+
Copyright 2024 The MathWorks, Inc.

SECURITY.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Reporting Security Vulnerabilities
2+
3+
If you believe you have discovered a security vulnerability, please report it to
4+
security@mathworks.com. Please see
5+
MathWorks Vulnerability Disclosure Policy for Security Researchers
6+
for additional information.

0 commit comments

Comments
 (0)