All problems
Stop-Loss Simulation Return
Problem
Simulate a long position with a stop loss: enter at prices[0] and exit at the first price at or below entry * (1 - stop_loss), or at the final price if the stop never triggers. Return exit / entry - 1.
Return 0.0 for empty prices, a non-positive stop loss, or a zero entry price.
Hint
The stop triggers on the first bar whose price reaches the threshold.
Test cases
case 1
in: [[100,95,90,80],0.1]
expected: -0.09999999999999998
case 2
in: [[100,95,105,110],0.1]
expected: 0.10000000000000009
case 3
in: [[100,90,80],0.05]
expected: -0.09999999999999998
case 4
in: [[10,10,10],0.2]
expected: 0
Starter codePython
def stop_loss_simulation_return(prices, stop_loss):
# Your code here
pass4 test cases · run your code against them in the editor