Building a Trading Robot in Python | Pt. 1

Sigma Coding
24 May 202029:05

Summary

TLDRIn this video, the creator introduces a series on building a trading robot using Python, specifically with the TD Ameritrade API. Aimed at viewers with a TD Ameritrade account and a solid understanding of Python, the series will guide through the process from start to finish. The script emphasizes the importance of understanding the underlying mechanics of the robot, including handling price data, managing account activity, and calculating technical indicators. The presenter outlines the need for objects like the robot, stock frame, portfolio, trade, and indicator objects, and mentions that the library is available on GitHub for cloning, with future episodes promising a detailed, hands-on coding approach.

Takeaways

  • 😀 The video introduces a series on building a trading robot using Python, specifically with the TD Ameritrade API.
  • 🔑 It's assumed that viewers have a TD Ameritrade account, but the presenter may cover other APIs in the future.
  • 🛠️ The tutorial is aimed at individuals with a solid understanding of Python and familiarity with libraries like pandas and numpy.
  • 🚫 The presenter advises beginners or those not comfortable with programming to hold off on this series due to the complexity.
  • 💡 The series emphasizes the importance of understanding the underlying mechanisms of the trading robot for effective use.
  • 📈 The robot will need to fetch price quotes, manage account data, enter and exit positions, and calculate technical indicators.
  • 🧩 The script outlines the creation of several objects: Robot, StockFrame, Portfolio, Trade, and Indicators, each with specific roles in the trading process.
  • 💼 The 'Robot' object acts as the primary interface with the TD Ameritrade API, handling tasks like getting quotes and placing orders.
  • 📊 The 'StockFrame' object is designed to organize and manipulate price data, making it easier to work with financial data in pandas.
  • 💰 The 'Portfolio' object mimics account activity to aid in decision-making, such as avoiding duplicate positions.
  • 📉 The 'Indicators' object is intended to simplify the calculation of technical indicators, ensuring consistency and flexibility.

Q & A

  • What is the main focus of the video series being introduced?

    -The main focus of the video series is to guide viewers through the process of building a trading robot from start to finish using Python, specifically with the TD Ameritrade API.

  • What is a prerequisite for viewers before starting the series?

    -Viewers should have a TD Ameritrade account, and it is assumed that they have a relatively decent understanding of Python and familiarity with libraries like pandas or numpy.

  • Why is it important for viewers to understand what's happening behind the scenes when building a trading robot?

    -Understanding what's happening behind the scenes helps viewers grasp how the robot is structured, how different objects interact, and why it was built in a certain way, which is crucial for effectively using and potentially modifying the robot.

  • What are some of the complexities involved in trading that the video series aims to address?

    -The series addresses complexities such as understanding order types, technical indicators, and account activity, all of which are essential for effective trading and are incorporated into the programming of the trading robot.

  • How does the presenter plan to make the process of building a trading robot more approachable?

    -The presenter plans to make the process more approachable by starting at a high level, defining objectives, and then gradually diving into the programming aspects, ensuring that viewers understand the purpose and functionality of each component.

  • What is the significance of the 'robot' object in the context of the trading robot being built?

    -The 'robot' object is significant as it mimics the user's interaction with the TD Ameritrade account, handling all communications with the TD Ameritrade API, including getting quotes, placing orders, and retrieving account activity.

  • Why is the 'stock frame' object necessary in the trading robot?

    -The 'stock frame' object is necessary for organizing price data in a consistent and finance-oriented manner, making it easier to add, delete, select, and manipulate data for trading decisions.

  • What role does the 'portfolio' object play in the trading robot?

    -The 'portfolio' object mimics the user's account activity, helping to manage and organize information about current positions and active orders, which is crucial for making informed trading decisions.

  • How does the 'trade' object simplify the process of managing orders in the trading robot?

    -The 'trade' object simplifies order management by providing methods to easily modify orders, add components like stop losses, and check the status of orders, making the process less complex and more streamlined.

  • What is the purpose of the 'indicator' object in the trading robot?

    -The 'indicator' object is designed to calculate and define technical indicators in a consistent and easy-to-use manner, ensuring that the robot can use these indicators effectively for trading decisions.

  • Where can viewers find the current state of the trading robot library discussed in the video?

    -Viewers can find the trading robot library on the presenter's GitHub account under the repository named 'Python trading robot', where they can clone the repository and explore the code.

Outlines

00:00

🤖 Introduction to Building a Trading Robot with Python

The speaker introduces a new series focused on building a trading robot using Python, specifically with the TD Ameritrade API. It's mentioned that the series has been highly requested and will be structured to go from start to finish in creating the robot. The speaker emphasizes the importance of understanding the underlying processes of the robot, not just using the library. It's assumed that the viewer has a TD Ameritrade account and a decent understanding of Python and libraries like pandas or numpy. The speaker also advises those new to programming to familiarize themselves with basic programming concepts before proceeding with the series.

05:01

📈 The Importance of Defining Trading Objectives and Details

The speaker discusses the importance of being explicit about trading objectives and the details of trading strategies when building a trading robot. It's highlighted that many people struggle with defining their trading methods precisely, which becomes necessary when programming a robot. The speaker suggests that traders should write out their trading strategies to understand the finer details, such as order types and quantities. The paragraph also touches on the need to understand the big questions and objectives of the trading robot, such as obtaining price information, entering and exiting positions, and managing account data.

