The trade insertion flow is responsible for inserting and storing all information regarding operations and portfolio movements during the day. This flow can be composed in two ways: Using an existing file stored on the platform, or by sending this file via API or Google Backup and Sync. A Google Drive Folder can be found at the end of this article, exemplifying the input data, codes and workflows presented here.
How to Insert Trades Manually
Using the File Retriever Worker, we will bring the Trades file to the workflow, and necessarily use a Parser Worker to transform the trade information to Everysk's Datastore and Symbology Formats.
1. Parsing the File
Once we have the file inside the workflow, we need a Parser Worker to map the trade information into the Everysk's Symbology and Datastore format automatically. For all the examples used in this article, the trade model to be inserted will be performed through the Everysk Trades Parser Worker. Link the File Retriever (or Storage) with parser on the workflow, and if you want to store the batch of trades, separatelly between the day, just set the Storage Mode of the worker as Create, however, you can use a Transient Storage Mode.
2. Storing Trades of a Day on Datastores
Using Worker Trades Storage, it is possible to store all trades that take place during a day in a single datastore. This worker is responsible for verifying if the Trade ID is already present in the datastore of that specific day and, if not, not executing the trade repeatedly, as well as assisting in the information that the trade was or was not inserted in the destination portfolio.
An example of a datastore generated by the Trades Storage Worker, can be seen in the image below.
3. Inserting Trades Into Portfolios
To insert trades into portfolios on the platform, you need choose between two operations modes of Trade Inserter Worker. If you choose the Option "Target Portfolio", trades that have arrived in batch will be entered into the specific portfolio you choose.
Otherwise, if you choose the option "Set By Portfolio UID", the Portfolio UID information, must be as a column in the trade file.
3.1 Notifying the Realtime Widget
After selecting the destination portfolio in which the trades will be inserted, you can also notify the RealTime P&L Widget of your portfolio, if it already exists.
If the insertion occurred successfully, a new highlight line will appear in the widget, indicating that is a trade, already interfering with the P&L calculation.
4. Split Trades defined by certain rules and insert this into multiple portfolios
Using the Split Trades Worker, you can enter trades into multiple portfolios from rules defined in configurations datastores.
From a trade property (Trader or Instrument Class), it is possible to split the trades with pre-defined percentages rules on the quantities which will be inserted in the portfolios
4.1 Assign a property to a rule by a Configuration Datastore
A configuration datastore for this tool is a datastore with two columns:
1) The key property (Trader or Instrument Class) that defines whice rule will be applied
2) The rule that will be applied.
Property | Rule |
Trader 1 | Rule A |
Trader 2 | Rule B |
Trader 3 | Rule C |
4.2 Rule Definition Datastore
A configuration datastore for this tool is a datastore with three columns:
1) The Rule Name
2) The Portfolio UID (usually a TAG, that defines the portfolio unique, like a CNPJ, for example)
3) The percentage destination for each portfolio.
Rule | Portfolio UID | Percentage |
Rule A | portfolio_sample_y | 0.8 |
Rule A | portfolio_sample_x | 0.2 |
Rule B | portfolio_sample_y | 0.7 |
Rule B | portfolio_sample_x | 0.3 |
For example:
If a PETR4 trade with a total amount of 12000 is processed by the Split Trades Worker, from Rule B, 3600 contracts will be added to Portfolio Sample A and 8400 contracts to Portfolio Sample B, considering the previous examples. The Storage Datastore will be like the image below.
How To insert Trades via API
To process trades via API, the first configuration to be done is search for the access keys. To find your credentials on the plataform, access "Settings" and "API", and copy the SID and Token info. If you don't have any API key, please send a message to support@everysk.com.
The insertion of trades and events via API is made through a REST application, whose documentation can be found here. From commands via API for the initialization of the flow, it is possible to send the trade data, in the format of CSV files (also called Grids), described in the previous topic. The Python code below exemplifies how to send a CSV file to start workflow through the Everysk Python REST API.
import everysk
import base64
CONFIG = {
'SID': '<SID>',
'TOKEN':' <TOKEN>',
'WORKSPACE': '<WORKSPACE>',
'WORKFLOW_ID': '<WORKFLOW_ID>'
}
def everysk_config(SID, TOKEN):
everysk.api_sid = SID
everysk.api_token = TOKEN
def send_event_file(file_name, event_type):
with open(f'{file_name}', 'rb') as file:
data = file.read()
params = {
'workspace': CONFIG['WORKSPACE'],
'parameters': {
'file': {
'name': f'{file_name}',
'content_type': 'application/csv',
'data': base64.b64encode(data).decode('utf8'),
'tags': ['trades', 'csv', 'date']
},
'event_type': event_type
}
}
everysk.Workflow.run(CONFIG['WORKFLOW_ID'], **params)
#########################
# SET EVERYSK API CONFIG
#########################
everysk_config(CONFIG['SID'], CONFIG['TOKEN'])
#########################
# SEND FILE
#########################
send_event_file('trade_file.csv', 'Trade')
To configure a workflow to receive data through API requests change the Starter Worker trigger type to "API" and configure the outputs like the image below.
If you want to trigger a workflow through the API, the File Retriever Worker inside the workflow must be replaced by a File Generator Worker, in order to save the received file. Once the file is stored it will be available for future queries and manipulations too.
Example Folder at Google Drive
To acess the files used in this tutorial, please click here.