Ejemplo n.º 1
0
def fetch_trades_info(ACCESS_TOKEN, ACCOUNT_ID):
    # Provides a current state for transactions
    api = API(access_token = ACCESS_TOKEN)
    r = trades.TradesList(ACCOUNT_ID)
    trades_full_info = api.request(r)
    trades_data = trades_full_info['trades']

    trade_list = list()
    for trade in trades_data:
        open_datetime = datetime.strptime(trade['openTime'].split('.')[0], '%Y-%m-%dT%H:%M:%S')
        trade_item_info = {
            'tradeID' : trade['id'],
            'open_time' : open_datetime,
            'currentUnits' : int(trade['currentUnits']),
            'trade_price' : float(trade['price']),
            # 'take_profit_price' : float(trade['takeProfitOrder']['price']),
            # 'unrealized_PL' : float(trade['unrealizedPL']),
            # 'financing' : float(trade['financing']),
            'net_profit' : float(trade['unrealizedPL']) + float(trade['financing']),
            # 'comment' : trade['clientExtensions']['comment']
            # 'trade_status' : 'suspended'
        }
        if datetime.now() - timedelta(hours=60) > open_datetime:
            trade_item_info['trade_status'] = 'suspended'
        else:
            trade_item_info['trade_status'] = 'fresh'
        trade_list.append(trade_item_info)

    return trade_list
Ejemplo n.º 2
0
def get_trades():
    """ Returns a json of all accounts trades """
    r = trades.TradesList(accountID)
    #print("REQUEST:{}".format(r))
    rv = api.request(r)
    #print("RESPONSE:\n{}".format(json.dumps(rv, indent=2)))
    return rv
Ejemplo n.º 3
0
def all_trades_info_request(ACCESS_TOKEN, ACCOUNT_ID):
    api = API(access_token=ACCESS_TOKEN)

    r = trades.TradesList(ACCOUNT_ID)
    print("REQUEST:{}".format(r))
    rv = api.request(r)
    return "RESPONSE:\n{}".format(json.dumps(rv, indent=2))
Ejemplo n.º 4
0
 def existing_id(self):
     r = trades.TradesList(self.accountID)
     rv = self.client.request(r)
     existing_id = []
     for trade in rv['trades']:
         existing_id.append(int(trade['id']))
     return existing_id
Ejemplo n.º 5
0
def get_trades(instruments, return_size=100):
    params = {"instrument": ",".join(instruments), "state": "ALL"}
    r = trades.TradesList(RUNNING_ENV.account.mt4, params=params)
    try:
        rv = RUNNING_ENV.api.request(r)
    except V20Error as err:
        logging.error(r.status_code, err)
    else:
        logging.info(json.dumps(rv, indent=2))
        res = rv.get('trades')[:return_size]
        return sorted(res, key=lambda x: x['id'])
Ejemplo n.º 6
0
 def create_order(self, ordertype, units, instrument_index):
     self.order["order"]["type"] = ordertype
     self.order["order"]["units"] = units  #str(position_table[-1]*100)
     self.order["order"]["instrument"] = self.instruments[instrument_index]
     r_order = orders.OrderCreate(self.account_id, data=self.order)
     self.oanda20.request(r_order)
     trade_list = trades.TradesList(
         accountID=self.account_id,
         params=self.instruments[instrument_index])
     trade_reponse = self.oanda20.request(trade_list)
     trade_price = trade_reponse['trades'][0]['price']
     return (trade_price)
Ejemplo n.º 7
0
def fetch_trades_info(ACCESS_TOKEN, ACCOUNT_ID):
    # Provides a current state for transactions
    api = API(access_token=ACCESS_TOKEN)
    r = trades.TradesList(ACCOUNT_ID)

    try:
        trades_full_info = api.request(r)
        trades_data = trades_full_info['trades']
        return trades_data
    except (ConnectionError, TimeoutError):
        print(
            '\n\n\n\n\n\n----Connection error in fetch_trades_info----\n\n\n\n\n\n'
        )
        time.sleep(2)
Ejemplo n.º 8
0
 def create_id(self, data, label, price, action):
     existing_id = self.existing_id()
     if not self.open_id == self.existing_id:
         for id in existing_id:
             if id not in self.open_id:
                 self.open_id.append(id)
                 r = trades.TradesList(self.accountID)
                 rv = self.client.request(r)
                 for trade in rv['trades']:
                     if int(trade['id']) == id:
                         file = open(f'train/{self.name}.txt', 'a+')
                         liste = []
                         liste.append(str(id) + '\n')
                         liste.append(action + '-' + str(price) + '\n')
                         liste.append(str(data) + '\n')
                         liste.append(str(label) + '\n')
                         file.writelines(liste)
                         file.close()
                         break
Ejemplo n.º 9
0
import oandapyV20.endpoints.trades as trades
import configparser

config = configparser.ConfigParser()
config.read('./config/config_v20.ini')
accountID = config['oanda']['account_id']
access_token = config['oanda']['api_key']
"""
# 1 Get list of current trades for an account
"""
client = oandapyV20.API(access_token=access_token)

params = {"instrument": "DE30_EUR,EUR_USD"}

# params are optional. use to retrieve trades for specific currency pair(s)
r = trades.TradesList(accountID=accountID, params=params)
client.request(r)
print(r.response)
"""
# 2 Get the list of open Trades for an Account.¶
"""
r = trades.OpenTrades(accountID)
client.request(r)
trade_id = r.response['trades'][0]['id']
print(trade_id)
"""
# 3 Get the details of a specific Trade in an Account.
"""
r = trades.TradeDetails(accountID, tradeID=trade_id)
client.request(r)
print(r.response)
Ejemplo n.º 10
0
port = config.get('fxpractice', 'port')
ssl = config.get('fxpractice', 'ssl')
token = config.get('fxpractice', 'token')
username = config.get('fxpractice', 'username')
active_account = config.get('fxpractice', 'active_account')
# domain = config.get('instance', 'prod')
# headers = {'Authorization': 'Bearer {}'.format(token)}

# get a list of trades
from oandapyV20 import API
import oandapyV20.endpoints.trades as trades
import json

print("Running ConnectTest...")

api = API(token)
# accountID = "001-001-2033767-001"
r = trades.TradesList(active_account)
# show the endpoint as it is constructed for this call
print("REQUEST:{}".format(r))
rv = api.request(r)
print("RESPONSE:\n{}".format(json.dumps(rv, indent=2)))

# api = API(access_token="bc05fa130d462a39c9d181f053eb4002-5d129c81a25f312a7528a423e57ce83f")
# accountID = "001-001-2033767-001"
# r = trades.TradesList(accountID)
# # show the endpoint as it is constructed for this call
# print("REQUEST:{}".format(r))
# rv = api.request(r)
# print("RESPONSE:\n{}".format(json.dumps(rv, indent=2)))