10:02

💼 Necessity of Account Activity and Technical Indicators in Trading

The speaker continues by emphasizing the need for a mechanism to obtain account activity and the importance of calculating technical indicators for trading decisions. It's explained that account activity can influence trading logic, such as not entering a new position if one already exists for the same ticker symbol. The speaker also discusses the challenges of calculating technical indicators and the intention to create a consistent and flexible way to calculate them through the use of an indicator object. The paragraph outlines the high-level objectives for the trading robot, focusing on the need for price information, position management, account data handling, and indicator calculations.

15:04

🛠️ Designing the Robot's Objects for Effective Trading

The speaker delves into the object-oriented programming approach for the trading robot, starting with the robot object that mimics the user's interaction with the TD Ameritrade API. The robot object is essential as it handles all interactions with the API, including getting quotes, placing orders, and retrieving account activity. The paragraph also introduces the concept of a stock frame object to organize price data and a portfolio object to mimic account activity. The speaker discusses the benefits of having these objects, such as ease of data manipulation and decision-making based on account data.

20:04

📊 Streamlining the Trading Process with Trade and Indicator Objects

The speaker further elaborates on the design of the trading robot by introducing the trade object, which encapsulates the complexity of orders, and the indicator object, which simplifies the calculation of technical indicators. The trade object is designed to handle modifications and additions to orders easily, while the indicator object ensures consistent and flexible calculation of indicators. The speaker also discusses the importance of having the stock frame and indicator objects communicate effectively, with the indicators recalculating as new data is added to the stock frame.

25:07

🔗 Accessing the Trading Robot Library and Preparing for the Next Steps

The speaker wraps up the presentation by directing viewers to the GitHub repository where the trading robot library is available for cloning. It's mentioned that the library is a work in progress and will undergo significant changes. The speaker provides instructions on how to clone the repository and gives a brief overview of its contents, including the main robot file, configuration requirements, and examples of how to use the library. The speaker encourages viewers to contribute to the project and hints at the next video, where the creation of the robot will begin from scratch.

Mindmap

Keywords

💡Trading Robot

A trading robot refers to an automated system or software that executes trades on behalf of a user based on predefined parameters and strategies. In the context of the video, the main theme revolves around building a trading robot using Python, specifically for the TD Ameritrade API. The video aims to guide viewers through the process of creating such a robot from scratch, highlighting its importance in automating and executing trades efficiently.

💡Python

Python is a high-level, versatile programming language that is widely used in various fields, including data analysis, artificial intelligence, and web development. In the video, Python is the chosen language for building the trading robot due to its readability, extensive library support, and its suitability for financial applications. The script mentions that viewers should have a decent understanding of Python to follow along with the coding process.

💡TD Ameritrade API

The TD Ameritrade API is a set of programming tools provided by TD Ameritrade that allows developers to access and interact with the brokerage's trading platform programmatically. The video series focuses on utilizing this API to build a trading robot that can fetch real-time market data, place orders, and manage account activities. The API is central to the robot's ability to perform trades and manage account information.

💡Pandas

Pandas is a powerful Python library used for data manipulation and analysis. It provides data structures and functions needed to work with structured data. In the video, the speaker mentions that the trading robot will use Pandas for organizing and manipulating financial data, such as stock prices and account activities, which are critical for making informed trading decisions.

💡Technical Indicators

Technical indicators are statistical tools used by traders to analyze and predict the price movements of financial markets. They are based on an asset's historical price, volume, or open interest. In the video, the speaker discusses the importance of calculating technical indicators as part of the trading robot's functionality, as they are used to generate trade signals and assess market conditions.

💡Object-Oriented Programming (OOP)

Object-Oriented Programming is a programming paradigm that uses objects and classes to design applications and complex software systems. The video script mentions that the trading robot will be built using OOP principles, which allow for a modular and organized approach to programming. This includes defining objects such as 'Robot', 'StockFrame', 'Portfolio', and 'Trade' to encapsulate specific functionalities and data.

💡Order Types

Order types refer to the various ways in which a trade can be executed in the financial markets. These include market orders, limit orders, stop orders, and more. The video script emphasizes the need for the trading robot to understand and execute different order types as part of its trading strategy, which is crucial for managing risk and ensuring that trades are executed as intended.

💡Account Activity

Account activity encompasses all the transactions and positions within a trading account. In the context of the video, managing account activity is a key function of the trading robot, which needs to track current positions, open orders, and other relevant data to make informed trading decisions and avoid conflicts, such as entering multiple positions in the same security.

💡GitHub

GitHub is a web-based platform for version control and collaboration that allows developers to work on projects, manage code, and collaborate with others. The video script mentions that the source code for the trading robot will be available on GitHub, allowing viewers to clone the repository, review the code, and even contribute to its development by submitting pull requests.

💡Unit Testing

Unit testing is a software development method where individual components of a program are tested to determine if they correctly perform their intended function. In the video, the speaker mentions that there is some unit testing included in the robot's codebase, which is important for ensuring that each part of the robot works as expected and for identifying and fixing bugs during development.

Highlights

Introduction to a series on building a trading robot using Python and the TD Ameritrade API.

Assumption that viewers have a TD Ameritrade account and a basic understanding of Python and libraries like pandas or numpy.

