How to set switch case instead of if...else if.. in future double method in flutter?
25/02/2023 by Manuka Kodithuwakku
In Flutter, you can use a switch statement to replace multiple if...else if... statements in a double method. Here's an example:
In the example above, we have a method that takes three arguments: the operation to perform (as a String), and two double values to operate on. Instead of using if...else if... statements to check the operation, we can use a switch statement.
The switch statement checks the operation variable and executes the corresponding case statement. If none of the case statements match the value of operation, it will execute the default case.
Note that if the default case is reached, we throw an ArgumentError to indicate that an invalid operation was provided.
You can call the method as follows:
How do I make a range/ sub array based on a condition, in order to compute returns before a threshold / stop loss? (in numpy or pandas)
26/02/2023, by manuka-kodithuwakku
To create a range/sub-array based on a condition in order to compute returns before a threshold/stop loss in NumPy or Pandas, you can use boolean indexing.
Here is an example code snippet to illustrate this:
In this example, we first create an array of stock prices. We then define a stop loss threshold of 30 and create a boolean array stop_loss_mask based on the condition stock_prices < stop_loss. We use this boolean array to create a sub-array of stock prices before the stop loss, stop_loss_prices. Finally, we compute the returns before the stop loss by taking the difference between consecutive elements in stop_loss_prices and dividing by the previous element, and store the result in stop_loss_returns.
Note that in this example we assume that the stop loss threshold is inclusive, meaning that the stock price at the threshold is included in the sub-array. If you want to exclude the threshold price from the sub-array, you can modify the boolean condition to stock_prices <= stop_loss instead.