Monday, July 13, 2015

Backtesting with Python

install python data science python vm and install zipline backtester & run the following command
sudo pip install zipline
sudo ipython notebook --profile=dst
You can use following code to import the libraries.
import pandas as pd
import pandas.io.data as web
import datetime
start = datetime.datetime(2005,1,1)
end= datetime.datetime(2010,1,1)
f = web.DataReader('F','yahoo',start,end)
It is possible to save the downloaded date to a csv file by using the following
f.to_csv('xlf_20050101-20100101.csv')
df = pd.read_csv('xlf_20050101-20100101.csv',index_col="Date",parse_dates=True)
df.head()
to calculate returns for prices use
df['c_p'] = 1-df ['Close'].shift(1)/df ['Close'] 
to add day of the week to the price use
df['weekday'] = df.index.dayofweek

returns by day of week.
returnbydayofweek = df.groupby('weekday')['c_p'].mean()
plot returns by day of week
a.plot(kind="bar")

Friday, December 24, 2010

Gold etf basket trading

image

time period is 22.12.2005 - 22.12.2010

gold etf basket AU CEF DGL GDX GFI GLD HMY IAU KGC

image

image

image

Money management:

Number of shares to buy is determined by the follown formula. 20% of initial position divided by (Close-2 time 13 period ATR). Start allocate capital from those ETFs which have highest Close/200daySMA ratio.

Long:

  • Take only long trades
  • 2 period RSI is less than 30
  • today is spy’s close is 1% above its 200 day SMA
  • today is lowest low of the past 7 days

Exit Long

  • method 1 : if 13 period adx is less than 30 and dmplus is higher than dminus and today is highest high of last 7 days
  • method 2 : 13 period adx is greater than 30 and close > (13day ema + 2*atr13) and dmplus is higher than dminus

Friday, October 8, 2010

xau gld system

image

image

image

date range 2004- 2010 daily

XAU Philadelphia Gold and Silver Sector Index

GLD SPDR Gold Shares

Strategy name kg-xau_gold system. This system buys gold etf.

send me and email to get the strategy of this system.

email : k...@g...l.com

gld – tlt system

image

image

 

image

date range 2004- 2010 daily

TLT is iShares Barclays 20+ Year Treas. Bond ETF

GLD SPDR Gold Shares

Strategy name kg-tlt_gold system. This system buys gold etf.

send me and email to get the strategy of this system.

email: k...@g...l.com

Thursday, October 7, 2010

Wide Range bars in the direction of the trend

image

Portfolio is MSCI International Country ETFs

image

image

01.01.2004– 05.10.2010 - daily data

Wide range bar is defined as “Today’s range is at least 50% greater than each of the previous five days.” StdDev of % wins is high. Because the system do not generate too much trades for each stock.

Win rate is high. Profit factor is good.

if wide range pattern occurs go long 

if Close > 200 day SMA and 10 day sma > 10 day sma yesterday buy stop at the high of today.

laps in the direction of the trend

 

image

Strategy is tested on top 20 liquid ETFs.

image

image

01.01.2004– 05.10.2010 - daily data

Lap occurs if open today is between close and high of yesterday. Laps has positive bias. Winrate and profit factor is good. no money management is used. strategy exists after 10 days.

if lap pattern occurs go long 

if Close > 200 day SMA and 10 day sma > 10 day sma yesterday buy stop at the high of today.

GAPS in the direction of the trade

image

image

S&P500 (^GSPC )  - 01.01.2004– 05.10.2010 - daily data

Gaps in the direction of the trend has positive bias. Win% and Profit factor is high. Netprofit is not spectacular but do not forger that we did not use any money management or stoploss system. System is exiting after 10 days without any initial stop.

when stocks gaps up in the direction of trend demand is high buyers are willing to pay more.

Gaps is defined as Open today > High of yesterday

Long

if Close > 200 day SMA and 10 day sma > 10 day sma yesterday buy stop at the high of today.