Skip to main content

Posts

Simple moving average trading strategy using Python

Hi All, I am presenting simple boiler point code that can quickly be applied to test technical indicator strategies using Python. The code: 1. downloads daily stock data from google, 2. calculates the short and long moving averages 3. generates the trading signals 4. calculates the daily returns 5. runs the moving average strategy and calculates the cumulative return 6. plots cumulative return of our simple strategy Here is the code ... enjoy trying it out and extend it as required: import numpy as np import pandas_datareader as datar import datetime import matplotlib.pyplot as plt date_start = datetime.datetime( 2017 , 1 , 1 ) date_end = datetime.datetime( 2017 , 6 , 30 ) data = datar.get_data_google( 'AAPL' , date_start , date_end) short_ma = 5 long_ma = 20 data[ 'short_ma' ] = data[ 'Close' ].rolling(short_ma).mean() data[ 'long_ma' ] = data[ 'Close' ].rolling(long_ma).mean() data[ 'masig' ] = data[ 'short_ma...

Type-2 Fuzzy Logic in High Frequency Trading

In a recent journal publication we investigate the viability of Type-2 fuzzy systems in high frequency trading. We propose Type-2 models based on a generalisation of the popular ANFIS model (ANFIS/T2). Type-2 models score significant risk adjusted performance improvements over Type-1. Benefits of Type-2 models increase with higher trading frequencies. Paper available at: http://www.sciencedirect.com/science/article/pii/S0957417416300203

What software architects/developers frequently forget ...

When looking at code or architectures, sometimes I get the feel that developers forget that the art of programming, or any other problem solving task, is really the beauty of being able to transform a complex problem into a series of simple, well defined logical steps. Here is what some of the most intelligent people the world has ever seen remind us: “If you can't explain it to a six year old, you don't understand it yourself.”  ―  Albert Einstein “Simplicity is the ultimate sophistication.”  ―  Leonardo da Vinci "Fools ignore complexity. Pragmatists suffer it. Some can avoid it. Geniuses remove it." - Alan Perlis So, the gist of the story, the role of an excellent developer or architect is not to complicate the simple, but to simplify the complicated. Sometimes people can easily get mislead about the beauty of a solution by admiring its complexity, but really and truly the greatness lies in the ability to simplify a complex problem.

Data Visualisation tools

At the moment I am looking for some data visualisation tools. Here is an interesting link with some great visualisation tools I met. Web Link

Interfacing C# .Net and R - Integrating the best of both worlds (Part 3) - Trader Desk Example

In this third and final part of the series ( Part 1 , Part 2 ), I am going to continue shaping the examples in the previous posts by quickly building a small application - a simple Trading Desk application. In the example I will use the same C# and R interface method together with a specific Quantitative Financial Modelling R library called Quantmod . Example Objective In the example the objective is to build a C# Web Form which acts as the main application controller that captures various user option parameters with the main functions being: 1. To automatically kick off an R routine to download data from Yahoo Finance. 2. To select specific dates of interest 3. To add or udpate different charts 4. To add different chart indicators 5. To calculate Period Return Statistics Most of these functions are provided through the set of R functions exposed by the Quantmod R library. Additions under the hood Below I am showing some of the salient additions done to the code over and abov...

Interfacing C# .Net and R - Integrating the best of both worlds (Part 2)

This post is a continuation from the previous post ( Part I ) focusing on interfacing C# with R using the R (D)COM. In this post I am going to enhance my previous exercise by creating a Facade .Net Class which facilitates access to specific functions in R. Creating the R Facade Class Creating a Facade Class (or a set of .Net classes) which acts as a .Net wrapper to R functions greatly facilitate the use of R functions and their integration within the .Net programming environment. Below I am showing an excerpt from the class RFacade that I have created in this example. using System; using System.Collections.Generic; using System.Linq; using System.Text; using StatConnectorCommonLib; using STATCONNECTORSRVLib; using System.Runtime.InteropServices; namespace R { class RFacade : IDisposable {      private StatConnector rconn;      private bool disposed = false;      public RFacade()    ...

Interfacing C# .Net and R - Integrating the best of both worlds

In specific software areas like in quantitative finance or else in other mathematical domains, data centric programming typically requires a good balance between three requirements - (1) a solid platform with rich mathematical/statistical functionality (2) having an easy to use, contemporary, programming environment which permits easy and flexible front end code development and (3) an easy to use interface between the two environments. In this artcile I am going to explain how such a balance can be attained by using two of the best products in their specific worlds - using the rich R library as the mathematical/statistical component but then interfacing with C# for the front end application design. As an interfacing option I banked on using R (D)COM which provides an easy to use interfacing method which keeps you away from spending hours identifying interfacing problems. The software required for this tutorial is the following: 1. R software ( download from here ) 2. R (D)COM In...

Installing OpenCV 2.0.0 on MacOS X 10.6.1 Snow Leopard ( Xcode IDE )

I have recently started experimenting with OpenCV. Setting the environment on Windows/Visual Studio is pretty straight forward and works fine. However since my preferred development environment is MacOS X / Unix i preferred to switch environments. I must say that the installation went quite smooth, even though on the internet i found a number of posts on a number of issues. The steps i followed were the following: 1. download opencv library from http://sourceforge.net/projects/opencvlibrary/ 2. go to the downloads/opencv-2.0.0 directory 3. execute ./make_frameworks.sh this creates an opencv framework which comes very handy when developing using Xcode The process takes quite some time .... Once completed using Xcode open the sample project (FaceTracker) for MaxOS x which is found under the opencv-2.0.0/samples directory clean, build and off you go ... camera on my mac book pro was as well recognized without problems.

SPSS Regression using Matrices

The SPSS syntax below does several linear regression calculations (model parameter values, sum of squares, F value, F test, R, Rsquare, t tests, etc) using matrices ... just change the data file and initialization part as required and off you go ...... /* ####### DATA FILE ################################################################################### */ FILE HANDLE data /NAME='c:\data\ratty2.sav'. /* ####### INITIALIZATION AND DATA MATRIX ############################################################## */ MATRIX. COMPUTE N=21. /* no of data rows */ COMPUTE IND_VARS=3. /* no of independet variables */ GET Y /FILE=data /VARIABLES =size. /* puts y-variable into column matrix called Y */ GET X1 /FILE=data /VARIABLES =t,z2,z3 /* put independent variables into a matrix called X1 */ /* ########## MATRIX CALCULATIONS #################################################################### */ COMPUTE j=make(N,...

Java Design Patterns

Today i had the pleasure to present a session at the monthly Java User Group Malta meeting ... its always a pleasure to meet other Java Developers in our community and aspiring students. Click on the post tile to link to the presentation ... Regards and good luck guys ... hope to meet again for other sessions.

Java Web Application Properties

One of the most common application requirements is usually to create a central place where to keep application properties or application settings. These properties are normally settings or general properties that are applied to the application during runtime. A number of options exist, including creating string properties on your application server and accessing them as JNDI resources. However one of the still most practical approaches is to keep a properties config file with a simple propertyName =propertyValue   type of structure. The example below is an example of an Application Config class that loads the application properties from a file named config.properties which is stored in the root of the source packages (root package) and hence automatically deployed with the application jar file during the build process. import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; public cl...

Glassfish and MySQL - Connection Issue

Hi all I recently downloaded the new Netbeans 6.1 including the Glassfish v2 package to replace my previous versions. Once I moved my application to the new platform I had to temporarily deploy the application database from Oracle to MySQL to test on my local machine. My application is built on JavaEE and for the persitence layer I am making use of data access objects (using Toplink) fronted by a set of session beans. After porting the database for testing I started getting the following error: Internal Exception: java.sql.SQLException: Error in allocation a connection. Cause: No PasswordCredential found This problem doesn't occur during development within Netbeans (hence all jdbc resources and Persistence units work fine from within netbeans) but occurs on deployment on Glassfish. From some google searches i found that this problem existed for some time with previous versions but still exists with the latest installation of Netbeans/Glassfish including a bundled MySQL. A solution ...

Mac OS X - first impressions

I recently had to buy a laptop for my personal use. To the surprise of my closest colleagues I have bought a Mac Book Pro. My first impressions are excellent, money well spent. From the OS perspective I found java pre-installed and all the nice unix tools like ssh which i have to use to connect to my linux servers available in the 64 bit unix base OS. Remote access using VNC works perfect as well. Setting up glassfish and netbeans was again very easy to do without an problems - performance was excellent, better than i ever seen on windows platforms for sure - an experienced IT person can immediately feel the solid OS thats running under the hood. When it comes to the leopard interface one cant comment, definitely one of the sleekest and most user friendly environments i've seen so far. Just to conclude my short note, for the java developers -> its definitely a nice environment to work in, an x platform with the slickest environment around.

Java Persistance

Recently I explored a bit how one can easily make use of the new Java Persistance API. The example below shows how one can very quickly create and search records from a Derby database table called "Course". To keep things simple and at the same time examine exactly the code required for such a task I created a simple console application from my netbeans 5.5.1 IDE. The new persistance API is much lighter than the previous version and was taken out from the Enterprise API thus making it possible to use it from simple java applications. Once I created my console app I added a new "Entity class from Database" to the project. A dialog is displayed and the connection chosen must be previously defined from the Runtime->Databases section in Netbeans. In the dialog I as well chose the option to "Create Persistance Unit" using the Toplink Library and named it "PUnit". Once one goes through these simple steps automatically Netbeans creates an Entity clas...