Hempfling Proposal
Hempfling Proposal
By
Joseph Hempfling
STETSON UNIVERSITY
2017
1
Contents
0.1 Abstract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1 Introduction 7
2 Definitions 10
3 Bayes’ Theorem 14
4 Kendall’s Tau 17
5.3 Thresholds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2
5.6 Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
7 Future Plans 35
Appendices 36
A Calculation Arrays 36
B Probability Arrays 37
C Thresholds 38
E Predictions 40
3
List of Tables
1 Known Probabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2 Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3 Pairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4 Concordance Calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
5 Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
6 Data Rearranged . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
7 Pairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
8 Concordance Calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4
List of Figures
Figure 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Figure 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5
0.1 Abstract
By
Joseph Hempfling
May 2018
Over the past few years, the finance industry has seen the use of algorithmic trading
explode. Money managers have shifted to this form of trading because of its high returns,
precision, and consistency. Quantitative analysts, or Quants, are professionals who fuse
their expertise in finance, math, statistics, and computer development to create algorithms
that trade profitably in the securities, foreign exchange, and commodities markets as well
as several other markets. Many algorithms use technical analysis as a main strategy. While
most algorithms have a certain level of mathematical thinking and probabilistic strategies, this
proposal aims to explore the possibility of using more advanced mathematical approaches to
judge a relationship between assets and thus improve the performance of trading algorithms.
It is proposed to analyze the effectiveness of using Bayes’ Theorem to determine when a
particular algorithm will trade profitably based on its past performance and whether there
exist strong Kendall’s tau measures of concordance for several currency pairs. In the algorithm
that’s been developed in this proposal, a strong Kendall’s tau value could help inform the
program on how many days of data it must use to optimize its performance in the future.
In addition, a strong tau value for two currency pairs could be very useful in indicating the
direction of a currency and could thus inform trading decisions.
6
1 Introduction
Algorithmic trading is the process by which computer programs execute trades at a speed
and sequence that is not achievable by human traders. The goal of algorithmic trading is to
perform more profitably than a human could by trading with higher accuracy, volume, and
speed. The objective of this research is to explore the possibility of adding an additional
level of sophistication to trading algorithms by incorporating such mathematical concepts
as Bayes’ Theorem and Kendall’s tau. Trading on the foreign exchange market is particu-
larly appealing to algorithmic traders because most foreign exchange brokers offer attractive
leverage amounts. Leverage trading is a process where loans are offered to investors by the
broker so investors can trade multiples of their initial investment amounts and hopefully re-
alize higher returns. While leverage trading can significantly amplify losses, most traders and
algorithms employ risk management techniques that limit those losses. In all that follows,
note "forex" or"FX" are interchangeable terms for "foreign exchange."
The foreign exchange market is where currencies are traded. These markets serve the pur-
pose of allowing people to exchange their currency for a different currency in order to make
transactions. This makes the foreign exchange market the most liquid market in the world.
In addition, the foreign exchange market is the biggest financial market in the world with a
daily trading volume between $2 trillion and $5 trillion. One unique characteristic of forex
is that there is no marketplace where the currencies are traded like there is for other assets.
Rather, trades are conducted over-the-counter which means they are conducted on computer
networks around the world between traders. Because of this, the market is open 24 hours a
7
day, five and a half days a week. When Americans are ending their trading days, new days
are beginning for people on the other side of the world.
There are two ways to trade forex on an exchange. The first is on the spot market
which refers to trading one currency for another at the price that is current to the exchange.
The second is on the futures market which is not an exchange of currencies but rather the
purchase of a contract that specifies the type of currency, the price of purchase, and the
future date of settlement. There is a third instrument used to trade forex called a forward
contract, but it is not traded on an exchange. Instead, these contracts require execution by a
bank because the details of the contract are customized to the fit the desires of the customer.
This proposal focuses on algorithms that trade on the spot market.[6]
Fluctuations in currency pairs are trivial, usually less than one cent daily or 1% relative
to the value of the currency. Usually, this low volatility would not allow for high returns to be
realized, so most forex brokers offer leverage trading so investors can realize large returns and
gain value from the small fluctuations in the currency. Leverage in the forex market can be
as large as 250:1, meaning investors can make trades up to 250 times their account balance.
While this sort of leverage allows investors to realize large returns in a low volatility market,
investors are also taking a huge risk and are able to lose their investments very quickly.[6][7]
8
fulfilled quickly and accurately. In most cases, a computer will recognize a trading oppor-
tunity and execute a trade before a human trader would even notice that the criteria for a
profitable trade has been met.
Algorithmic trading has a multitude of benefits including trades being executed at the
best possible prices and times, instant order placement, decreased transaction costs, simul-
taneous multi-factor decision making, elimination of human manual error, and elimination
of emotional and psychological decision making. Furthermore, algorithms allow traders to
backtest their methods by running the program through real-time data and seeing how the
program would have performed.
Trading algorithms use several types of strategies to inform their decisions. These in-
clude strategies that take advantage of macroeconomic news, fundamental analysis, statistical
analysis, mathematical models, technical analysis, and the market microstructure. The ob-
jective of this research is to enhance trading algorithms by adding mathematical models that
will inform and improve decision making.[6]
9
2 Definitions
This proposal aims to assess the effectiveness of using several mathematical techniques and
models to make decisions about trading on the foreign exchange market. The objective is
to find techniques that are highly accurate and implement them in trading algorithms to
enhance the intelligence of decision making and thus increase profitability. This chapter will
introduce and define the financial and mathematical terms used in this paper as well as those
that form the underlying basis of this paper.
Definition 2.1.2. Currency pair refers to the notation used in the foreign exchange market
that expresses the value of a currency relative to another, namely, a ratio. The first currency
listed is known as the base currency and the second is known as the quote currency. The
pair indicates how much of the quote currency is required to purchase one unit of the base
currency. Thus, the purchase of a currency pair corresponds to the simultaneous purchase of
the base currency and sale of the quote currency.[6]
Definition 2.1.3. Currency trading is the act of buying and selling currencies on the foreign
exchange market.[6]
Definition 2.1.4. The bid price is the maximum price that buyers are willing to pay for an
asset.[6]
Definition 2.1.5. The ask price is the minimum price that sellers are willing to accept for
an asset.[6]
10
Definition 2.1.6. Spread refers to several different occurrences in the finance industry. In
terms of foreign exchange, spread is the difference between the bid and ask prices. [6]
Definition 2.1.7. A pip is the smallest measurable change in an exchange rate. Currency
pairs are expressed up to 4 decimal places, so 1 pip is equivalent to .0001 of that given
currency. In other terms, a pip refers to 1/100th of 1%. The term basis point is also used to
express this value.[6]
Definition 2.1.8. A foreign exchange index is a geometric weighted average of several foreign
currencies against one given currency. [6] For example, the U.S. Dollar Index is a weighted
geometric mean of the dollar’s value against these six currencies:
Definition 2.1.9. Leverage is an investment strategy that involves the use of borrowed
assets to increase potential returns. Leveraging ratios are commonly high in foreign exchange
because of the low volatility in currency value fluctuation. Leveraging makes foreign exchange
a much more attractive investment.[6]
Definition 2.1.10. A margin account is an account with a broker that offers leveraging.[6]
11
Definition 2.1.11. Liquidity is the extent to which an asset can quickly and easily be bought
or sold without affecting the underlying value of the asset. It essentially measures the ease
by which an asset can be traded for its true value.[6]
Definition 2.1.12. Over-The-Counter (OTC) refers to assets that are traded through a
network of dealers and traders rather than on a formal exchange. Foreign exchange is an
OTC market.[6]
Definition 2.1.14. An option is a financial derivative which does not involve the exchange
of an asset. Instead, an option is a contract that grants the buyer the right, but not the
obligation, to buy (call) or sell (put) an asset at an agreed-upon strike price on a specific
date.[6]
Definition 2.1.15. The strike price is the price at which a financial derivative can be
exercised.[6]
Definition 2.1.17. Technical analysis is a trading technique used to forecast the future price
of an asset by evaluating several measures and statistics like price and volume.[6]
12
2.2 Mathematics Concepts
Definition 2.2.1. Concordant pair is a pair of data points of two variables, (X1 , Y1 ) and
(X2 , Y2 ), that has the following property:
sgn(X2 − X1 ) = sgn(Y2 − Y1 )
[2]
Definition 2.2.2. Discordant pair is a pair of data points of two variables, (X1 , Y1 ) and
(X2 , Y2 ), that has the following property:
sgn(X2 − X1 ) = −sgn(Y2 − Y1 )
[2]
Definition 2.2.3. Tied pair is a pair of data points of two variables, (X1 , Y1 ) and (X2 , Y2 ),
that has the following property:
X1 = X2 and/or Y1 = Y2
[2]
13
3 Bayes’ Theorem
P (A ∩ B)
P (A|B) = .
P (B)
Thus, this equation can thus be written as a expression of more accessible probabilities.
P (B|A)P (A)
P (A|B) =
P (B)
[1]
A computer produced by The Quality Computer Company was found to be defective (D).
There are three manufacturing facilities (A, B, C) where these computers are produced. An
executive for the company wants to determine from which facility the defects come from.
Suppose the executive has this information about the computer manufacturing facilities.
14
Table 1: Known Probabilities
He hopes to find the probability that a computer comes from a facility given it’s a defect.
Bayes’ Theorem will be used to calculate P (A|D), P (B|D),and P (C|D). These probabilities
can be calculated as
P (A ∩ D) P (D|A)P (A)
P (A|D) = =
P (D) P (D)
P (B ∩ D) P (D|B)P (B)
P (B|D) = =
P (D) P (D)
P (C ∩ D) P (D|C)P (C)
P (C|D) = = .
P (D) P (D)
Most information for this calculation is given. However, the value for P(D) must be
found. This probability can be expressed as
15
P (D|C)P (C) (.019)(.36)
P (C|D) = = = .3570
P (D) (.01916)
Therefore, the executive can conclude that given a computer is defective, the probabil-
ities of it coming from branches A, B, and C are .2349, .4081, and .3570, respectively.
16
4 Kendall’s Tau
Kendall’s tau, or Kendall rank correlation coefficient, is a coefficient that describes the con-
cordance between sets of paired data. The coefficient ranges between -1 and 1. A value
near 1 indicates a strong level of concordance, a value near -1 indicates a strong level of
discordance, and a value near 0 indicates a weak relationship. Two variations of Kendall’s
tau are tau-a and tau-b. Tau-b is used when there are tied pairs. The appropriate use of
tau-b will be displayed later. Kendall’s tau is calculated as the difference in the number of
concordant and discordant pairs divided by the number of choices of two ordered pairs. This
is equivalent to the difference of probability of concordance and probability of discordance.
Let the reader be aware that tau-a refers to the probabilistic interpretation just described
and is interchangeable with the general version of Kendall’s tau. [2]
C −D
τ=
C +D
where
C = number of concordant pairs
The denominator, C + D, is the total number of ways to choose two pairs from n pairs,
assuming there are no tied pairs. This can also be expressed as n2 = 2!(n−2)!
n!
= n(n−1)
2
. [2]
17
4.1 Example: Tau-a
Consider the following set of paired data to calculate its corresponding Kendall’s Tau coeffi-
cient.
Table 2: Data
x y
1 49
2 23
3 36
4 16
5 8
There is one concordant set of pairs and nine discordant sets of pairs, C = 1 and D = 9.
The difference of these, C − D, is obviously -8, but with larger data sets, simply counting the
number of each type of pair is inefficient. Instead, the sum of the sgnx × sgny column will
18
Table 3: Pairs
C −D 8
τ= = − = −.8
C +D 10
Suppose a hypothesis test was performed to test the null hypothesis that no concordance
exists in this data at the 5% significance level.
H0 : τ̂ = 0 vs H1 : τ̂ 6= 0
α = .05
p
3 · τ̂ n(n − 1)
z= p
2(2n + 5)
19
Table 4: Concordance Calculation
In this case,
p
3(−.8) 5(5 − 1)
z= p = −1.95959
2(2(5) + 5)
α
which has a p-value of .02502 which is greater than 2
= .025, so the test statistic is not
significant and it is not appropriate to reject the null hypothesis.
In the previous example, Kendall’s tau is an appropriate measure. However, in a data set
with tied pairs, tau-b is often more appropriate. Tau-b is
C −D
τb = q
n n
( 2
− X0 )( 2
− Y0 )
where X0 is the number of tied pairs on the x variable and Y0 is the number of tied pairs on
20
the y variable. In essence, tau-b divides the geometric mean of the untied x pairs and the
untied y pairs into the difference of concordant and discordant pairs.
Table 5: Data
x y
1 49
2 23
3 36
4 23
5 8
x/y 1 2 3 4 5
8 1
23 1 1
36 1
49 1
21
Table 7: Pairs
1 (1,49) (2,23)
2 (1,49) (3,36)
3 (1,49) (4,23)
4 (1,49) (5,8)
5 (2,23) (3,36)
6 (2,23) (4,23)
7 (2,23) (5,8)
8 (3,36) (4,23)
9 (3,36) (5,8)
10 (4,23) (5,8)
While none of the x terms are the same, the pair [(2,23),(4,23)] is a tied pair because
their y values are equivalent.
22
Table 8: Concordance Calculation
5 (2,23) (3,36) 1 1 13 1 1
6 (2,23) (4,23) 2 1 0 0 0
There is one concordant set of pairs, eight discordant sets of pairs, and one tied set of
pairs in the y coordinate so that X0 = 0 and Y0 = 1.
1−8
τb = p ≈ −.7379
(10 − 0)(10 − 1)
Suppose a hypothesis test was performed to test the null hypothesis that no concordance
exists in this data at the 5% significance level.
H0 : τ̂b = 0 vs H1 : τ̂b 6= 0
α = .05
23
The test statistic, z, can be computed as
C −D
z=p
V (s)
v0 − vt − vu vt v2
V (s) = + +
18 2n(n − 1) 9n(n − 1)(n − 2)
where
v0 = n(n − 1)(2n + 5)
X
vt = tk (tk − 1)(2tk + 5)
k
X
vu = ul (ul − 1)(2ul + 5)
l
X X
v1 = ( tk (tk − 1))( ul (ul − 1))
k l
X X
v2 = ( tk (tk − 1)(tk − 2))( ul (ul − 1)(ul − 2))
k l
and tk is the number of ties from a particular value in the x variable and ul is the number
of ties from a particular value in the y variable. Because there are no ties in the x variable,
tk = 0. Since there are two tied values on the y variable when y = 23, ul = 2.
vt = 0
24
vu = 2(2 − 1)(2(2) + 5) = 18
v1 = 0
v2 = 0
So,
300 − 0 − 18
V (s) = ≈ 15.67
18
and
1−8
z=√ ≈ −1.77
15.67
α
which has a p-value of .038 which is greater than 2
= .025, so the test statistic is not
significant and it is not appropriate to reject the null hypothesis.
25
5 Application of Bayes’ Theorem
This proposal aims to address the use of Bayes’ Theorem as an effective tool for making
algorithmic trading decisions. There are only two types of trades these algorithms can execute,
buy and sell. Additionally, these algorithms only execute one type of trade per day, either
all buys or all sells. Therefore, if it can be predicted that an algorithm will lose money on
a particular day, the trades for the day could simply be reversed and make a profit that is
near equivalent to the expected loss. A MATLAB program has been developed that uses
Bayes’ Theorem to determine whether an algorithm on the foreign exchange market will
perform profitably on a given day given its performance on the previous day. The program
first determines whether the algorithm previously traded profitably (good previous day) or
unprofitably (losing previous day). It then computes the probability of making or losing
money on the current day based off that condition. Let the following notation represent
losing day, losing previous day, and good previous day in what follows.
LD = losing day
The Bayesian program calculates, on a daily basis, two probabilities: the probability of having
a losing day given a winning previous day, i.e. P (LD|GP D), and the probability of having a
losing day given a losing previous day, i.e. P (LD|LP D). It is important to note that these
are not complementary probabilities. In fact,
26
and
P (LP D|LD)P (LD)
P (LD|LP D) = .
P (LP D)
With these two probabilities, it is able to be determined, with some confidence, whether an
algorithm will make a profit on any given day. Using the information from the Bayesian
probability, trades could simply be reversed to the opposite (buy to sell or sell to buy) and a
profit would be realized from what would have been a loss. The expected loss is not equivalent
to the expected profit is because the spread is trivial. However, the two values are nearly
equivalent and the difference is meaningless for the sake of this program since the spread is
negligible.
Originally, these probabilities were calculated using all data points and compared against
a 50% threshold to determine if it was likely that a losing day would occur. If the probability
exceeded that threshold, the trade would reverse from buy to sell or sell to buy. Otherwise,
the trade would execute as normal. However, there were two problems with this method.
First, as the sample size increased, the probabilities began to approach a certain value and
new data would not change the probabilities enough to give an accurate prediction at that
point in time. Second, the 50% threshold was too low and resulted in too many reversals.
To combat this, a parameter, hind, was created that specifies how many days the program
should look back to calculate the probabilities and the threshold for which the probabilities
would be compared. The threshold is simply computed as an arithmetic mean of the amount
of days of probabilities as indicated by hind.
In order to compute these probabilities, it is essential to create arrays that keep track
of how many instances of each type of day occur in hind days. Therefore, for each type of
event, an array was created that marked each instance of that type of day occurring and
another array was created to sum those occurrences for later calculation use.
27
5.1 Calculation Arrays
The MATLAB code for the calculation arrays is found in Appendix A. Array A is used to
store the daily ending balances from the basic financial algorithm that this proposal intends
to improve upon. The balance of the trading account starts at $50,000 every quarter so
A(1) = 50000 and every element of A that is the first trading day of the quarter is 50000.
If on the second day the algorithm made $250, the second element of A, A(2), would be
50000+250 = 50250. On the next day, if the algorithm lost $100, A(3) would be 50250−100 =
50150. A comparison array, C, is used to determine whether a day of trading is profitable.
It allows an element of A to be compared to the corresponding element of C to determine
when a losing day and when a profitable day occurs. Instances of good days and losing days,
with various conditions, are found and summed in different arrays. These arrays are crucial
to calculating probabilities later in the script.
The MATLAB code for the probability arrays is found in Appendix B. The conditional
probabilities are calculated using the arrays found in the previous section.
5.3 Thresholds
The MATLAB code for the thresholds is found in Appendix C. The thresholds are calculated
as an average of the last hind many probabilities. Two thresholds are calculated: one for
P (LD|GP D) and one for P (LD|LP D). In the comparison tests, the probabilities are com-
pared against the thresholds and a decision for reversal is made. If the probability exceeds
28
the threshold, a reversal will be executed. Otherwise, the trade stays as it was.
The MATLAB code for the calculating profit and loss is found in Appendix D. The overall
profit and loss is calculated with the reversals in effect.
The MATLAB code for finding the predictions is found in Appendix E. The entire Bayes’
process is performed again where the program calculates new probabilities and thresholds
taking the reversals into account. Once that’s complete, a final decision needs to be made on
whether to reverse the trading strategy for the next day. The output from this part of the
script is either 0 or 1 where 0 indicates reversing for the next day and 1 indicates sticking
with the strategy.
5.6 Results
The preliminary results show that using Bayes’ Theorem is an effective method for predicting
and adjusting algorithm performance. Here are results from two algorithms enhanced by the
MATLAB program.
29
5.6.1 Sell Stop Algorithm
Figure 1
This graph shows the overall account balance from 2011-2017(Q3) where the account resets
its balance at $50,000 every quarter. In other words, these results do not take into account
returns carried over between quarters. The pre-enhancement account balance is $41,969.89
while the post-enhancement account balance is $149,079.47.
30
5.6.2 Sell Limit Algorithm
Figure 2
Similarly, this graph shows the overall account balance from 2011-2017(Q3) where the account
resets its balance at $50,000 every quarter. In other words, these results do not take into
account returns carried over between quarters. The pre-enhancement account balance is
$27,502.19 while the post-enhancement account balance is $137,789.41.
31
6 Application of Kendall’s Tau
To potentially enhance this program further, the use of Kendall’s tau was explored. Kendall’s
tau is typically used on ordinal data, but can also be used on the ratio and interval levels
of measurement. Kendall’s tau is often used with dichotomous data. Given that there is
dichotomy in these scenarios in regard to profit and loss as well as buy and sell, Kendall’s
tau is an appropriate option. Finding the correlation between currency pairs is appropriate
because the data is independent from one another. In this section, several currency pairs are
compared resulting in several tau values. Caution was exercised in interpreting these results
as a large number of correlation coefficients can be misleading. Kendall’s tau is used instead
of alternative correlation coefficients because of it does not rely on linearity. Rather, tau’s
value is derived from the level of concordance. Finally, strong values of tau do not imply
causation. These strong values would instead allow the algorithm to make more informed
trading decisions. Finally, tau-a is used instead of tau-b because no tied pairs exist in this
data.
U SD = U.S. Dollar = $
EU R = Euro = e
32
The correlations between these three currency pairs were found:
EU R
U SD
GBP
U SD
GBP
EU R
A short MATLAB script, found in Appendix F, was developed to calculate these values.
Data was used for three different time frames: one year before Brexit, one year after Brexit,
and the most recent year Oct 1, 2016 - Oct 1, 2017. Calling this function with any two
currency pairs would output the associated Kendall’s tau value.
EU R GBP GBP
Currency Pair U SD U SD EU R
EU R
U SD
1 0.68 0.61
GBP
U SD
- 1 -0.30
GBP
EU R
- - 1
EU R GBP GBP
Currency Pair U SD U SD EU R
EU R
U SD
1 -0.06 0.42
GBP
U SD
- 1 0.64
GBP
EU R
- - 1
33
Table 11: Tau Values for One Year After Brexit
EU R GBP GBP
Currency Pair U SD U SD EU R
EU R
U SD
1 0.51 0.13
GBP
U SD
- 1 0.36
GBP
EU R
- - 1
These results were calculated to familiarize myself with Kendall’s tau and its possible appli-
cation to this project.
34
7 Future Plans
The future plans for this research are to analyze Kendall’s tau values between more currency
pairs as well as using different time periods of historical trading data. The selected value of
the hind parameter has a large impact on the performance of the program. For this reason,
it is important to explore ways to deduce the optimal hind value before trading begins. The
idea was to use measures of concordance to inform a decision on selecting the value of hind.
While there isn’t enough evidence to show that these measures of concordance will inform a
good decision, the possibility is not ruled out. Some values of tau are too high to be ignored.
Other methods to optimize the value of hind will be considered and explored. The MATLAB
program will also be translated into java code so it can be seamlessly integrated into the
original trading algorithms.
35
Appendices
A Calculation Arrays
%%%CALCULATION ARRAYS%%%
z = s i z e (A, 1 ) ;
%%Days%%
days = z e r o s ( z , 1 ) ;
days ( 1 ) = 1 ;
f o r counterdays = 2: z
days ( c o u n t e r d a y s )= days ( c o u n t e r d a y s −1)+1;
end
%%Comparison%%
C = zeros (z , 1 ) ;
for j = 2: z
C( j ) = A( j −1);
end
f o r mapcount = 1 : l e n g t h (map)
C(map( mapcount ) ) = 5 0 0 0 0 ;
end
%%Days Thus Far%%
DTF = z e r o s ( z , 1 ) ;
DTF( 1 ) = 1 ;
f o r count = 2 : hind
DTF( count )=DTF( count −1)+1;
end
f o r count = ( hind +1): z
DTF( count )= hind ;
end
%%L o s i n g Day Array%%
LD = A<C ;
%%L o s i n g Days Thus Far%%
LDTF = z e r o s ( z , 1 ) ;
f o r count = 1 : hind
LDTF( count ) = sum (LD ( 1 : count ) ) ;
end
f o r count = ( hind +1): z
LDTF( count ) = sum (LD( ( count−hind ) : count ) ) ;
end
%%Good P r e v i o u s Day%%
GPD = z e r o s ( z + 1 , 1 ) ;
f o r count = 2 : ( z +1)
GPD( count ) = (1−LD( count − 1 ) ) ;
end
%%Good P r e v i o u s Day Thus Far%%
GPDTF = z e r o s ( z , 1 ) ;
f o r count = 1 : hind
GPDTF( count ) = sum (GPD( 1 : count ) ) ;
end
f o r count = ( hind +1): z
GPDTF( count ) = sum (GPD( ( count−hind ) : count ) ) ;
end
%Good P r e v i o u s Day Given L o s i n g Day%%
GPDLD = z e r o s ( z , 1 ) ;
f o r count = 2 : z
i f LD( count ) == 1
i f GPD( count ) == 1
36
GPDLD( count ) = 1 ;
end
end
end
%%Good P r e v i o u s Day Given L o s i n g Day Thus Far%%
GPDLDTF = z e r o s ( z , 1 ) ;
f o r count = 1 : hind
GPDLDTF( count ) = sum (GPDLD( 1 : count ) ) ;
end
f o r count = ( hind +1): z
GPDLDTF( count ) = sum (GPDLD( ( count−hind ) : count ) ) ;
end
%%L o s i n g P r e v i o u s Day%%
LPD = z e r o s ( z + 1 , 1 ) ;
f o r count = 2 : ( z +1)
LPD( count ) = LD( count −1);
end
%%L o s i n g P r e v i o u s Day Thus Far%%
LPDTF = z e r o s ( z , 1 ) ;
f o r count = 1 : hind
LPDTF( count ) = sum (LPD( 1 : count ) ) ;
end
f o r count = ( hind +1): z
LPDTF( count ) = sum (LPD( ( count−hind ) : count ) ) ;
end
%%L o s i n g P r e v i o u s Day Given L o s i n g Day%%
LPDLD = z e r o s ( z , 1 ) ;
f o r count = 2 : z
i f LD( count ) == 1
i f LPD( count ) == 1
LPDLD( count ) = 1 ;
end
end
end
%%L o s i n g P r e v i o u s Day Given L o s i n g Day Thus Far%%
LPDLDTF = z e r o s ( z , 1 ) ;
f o r count = 1 : hind
LPDLDTF( count ) = sum (LPDLD( 1 : count ) ) ;
end
f o r count = ( hind +1): z
LPDLDTF( count ) = sum (LPDLD( ( count−hind ) : count ) ) ;
end
B Probability Arrays
%%%PROBABILITIES%%%
%%P(LD |GPD)%%
%P(LD)
PLD = z e r o s ( z , 1 ) ;
f o r count = 1 : z
PLD( count ) = LDTF( count ) /DTF( count ) ;
end
%P(GPD)
PGPD = z e r o s ( z , 1 ) ;
f o r count = 1 : z
PGPD( count ) = GPDTF( count ) /DTF( count ) ;
end
%P(GPD| LD)
PGPDLD = z e r o s ( z , 1 ) ;
37
f o r count = 1 : z
PGPDLD( count ) = GPDLDTF( count ) /LDTF( count ) ;
end
%P(LD |GPD)%
PLDGPD = z e r o s ( z , 1 ) ;
f o r count = 1 : z
i f PGPD( count ) == 0
PLDGPD( count ) = 0 ;
else
PLDGPD( count ) = PGPDLD( count ) ∗PLD( count ) /PGPD( count ) ;
end
end
%%P(LD | LPD)%%
%P(LPD)
PLPD = z e r o s ( z , 1 ) ;
f o r count = 1 : z
PLPD( count ) = LPDTF( count ) /DTF( count ) ;
end
%P(LPD | LD)
PLPDLD = z e r o s ( z , 1 ) ;
f o r count = 1 : z
i f LDTF( count ) == 0
PLPDLD( count ) = 0 ;
else
PLPDLD( count ) = LPDLDTF( count ) /LDTF( count ) ;
end
end
%P(LD | LPD)
PLDLPD = z e r o s ( z , 1 ) ;
f o r count = 1 : z
i f PLPD( count ) == 0
PLDLPD( count ) = 0 ;
else
PLDLPD( count ) = PLPDLD( count ) ∗PLD( count ) /PLPD( count ) ;
end
end
C Thresholds
%%%THRESHOLDS%%%
%%Running Average T h r e s h o l d%%
AVG = z e r o s ( z , 1 ) ;
RunningProb = z e r o s ( hind , 1 ) ;
counterrow = 1 ;
counterrs = 1;
success = 1;
%C r e a t e s i n i t i a l a r r a y o f ’ hind ’ PLDGPD
w h i l e s u c c e s s < hind
w h i l e c o u n t e r r s <= hind
i f GPD( c o u n t e r r o w ) == 1
RunningProb ( c o u n t e r r s ) = PLDGPD( c o u n t e r r o w ) ;
counterrs = counterrs + 1;
success = success + 1;
end
AVG( c o u n t e r r o w ) = sum ( RunningProb ( 1 : ( c o u n t e r r s − 1 ) ) ) / ( c o u n t e r r s −1);
counterrow = counterrow + 1 ;
end
end
38
%Moves t h e e l e m e n t s o f a r r a y and adds new one a t end
w h i l e c o u n t e r r o w <= z
i f GPD( c o u n t e r r o w ) == 1
RunningProb = c i r c s h i f t ( RunningProb , [ − 1 , 0 ] ) ;
RunningProb ( hind ) = PLDGPD( c o u n t e r r o w ) ;
end
AVG( c o u n t e r r o w ) = sum ( RunningProb ( 1 : hind ) ) / hind ;
counterrow = counterrow + 1 ;
end
%%Comparison Test%%
COMP = o n e s ( z , 1 ) ;
f o r countercomp = 2 : z
i f GPD( countercomp ) == 1
i f PLDGPD( countercomp −1) >= (AVG( countercomp −1)+ b u f f e r 1 )
COMP( countercomp ) = −1;
else
i f PLDGPD( countercomp −1) <= (AVG( countercomp −1)+ b u f f e r 1 )
COMP( countercomp ) = 1 ;
end
end
end
end
%%Running Average T h r e s h o l d 2%%
AVG2 = z e r o s ( z , 1 ) ;
RunningProb2 = z e r o s ( hind , 1 ) ;
counterrow2 = 1 ;
counterrs2 = 1;
success2 = 1;
%C r e a t e s i n i t i a l a r r a y o f ’ hind ’ PLDLPD
w h i l e s u c c e s s 2 < hind
w h i l e c o u n t e r r s 2 <= hind
i f LPD( c o u n t e r r o w 2 ) == 1
RunningProb2 ( c o u n t e r r s 2 ) = PLDLPD( c o u n t e r r o w 2 ) ;
counterrs2 = counterrs2 + 1;
success2 = success2 + 1;
end
AVG2( c o u n t e r r o w 2 ) = sum ( RunningProb2 ( 1 : ( c o u n t e r r s 2 − 1 ) ) ) / ( c o u n t e r r s 2 −1);
counterrow2 = counterrow2 + 1 ;
end
end
%Moves t h e e l e m e n t s o f a r r a y and adds new one a t end
w h i l e c o u n t e r r o w 2 <= z
i f LPD( c o u n t e r r o w 2 ) == 1
RunningProb2 = c i r c s h i f t ( RunningProb2 , [ − 1 , 0 ] ) ;
RunningProb2 ( hind ) = PLDLPD( c o u n t e r r o w 2 ) ;
end
AVG2( c o u n t e r r o w 2 ) = sum ( RunningProb2 ( 1 : hind ) ) / hind ;
counterrow2 = counterrow2 + 1 ;
end
%%Comparison Test 2%%
COMP2 = o n e s ( z , 1 ) ;
f o r countercomp2 = 2 : z
i f LPD( countercomp2 ) == 1
i f PLDLPD( countercomp2 −1) >= (AVG2( countercomp2 −1)+ b u f f e r 2 )
COMP2( countercomp2 ) = −1;
else
i f PLDLPD( countercomp2 −1) <= (AVG2( countercomp2 −1)+ b u f f e r 2 )
COMP2( countercomp2 ) = 1 ;
end
end
end
end
39
D Profit and Loss
%%%PROFIT/LOSS%%%
profit = zeros (z , 1 ) ;
agprofit = zeros (z , 1 ) ;
for counterprofit = 2: z
i f GPD( c o u n t e r p r o f i t ) == 1
p r o f i t ( c o u n t e r p r o f i t ) = (A( c o u n t e r p r o f i t )−A( c o u n t e r p r o f i t −1))∗COMP( c o u n t e r p r o f i t ) ;
f o r mapcount1 = 1 : l e n g t h (map)
p r o f i t (map( mapcount1 ) ) = (A(map( mapcount1 ) ) − ( 5 0 0 0 0 ) ) ∗COMP(map( mapcount1 ) ) ;
end
else
i f LPD( c o u n t e r p r o f i t ) == 1
p r o f i t ( c o u n t e r p r o f i t ) = (A( c o u n t e r p r o f i t )−A( c o u n t e r p r o f i t −1))∗COMP2( c o u n t e r p r o f i t ) ;
f o r mapcount1 = 1 : l e n g t h (map)
p r o f i t (map( mapcount1 ) ) = (A(map( mapcount1 ) ) − ( 5 0 0 0 0 ) ) ∗COMP2(map( mapcount1 ) ) ;
end
end
end
end
for counteragprofit = 2: z
a g p r o f i t ( c o u n t e r a g p r o f i t ) = a g p r o f i t ( c o u n t e r a g p r o f i t −1) + p r o f i t ( c o u n t e r a g p r o f i t ) ;
end
E Predictions
%%%REVERSAL OUTPUT%%%
newCOMP = COMP+COMP2;
newcomp = comp10+comp11 ;
f o r counternC = 1 : z
i f newCOMP( counternC ) == 2
newCOMP( counternC ) = 1 ;
end
end
f o r counternc = 1: z
i f newcomp ( c o u n t e r n c ) == 2
newcomp ( c o u n t e r n c ) = 1 ;
end
end
r e v e r s e = newCOMP+newcomp ;
for counterrev = 1: z
i f r e v e r s e ( c o u n t e r r e v ) == 0 | r e v e r s e ( c o u n t e r r e v ) == 2
reverse ( counterrev ) = 1;
else
reverse ( counterrev ) = 0;
end
end
%1 t o keep
%0 t o r e v e r s e
40
comp11 ( z +1) = 1 ;
f o r c o u n t e r n d = ( z +1)
i f GPD( c o u n t e r n d ) == 1
i f PLDGPD( counternd −1) >= (AVG( counternd −1)+ b u f f e r 1 )
COMP( c o u n t e r n d ) = −1;
else
i f PLDGPD( counternd −1) <= (AVG( counternd −1)+ b u f f e r 1 )
COMP( c o u n t e r n d ) = 1 ;
end
end
end
end
f o r c o u n t e r n d 2 = ( z +1)
i f LPD( c o u n t e r n d 2 ) == 1
i f PLDLPD( counternd2 −1) >= (AVG2( counternd2 −1)+ b u f f e r 2 )
COMP2( c o u n t e r n d 2 ) = −1;
else
i f PLDLPD( counternd2 −1) <= (AVG2( counternd2 −1)+ b u f f e r 2 )
COMP2( c o u n t e r n d 2 ) = 1 ;
end
end
end
end
f o r c o u n t e r n d 3 = ( z +1)
i f gpd ( c o u n t e r n d 3 ) == 1
i f pldgpd ( counternd3 −1) >= ( avg ( counternd3 −1)+ b u f f e r 1 0 )
comp10 ( c o u n t e r n d 3 ) = −1;
else
i f pldgpd ( counternd3 −1) <= ( avg ( counternd3 −1)+ b u f f e r 1 0 )
comp10 ( c o u n t e r n d 3 ) = 1 ;
end
end
end
end
f o r c o u n t e r n d 4 = ( z +1)
i f l p d ( c o u n t e r n d 4 ) == 1
i f p l d l p d ( counternd4 −1) >= ( avg2 ( counternd4 −1)+ b u f f e r 1 1 )
comp11 ( c o u n t e r n d 4 ) = −1;
else
i f p l d l p d ( counternd4 −1) <= ( avg2 ( counternd4 −1)+ b u f f e r 1 1 )
comp11 ( c o u n t e r n d 4 ) = 1 ;
end
end
end
end
newCOMP( z +1) = COMP( z+1)+COMP2( z +1);
newcomp ( z +1) = comp10 ( z+1)+comp11 ( z +1);
f o r counternC1 = ( z +1)
i f newCOMP( counternC1 ) == 2
newCOMP( counternC1 ) = 1 ;
end
end
f o r c o u n t e r n c 1 = ( z +1)
i f newcomp ( c o u n t e r n c 1 ) == 2
newcomp ( c o u n t e r n c 1 ) = 1 ;
end
end
r e v e r s e ( z +1) = newCOMP( z+1)+newcomp ( z +1);
f o r c o u n t e r r e v 1 = ( z +1)
i f r e v e r s e ( c o u n t e r r e v 1 ) == 0 | r e v e r s e ( c o u n t e r r e v 1 ) == 2 %#ok<∗OR2>
reverse ( counterrev1 ) = 1;
else
reverse ( counterrev1 ) = 0;
end
end
%1 t o keep
41
%0 t o r e v e r s e
f u n c t i o n tau ( a , b )
bsign = zeros ( length (a ) ) ;
jcount = 2;
for i = 1: length (a)
for j = jcount : length (a)
b s i g n ( j , i ) = ( a ( i )−a ( j ) ) ∗ ( b ( i )−b ( j ) ) ;
end
j c o u n t = j c o u n t +1;
end
csum = sum ( s i g n ( b s i g n ) ) . ’ ;
f i n a l s u m = sum ( csum ) ;
choose = l e n g t h ( a ) ∗ ( l e n g t h ( a ) −1)/2;
kt = f i n a ls u m / choose ;
d i s p ( kt )
end
42
References
[1] Ghahramani, Saeed. Fundamentals of Probability with Stochastic Processes, Third Edition.
[2] Nelsen, Roger B. An Introduction to Copulas. Springer-Verlag New York. 1998, 126.
[3] King, Lengerich, Bai. Design and Analysis of Clinical Trials: Correlation and Agreement, nd.
https://onlinecourses.science.psu.edu/stat509/
dukascopy.com
www.mathworks.com
www.investopedia.com
www.investing.com
43