worx with curency, if digit 4 after dot is a pip.

Example - run script in shell with close Positions from 2013/08/08 (Dukascopy export csv file):

./pips.sh 20130808.csv

66.30 Pips total | 17.10 Pips lost (25.8%) | 49.20 Pips won (74.2%)
Gain/Lost 32.10 Pips


Testing in a Bash Shell (thx iwdisb)

create a "pips.sh" file.


#!/bin/bash
#
# Floating point number functions.

#####################################################################
# Default scale used by float functions.

float_scale=10

#####################################################################
# Evaluate a floating point number expression.

function float_eval()
{
local stat=0
local result=0.0
if [[ $# -gt 0 ]]; then
result=$(echo "scale=$float_scale; $*" | bc -q 2>/dev/null)
stat=$?
if [[ $stat -eq 0 && -z "$result" ]]; then stat=1; fi
fi
echo $result
return $stat
}

#####################################################################
# Evaluate a floating point number conditional expression.

function float_cond()
{
local cond=0
if [[ $# -gt 0 ]]; then
cond=$(echo "$*" | bc -q 2>/dev/null)
if [[ -z "$cond" ]]; then cond=0; fi
if [[ "$cond" != 0 && "$cond" != 1 ]]; then cond=0; fi
fi
local stat=$((cond == 0))
return $stat
}

fname=$1
#fname=Close_Positions.csv


n=-1
neg=0
pos=0

while read line; do
n=$(expr $n + 1)
if [ $n -gt 0 ]; then
p1=$(echo $line | cut -d, -f6)
p2=$(echo $line | cut -d, -f7)
p3=$(echo $line | cut -d, -f8)

abs=$(float_eval "$p1 - $p2" | awk '{if($1>=0) {print $1} else {print $1*-1}}')
if float_cond "$p3 < 0"; then
neg=$(float_eval "$neg - $abs")
else
pos=$(float_eval "$pos + $abs")
fi
fi
done < $fname
neg=$(float_eval "$neg * -10000")
pos=$(float_eval "$pos * 10000")
ges=$(float_eval "$pos + $neg")
wl=$(float_eval "$pos - $neg")
aneg=$(float_eval "$neg / $ges * 100")
apos=$(float_eval "$pos / $ges * 100")
echo "$(printf "%.2f" $ges) Pips total | $(printf "%.2f" $neg) Pips lost ($(printf "%.1f" $aneg)%) | $(printf "%.2f" $pos) Pips won ($(printf "%.1f" $apos)%)"
echo "Gain/Lost $(printf "%.2f" $wl) Pips"


Translate to English Show original