Skip to content

Commit

Permalink
bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wvanamstel committed Feb 9, 2015
1 parent 632d9c1 commit d9230a1
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
1 change: 1 addition & 0 deletions forex/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(self, instrument, units, order_type, side):

class SignalEvent(Event):
def __init__(self, instrument, order_type, side):
self.type = 'SIGNAL'
self.instrument = instrument
self.order_type = order_type
self.side =side
Expand Down
8 changes: 4 additions & 4 deletions forex/position.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ def calc_pips(self):
mult = -1
return mult * (self.cur_price - self.avg_price)

def cal_profit_base(self):
def calc_profit_base(self):
# calc nominal pnl of total position
pips = self.calc_pips()
return pips * self.exposure / self.cur_price

def cal_profit_perc(self):
def calc_profit_perc(self):
# calc relative pnl of total position
return self.profit_base / self.exposure * 100.

def update_position_price(self, cur_price):
# update pnl metrics to current instrument price
self.cur_price = cur_price
self.profit_base = self.cal_profit_base()
self.profit_perc = self.cal_profit_perc()
self.profit_base = self.calc_profit_base()
self.profit_perc = self.calc_profit_perc()

4 changes: 2 additions & 2 deletions forex/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ def calc_signals(self, event):
self.ticks += 1
if self.ticks % 5 == 0:
if self.invested == False:
signal = SignalEvent(instrument, 'market', 'buy')
signal = SignalEvent(self.instrument, 'market', 'buy')
self.events.put(signal)
self.invested = True
else:
signal = SignalEvent(instrument, 'market', 'sell')
signal = SignalEvent(self.instrument, 'market', 'sell')
self.events.put(signal)
self.invested = False

4 changes: 3 additions & 1 deletion forex/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ def stream_to_queue(self):
print 'Error converting into json:\n' + str(e)
return
if msg.has_key('instrument') or msg.has_key('tick'):
print msg
instrument = msg['tick']['instrument']
time = msg['tick']['time']
bid = msg['tick']['bid']
ask = msg['tick']['ask']
print instrument + ' ' + str(bid) + ' -- ' + str(ask) +\
' ' + str(time)

self.cur_bid = bid
self.cur_ask = ask
tev = TickEvent(instrument, time, bid, ask)
Expand Down
23 changes: 13 additions & 10 deletions helper_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class GetData(object):
IN: all strings; symbol, interval in secs, time period, open/close/etc
OUT: data frame with historical minute prices
'''
def __init__(self, interval='60', period='1d', dat='d,o,h,l,c,v'):
def __init__(self, interval='60', period='5d', dat='d,o,h,l,c,v'):
self.interval = interval # default is 60 seconds
self.period = period # number of days history, default is 5
self.dat = dat # 'date', 'open', 'high', etc
Expand Down Expand Up @@ -98,15 +98,18 @@ def get_rt_quote(self, symbol):


if __name__ == '__main__':
a = GetData()
lst = ['EURUSD']
a = GetData(period='1d')
lst = 'JNPR'
quotes = a.get_minute_data(lst)
#test = a.get_minute_data('EURUSD')

for i in range(5):
for sym in ['EURUSD',2]:
print time.ctime()
print sym + ' ' + a.get_rt_quote(sym)[0]

print '========================'
time.sleep(60)
#==============================================================================
# for i in range(5):
# for sym in ['EURUSD',2]:
# print time.ctime()
# print sym + ' ' + a.get_rt_quote(sym)[0]
#
# print '========================'
# time.sleep(60)
#==============================================================================

6 changes: 0 additions & 6 deletions vol_measures.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def __init__(self, data, window=30, total_days=252):
def close_close(self):
log_return = (self.data['Close']/self.data['Close'].shift(1)).apply(np.log)
vol = pd.rolling_std(log_return, self.window) * np.sqrt(self.total_days)
vol.index = self.data['Date']

return vol

Expand All @@ -30,7 +29,6 @@ def rs(self):

rsy = log_hc * log_ho + log_lc * log_lo
out = self.annualise(rsy)
out.index = self.data['Date']

return out

Expand All @@ -39,7 +37,6 @@ def parkinson(self):

park = (1/(4*np.log(2))) * log_hl**2
out = self.annualise(park)
out.index = self.data['Date']

return out

Expand All @@ -50,7 +47,6 @@ def gk(self):

gk = 1./2 * log_hl**2 - (2 * np.log(2) - 1) * log_co**2
out = self.annualise(gk)
out.index = self.data['Date']

return out

Expand All @@ -66,7 +62,6 @@ def gkyz(self):
gkyz = log_oc**2 + 1./2 * log_hl**2 - (2*np.log(2) - 1)*log_co**2

out = self.annualise(gkyz)
out.index = self.data['Date']

return out

Expand All @@ -87,7 +82,6 @@ def yz(self):
k = 0.34/(1. + (self.window + 1.)/(self.window - 1.))

out = (s2o + k * s2c + (1-k)*s2rs).apply(np.sqrt)
out.index = self.data['Date']

return out

Expand Down

0 comments on commit d9230a1

Please sign in to comment.