Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
raidastauras authored Jan 7, 2018
1 parent 5be4bae commit 02396d1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
38 changes: 35 additions & 3 deletions helpers/oanda_api_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ class TradingSession:
# initiate objects
def __init__(self, accountID, access_token):
self.accountID = accountID
self.access_token = access_token
self.api = oandapyV20.API(access_token=access_token, environment="practice")
self.order_book = self.sync_open_positions()
self.order_book = self.oanda_order_book()

# initiate methods
def send_request(self, request):
Expand Down Expand Up @@ -62,7 +63,7 @@ def open_order(self, instrument, units):
# check if request was fulfilled and save its ID
if request_data is not 1:
instrument = request_data['orderCreateTransaction']['instrument']
self.order_book[instrument]['tradeID'] = (request_data['lastTransactionID'])
self.order_book[instrument]['tradeID'] = request_data['lastTransactionID']
self.order_book[instrument]['order_type'] = -1 if units < 0 else 1
print('{}: {}'.format('Long' if units > 0 else 'Short', instrument))
return 0
Expand Down Expand Up @@ -98,7 +99,7 @@ def check_account_summary(self):
r = accounts.AccountSummary(self.accountID)
return self.send_request(r)

def sync_open_positions(self):
def oanda_order_book(self):
"""Synchronize open positions with this object's order_book"""
order_book_oanda = self.check_open_positions()
order_book = {'EUR_USD': {'order_type': None, 'tradeID': None},
Expand All @@ -114,6 +115,37 @@ def sync_open_positions(self):
order_book[pos['instrument']]['order_type'] = order_type
return order_book

def sync_with_oanda(self):
self.order_book = self.oanda_order_book()

def close_all_open_positions(self):
"""Close all opened positions"""

# check oanda for open positions
try:
open_positions = self.check_open_positions()['positions'][0]
except IndexError:
self.order_book = self.oanda_order_book()
print('No opened positions')
return 0

# get ID's of open positions
trade_ids = []
try:
[trade_ids.append(x) for x in open_positions['short']['tradeIDs']]
except KeyError:
pass
try:
[trade_ids.append(x) for x in open_positions['long']['tradeIDs']]
except KeyError:
pass

# close orders by ID
[close_order_manually(self.accountID, self.access_token, x) for x in trade_ids]
self.order_book = self.oanda_order_book()
print('All positions closed')
return 0


def close_order_manually(accountID, access_token, tradeID):
"""
Expand Down
23 changes: 18 additions & 5 deletions helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,33 @@ def portfolio_value(price, signal, trans_costs=0.0001):
return value


def get_data_batch(list_of_items, batch_size):
"""Returns a batch of sequential data"""
indexes = np.random.choice(len(list_of_items[0]) - (batch_size+1))
def get_data_batch(list_of_items, batch_size, sequential):
"""Returns a batch of data. A batch of sequence or random points."""
if sequential:
indexes = np.random.randint(len(list_of_items[0]) - (batch_size+1))
else:
indexes = np.random.randint(0, len(list_of_items[0]), batch_size)
batch_list = []
for item in list_of_items:
batch = item[indexes:indexes+batch_size, ...]
batch = item[indexes:indexes+batch_size, ...] if sequential else item[indexes, ...]
batch_list.append(batch)
return batch_list


def get_lstm_input_output(x, y, time_steps):
"""Returns a batch of sequential data for lstm shaped like [batch_size, time_steps, features]"""
"""Returns sequential lstm shaped data [batch_size, time_steps, features]"""
data_points, _ = np.shape(x)
x_batch_reshaped = []
for i in range(data_points - time_steps):
x_batch_reshaped.append(x[i: i+time_steps, :])
return np.array(x_batch_reshaped), y[time_steps:]


def get_cnn_input_output(x, y, time_steps=12):
"""Returns sequential cnn shaped data [batch_size, features, time_steps]"""
data_points, _ = np.shape(x)
x_batch_reshaped = []
for i in range(data_points - time_steps):
x_batch_reshaped.append(x[i:i+time_steps, :])
x_batch_reshaped = np.transpose(np.array([x_batch_reshaped]), axes=(1, 3, 2, 0))
return np.array(x_batch_reshaped), y[time_steps:]

0 comments on commit 02396d1

Please sign in to comment.