Emphasis on the importance of understanding the underlying processes of the trading robot for effective usage.

Advice for beginners to gain more programming knowledge before engaging with the series.

Explanation of the complexity involved in trading and the additional layer of complexity introduced by programming.

The necessity to define clear objectives for the trading robot at a high level.

The importance of being explicit about trading strategies and order types when programming a trading robot.

The need for a mechanism within the robot to obtain price information from the market.

Discussion on the functionality required for the robot to enter and exit trading positions based on signals.

The role of the robot in managing account data and its influence on trading decisions.

The significance of calculating technical indicators and their impact on trading decisions.

Introduction to the 'robot' object, which mimics user interaction with the TD Ameritrade account.

Explanation of the 'stock frame' object for organizing and managing price data.

Description of the 'portfolio' object that reflects account activity and assists in decision-making.

The 'trade' object facilitates complex order management and real-time trading activities.

The 'indicator' object standardizes the calculation of technical indicators for consistency.

Availability of the library on GitHub for cloning and potential contributions.

Instructions on how to clone the repository and the current state of the library with its dependencies.

Overview of the main robot file, configuration requirements, and examples of how to use the library.

The intention to rewrite the library from scratch in the next video, showcasing the setup and core functionalities.

Transcripts

play00:00

well welcome back so I know a lot of

play00:04

people will be excited about this series

play00:06

we are kicking it off yes we are gonna

play00:10

cover a series that has been highly

play00:12

requested by so many people we are gonna

play00:14

be showing you how to from start to

play00:17

finish

play00:17

build a trading robot using Python and

play00:21

more specifically we're gonna be showing

play00:22

it using the TD Ameritrade API so yes

play00:25

this again just putting that out there

play00:27

right now this will be assuming that you

play00:29

have a TD Ameritrade account that is not

play00:32

to say down the road I will not do

play00:33

Interactive Brokers or the other ones

play00:35

but right now this one week covering

play00:37

purely TD Ameritrade so this wonderful

play00:43

series it's been a few months in the

play00:45

making so there's a lot of stuff that

play00:49

kind of comes along but this I will say

play00:52

that additionally I do want to spend a

play00:56

few minutes just going over it right so

play00:59

I don't want to jump right into the

play01:00

coding I really want to take some time

play01:02

let's talk about what are we trying to

play01:04

do and how are we trying to do it for

play01:07

those of you who I know there's a lot of

play01:10

people out there they kind of just want

play01:11

to use the library they don't want to

play01:13

necessarily kind of care about what's

play01:15

going on behind the scenes that is okay

play01:18

to a certain extent but I will say this

play01:21

you really should know kind of what's

play01:23

going on behind the scenes the only

play01:26

reason I say that is you know there's a

play01:29

lot of things that are being calculated

play01:30

and it's a little bit easier to

play01:33

understand how to use it if you kind of

play01:34

understand how it was built so even

play01:37

though things will probably change a

play01:39

little bit from what you're gonna be

play01:40

seeing now it's gonna change a lot but I

play01:42

think having that idea of like hey how

play01:45

did we structure the code how do we

play01:48

think about the different objects and

play01:49

how do they kind of all interact

play01:51

together yeah you might not necessarily

play01:53

go out there and rewrite the library but

play01:56

it's a good idea to kind of just

play01:57

understand why was it built this way and

play02:01

kind of how's everything talking to each

play02:02

other additionally I will say this is

play02:07

well it is assumed that if you're

play02:12

watching this series

play02:13

that you're coming in with you know a

play02:15

relatively decent understanding of

play02:18

Python and some level of familiarity

play02:20

with certain popular libraries like

play02:22

pandas or numpy you know something like

play02:26

that if you're very new to programming

play02:28

I'm actually gonna say hold off on this

play02:31

series for a little bit you know you

play02:32

might want to go get familiar with

play02:34

certain concepts like you should be

play02:36

comfortable with you know class objects

play02:38

you should be very comfortable with if

play02:40

statements for loops anything like that

play02:42

I know sometimes we kind of just want to

play02:45

jump into it but the thing is this can

play02:47

actually make real trades for you and if

play02:49

you don't understand how it's working

play02:50

and if you don't know how to structure

play02:52

the code you're gonna be making real

play02:54

trades with real money so with that

play02:58

being said you know I am saying if

play03:00

you're not comfortable with programming

play03:02

in that regard you need to get familiar

play03:03

with it and then kind of come back to

play03:06

this series I do have some videos that

play03:07

cover different topics but there are

play03:09

also plenty of other channels out there

play03:10

who have a ton of content that will more

play03:14

than likely get you right up to speed so

play03:17

with that being said let's get started

play03:21

all right first things first this is

play03:26

actually a really complex topic trading

play03:30

is already hard enough right like a lot

play03:32

of people who day trade who just even

play03:35

you don't even really day trade I mean

play03:37

just trade in general you know you learn

play03:39

right away that it's not easy right I

play03:41

mean there's a lot of things you have to

play03:42

understand there's all these fancy terms

play03:44

there's all these technical indicators

play03:47

out there and there's all this kind of

play03:49

background knowledge that you have to

play03:50

understand in order to trade effectively

play03:53

so you've got to understand order types

play03:54

you've got to understand indicators if

play03:56

we got to understand you know different

play03:58

