예제 #1
0
 def __init__(self):
     self.__default_credentials = Credentials(
         int_account=None,
         username=config.DG_USERNAME,
         password=config.DG_PASSWORD,
         one_time_password=None,
         totp_secret_key=config.DG_TOTP_SECRET,
     )
     self.__trading_api = TradingAPI(
         credentials=self.__default_credentials, )
예제 #2
0
# SETUP CONFIG DICT
with open('config/config.json') as config_file:
    config_dict = json.load(config_file)

# SETUP CREDENTIALS
username = config_dict['username']
password = config_dict['password']
credentials = Credentials(
    int_account=None,
    username=username,
    password=password,
)

# SETUP TRADING API
trading_api = TradingAPI(credentials=credentials)

# CONNECT
trading_api.connect()

# ACCESS SESSION_ID
session_id = trading_api.connection_storage.session_id

# Waiting
sleep_time = random.uniform(1, 5)
print(f'Waiting : {sleep_time}s ')
time.sleep(sleep_time)

# FETCH CONFIG TABLE
print(len(trading_api.get_config()))
예제 #3
0
# SETUP CONFIG DICT
with open('config/config.json') as config_file:
    config_dict = json.load(config_file)

# SETUP CREDENTIALS
int_account = config_dict['int_account']
username = config_dict['username']
password = config_dict['password']
credentials = Credentials(
    int_account=int_account,
    username=username,
    password=password,
)

# SETUP TRADING API
trading_api = TradingAPI(credentials=credentials)

# CONNECT
trading_api.connect()

