Quantcast aiQUANT » Trading

Archive for the 'Trading' Category

Visualizing Price Data as a Complex Phasor

Hilbert Transform, Signal Processing, Technical Indicators 22 Comments »

In a previous post regarding a modified wavelet feature extraction model I talked about replacing the DWT/FFT part with a customized Hilbert Transform for the purpose of capturing cyclic behavior of the price data. Here I discuss this in more detail and introduce the first of 3 Hilbert Transformer variants I am excited about.

For those not too familiar with phasors please have a look at the following links:

Simply put a phasor is an alternative way of representing a time-domain signal using a complex set of measures namely InPhase and Quadature. Recall that Hilbert Transformed signal takes the form \small\text{a + ib} where \small\text{a} is the “real” or InPhase part and \small\text{b} is the “imaginary” or Quadrature part. Plotting \small\text{a} and \small\text{b} on the complex plane and tracing out the trajectory formed by the phasor is supposed to reveal, among other things, the cyclic components of the signal.

There are many variants of the Hilbert Transform, but they all have a common structure which was popularized by [Ehlers]. For future reference we shall refer to Ehlers’ model as Variant 1.

HilT_var1.png

 

Applying a chirp signal to the input:

ChrResHT2.png

 

cplxPhasVar1.pngDue to the phase differential between the InPhase and Quadrature signals it becomes possible to trace out the trajectory taken by the phasor on the complex plane. The figure to the left shows this for a signal with frequency 20bars/cycle. Now if we superimpose a shortShowCycVar1_2.png term cycle on a longer one we see a change in the trajectory due to the presence of the new cycle. The figure to the right shows this. Notice that the cycle only shows up on the phasor plot with a lag of 4 bars. We were expecting a shift after the 10th bar, but only appeared after the 14th bar.

 

Short term cycles show up as a distortion of circles with bigger diameter and it is possible to use this feature to visualize underlying behavior in price data or any financial time series for that matter. I simulated a 31 step Brownian motion with drift = 10, diffusion = 10 and evaluated it’s complex phasor. We can clearly see how short term cycles influence longer term cycles, which is not obvious by inspecting the time series alone.

 

brnMotPhasor.png

Inspecting the phasor plot we can see the presence of cycles as represented by circular features being formed by individual points. The following cycles exist:

  1. Cycle 1: On Phasor plot points 7, 8, 9 => 3 bar cycle. On time series plot this cycle pertains to points 3, 4, 5.
  2. Cycle 2: On Phasor plot points 12, 13, 14, 15, 16, 17 => 6 bar cycle. On time series plot this cycle pertains to points 8, 9, 10, 11, 12, 13.
  3. Cycle 3: On Phasor plot points 25, 26, 27, 28, 29, 30, 31 => incomplete cycle. On time series plot this cycle pertains to points 21, 22, 23, 24, 25, 26, 27.

We also make the following observations:

  • Cycles 1 and 2 could actually be noise. We can only confirm this if we define a minimum diameter for a circular feature to qualify as a cycle and ignore all those with diameter less than the minimum.
  • Cycle 1 & 2 distort cycle 3.
  • Cycle 3 is incomplete but is the dominant one. The only way can predict its frequency is to count the number of points in the 2nd quadrant (points 26, 27, 28, 29, 30) and multiply by 4. Given that there are 5 points we predict that Cycle 3 is roughly a 20 bar cycle.

 

Key Points:

  • The Complex Phasor plot provides an excellent way of visualizing cycles and how they interact with each other. A cycle is one complete circular feature whose frequency is equal to the number of complex price points forming that circle.
  • Due to the lag constraint of Hilbert Transformer model used in this test (Variant 1) it is only possible to view cycles with a lag of 4 bars. In future I shall discuss Variants 2 and 3 which attempt to detect cycles with less lag.
  • It is possible to develop a trading strategy that exploits the cycle information obtained via Hilbert Transform. For instance we could use the InPhase vector as an anchor to decide when to buy or sell depending on whether pre-defined lines/points on the complex plane have been crossed. I have high hopes for such a method, particularly in an algorithmic trading setting.
  • It may actually be usefull to filter the input data before evaluating its complex phasor, but this may distort any micro-structure that may exist in the time series. It all depends how much filtering is acceptable.