things related to account activity so

play04:00

we've already taken a pretty complex

play04:03

topic and now we've made it even more

play04:05

complex by adding this whole wonderful

play04:07

world of programming into it so that

play04:10

just makes a little bit more challenging

play04:11

doesn't make an impossible and doesn't

play04:14

even make it where it can't be done it

play04:15

just means we need to be very focused

play04:18

let me go about this whole process and

play04:20

part of being focused means we really

play04:22

need to start at a high level by

play04:24

defining our objectives

play04:27

what I have found is what I found is a

play04:35

lot of times people will reach out and

play04:36

say hey I'm trying to build a training

play04:38

robot and I've kind of picked their

play04:39

brain a little bit and I say okay well

play04:41

what are you trying to do like how are

play04:42

you trying to do it and something that

play04:44

kind of became very apparent to me

play04:46

relatively quickly was people were

play04:49

trading but they also weren't

play04:51

understanding how they were trading in

play04:53

very explicit terms because behind the

play04:56

scenes they were making all these

play04:58

assumptions right and certain things

play05:00

like you know when they would make an

play05:03

order you know for a lot of people maybe

play05:04

for example a big assumption was it's

play05:06

just a market order well now all this

play05:09

sudden I'm gonna have to you're gonna

play05:10

have to ask I'm gonna have to ask you

play05:11

that right I'm gonna have to you're

play05:12

gonna have to tell me what type of order

play05:14

you want to place so all the sudden I'm

play05:17

gonna start requiring to be very

play05:19

explicit with how you trade because you

play05:23

don't get that luxury anymore

play05:24

you have to be able to tell that robot

play05:26

hey you need to trade like this and if

play05:28

you want it to truly mimic your trading

play05:30

you have to give it all that information

play05:32

and you have to tell it upfront so hey

play05:34

you know it got to be this order you've

play05:35

got to be using this price you've got to

play05:39

be you know buying this amount of

play05:41

quantity so all of the sudden this is

play05:44

where people tend to struggle a little

play05:45

bit because now I'm forcing them to

play05:47

really be extremely explicit about how

play05:50

they're trading so a lot of times what I

play05:53

have found is having people take a step

play05:56

back and really sit down right I mean

play05:58

write it out I mean there's nothing

play05:59

wrong with writing out but write out how

play06:01

do you trade you know it's not just

play06:03

sitting there well I just placed an

play06:04

order it's more than that and you know

play06:06

that it's you know there's a type of

play06:08

order there's a quantity you know there

play06:09

there's all these little finer details

play06:11

that are going into it and you now need

play06:13

to start identifying those finer details

play06:15

so that's usually where again for at

play06:19

least from my experience this is where

play06:21

people kind of struggle a little bit

play06:22

because hey I'm forcing them be very

play06:25

explicit and it's very easy to get

play06:27

overwhelmed so naturally what I do is I

play06:29

kind of take a step back and say okay

play06:30

well you don't have to answer every

play06:32

question but ideally you want to have a

play06:34

general idea about kind of the kind of

play06:37

the big the big questions right

play06:40

the same kind of relates here when we're

play06:42

talking about the objective right we're

play06:43

not gonna have to necessarily define

play06:45

every little fine detail yet but we

play06:48

should generally have a decent

play06:50

understanding about what are the big

play06:52

things that this trading robot needs to

play06:54

do now this is not an exhaustive list

play06:56

this doesn't contain every possible

play06:58

thing it needs to do but when I was kind

play07:01

of approaching this I kind of asked

play07:03

myself what is really the things that

play07:05

this thing needs to do right so what are

play07:07

the objectives of this training robot

play07:09

and what does it have to do in order to

play07:11

function the way I think it should

play07:13

function well the probably the biggest

play07:15

thing is it needs to string quotes right

play07:18

so it needs to be able to get price

play07:19

information from somewhere because that

play07:22

price information is used in so many

play07:24

different parts it's used when we're

play07:26

calculating indicators it's used when

play07:28

replacing orders it's used when we're

play07:31

trying to organize our data so one of

play07:33

the first things we have to have built

play07:35

into this is we've got to have some type

play07:38

of mechanism for getting price

play07:41

information once we have that price

play07:43

information we can manipulate it and do

play07:45

whatever we want with it but we have to

play07:47

have some type of mechanism for getting

play07:49

that information now right now the way

play07:53

I've built the library is I am using not

play07:57

this TD streaming API I'm using the TD

play08:00

standard API so it's not to say the

play08:03

streaming API won't come in at some

play08:04

point but right now just to kind of keep

play08:07

things a little bit more simple I'm

play08:08

relying purely on the standard API and I

play08:12

will show you there's ways we can kind

play08:14

of mimic that's a loose word but we'll

play08:17

stick with it mimic the streaming API in

play08:19

certain regards so the big first thing

play08:21

we need to have is price information so

play08:23

we're gonna have to basically have a

play08:24

robot somehow get price additionally we

play08:28

need some type of functionality where we

play08:29

can enter and exit positions right

play08:32

that's trading you're gonna enter a

play08:34

position because you got a buy signal

play08:35

maybe and then maybe you're gonna have

play08:39

to exit a position because you got a

play08:40

sell you know a sell signal right so we

play08:42

have to have some type of functionality

play08:43

those robots got to be able to hey take

