Stop function if account gets too large

Hi all,
So I'm doing the famous (or infamous) Labouchere system simulations in R. I have a function that stops when either the cycle (i.e., 10 - 10 - 10 - 10) is completed OR when you run out of money in you bank(mval). The function works and stops when a cycle is complete but the second part (&& bet < mval) is not working how I wanted it to. It stops the function when a given bet (first number + last number) exceeds the mval. I want this instead to stop when the total amount lost reaches the mval

For example:
When running labouchere(mval = 300) I want 300 to represent the total amount in the bank willing to lose, but this only stops when a specific bet reaches 300. I'm trying to make it stop when the accumulative losses reaches 300.

Created on 2020-07-21 by the reprex package (v0.3.0)

labouchere <- function(mval) {
    numbers <- c(10, 10, 10, 10, 10)
    earning <- 0
    bet <- numbers[1] + numbers[length(numbers)]
    counter <- 0
    while (length(numbers) >= 1 && bet < mval) {
        result <- sample(c(-bet, bet), 1, replace = T, prob = c(0.5, 
            0.5))
        if (result > 0) {
            earning <- earning + result
            numbers <- numbers[-c(1, length(numbers))]
            if (length(numbers) == 1) {
                bet <- numbers
            }
            else {
                bet <- numbers[1] + numbers[length(numbers)]
            }
        }
        else {
            earning <- earning + result
            numbers <- c(numbers, abs(result))
            bet <- numbers[1] + numbers[length(numbers)]
        }
        counter <- counter + 1
    }
    c(earning, counter)
}

Created on 2020-07-21 by the reprex package (v0.3.0)

Pleaseee any advice??

Thank you!


labouchere <- function(mval) {
  numbers <- c(10, 10, 10, 10, 10)
  earning <- 0
  bet <- numbers[1] + numbers[length(numbers)]
  counter <- 0
  cbet<-0
  while (length(numbers) >= 1 && cbet <= mval) {
    result <- sample(c(-bet, bet), 1, replace = T, prob = c(0.5, 
                                                            0.5))
    if (result > 0) {
      earning <- earning + result
      numbers <- numbers[-c(1, length(numbers))]
      if (length(numbers) == 1) {
        bet <- numbers
      }
      else {
        bet <- numbers[1] + numbers[length(numbers)]
      }
    }
    else {
      earning <- earning + result
      numbers <- c(numbers, abs(result))
      bet <- numbers[1] + numbers[length(numbers)]
    }
    counter <- counter + 1
    cbet <- cbet+bet
  }
  c(earning, counter)
}

Thank you so much for responding!! I just want to confirm what the new variable, cbet, is actually doing. Is it adding every single bet placed regardless of the outcome and then stops when it reaches the maximal amount we give (mval)? Or is it adjusting the amount left in the "bank" based on wins/loses and stopping when cbet reaches that maximal amount we give.

It appears that it is adding every single bet placed regardless of the outcome and stops when either the cycle ends or when it reaches the maximal amount.

Is there a way that it can monitor the total amount lost and stop at that value? For example:

labouchere(mval = 300) # Runs the function with 300dollars in the bank

First bet = $20 (10+10) and is a loss 280 left
Next bet = $30 (10 + 20) and is a loss 250 left
Next bet = $40 (10 + 30) and is a win 290 left
......... Continues until either the original cycle (10-10-10-10-10) is completed OR there is 0 money left.

I hope I explained that correctly and that it makes sense.
Thank you again!

Yes it does that was it, it stops once the negative value reaches or exceeds the mval that I indicate.

nirgrahamuk - thank you very much for the help my friend! All my best

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

would that be the same as earning going negative by the amount that the budget is, if so cbet can be eliminated and earning can be used more directly

Yes! Now that I think of it you're right.
So would I then use ... while (length(numbers) >= 1 && earning <= mval) { ??

i think that while mval + earning > 0 , then you are good to loop.
when mval - earning <= 0 you are out of budget

1 Like