Negative lag filter - new algorithm

Filter Design, Signal Processing, Technical Indicators 2 Comments »

In an earlier post I mentioned a filtering algorithm which demonstrated negative lag for low frequencies (trending market) and high magnitude gain for higher frequencies (cyclic market).  I modified the algorithm to deal with the high frequency overshoot problem by removing the dependency on the second order power of the price.  Although the filter copes relatively well at higher frequencies, we no longer have negative lag at the low frequency.  Perhaps it would be nice to compare lag and magnitude gain of both filters across all frequencies to understand the actual differences.

negLagNew.png

Wavelets and High-frequency data

High Frequency, Signal Processing, Wavelet Transform 1 Comment »

This presentation covers almost everything I have been wanting to convey about the usefulness of wavelets.

The researchers propose a prototype feature extraction system, the block diagram of which is reproduced below:

wavFXsys.png

Questions I ask:

  1. It appears that the researchers settled for a fixed wavelet function for doing the decomposition. Are there any benefits in using a hybrid of different wavelet families for decomposing different parts of the time series?
  2. Is it possible to create a custom wavelet family with the aim that it works better than the other wavelet families commonly used? What should be the line of approach to this kind of problem? Inductive or Deductive?

I am tempted to write a Matlab model to replicate this system and maybe adapt it a little bit, but there is a paper written by the researchers that needs to be understood first. You can download it from here.

A filter with negative lag

Filter Design, Technical Indicators 6 Comments »

The problem with technical indicators is that they suffer lag, resulting in signals appearing after events have occurred in time series context. Recently I’ve been investigating ways of reducing lag in these filters, building upon the results from a previous post which shows the workings of a zero-lag indicator derived from first principles. It seems something can be done, although the technique as it stands needs further refining. The graph below shows how it compares with the Exponential Moving Average (EMA) and the Moving Average (MA):

predictFil.png

The thing with this filter is that it has negative lag when the market is trending but has high gain when the market is in cycle mode. The reason for this is that in the filtering algorithm there is a part where I multiply the actual price by itself which results in noise components being amplified. To understand this let us consider the following:

Price data can be seen as comprising of two components: a signal part which is useful and a noise part which is not useful.

\small \text{Price=Signal+Noise}

The effect of multiplying Price by itself is a quadratic equation with signal and noise terms:

\small \text{Price*Price}=\left\{\text{(S+N)(S+N)} \\ \text{S^{2}+SN+NS+N^{2}}\right.

We have three noise terms, two of which affect our signal. I think this is the cause of the laggish behavior when the market is cyclic because the high frequency noise components are amplified.

Now there are two ways I can think of improving the performance of this filter

  1. Find a means of modifying the value of alpha when the filter detects that the market is no longer trending. This will require an additional algorithm to estimate whether the market is in trend mode or cycle mode - something which the Hilbert Transform can take care of.
  2. Find an alternative to multiplying the price by itself so that additional noise terms are not introduced.

I am biased towards 1 so shall spend more time on that. Because I consider this work in progress, I am tempted to discuss the actual workings of the filtering algorithm at a future date when we have some acceptable performance.

Statistical Arbitrage and Genetic Programming

Genetic Programming, Statistical Arbitrage 1 Comment »

The main idea in this presentation is that of co-evolving 2-branch type trees, where one branch represents buy rules and the other represents sell rules for trading. The intuition is that when the branches are evolved together, your final genetic program ends up with buy and sell rules that are duals of each other. Hence an optimum buy rule can be paired with an optimum sell rule - which forms a dual. This is unlike the approach where a sell rule is triggered only if certain criteria for a buy are not met (which may not necessarily be optimum condition for the sell). Although the results in the presentation are somewhat ambiguous, the author points out that it is possible to discover profitable trading rules in the presence of transaction costs under a statistical arbitrage framework. The author also cites a few papers which have applied GP techniques to Statistical Arbitrage. I shall dig up these papers to investigate the approach taken.

Trading Puzzle #2

Trading Puzzle 4 Comments »

While scanning the market for good picks, a Trader comes across a stock that exhibits properties of a Geometric Brownian motion.  Determine whether one is going to make or lose money if they decide to invest in this stock.

Hint:  For a Geometric Brownian motion, there is a 50% probability that the path will go either up or down from its current position.

Trading Puzzle #1

Trading Puzzle 5 Comments »

Lets say we have a trading model that has only a single parameter which can be modified in-order to change the profits the model can generate. Presently, the model’s parameter is set to 160 and generates profits of £15million / year.

A Quant thinks this parameter is not ideal, and that the best value to achieve maximum profits lies in the range 150 to 170.

Calculate the difference in profitability that may be expected as a result of tweaking this value.

Creating “Complex” price from “Real” price

Hilbert Transform, Technical Indicators No Comments »

There are many benefits of converting price data in complex number form. The presence of an Imaginary component gives one the freedom to create other kinds of indicators which can provide further insight into the statistical properties of the asset price. For example, indicators that fall into the categories below can only be derived from complex price data:

  • Signal to Noise ratio (SNR)
  • Cycle Period
  • Phasor diagrams
  • Predictive Indicators (i.e. filters with Negative Lag)
  • Power Spectral Density of the price

A complex number takes the form

a + ib where i=sqrt{-1}

Real price data only have an a component, aka the InPhase component. The aim is to derive the corresponding Quadrature component using the InPhase data of the price at a given bar. The Hilbert Transform provides a way of doing this, and an excellent description can be found at wikipedia.

To complement the notes we can think of the Hilbert Transform as a control system, the simplest of which looks like:

HilTransf.png

Note that z^{-n} represents the z-domain delay operator. It remembers its input value for n samples into the future.

We shall discuss Indicators based on Complex Price Data in due course.

How to Optimally Reward a Trader

Trading No Comments »

I haven’t yet started writing the Matlab MEX function but I hope to start today and post results by Saturday latest. In the mean time have a look at this paper which gives a fairly mathematical treatment about Rewarding a Trader.  Note that the figures and diagrams are shown at the end of the paper.

Abstract:

Traders are compensated by bonuses, in addition to their basic salary. However, little s known about how to optimally reward a trader. In this article we build a framework for the study of this problem and explore a variety of possible compensation structures.

You can freely download it from here.

Forecasting via Genetic Algorithm: Part 4

Forecasting, Genetic Algorithm, Model Development No Comments »

Part 4: Roulette Selection

So we have initialised our population of chromosomes each of which is made up of 13 genes.  We can represent our population in the from of a matrix where individual chromosomes are represented vertically and it’s genes horizontally.  We have something like:

matrixFitn.png

Each member of the population is assigned a fitness value based on the fitness function, which in our case is defined as

eq2.PNG

 

Why Roulette Selection?

Roulette wheel sampling was first suggested by Goldberg and provides a method of global selection when applied to members of a population.  In order for genes of fit chromosomes to be passed on to future generations, there must be some form of selection method to decide which members of the population will be selected to breed and which ones will be replaced.  It seems sensible to breed from genes belonging to the fittest chromosomes and replace the least fit ones, but this has a draw back: the GA can converge to local minimum as the search space is restricted to the genes covered by the fittest chromosomes only.  One of the mechanisms that enable genes of weaker organism to be passed on while at the same time favouring fitter chromosomes is Roulette Selection.  To understand this consider the following example:

Assume we have a population of 4 chromosomes each of which has their fitness value evaluated. To construct a Roulette wheel for this population we calculate the Roulette probability of each chromosome as a ratio of its fitness to the fitness of the entire population. That is
RouletteProbability=\frac{chromosomeFitness}{populationFitness}
The illustration below shows the Roulette wheel for our hypothetical 4-chromosome population:

rouWeeEg.png

Extending the method to the problem at hand, we have:

rouWee.png

 Note that our Roulette wheel represents a population size of 10.  Chromosome 2 for instance has a higher probability of being picked compared to to Chromosome 6.  This off-course doesn’t mean that Chromosome 2 will always be picked.  It all depends on where the Roulette wheel “stops”.  Also note that different population size would have a different representation to its Roulette wheel because every chromosome is allocated a section corresponding to its fitness value.

Main Points:

  • Roulette Selection is an efficient way of allowing excellent genes embedded in a poor performing chromosome to be passed on to future generations.
  • Roulette Selection enables convergence to global minimum.
  • Think of this method as chromosomes represented on a Roulette wheel which is “spun” whenever a parent needs to be selected for crossover/breeding.
Close
E-mail It