play08:46

a signal and then you know act on that

play08:49

signal and the way it acts is it

play08:51

determines well maybe I need to create a

play08:53

new

play08:53

order that will then reverse the

play08:55

position I just created right so we have

play08:57

to have some type of functionality like

play08:58

that additionally we need to have some

play09:01

type of functionality where it goes in

play09:04

it can help us manage our account data

play09:06

right and so we have a lot of account

play09:09

data out there and very often we will

play09:11

find that that account data is critical

play09:14

when it comes to making trades for

play09:18

example I have some individuals who will

play09:20

say once I own a position I do not want

play09:22

to enter another position for that same

play09:24

ticker symbol so we now have to build in

play09:28

some logic that says hey the minute that

play09:30

person enters a position even if I get a

play09:33

buy signal do not enter a new position

play09:36

so this is all of the sudden we're being

play09:41

explicit is very important because

play09:43

sometimes people don't realize that I

play09:46

some people won't trade like that and

play09:48

some people say I don't care if there's

play09:49

another bias appeal maybe we just add to

play09:51

the position not saying it's right or

play09:53

wrong but I'm saying as you have to

play09:55

define that role beforehand so because

play09:59

of that and because sometimes the logic

play10:01

that we build depends on what we already

play10:04

have in our account activity we need to

play10:06

have some type of mechanism for getting

play10:09

that account activity and organizing it

play10:12

in a fashion where we can then use it in

play10:15

other decision-making processes of our

play10:17

robot and probably the big one is we

play10:20

need to have a mechanism for calculating

play10:23

technical indicators a lot of

play10:26

individuals use technical indicators in

play10:28

order to trade they use it to determine

play10:31

when to enter or exit a position they

play10:33

use it to determine when the market is

play10:35

behaving more in a more active manner

play10:38

compared to a more you know I don't want

play10:41

to say it's a sedative is that what a

play10:43

sedative mean I don't know regardless

play10:45

they use it in order to make decisions

play10:48

sometimes it's determined when to start

play10:51

looking for signals other times it's use

play10:55

to determine when to enter or exit a

play10:57

position so we have to have some type of

play10:59

mechanism for helping us calculate those

play11:02

indicators so with that we have an

play11:06

objective

play11:07

this is ideally at a very high level

play11:10

this is what we want our robot to do

play11:14

when we think about our solution we now

play11:18

have to kind of start going into our

play11:19

programming world right well a lot of

play11:22

times we're thinking a very

play11:23

object-oriented programming so with that

play11:26

being said we need to start thinking

play11:27

about our objects what are the objects

play11:30

that we're gonna be creating and what

play11:33

are they gonna be doing for us like how

play11:35

does it fit into the bigger picture of

play11:37

our robot well the first thing that

play11:41

we're gonna have to do is we're gonna

play11:42

have to mimic guess what you write so we

play11:45

have to mimic you as an individual the

play11:48

way we mimic you is we define our robot

play11:51

object our robot object is mimicking how

play11:55

you interact with your TD Ameritrade

play11:57

account but what this robots gonna do is

play12:00

it's gonna interact with the TD

play12:01

Ameritrade API so the robot is really

play12:05

designed to mimic you and the way it's

play12:08

gonna be mimicking you is it's gonna be

play12:09

making requests to the TD Ameritrade API

play12:12

right so it's gonna be getting quotes

play12:14

it's gonna be placing orders it's gonna

play12:17

be getting account activity so this

play12:19

robot is gonna be handling all the

play12:22

talking to the TD Ameritrade API and so

play12:25

the way I also like to think about this

play12:28

is this is the highest level of our kind

play12:31

of hierarchy if we think about our

play12:33

objects as a hierarchy we can't do

play12:36

anything unless we have a robot so we

play12:38

can't do any trading unless we have you

play12:40

in the picture so even though you're not

play12:43

necessarily there physically

play12:45

manipulating the robot the robot is

play12:48

designed to mimic you so it's like we're

play12:50

copying you or putting you into a

play12:52

program and we're saying hey this robot

play12:54

has to be the main point of entry if I

play12:57

don't have a robot I can't do anything

play12:59

else so that's our first object our

play13:02

second object we're gonna be working

play13:05

with a ton of price data and we are

play13:08

going to need to organize that price

play13:10

data in a very consistent manner and

play13:13

we're going to have to have easi

play13:15

mechanisms for adding data appending

play13:18

data well I guess

play13:21

adding an appending are the same thing

play13:22

maybe in some cases deleting data

play13:25

selecting data and just keeping all of

play13:29

our information organized in a very easy

play13:31

fashion so we're gonna create a object

play13:34

but we're gonna call a stock frame now

play13:37

maybe a stock frame isn't the best name

play13:39

and I've actually debated if that's even

play13:41

remotely correct but for right now we're

play13:43

gonna keep it like that the stock frame

play13:46

at the end of the day is gonna be just a

play13:48

panda's data frame but I don't want to

play13:52

make it too general right I don't want

play13:54

to necessarily say oh yeah it's a

play13:57

panda's data frame so why are you doing

play13:58

all this extra fluff and I debated about

play14:01

that I mean initially I said you know as

play14:02

am I just doing overkill right now like

play14:04

is it really necessary to just define a

play14:07

new object that really is just a panda's

play14:09

data frame and I found it kind of is

