00:00:19.320
hey hello Ruby is nice to see you here
00:00:23.949
so today I will talk about algorithmic
00:00:25.779
trading for fun and profit so this is me
00:00:28.419
my nation long I'm a developer investor
00:00:30.759
and just a bit of disclaimers just to
00:00:33.789
make sure that I wouldn't get arrested
00:00:34.600
by monetary authority of singapore yeah
00:00:37.690
so okay you have heard of different kind
00:00:40.059
of terminology of her you have heard of
00:00:42.039
a algo trading high frequency trading
00:00:43.929
mechanical trading so and so forth but
00:00:46.239
as far as this store is concerned I'll
00:00:47.500
just stick with this definition so algo
00:00:49.960
train is about developing a computer
00:00:51.999
program that has got a predefined set of
00:00:54.910
rules in it that helps you to automate
00:00:57.160
the process of buying and selling in a
00:00:58.899
financial market such a stock market why
00:01:01.120
could be 4x or something else bitcoins
00:01:02.920
for example just like some other
00:01:05.980
scientific process there are few process
00:01:07.660
involved so we have the familiar
00:01:09.280
objective and hypothesis like how much
00:01:10.900
one you want to earn and then you
00:01:12.160
develop training strategy you back task
00:01:14.200
your strategy with the historical data
00:01:15.670
and you measure and optimize if it is
00:01:18.580
good you deploy to production if it's
00:01:20.410
not good then you have to start from
00:01:21.490
step 2 again and if you want to build an
00:01:24.820
algo trading system their very first
00:01:26.770
thing you need is actually financial
00:01:28.180
data so i call this component of feeder
00:01:31.120
so what it does is that he actually
00:01:32.680
fetches financial data feed from
00:01:34.630
external sources so there are a few data
00:01:37.420
sources I'm actually a hobbyist so I
00:01:40.090
hope for the free one like yahoo finance
00:01:42.160
but let's see if you are Quan trader
00:01:43.980
maybe you have a little bit more money
00:01:45.820
you might want to subscribe to like
00:01:47.500
commercial data provider for more
00:01:50.320
precise data and this is actually how
00:01:53.380
the csv file looks like when you
00:01:54.850
download it from yahoo finance so this
00:01:56.860
is actually the closing prices for apple
00:02:00.250
on a daily basis and this is actually
00:02:03.580
the JSON response data that we get from
00:02:06.130
stock please api so you can actually
00:02:08.019
find out what people are talking about a
00:02:09.820
particular stop on their social network
00:02:11.530
whether people are feeling optimistic or
00:02:13.740
domestic about stock and I basically
00:02:16.110
used high fees gem because I need to
00:02:18.330
download slow data for a lot of our
00:02:20.430
stocks so it's good for me to do these
00:02:22.800
requests in parallel and depending on
00:02:25.050
the data format i use csv DJ sonu kogi
00:02:27.150
to pass the data and after we have got
00:02:29.790
the market data we have to develop a
00:02:32.160
strategy the texting and market data is
00:02:34.050
input and then we generate some trade
00:02:36.300
signals as output based on the rules
00:02:38.370
that we defined so the question is how
00:02:40.470
do we define the rules so well a
00:02:42.360
different kind of techniques some people
00:02:43.500
for fundamental technicals news and sati
00:02:45.870
moon it really depends on your style but
00:02:48.260
today I'm going to talk about technical
00:02:50.970
analysis so as you can see on a church
00:02:53.130
this is actually the dailies prices of
00:02:56.550
Apple for the past like two years so I'm
00:03:01.050
going to talk about this technical
00:03:02.520
indicator call moving average this is
00:03:04.440
kind of like shallower example in the
00:03:06.270
technical analysis so the red color line
00:03:09.030
is actually fast moving average over 15
00:03:10.860
days whereas the green color line is
00:03:12.570
actually slow moving average over 50
00:03:14.940
days and it's actually not that
00:03:16.740
difficult to program this in Ruby in
00:03:18.600
fact as you can see just a few lines
00:03:19.890
what you have to what you have to do is
00:03:21.750
simply just you know calculate the mean
00:03:24.870
of the closing data for the past X
00:03:27.870
number of days so if you're looking at
00:03:29.370
simple moving average on 15 days they
00:03:31.620
just calculate mean of top prices over
00:03:34.530
the past 15 days and lower eyes for the
00:03:36.750
slow one and once we have the data what
00:03:39.750
do we do we actually have to identify
00:03:41.730
the crossover so whenever the fast
00:03:44.010
moving average crosses above the slow
00:03:45.750
moving average in trading time we say
00:03:47.910
that the generator the program can
00:03:49.590
generate a long signal so what does long
00:03:52.230
signal means it means that the curve is
00:03:54.060
actually on uptrend so on and on the
00:03:57.660
other hand if it's a shot signals that
00:03:59.610
means that the curve is actually on
00:04:00.840
downtrend and what do I mean by long I
00:04:04.790
yea sorry Karen I stories from you but I
00:04:07.920
really like go beep up whenever I think
00:04:09.690
of long there I thought it is yeah it's
00:04:11.250
pretty cool so like okay so this signals
00:04:14.130
actually advice portfolio in making
00:04:17.130
trading decisions so when you have a
00:04:19.140
long signals what you have got to do is
00:04:21.270
that all you think that the store is
00:04:22.740
actually bullish you think that the
00:04:24.270
price may go up in the future so you
00:04:26.710
got to stop yet you may want to buy some
00:04:28.210
and if a signal is actually shot this
00:04:30.910
indicate that the price may go down in a
00:04:33.190
future so you may want to sell it you
00:04:34.509
haven't got or you may wanna sell it if
00:04:36.490
you haven't bought it and popular
00:04:38.110
actually keep struggle for cash your
00:04:39.729
market position as well as the holding
00:04:41.319
values and most importantly actually
00:04:43.990
helps you to assess returns and risks
00:04:45.819
because you have to know your profit and
00:04:47.560
loss as well as your risk I mean you can
00:04:49.900
use a few matrix like Sharpe ratio and
00:04:52.210
you calculate your downturn things like
00:04:53.919
that and the last components actually
00:04:56.349
broker is actually a piece of code that
00:04:58.270
executes rate by placing order in the
00:05:00.610
market of course in the back testing
00:05:02.110
environment you want to simulate the
00:05:03.550
market behavior but in life trading
00:05:05.259
environment you want to invoke your
00:05:07.539
brokers API to actually place the order
00:05:09.520
in the actual stock exchange and when I
00:05:12.280
order gets filled you have two more you
00:05:14.259
have to notify your portfolio to update
00:05:16.479
the position and and holding the game
00:05:18.699
and all these components are actually
00:05:21.610
why out by using to what I call the
00:05:23.919
loops there's actually an outer loop and
00:05:26.289
inside that there is actually an ester
00:05:28.150
loop and this piece of code can be
00:05:30.400
represented by this diagram hope that is
00:05:32.440
easier to understand so basically you
00:05:34.389
have got a feeder that gets the next bar
00:05:36.310
a bar it's actually an abstraction so
00:05:38.620
it's like a trading activity over a
00:05:40.810
period of time so if you're looking at
00:05:42.099
daily data so this is the trade the
00:05:45.039
prices for that day by if you you can
00:05:46.960
actually look at frequencies like
00:05:47.979
minutes even up two seconds and then
00:05:50.229
once you go to data are then your
00:05:51.250
strategy analyze market data generate
00:05:52.870
signals for a portfolio place order and
00:05:54.669
then you execute orders and up their
00:05:56.740
holdings and game and for all of these i
00:05:58.719
actually created this project coahoma
00:06:00.610
which is a proof of concept of why i say
00:06:02.620
just now and if you are interested you
00:06:05.169
can go there and far more about it and
00:06:06.669
thank you
00:06:12.980
you