# SETUP REQUEST
today = datetime.date.today()
from_date = OrdersHistory.Request.Date(
    year=2020,
    month=10,
    day=1,
)
to_date = OrdersHistory.Request.Date(
    year=today.year,
    month=today.month,
    day=today.day,
예제 #4
0
# SETUP CONFIG DICT
with open('config/config.json') as config_file:
    config_dict = json.load(config_file)

# SETUP CREDENTIALS
username = config_dict['username']
password = config_dict['password']

credentials = Credentials(
    int_account=None,
    username=username,
    password=password,
)

# SETUP TRADING API
trading_api = TradingAPI(credentials=credentials)

# CONNECT
trading_api.connect()

# SETUP REQUEST
request = NewsByCompany.Request(
    isin='NL0000235190',
    limit=10,
    offset=0,
    languages='en,fr',
)

# FETCH DATA
news_by_company = trading_api.get_news_by_company(request=request, raw=True)
예제 #5
0
# SETUP CONFIG DICT
with open('config/config.json') as config_file:
    config_dict = json.load(config_file)

# SETUP CREDENTIALS
int_account = config_dict['int_account']
username = config_dict['username']
password = config_dict['password']
credentials = Credentials(
    int_account=int_account,
    username=username,
    password=password,
)

# SETUP TRADING API
trading_api = TradingAPI(credentials=credentials)

# CONNECT
trading_api.connect()

# SETUP REQUEST
request = Agenda.Request()
request.start_date.FromJsonString('2021-06-21T22:00:00Z')
request.end_date.FromJsonString('2021-11-28T23:00:00Z')
request.calendar_type = Agenda.CalendarType.DIVIDEND_CALENDAR

# FETCH DATA
agenda = trading_api.get_agenda(
    request=request,
    raw=False,
)
예제 #6
0
class DegiroModel:
    def __init__(self):
        self.__default_credentials = Credentials(
            int_account=None,
            username=config.DG_USERNAME,
            password=config.DG_PASSWORD,
            one_time_password=None,
            totp_secret_key=config.DG_TOTP_SECRET,
        )
        self.__trading_api = TradingAPI(
            credentials=self.__default_credentials, )

    def __hold_fetch_additional_information(
        self,
        positions: pd.DataFrame,
    ) -> pd.DataFrame:
        """Fetch extra information about the positions like :
            - name
            - isin
            - symbol
            - ...

        Parameters
        ----------
        positions : pd.DataFrame
            Positions from which we want extra fields.

        Returns
        -------
        pd.DataFrame
            Positions with additional data.
        """

        # GET ATTRIBUTES
        trading_api = self.__trading_api

        # EXTRACT POSITIONS IDS
        positions_ids = positions["id"].astype("int32").tolist()

        # FETCH EXTRA DATA
        request = ProductsInfo.Request()
        request.products.extend(positions_ids)
        products_info_pb = trading_api.get_products_info(
            request=request,
            raw=False,
        )

        # CONVERT TO DICT
        products_info_dict = payload_handler.message_to_dict(
            message=products_info_pb, )

        # CONVERT TO DATAFRAME
        products_info = pd.DataFrame(products_info_dict["values"].values())

        # MERGE DATA WITH POSITIONS
        positions_full = pd.merge(positions, products_info, on="id")

        return positions_full

    def __hold_fetch_current_positions(self) -> pd.DataFrame:
        # GET ATTRIBUTES
        trading_api = self.__trading_api

        # FETCH HELD PRODUCTS
        request_list = Update.RequestList()
        request_list.values.extend([
            Update.Request(
                option=Update.Option.PORTFOLIO,
                last_updated=0,
            ),
        ])

        update_pb = trading_api.get_update(
            request_list=request_list,
            raw=False,
        )

        # CHECK EMPTINESS
        if len(update_pb.portfolio.values) == 0:
            return pd.DataFrame()
        else:
            positions_partial = self.__hold_filter_current_positions(
                portfolio=update_pb.portfolio, )

            # FETCH ADDITIONAL DATA ON PRODUCTS
            positions = self.__hold_fetch_additional_information(
                positions=positions_partial, )

            return positions

    @staticmethod
    def __hold_filter_current_positions(
        portfolio: Update.Portfolio, ) -> pd.DataFrame:
        """Filter the positions in order to keep only held ones.

        Parameters
        ----------
        portfolio : Update.Portfolio
            Portfolio returned from the API.

        Returns
        -------
        pd.DataFrame
            Filtered portfolio.
        """

        # CONVERT TO DATAFRAME
        portfolio_dict = payload_handler.message_to_dict(message=portfolio)
        positions = pd.DataFrame(portfolio_dict["values"])

        # SETUP MASK
        mask_product = positions["positionType"] == "PRODUCT"
        mask_not_empty = positions["size"] > 0
        mask_current_position = mask_product & mask_not_empty

        # FILTER
        positions = positions[mask_current_position]

        return positions

    def __setup_extra_credentials(self):
        trading_api = self.__trading_api
        client_details_table = trading_api.get_client_details()
        int_account = client_details_table["data"]["intAccount"]
        trading_api.credentials.int_account = int_account

    def cancel(self, order_id: str) -> bool:
        return self.__trading_api.delete_order(order_id=order_id)

    def companynews(self, isin: str) -> NewsByCompany:
        trading_api = self.__trading_api
        request = NewsByCompany.Request(
            isin=isin,
            limit=10,
            offset=0,
            languages="en,fr",
        )

        # FETCH DATA
        news = trading_api.get_news_by_company(
            request=request,
            raw=False,
        )

        return news

    def create_calculate_product_id(
        self,
        product: int,
        symbol: str,
    ) -> Union[int, None]:
        trading_api = self.__trading_api

        if product is None:
            request_lookup = ProductSearch.RequestLookup(
                search_text=symbol,
                limit=1,
                offset=0,
                product_type_id=1,
            )

            products_lookup = trading_api.product_search(
                request=request_lookup,
                raw=False,
            )
            products_lookup_dict = payload_handler.message_to_dict(
                message=products_lookup, )
            product = products_lookup_dict["products"][0]

            if len(products_lookup.products
                   ) <= 0 or product["symbol"] != symbol:
                return None
            else:
                return int(product["id"])
        else:
            return product

    def create_calculate_size(
        self,
        price: float,
        size: int,
        up_to: float,
    ):
        if size is None:
            return math.floor(up_to / price)
        else:
            return size

    def create_check(self,
                     order: Order) -> Union[Order.CheckingResponse, bool]:
        return self.__trading_api.check_order(order=order)

    def create_confirm(
        self,
        confirmation_id: str,
        order: Order,
    ) -> Union[Order.ConfirmationResponse, bool]:
        return self.__trading_api.confirm_order(
            confirmation_id=confirmation_id,
            order=order,
        )

    def hold_positions(self) -> pd.DataFrame:
        return self.__hold_fetch_current_positions()

    def lastnews(self, limit: int) -> LatestNews:
        trading_api = self.__trading_api
        request = LatestNews.Request(
            offset=0,
            languages="en,fr",
            limit=limit,
        )
        news = trading_api.get_latest_news(request=request, raw=False)

        return news

    def login(self, credentials: Credentials):
        self.__trading_api = TradingAPI(credentials=credentials)
        self.__trading_api.connect()
        self.__setup_extra_credentials()

    def login_default_credentials(self):
        return self.__default_credentials

    def logout(self) -> bool:
        return self.__trading_api.logout()

    def lookup(
        self,
        limit: int,
        offset: int,
        search_text: str,
    ) -> ProductSearch:
        trading_api = self.__trading_api
        request_lookup = ProductSearch.RequestLookup(
            search_text=search_text,
            limit=limit,
            offset=offset,
        )
        product_search = trading_api.product_search(
            request=request_lookup,
            raw=False,
        )

        return product_search

    def pending(self) -> Update.Orders:
        trading_api = self.__trading_api
        request_list = Update.RequestList()
        request_list.values.extend([
            Update.Request(option=Update.Option.ORDERS, last_updated=0),
        ])
        update = trading_api.get_update(request_list=request_list)

        return update.orders

    def topnews(self) -> TopNewsPreview:
        return self.__trading_api.get_top_news_preview(raw=False)

    def update(self, order: Order) -> Update.Orders:
        return self.__trading_api.update_order(order=order)

    def update_pending_order(self, order_id: str) -> Union[None, Order]:
        trading_api = self.__trading_api
        request_list = Update.RequestList()
        request_list.values.extend([
            Update.Request(option=Update.Option.ORDERS, last_updated=0),
        ])
        update = trading_api.get_update(request_list=request_list)

        if len(update.orders.values) > 0:
            for order in update.orders.values:
                if order.id == order_id:
                    return order
            return None
        else:
            return None
예제 #7
0
logging.basicConfig(level=logging.DEBUG)

# SETUP CONFIG DICT
with open('config/config.json') as config_file:
    config_dict = json.load(config_file)

# SETUP CREDENTIALS
username = config_dict['username']
password = config_dict['password']
credentials = Credentials(
    int_account=None,
    username=username,
    password=password,
)

# SETUP TRADING API
trading_api = TradingAPI(credentials=credentials)

# CONNECT
trading_api.connect()

# FETCH DATA
product_isin = 'FR0000131906'
financial_statements = trading_api.get_financial_statements(
    product_isin=product_isin,
    raw=True,
)

# DISPLAY DATA
print(financial_statements)
예제 #8
0
logging.basicConfig(level=logging.DEBUG)

# SETUP CONFIG DICT
with open('config/config.json') as config_file:
    config_dict = json.load(config_file)

# SETUP CREDENTIALS
username = config_dict['username']
password = config_dict['password']
credentials = Credentials(
    int_account=None,
    username=username,
    password=password,
)

# SETUP TRADING API
trading_api = TradingAPI(credentials=credentials)

# CONNECT
trading_api.connect()

# FETCH DATA
product_isin = 'FR0000131906'
company_ratios = trading_api.get_company_ratios(
    product_isin=product_isin,
    raw=True,
)

# DISPLAY DATA
print(company_ratios)
예제 #9
0
# SETUP CONFIG DICT
with open('config/config.json') as config_file:
    config_dict = json.load(config_file)

# SETUP CREDENTIALS
int_account = config_dict['int_account']
username = config_dict['username']
password = config_dict['password']
credentials = Credentials(
    int_account=int_account,
    username=username,
    password=password,
)

# SETUP TRADING API
trading_api = TradingAPI(credentials=credentials)

# CONNECT
trading_api.connect()

# SETUP REQUEST
today = datetime.date.today()
from_date = TransactionsHistory.Request.Date(
    year=2020,
    month=10,
    day=1,
)
to_date = TransactionsHistory.Request.Date(
    year=today.year,
    month=today.month,
    day=today.day,
예제 #10
0
# SETUP CONFIG DICT
with open('config/config.json') as config_file:
    config_dict = json.load(config_file)

# SETUP CREDENTIALS
int_account = config_dict['int_account']
username = config_dict['username']
password = config_dict['password']
credentials = Credentials(
    int_account=int_account,
    username=username,
    password=password,
)

# SETUP TRADING API
trading_api = TradingAPI(credentials=credentials)

# CONNECT
trading_api.connect()

# SETUP REQUEST
today = datetime.date.today()
from_date = AccountOverview.Request.Date(
    year=2020,
    month=1,
    day=1,
)
to_date = AccountOverview.Request.Date(
    year=today.year,
    month=today.month,
    day=today.day,
예제 #11
0
# SETUP LOGGING LEVEL
logging.basicConfig(level=logging.DEBUG)

# SETUP CONFIG DICT
with open('config/config.json') as config_file:
    config_dict = json.load(config_file)

# SETUP CREDENTIALS
username = config_dict['username']
password = config_dict['password']
one_time_password = config_dict['one_time_password']

credentials = Credentials(
    int_account=None,
    username=username,
    password=password,
    one_time_password=one_time_password,
)

# SETUP TRADING API
trading_api = TradingAPI(credentials=credentials)

# CONNECT
trading_api.connect()

# ACCESS SESSION_ID
session_id = trading_api.connection_storage.session_id

print('You are now connected, with the session id :', session_id)
예제 #12
0
# SETUP CONFIG DICT
with open('config/config.json') as config_file:
    config_dict = json.load(config_file)

# SETUP CREDENTIALS
int_account = config_dict['int_account']
username = config_dict['username']
password = config_dict['password']
credentials = Credentials(
    int_account=int_account,
    username=username,
    password=password,
)

# SETUP TRADING API
trading_api = TradingAPI(credentials=credentials)

# CONNECT
trading_api.connect()

# FETCH DATA - MESSAGE
products_config = trading_api.get_products_config(raw=False)

# DISPLAY - MESSAGE
for item in products_config.values:
    print(item)

# FETCH DATA - DICT
products_config_dict = trading_api.get_products_config(raw=True)
products_config_pretty = json.dumps(
    products_config_dict,
예제 #13
0
# SETUP CONFIG DICT
with open('config/config.json') as config_file:
    config_dict = json.load(config_file)

# SETUP CREDENTIALS
username = config_dict['username']
password = config_dict['password']
credentials = Credentials(
    int_account=None,
    username=username,
    password=password,
)

# SETUP TRADING API
trading_api = TradingAPI(credentials=credentials)

# CONNECT
trading_api.connect()

# FETCH CONFIG TABLE
client_details_table = trading_api.get_client_details()

# EXTRACT DATA
int_account = client_details_table['data']['intAccount']
user_token = client_details_table['data']['id']
client_details_pretty = json.dumps(
    client_details_table,
    sort_keys=True,
    indent=4,
)
예제 #14
0
# SETUP CONFIG DICT
with open('config/config.json') as config_file:
    config_dict = json.load(config_file)

# SETUP CREDENTIALS
int_account = config_dict['int_account']
username = config_dict['username']
password = config_dict['password']
credentials = Credentials(
    int_account=int_account,
    username=username,
    password=password,
)

# SETUP TRADING API
trading_api = TradingAPI(credentials=credentials)

# ESTABLISH CONNECTION
trading_api.connect()

# SETUP REQUEST
request_stock = ProductSearch.RequestStocks(
    index_id=5,
    is_in_us_green_list=False,
    stock_country_id=886,
    offset=0,
    limit=100,
    require_total=True,
    sort_columns='name',
    sort_types='asc',
)
예제 #15
0
# SETUP CONFIG DICT
with open('config/config.json') as config_file:
    config_dict = json.load(config_file)

# SETUP CREDENTIALS
username = config_dict['username']
password = config_dict['password']

credentials = Credentials(
    int_account=None,
    username=username,
    password=password,
)

# SETUP TRADING API
trading_api = TradingAPI(credentials=credentials)

# CONNECT
trading_api.connect()

# FETCH CONFIG TABLE
config_table = trading_api.get_config()

# EXTRACT DATA
user_token = config_table['clientId']
session_id = config_table['sessionId']

# DISPLAY DATA
config_pretty = json.dumps(config_table, sort_keys=True, indent=4)

print('Your "user_token" is :', user_token)
예제 #16
0
# SETUP LOGGING LEVEL
logging.basicConfig(level=logging.DEBUG)

# SETUP CONFIG DICT
with open('config/config.json') as config_file:
    config_dict = json.load(config_file)

# SETUP CREDENTIALS
int_account = config_dict['int_account']
username = config_dict['username']
password = config_dict['password']
credentials = Credentials(
    int_account=int_account,
    username=username,
    password=password,
)

# SETUP TRADING API
trading_api = TradingAPI(credentials=credentials)

# CONNECT
trading_api.connect()

# FETCH DATA
account_info_table = trading_api.get_account_info()

# DISPLAY DATA
account_info_pretty = json.dumps(account_info_table, sort_keys=True, indent=4)
print(account_info_pretty)
예제 #17
0
def trading_api(credentials) -> TradingAPI:
    trading_api = TradingAPI(credentials=credentials)
    trading_api.connect()

    return trading_api
예제 #18
0
logging.basicConfig(level=logging.DEBUG)

# SETUP CONFIG DICT
with open('config/config.json') as config_file:
    config = json.load(config_file)

# SETUP CREDENTIALS
int_account = config['int_account']
username = config['username']
password = config['password']
credentials = Credentials(int_account=int_account,
                          username=username,
                          password=password)

# SETUP TRADING API
trading_api = TradingAPI(credentials=credentials)

# CONNECT
trading_api.connect()

# SETUP REQUEST
request_list = Update.RequestList()
request_list.values.extend([
    Update.Request(option=Update.Option.ORDERS, last_updated=0),
    Update.Request(option=Update.Option.PORTFOLIO, last_updated=0),
    Update.Request(option=Update.Option.TOTALPORTFOLIO, last_updated=0),
])

update = trading_api.get_update(request_list=request_list, raw=False)
update_dict = pb_handler.message_to_dict(message=update)
예제 #19
0
# SETUP CONFIG DICT
with open('config/config.json') as config_file:
    config_dict = json.load(config_file)

# SETUP CREDENTIALS
int_account = config_dict['int_account']
username = config_dict['username']
password = config_dict['password']
credentials = Credentials(
    int_account=int_account,
    username=username,
    password=password,
)

# SETUP TRADING API
trading_api = TradingAPI(credentials=credentials)

# CONNECT
trading_api.connect()

# FETCH DATA - MESSAGE
favourites_list = trading_api.get_favourites_list(raw=False)

# DISPLAY - MESSAGE
for list in favourites_list.values:
    print('id:', list.id)
    print('name:', list.name)
    print('is_default:', list.is_default)
    print('product_ids:', list.product_ids)
    print('-')
예제 #20
0
logging.basicConfig(level=logging.DEBUG)

# SETUP CONFIG DICT
with open('config/config.json') as config_file:
    config_dict = json.load(config_file)

# SETUP CREDENTIALS
username = config_dict['username']
password = config_dict['password']
credentials = Credentials(
    int_account=None,
    username=username,
    password=password,
)

# SETUP TRADING API
trading_api = TradingAPI(credentials=credentials)

# CONNECT
trading_api.connect()

# FETCH DATA
product_isin = 'FR0000131906'
company_profile = trading_api.get_company_profile(
    product_isin=product_isin,
    raw=True,
)

# DISPLAY DATA
print(company_profile)
예제 #21
0
 def login(self, credentials: Credentials):
     self.__trading_api = TradingAPI(credentials=credentials)
     self.__trading_api.connect()
     self.__setup_extra_credentials()
예제 #22
0
# SETUP CONFIG DICT
with open('config/config.json') as config_file:
    config_dict = json.load(config_file)

# SETUP CREDENTIALS
int_account = config_dict['int_account']
username = config_dict['username']
password = config_dict['password']
credentials = Credentials(
    int_account=int_account,
    username=username,
    password=password,
)

# SETUP TRADING API
trading_api = TradingAPI(credentials=credentials)

# ESTABLISH CONNECTION
trading_api.connect()

# SETUP REQUEST
request_lookup = ProductSearch.RequestLookup(
    search_text='APPLE',
    limit=2,
    offset=0,
    product_type_id=1,
)

# FETCH DATA
products_lookup = trading_api.product_search(request=request_lookup, raw=False)
products_lookup_dict = payload_handler.message_to_dict(message=products_lookup)
예제 #23
0
# SETUP CONFIG DICT
with open('config/config.json') as config_file:
    config_dict = json.load(config_file)

# SETUP CREDENTIALS
username = config_dict['username']
password = config_dict['password']

credentials = Credentials(
    int_account=None,
    username=username,
    password=password,
)

# SETUP TRADING API
trading_api = TradingAPI(credentials=credentials)

# CONNECT
trading_api.connect()

# SETUP REQUEST
request = LatestNews.Request(
    offset=0,
    languages='en,fr',
    limit=20,
)

# FETCH DATA
latest_news = trading_api.get_latest_news(request=request, raw=True)

# DISPLAY DATA
예제 #24
0
# SETUP LOGGING LEVEL
logging.basicConfig(level=logging.DEBUG)

# SETUP CONFIG DICT
with open('config/config.json') as config_file:
    config_dict = json.load(config_file)

# SETUP CREDENTIALS
username = config_dict['username']
password = config_dict['password']

credentials = Credentials(
    int_account=None,
    username=username,
    password=password,
)

# SETUP TRADING API
trading_api = TradingAPI(credentials=credentials)

# CONNECT
trading_api.connect()

# FETCH DATA
top_news_preview = trading_api.get_top_news_preview(raw=True)

# DISPLAY DATA
config_pretty = json.dumps(top_news_preview, sort_keys=True, indent=4)

print('Here is the top news preview :', config_pretty)
예제 #25
0
# SETUP CONFIG DICT
with open('config/config.json') as config_file:
    config_dict = json.load(config_file)

# SETUP CREDENTIALS
int_account = config_dict['int_account']
username = config_dict['username']
password = config_dict['password']
credentials = Credentials(
    int_account=int_account,
    username=username,
    password=password,
)

# SETUP TRADING API
trading_api = TradingAPI(credentials=credentials)

# CONNECT
trading_api.connect()

# SETUP REQUEST
today = datetime.date.today()
from_date = CashAccountReport.Request.Date(
    year=2020,
    month=1,
    day=1,
)
to_date = CashAccountReport.Request.Date(
    year=today.year,
    month=today.month,
    day=today.day,
예제 #26
0
# SETUP CONFIG DICT
with open('config/config.json') as config_file:
    config_dict = json.load(config_file)

# SETUP CREDENTIALS
int_account = config_dict['int_account']
username = config_dict['username']
password = config_dict['password']
credentials = Credentials(
    int_account=int_account,
    username=username,
    password=password,
)

# SETUP TRADING API
trading_api = TradingAPI(credentials=credentials)

# CONNECT
trading_api.connect()

# SETUP REQUEST
request = ProductsInfo.Request()
request.products.extend([96008, 1153605, 5462588])

# FETCH DATA
products_info = trading_api.get_products_info(
    request=request,
    raw=True,
)

# DISPLAY PRODUCTS_INFO