Am I taking the wrong approach to this data problem?

I’m trying to basically build a paper (virtual) trading app and I’m stuck on how I should approach my data base design.

First, the user creates a new position in a stock. The inputs are Action [Buy or Sell Short], Symbol [the stock ticker], Price, # Shares. This goes into a data type called Positions. When the position is opened, the field Status (open/closed) is set to open.

Now the hard part is when the user wants to close or modify an open position. What would be the best route to take for this. What I’m currently attempting - a repeating group with the user’s open positions is displayed. Whichever position the user wants to modify - they click that cell in the repeating group, enter the action [Sell or Buy to Cover], price, #shares, etc. I’ve gotten this far.

Now in the workflow I “make a change” to the data type Positions where my open position is. First I have to make sure it is a sell order, then I have to take into account the number of shares. Because a user can sell some of their shares but not exit the position fully, thus leaving the position “open”.

Here is how I have arranged it so far. It just doesn’t feel right, am I taking the right approach?

Thanks

I would approach it a different way.

I would have 2 types of thing:

a) Position - this keeps the folllowing data (ticker, net position, cost)
b) Transaction (read below)

For transaction, it will be buys, sells, etc. This will be what you described above. Each record is either a buy, sell, split, etc. Do not change this record once created. Once you create the new transaction, this data will be used to either:

  • create a new Position record (if this is a new position) or

  • edit an existing Position.

Hence, for the user, you just need to show them their position by referring to the Position thing. This is supported by a repeating group of transactions (to be shown as required) to show how the current net position is derived.

Doing it this way, you only need to do revaluation using the position record.

2 Likes

Thank you, I will take that route.

Now when I want to make a change to a position, I will have to create multiple similar workflow actions because changing a position can be either a buy/sell/buy to cover a short. It doesn’t seem like Bubble can handle more than one “if” statement per action so my worry is that it’s going to create a lot of steps for the workflow and each time the “thing to change” has to do a search to find the matching portfolio.

Is there a way that once the workflow has done the first matching search for portfolio, I can avoid having to do the same search in subsequent actions to modify that same portfolio?

Hi, Bubble is more flexible than that. Each workflow can have many actions. Just make sure that each of them is sufficiently different.

For example
Action 1 is restricted to “Buy”
Action 2 is restricted to “Sell”
Action 3 is restricted to “Buy to Cover”

You need to make sure that each action’s conditions will exclude the other actions from happening.

Hence Action 1 + Action 2 + Action 3 = 100% of all possible actions and not 90% (where you have missed out a possible outcome) or 110% where Action 1 conditions is not so strict that Action 2 conditions can will also trigger action 2 when 1 is also triggered earlier.

An alternative strategy is to do a Terminate Workflow action after every action to prevent another condition from running (if the earlier workflow is deemed to satisfy the entire workflow run requirement).

For example:

Your 1st action is “Buy to Open”. Once you detect “Buy to Open” you know that there can be no other action that comes after that e.g. preclude “Sell an Existing Position”, “Borrow to Open a Short Sale”, “Buy to Cover” or many other variations (more if you are doing options).

Say your Workflow (WF) Action 1 is detect “if Open Action is Buy”, you already know that this is the transaction. What I would do is to put the next action as “Terminate Workflow” using the same condition as Action 1 i.e. “if Open Action is Buy”. This will ensure that no other Action runs in this workfow after Action 1 “first detection of Open Action is Buy” and After 2 to Terminate this Workflow.

What I would do is not have Workflow Action 1 do the editing of Positions but instead convert Workflow Action 1 to call up a Custom Workflow that will contain further actions for the condition “if Open Action is Buy”. You most probably will have more complex actions in that one transaction, e.g. Deduct funds from the account (you will probably keep records of the users funds), calculate limits of borrowing (maybe another thing), Send email or messaging to user, calculate/create profit and loss records (another thing).

If the actions that you want is more complex and contain iterations, you should consign it to Backend Workflows, hence relegating the task to the server backend.

1 Like

Yes! I think this is a great solution. Last question - do I achieve this custom workflow by using “trigger a custom event”?

Yes you are correct. You will need to create the Custom Workflow first before you can Trigger a custom event.

1 Like

Thank you, you’ve been a great help