play14:12

sometimes in the only reason why it is

play14:15

our data is going to be very finance

play14:19

oriented right and there's a lot of

play14:21

finance operations that you can do in

play14:23

pandas that would just be nice or a

play14:27

little bit easier to do if it was set up

play14:29

correctly right so things like you know

play14:31

maybe it makes sense to have a multi

play14:33

index data frame instead of just a

play14:36

normal data frame where you know the

play14:38

index is all right there so that might

play14:40

necessarily be beneficial

play14:42

additionally there are some mechanisms

play14:45

where adding data to a data frame could

play14:47

be a little bit challenging sometimes

play14:48

depending on you know how its organized

play14:50

and so I wanted to kind of take away the

play14:54

hassle of managing that and just say you

play14:55

know what as long as we're interacting

play14:58

with the API consistently all I really

play15:01

care about is doing common operations so

play15:03

maybe that's adding new rows right and

play15:05

then once I've added new rows there

play15:06

might be certain other things I might

play15:08

want to do right so for example if I

play15:10

have indicators in my stock data frame I

play15:13

might want to refresh those indicators

play15:15

or in other words recalculate them with

play15:17

the new data in mind and so what we'll

play15:20

find is that stock frame is going to

play15:22

make that process very easy for us very

play15:25

very easy for us so you don't have to

play15:27

worry about that now you're just gonna

play15:29

call a method and guess what it's gonna

play15:31

do a lot of magic behind the scenes but

play15:33

that's what it

play15:34

to do it's supposed to make it easier to

play15:35

work with so the stock frame is gonna

play15:38

really be helping us to organize our

play15:40

data additionally we're gonna have our

play15:43

next object which is a portfolio our

play15:46

portfolio is designed to kind of mimic

play15:48

our account activity

play15:50

maybe that's the orders that we already

play15:52

have active right or maybe that's the

play15:54

positions that we have currently in our

play15:56

account that are have been filled right

play15:58

so the portfolio is really designed to

play16:03

mimic our account activity because a lot

play16:05

of times we'll be making decisions based

play16:07

on data that comes from our account

play16:09

right so say for example I don't want to

play16:12

enter a position if I already have that

play16:14

stock already in my portfolio well this

play16:18

way by having a portfolio object we can

play16:21

easily ask those questions and then

play16:23

transfer that information to our

play16:25

decision making process so with our

play16:28

portfolio object it will be very useful

play16:30

when it comes to organizing that

play16:32

information and asking questions as

play16:34

we're trading that's really how I think

play16:36

about is that asking questions component

play16:38

do I have that stock well let me just go

play16:40

check my portfolio and then if I do oh

play16:42

great I'll just go trade I don't okay

play16:44

well no I won't trade so that's really

play16:47

important additionally there's some

play16:49

times where our trading can really

play16:52

depend on certain metrics about report

play16:54

folio right so for example maybe I don't

play16:57

want more than 60% of my portfolio in

play16:59

equity right so it would be nice if we

play17:02

had some type of free built

play17:03

functionality where we could easily

play17:05

calculate those metrics on the fly and

play17:07

so ideally with this portfolio object we

play17:11

would be able to answer those questions

play17:13

and then calculate those metrics as we

play17:16

need and then additionally as we may

play17:18

might need to do more advanced

play17:19

operations we can now easily add that

play17:21

functionality but keep it contained to

play17:24

where it belongs and that really is that

play17:26

portfolio aspect so that's the portfolio

play17:29

object next object trading right so

play17:32

we're gonna be doing a lot of training

play17:33

so we need to define a trade object

play17:36

right so our trade is really our order

play17:38

but we also understand that an order can

play17:41

be relatively complex depending on how

play17:43

you're trading it might not just be a

play17:45

market order it could be a very it could

play17:47

be no

play17:47

you order it could be a child order

play17:49

incorporated into it so all of a sudden

play17:51

we have to have some type of structure

play17:53

where we can easily modify our orders or

play17:56

we can easily add components to it in an

play18:00

easy fashion and this is normally where

play18:02

people struggle a lot because

play18:04

unfortunately the TD Ameritrade API was

play18:07

really lacking when it came to the

play18:09

examples of how to place orders and how

play18:12

to do that and so over the you know many

play18:15

months of using this API I've kind of

play18:17

figured out ok well this is how you do

play18:18

that and I wanted to kind of have that

play18:21

functionality pre-built for you so that

play18:23

way if you want to add things like a

play18:25

stop loss or a stop loss and take profit

play18:27

just just an order you already placed

play18:29

well you can just call a method and it

play18:32

will do all that for you behind the

play18:33

scenes instead of you having to take

play18:35

these very nested dictionaries that can

play18:37

get very messy very quickly and instead

play18:40

use that trade object to do it and then

play18:43

additionally what I would like to do is

play18:44

I'd like to see that trade object help

play18:46

us answer questions like has it been

play18:48

filled yet has it was it partially

play18:52

filled so ideally I'd like this trade

play18:55

object to help us answer even more

play18:57

complex questions that might come down

play18:59

the road that functionality might not be

play19:01

there right now but I could definitely

play19:02

envision that object actually containing

play19:05

it

play19:05

so that's why I thought it was important

play19:08

to help encapsulate all that into that

play19:10

kind of just that single object because

play19:12

