Skip to content

Instantly share code, notes, and snippets.

@pazworld
Last active August 29, 2015 14:10
Show Gist options
  • Save pazworld/7ffccfd8d7594f054578 to your computer and use it in GitHub Desktop.
Save pazworld/7ffccfd8d7594f054578 to your computer and use it in GitHub Desktop.
#!/bin/sh
# calculate maximum drawdown.
awk '
START {max=0;maxdrawdown=0}
max<$3 {max=$3;print "set max",$1,max}
{drawdown=max-$3}
maxdrawdown<drawdown {
maxdrawdown=drawdown
maxdate=$1
print "set maxdrawdown",maxdate,maxdrawdown
}
END {print "Max drawdown:",maxdate,maxdrawdown}
'
curl "http://k-db.com/futures/F102-0000?download=csv"
#!/usr/bin/gnuplot
set xdata time
set timefmt "%Y-%m-%d"
set ytics nomirror
set y2tics
set grid
set terminal png size 1024,768
set output "result.png"
plot 'data.txt' using 1:2:3:4:5 with candlesticks linetype -1, \
'result.txt' using 1:3 with lines axis x1y2
set output
#!/bin/sh
# make graph pdf of data from stdin.
cat - >data.txt
cat data.txt | ./trade01.sh | ./sum.sh >result.txt
./graph.gp
#/bin/sh
# sum trade result
awk '
BEGIN {sum=0}
# {print $1 ":" $2 ":" $3}
$2=="long" {position="L";price=$3}
$2=="short" {position="S";price=$3}
$2=="close" && position=="L" {
dif=$3-price
sum=sum+dif
print $1,dif,sum
position=""
}
$2=="close" && position=="S" {
dif=price-$3
sum=sum+dif
print $1,dif,sum
position=""
}
'
#/bin/sh
# trade strategy No.01
awk '
BEGIN {
none=0;long=1;short=-1;position=none
high=high1=high2=low=low1=low2=0
}
{
date=$1;open=$2
high2=high1;high1=high;high=$3
low2=low1;low1=low;low=$4
clos=$5
done=0
# print date " low: " low ", high: " high
}
NR==1 {clos2=clos;done=1}
NR==2 {clos1=clos;done=1}
NR==3 && clos1>=clos2 {
position=long
position_price=open
print date " long " open
done=1
}
NR==3 && clos1<clos2 {
position=short
position_price=open
print date " short " open
done=1
}
done==0 && position==long && low2>low {
print date " close " low2
position=short
position_price=low2
print date " short " low2
done=1
}
done==0 && position==short && high2<high {
print date " close " high2
position=long
position_price=high2
print date " long " high2
done=1
}
'
#!/bin/sh
# unify nikkei225 data.
# set language to compare properly with sort command.
export LC_ALL=C
sort |
sed -n "/^[0-9]/p" |
grep -v "0,0" |
awk -F"," '
BEGIN {date=0}
# same day (night time)
$1==date && $4>high {high=$4}
$1==date && $5<low {low=$5}
$1==date {clos=$6}
# new day (day time)
$1!=date && 0!=date {print date,open,high,low,clos}
$1!=date {date=$1;open=$3;high=$4;low=$5;clos=$6}
END {print date,open,high,low,clos}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment