Forum Academy Marketplace Showcase Pricing Features

Finding Consecutive Positive Trade Streak within Selected Dates


I’m working on a trading app and I’m trying to determine the record of consecutive positive or negative trades based on user-selected dates.

I have a list of trades where each trade can result in a positive or negative value. I’ve already implemented a provisional solution using counters, but this only works when the user enters trade data. However, it doesn’t help me when I want to check the streak within a specific date range.

I need to find the streak of consecutive positive trades (before encountering a negative trade) within the dates specified by the user, directly from the database.

I’ve searched extensively for a solution but haven’t found one. Any help or guidance would be greatly appreciated. Thank you!

I can’t think of a efficient solution to run on the browser without using javascript, but I guess a workaround would be to already start calculating and saving the streak number in each trade data(assuming it’s saved in Bubble’s database) so that you can look for the number when filtering.

Thank you very much for your response, Jordan. It has worked perfectly.

I created separate fields for positive and negative streaks. After creating a trade, if it is positive, I search for previous trades, and if the last one is positive, I increment the positive streak field by 1, if it is positive trade, I increment the negative streak field by 1, if it is negative trade. I used a state to temporarily store the value of the last streak.

I hope this explanation is clear in case anyone else wants to replicate it.

My current issue is generating the report. If the user selects specific dates, for example, the month of April, but there is an ongoing streak from the previous month, I am unable to differentiate them. However, I’m confident that this issue will be resolved.

Thanks again, Jordan.

You could actually go one step further and store the winning/losing trades as a list in each trade. you can then check the attributes of the ‘streak’ trades. However, it probably adds to the database clutter haha.

I’m still struggling to find the best way to calculate the positive streak or negative streak based on whether the NET P&L (Net Profit and Loss) result is positive or negative.

If the user enters a new trade with a positive NET P&L, it should be recorded as a “1” in the “Positive Streak” data field. Similarly, if the NET P&L is negative, it should be recorded as a “1” in the “Negative Streak” data field.

Now, how can I sum the "1"s until I encounter a “0” in the streak, but based on the list of trades selected by the user according to their dates or any other filter? I have a plugin that allows me to execute a workflow on each current cell, but I’m struggling to come up with the best approach to do it.

I think, you have to use javascript here. If you have developed a plugin, you can add an element to it that will calculate streaks from a set of trades. You don’t even need positive/negative streak fields. Add an element (let’s name it Trade Streak) that has no visual presentation, add field Trades to it of type Any thing with fields, with as list checkbox checked, add processing logic to Update function that will populate Positive Trade Sequence and Negative Trade Sequence states with lists of corresponding sequential trades. When done, place an element somewhere on the page and feed the field of the element with something like “RepeatingGroup’s list of Treades”. Note that you need to get technical names of properties of Trade object as they differ from what you see in bubble editor. You should address those properties using those techincal names. You can either look at your app export file or just print them to console during testing to understand which properties you should look at.

Regarding streak fields, you should be aware that bubble is prone to race conditions. You have to be sure that each trade entered is being saved to DB strictly after the previous trade has already been saved. If trades are generated frequently, you may encounter a situation when you miss some trades during negative/positive streak detection. Suppose you have sequence of trades of the same user:

  • T1P - trade 1, positive
  • T2N - trade 2, negative
  • T3P - trade 3, positive

If those trades will be created in a period of milliseconds, there is a chance, that T3P will be processed before T2N is saved. In this case T3P might be saved as a positive streak, while it’s not.

I’m sorry, I meant I’m currently using a plugin, but it’s not mine. I didn’t develop it.
I don’t have a plug-in to add an element to.
Is there another way?

Then you can add javascript function to your page (Page HTML Header section of page’s properties) or to the whole app (SEO/metatags section of the app Settings tab) in a script tag and invoke it using Toolbox plugin’s Javascript to bubble element.
Sorry, Javascript to bubble doesn’t accept arguments… Though there is a way to work this around, I’d still suggest creating a private plugin exclusively for your app.