it's very important and it really is

play19:13

kind of I would say the meat of our kind

play19:17

of real-time activity we want to be able

play19:19

to ask those questions on the fly and so

play19:23

that trade object will help us do that

play19:25

final object indicators indicators we've

play19:31

already said this multiple times a lot

play19:34

of people use technical indicators in

play19:36

order to make trades now right now we

play19:40

are purely doing it from a technical

play19:42

analysis standpoint that is not to say

play19:45

that down the road we might not include

play19:48

include a fundamental analysis but right

play19:51

now it is purely technical analysis and

play19:54

so this indicator object is going to

play19:58

help us calculate and define

play20:01

those indicators for us in an easy

play20:03

consistent fashion I think the biggest

play20:06

thing here is the consistent fashion

play20:09

it's very easy to get overwhelmed when

play20:12

it comes to calculating indicators and

play20:14

it's very easy to find yourself in a

play20:18

position where all the sudden you're

play20:19

working with this pandas dataframe and

play20:21

it gets really hard to do it because

play20:22

what was it grouped was it not grouped

play20:24

was it organized was it not organized

play20:26

you know and all of a sudden you will

play20:27

start pulling your hair out just like I

play20:30

did because all of the sudden it's not

play20:33

straight forward and that was like the

play20:36

thing that drove me crazy so what I

play20:39

found was let's define an indicator

play20:42

object where we can easily calculate

play20:45

those indicators and as long as I know

play20:47

what you're passing me through hint hint

play20:51

it's the stock frame but as long as I

play20:54

know what you're passing through

play20:56

I can easily calculate all those

play20:58

indicators for you in fact initially

play21:00

what I was actually gonna do is I was

play21:02

gonna use a few libraries to to

play21:04

calculate those indicators cuz I thought

play21:05

man this is great it's like that's gonna

play21:07

be super easy no problem whatsoever and

play21:10

then what I found out was it was always

play21:12

assumed that you were giving a very

play21:14

standard data frame and I said well

play21:16

that's assuming the columns are named

play21:17

the same that's assumed that this is

play21:19

named the same and all this other stuff

play21:21

and that it's organized and then they're

play21:23

assuming that when it's going in there

play21:25

that it's sorted and well I said well

play21:26

what if it's not sorted are they gonna

play21:27

sort that for you and so all the sudden

play21:30

I found man this could actually be a

play21:32

little bit challenging because I'm gonna

play21:34

have to do that on my end because I

play21:36

don't know if they're necessarily doing

play21:37

that on their end and so it's not to say

play21:41

maybe down the road I can't integrate

play21:42

those libraries but right now I had to

play21:44

actually define some of these indicators

play21:46

myself because I was finding that I know

play21:49

they weren't nest they were assuming

play21:52

certain things when it was being put in

play21:53

there and so for example that I think

play21:56

the biggest thing was how do you

play21:58

calculate it if you have multiple what

play22:00

is it multiple ticker symbols in the

play22:02

same data frame a lot of those libraries

play22:04

can't handle that and so all this I said

play22:07

man if I pass through groups it's not

play22:08

going to work I said I'm gonna have to

play22:10

filter or do XYZ and I said man this

play22:12

this could get challenging really

play22:14

and so the indicator client object was

play22:16

really designed to kind of help make

play22:18

that interaction consistent but still

play22:20

give you the flexibility where you can

play22:22

have all your data in a single data

play22:24

frame additionally a lot of indicators

play22:27

can be calculated using different

play22:29

methodologies I want that flexibility

play22:32

for the user so I want the user to be

play22:33

able to say hey I want to calculate the

play22:35

RSI using this methodology great just

play22:38

tell me you want to do that and then

play22:39

once I have that information

play22:40

I can calculate that for you so that was

play22:44

really the intent of the indicator

play22:46

object was make it consistent and make

play22:49

it ideally easier to calculate those

play22:52

indicators real time and then probably

play22:55

the biggest thing was having that stock

play22:57

frame and those indicator objects talk

play23:00

to each other so as the stock frame was

play23:02

adding new data the indicators would

play23:04

recalculate that is very challenging to

play23:07

do sometimes in pandas and honestly that

play23:10

was one of the one things that I think

play23:12

always surprised me about pandas and

play23:16

maybe it could just be the fact that I

play23:17

just maybe I'm seeing the answer right

play23:19

in front of me and I'm just not seeing

play23:20

it but something where it's like I'm

play23:23

just adding rows like how are these

play23:24

calculated columns not updating and so I

play23:27

basically created what I'm calling is a

play23:29

refresh method and basically what I've

play23:32

done is when you call your indicator

play23:34

client I've saved all the arguments

play23:36

you've passed through so things like the

play23:38

period that you use the methodology that

play23:40

you specified and I save that in the

play23:43

indicator client itself and so what

play23:45

you'll find is when I call the refresh

play23:47

method I'm literally calculating those

play23:49

indicators again but I'm using the saved

play23:51

information you sent me when you

play23:54

initially initialized those indicators

play23:56

so it's pretty neat honestly I out I

play23:59

thought that was kind of you know that's

play24:00

this is maybe where Python really shines

play24:02

a little bit but I thought that was kind

play24:04

of cool so at a high level this is how

play24:07

we're gonna start thinking about

play24:08

building our solution so with that being

play24:11

