大家好，本视频系列旨在向您展示如何自动进行交易 hello everyone this video series is to show you how to automate your trading
从零开始的想法或交易策略，我将在盈透证券上工作 ideas or trading strategy from scratch I'm going to work on Interactive Brokers
平台，我将使用IV Pi，它是用于连接IB或交易的Python API platform I'm going to use IV Pi which is a Python API to connect to IB or trading
我将完成该项目的每个步骤，包括连接到IB获取数据 I will go through each steps of this project including connect to IB get data
从ABI实施交易策略下单将我们整个想法 from ABI implement trading strategy place orders put us the whole idea
在线，然后我们可以看到真实结果，我们可以进行分析回测 online and then we can see the real result we can do the analysis back test
进行改进，或者我们可以优化，以使该系列易于理解 hat improve or maybe we can optimize that I'll try to make the series easy to
尤其是对于没有任何编程经验的人来说， follow especially for people without any programming experience I think
使用Python进行编程非常容易，因此如果您有个好主意并想 programming in Python is very easy so if you have a great idea and want to
自动化，希望这个视频系列可以帮助您，我将使用sp500 automate that I hope this video series can help you I'm going to use sp500
emini期货作为交易工具并习惯于在哪里-已知简单 emini futures as the trading instrument and used to wheree where-- known simple
移动平均交叉是交易策略，首先让我们看一下 moving average crossover that's the trading strategy first let's look at a
图表以查看我现在要做什么，所以这是11月的日内图表 chart to see what I'm going to do now so here is the intraday chart it's November
第11条蓝线是50移动平均线实现的是120移动平均线 11th the blue line is 50 moving average to realize is 120 moving average so one
是缓慢移动的区域，最后一个是一般的想法 is the slow moving area and one is the last one the general idea is when the
快速移动的空气是您越过单个产生的aa的慢速移动器 fast moving air is you cross over the slow mover you generate a a by a single
所以在这里 so it's here here
这是11月12日，应该买入信号再买入信号，然后买入 this is November 12 it's supposed to buy signal another buy signal and then buy a
在这里和这里发信号13这里是1 2 3 4 5 6 7好的亲爱的 signal here and here 13 here what is 1 2 3 4 5 6 7 ok dear
请通知好吧，请在此处延伸仅用于演示 please be notified okay please the stretch here is only for demonstration
绝不建议您使用它们只是为了示范好，所以 by no means I I suggest you use their it's only for demonstration okay so to
对此进行编码以为此程序编写程序，我们需要设置环境，因此在 code this to do the program for this one we need to set up our environment so in
合唱之一是我们有互动经纪人来tws平台我 chorus one is we have the interactive brokers to come the tws platform I
已经安装，然后第二个我们创建一个文件夹我所有的文件 already installed that then second we create a folder all my files
所以第三个是Python 3，然后我们安装I beep hi软件包，这是 so the third one is a Python 3 and then we install the I beep hi package this is
干净的Windows 10 PC我没有安装Python，让我们先做 clean Windows 10 PC I don't have Python installed so let's do that first
或Windows Python睡在八位，我已经下载了这一个 or Windows Python sleep on eight I already download this one
让我们打开那里好吧那里的安装，请记住检查一下 let's open there okay down there install that please remember to check this at
Python 3.8安装Python我们可以在这里进行搜索我发出哔哔声 Python 3.8 install Python we can go through here search I beep I can see in
您可以从此命令行安装Python IV PI，也可以 the nose you can install Python IV PI from this command line or you can
从这里下载整个内容，然后您可以通过PI声音看到它 download the whole thing from here and then you can you saw that by PI sound
在Python中安装P white，可以在关闭后停止在此处安装，这样您可以尝试 setup P white install okay in the Python you stop here after close so you can try
看看你是否盯着Python to see whether the Python you stare ok
让我们尝试安装我是P IP的PI p IP是要安装的Python实用程序 let's try to install I be PI by P IP p IP is a Python utility to install
不同的包装 different package
我很高-好的，我们去吧，就像它很好，所以我们有 I be high - okay here we go it's like it's good so we have
一切都安装好了，我们要自动进行作弊，第一个是 everything installed and we're gonna do the auto me cheating the first one is of
当然是连接器Ivysaur好的，让我们来做我要使用的 course is connector Ivysaur okay so let's do that I'm going to use
偶像ID Arry idol ID Arry
那就是我们的代码编辑器成为蜘蛛 that's our code editor become a spider
让我们一起看一下吧 let's see well let's ping this one too
被连接称为桩 it's called the pile by a connection
它是这里定义的唯一一个函数，您的名字就是函数名 it's only one function here is defined well your name a function name it's it's
真的，我的意思是视您的口味而定，使用更长的时间并不可耻 really I mean depends on your taste it's not a shame to use the longer one
在sli，您可以提供它可以帮助您理解代码，所以让我们 at sli's you can give it can help you understand the code so let's make a
如果找到函数调用give，再给另一个名字好吧 longer another name okay if you find the function call give
我和这个IB连接的连接他就是我们需要的IP连接 me and connection this IB connection he's just this IP connect here we need
给它起一个端口名，或者因为我听到哔哔声，我看到写存储实际上可以 to give it a port name or because I beep I saw writing store actually you can
从这里测试好朗姆酒我不选择 test from here ok rum I be not opt
连接，连接，我给你粥74 96是 connection connection see that I give you the porridge 74 96 for is
您可以给他们任何数字，让我们进入墙壁，然后返回 you can given them any number let's get at the wall and then return this
连接，就是这样，实际上这只是声明了什么，并没有真正连接，是的， connection that's it actually this just declared nothing really connect yeah and
然后我们可以测试一下，以便在滚动条上命名茬 then we can test that out so you name stubble on the scroll I wonder snowman
让我们称我好吧，所以在此我们知道是银，因为我是你 cast let's call that me okay so in this we know is that silver since I'm your
60美元的连接时间就是这样，我们什么也没看到 $60 time at connection that's it we didn't see anything else so what
碰巧看到了，所以如果您去IB，请单击此数据，看看会发生什么 happened to see so if you go to the IB you click this data see what happens
在这里，皮埃尔开心可怜，所以实际上我们建立了联系，但是我们没有看到 here so Pierre happy poor so actually we connected but we just didn't see
现在发生任何事情，我们需要找到“我监视”消息，让我们定义 anything happens now we need to find the me monitor the message let's define
另一个功能还可以，我再次使用更长的名称C another function okay the function again I use the longer name C is
消息环消息这是所有的连接注册并证明 message ring message this is the connection register all and prove the
天空在这里，它们意味着什么 sky here and what they do it means register all means for whatever
来自服务器的消息，我要调用此函数，那里没有句柄 message coming from the server I'm going to call this function no handle there
通常人们称此消息处理程序为这就是我们处理所有内容的原因 generally people call this message handler that's why we handle everything
好吧，让我们尝试一下，不要在这里开火，我们找到了解决方案 okay let's try that out don't fire here we go we got a solution
就像以前和现在一样，我们可以设法将其计入我的本地列表，然后我们 just like before and now we can manage to count this my local list and then we
得到下一个有效的ID已经是一个，然后我们得到一些错误好吧，这是 get next valid ID already is one and then we get some error okay see this is
每个农场我们形成的是未来和我们的选择以及一条消息，这是 each farm us form yes future and us option and a message and here is the
Port Cornetti港口地位费接受了，所以现在我们 port cornetti status fees accept it so now we
可以连接，所以消息让我们开始播放以查看所有内容 have connection okay and so the message let's do that play around to see all
那些消息好吗 those messages okay
所以消息实际上是一个结构字符串数据，而不仅仅是学生 so the message actually is a structure string data it's not just the student
使用拖拉机，所以 uses tractor so that's so
太太。台北名称好吧，让我们再次运行它，看看会发生什么吧，我们 mrs. taipe name okay let's run that again to see what happens okay here we
现在再来一次，该服务器与lambda说，所以我们有了一个连接时间，现在 go now again this server versus lambda say so we got a connection time and now
我们看到他们的消息类型是托管帐户，所以这是消息时间， we see their message type is managed account so this is message time and the
这是谁的消息，然后消息时间是下一个有效ID， who message is this one and then the message time is next valid ID and the
消息是这个，然后消息类型是错误，消息是这个 message is this one and then the message type is error and the message is this
最旧的消息，因此此错误是消息类型不是真正的箭头 oldest message so this error here is the message type is not a real arrow nothing
错误的只是一个消息时间，使用消息区域学期有点奇怪 wrong it's just a message time it's kind of weird to use messaging area semester
时间，但是现在我们知道了，您可以看看那是可以接受的，所以 time but now we know that okay and you can look at that that is accept it so
获得联系并尝试成为柯蒂斯 get the connection and try to be Curtis
断开连接简称为断开连接，让我们运行该连接并查看 the disconnect is simply called disconnect and let's run that and see
发生了什么，这试图变得有趣，所以在这里我们去看看 what happens it's trying to be interesting so here we go see that the
旧的方法是断开新方法，在这里我们有了连接时间， old one is to disconnect the new way and here we got a connection time and
没有别的好吗，那是为什么，因为我们说连接和 nothing else okay why is that because we said that the come on the connect and
断开连接快一点，我他没有足够的时间向Sun返回任何消息，所以 disconnect go fast I he doesn't have enough time to Sun back any message so
让我们到那里玩给我，请在这里睡三秒钟，看看 let's play around there give me please place a sleep here three seconds see
发生了什么事情连接到这里我们去你说睡眠没有定义 what happens connect here we go you said sleep is not defined
好的，所以睡眠不是馅饼中的内置功能-解决该问题的方法 okay so sleep is not a built-in function in pies up there - way to do that wrong
时间，所以我们从时间上看到了时间包，我们重要地发挥了作用，它没有用 time so we've seen the time package from time we importantly function no it works
很好，很好，在这里我们要做的就是在得到之前 fine okay nice do that here we go just exactly that before we got that
连接，然后用这个婴儿，这就是我可以监视的窗口 connected and then with this baby that's what I can monitor this window
看到接受它三秒钟断开连接，好吧，这就是我们要做的 see accept it three seconds disconnecting okay that's how we do that
现在穷人是74 96 97发生什么了 and now the poor is 74 96 97 what happens okay this is message time is
那里是Ares Erica，我在那里有六个无法与EWS妥协连接 there Ares Erica I was there six couldn't connect with EWS compromise in
从来没有采取任何行动，或者根本无法解决的问题，好吧，港口是 never acted or whatever basically you couldn't get back that okay the port is
未打开连接 not open connected
在这里，我们将端口4-7更改为死端口，但情况更糟， here we go you change the port 4-7 then deceased them there was worse let's
改回我，我不是，我真的不建议您更改任何要复制的内容 change that back I don't I don't really suggest you to change anything for copy
原因是因为一开始我们只是尝试了解这一点，所以 reason because at the very beginning we just one try to understand that okay so
我们到达这里是什么，我们建立了联系，我们收到了所有这些消息 what do we got here we got the connection we got all this message back
然后我们知道消息是一个结构，数据存在一个消息 and then we know that the message is a structure and data there is a message
类型，我们可以按数据或消息句柄捕获所有这些消息 type and we can catch all those message by data or message handle okay handle
所有这些数据，所以对于IB来说，实际上是时间轴，让我尝试另一个 all those data so for IB actually this is the timeline let me try another okay
在连接消息被冻结后立即进入此处 here after iced under the connect message immediately
好吧，让我们去那里，看看我们在这个夏天做那个连接，然后 okay let's do there so see we do this summertime and that connection and then
连接出现在东部，然后我们收到所有这些消息的消息时间 connects come on East and and then we get this message time all those messages
回来，所以我的意思是说，当我们联系时，我们并没有真正得到 come back so what I mean that means when we contact that we didn't really get
消息，如果它是屏幕，则它不是连续的，因此我们应该 message immediately it's not sequential if it's a screen so then we should
收集所有这些消息，我们应该在收到所有这些消息之前 gather all those message back we should got all those message back before the
弹簧模型好吧，不是屏幕，所以他叫水槽好吧 spring model right well not so it's not screen so he called a sink okay which
意味着就像邮递员一样，他们来到餐吧，他们通过 means like just like a mailman okay they come to the meal bars and they through a
那里的报纸，然后一顿饭就走了，他不在乎你读而不是 newspaper in there and then the meal one just go he doesn't care you read and not
看报纸，你只是把报纸扔掉甚至烧掉，你被烧死了 read the newspaper you just throw that away or even burn you're burned in your
对我们的住所的看法，他每天都来，并将它们放入我们的报纸中 opinion of our place he just come every day and put them the newspaper in of our
就像您认为一个连接，他们只是不断获取数据 it's kind of like you think a connection they just keep getting the data getting
所有这些消息，然后就是这样，消息檀香山成了一团糟 all those messages and then that's it let the message Honolulu job the huddle
花了不到一个小时的时间来处理数据 has spent under an hour to process the data
哦，只是我们在家庭中度过的三秒钟而已 oh just what we spend the families spend three seconds it doesn't matter the
连接只是不断获取数据，而不管您如何处理这是 connection just keep getting data it doesn't care how you handle that this is
叫你以为编程好吧，我想你不是他们，他们只是一个 called you think programming okay I think you're not what are they just a
小程序给我们连接的实际想法 little program give us the connection actually idea
只有一二三，这是三行一到两轻圣洁两行 only one two three this is three lines one to two light holiness two lines for
连接和此标志，此功能用于监视消息I the connection and this sign and this function is for monitor the message I
今天想得足够多，我们可以得到每一个人一个 think enough for today what do we get one per one we can
连接到IB，我们可以从每个服务器获取消息，我们可以对其进行监视 connect to IB we can get message from every server and we can monitor this
消息所在的消息时间，这有点延迟或高兴 messages there where it's message times and it's some kind of delay or happy to
回覆我想您今天就知道，如果您有任何疑问，可以在Google中使用 reply I think you know for today if you get a question you can in just Google
并在网上发布，或者您可以在某个区域发布，我将尽力表示感谢 and there on the web or you can just post on an area I'll try my best thank
你看着再见 you for watching goodbye