said though that actually finishes our

play24:14

presentation only three slides but of

play24:16

course I was rambling no there's that

play24:19

too

play24:20

so for those of you who are interested

play24:23

in it on the library is currently posted

play24:26

on github

play24:27

so you are more than welcome to clone it

play24:29

and is not on pi PI so you cannot

play24:32

install it at least not yet it does have

play24:35

a few dependencies so pandas numpy and

play24:37

TD Ameritrade big one is pandas I'm

play24:41

pretty sure once you install pandas you

play24:43

don't technically need this but I'm just

play24:45

thinking out there just to be safe but

play24:48

really if you want to kind of just

play24:50

high-level like what does it look like

play24:53

well first thing is you want to go to my

play24:58

github account so a breed 1 1 9 2 then

play25:01

you can see this wonderful photo which

play25:03

is horrible and then you will see some

play25:06

repos that I currently have available

play25:08

and one of them is called the Python

play25:11

trading robot and so this is the one

play25:13

that I will be recreating in a series

play25:16

again just to be very front lots of

play25:19

changes coming so this will change a lot

play25:20

so don't be surprised if you go in here

play25:22

ever you're so off you're like wait he

play25:24

changed it again oh yeah lots coming and

play25:27

then from here if you want you can

play25:29

always clone it so if you just go over

play25:31

here to the green little button clone or

play25:34

download and then if you copy it you can

play25:37

just do it's like so I've covered that

play25:40

in other videos so I will put a link to

play25:42

one where I've showed how to clone a

play25:44

repo but again hopefully pretty

play25:46

straightforward there's some unit

play25:48

testing kind of built in I'm adding to

play25:51

it but right now there's some stuff in

play25:53

there most of it is for the main robot

play25:56

itself so just you know again asking

play25:58

some general questions and then there's

play26:01

some indicators portfolios and you know

play26:03

stuff like that and then documents well

play26:06

unfortunately nothing right now but at

play26:08

least a template and then you can

play26:12

actually see the main robot file so if

play26:14

you were running it this stuff again I

play26:17

do have a config file I get this asked a

play26:20

lot a lot a lot a lot you need you don't

play26:24

have to do this part you don't have to

play26:26

but you need to have your client ID and

play26:29

you need to have your redirect URI and

play26:31

you need to have your credentials path I

play26:33

am NOT you have to provide that

play26:35

information for you how you choose to

play26:37

provide it is totally up to you you can

play26:40

hard-coded in your script I don't

play26:42

recommend that if you're sharing it with

play26:43

other people um you really should keep

play26:45

it secure

play26:46

but you can't so I have a config file so

play26:50

I write it every time I create a new

play26:51

library and then I just import it as I

play26:54

need it and then you'll see here I

play26:57

create a robot and then I do things like

play26:59

add positions just ask certain questions

play27:02

and stuff like that so ideally this

play27:05

should be a little bit easy to hopefully

play27:08

follow I would hope but sometimes not

play27:10

and then um again you can go through

play27:14

here I've been trying to document

play27:15

everything so I've been trying to add

play27:17

doc strings and then I'm gonna try to

play27:19

use like usage cases as well so I'm

play27:23

trying to make sure that's all organized

play27:24

if I've missed one you're more than

play27:27

welcome if you want to either bring it

play27:29

up or just do a pull request and then

play27:30

just add it for me but yeah so I mean

play27:33

there's lots of good stuff here

play27:34

hopefully again I'm trying to be as

play27:36

clear as possible about how to use it

play27:39

so hopefully it's it's hopefully

play27:41

straightforward that's that's the goal

play27:43

at least but I'll be probably again just

play27:46

making lots of changes so again please

play27:48

don't be surprised if you come here when

play27:49

you're like geez he just changed like

play27:51

five files but yeah so I mean you can

play27:55

obviously clone it by all means if you

play27:58

have suggestions very open to it people

play28:00

have been contributing to the

play28:01

Interactive Brokers one the TD

play28:03

Ameritrade one so again people have been

play28:05

great with that and so I'm always open

play28:07

to hearing that might not get back right

play28:09

away but always hoping to hearing some

play28:11

suggestions other than that I will say

play28:15

I'm gonna think that's gonna be it for

play28:16

this particular video next video I will

play28:21

be writing everything from scratch so I

play28:24

will not be cloning it we will just be

play28:26

basically recreating what we're seeing

play28:28

here - the setup and a lot of this other

play28:31

fancy stuff but for the most part we

play28:33

will be creating everything that we're

play28:36

kind of seeing right here so that's

play28:38

what's gonna be coming up in the next

play28:39

video you got any questions at this

play28:41

point by all means ask away hopefully I

play28:44

have an answer if I don't I'm sorry but

play28:48

yeah that's pretty much it so hopefully

play28:50

everyone's happy now we're doing a

play28:53

trading robot

play28:54

that's the exciting part right and then

play28:56

hopefully hopefully it's profitable

play28:58

right

play28:59

we'll see but yeah all right we'll see

play29:02

you in the next video

Rate This

5.0 / 5 (0 votes)

Étiquettes Connexes
Trading RobotPython CodingTD AmeritradeAPI IntegrationFinancial TechAlgorithmic TradingStock MarketTechnical AnalysisInvestment SeriesEducational Content
Besoin d'un résumé en anglais ?