Alex Leontaridis
Alex Leontaridis

Reputation: 41

Simplifying this complex logical expression

Do you know how I can simplify this logical expression? I am trying to fix it for hours, but I can't.

if (trade_dur == 0 and (
    (
        is_short
        # Red candle (for longs)
        and row[OPEN_IDX] < row[CLOSE_IDX]  # Red candle
        and trade.open_rate > row[OPEN_IDX]  # trade-open above open_rate
        and close_rate < row[CLOSE_IDX]  # closes below close
    )
    or
    (
        not is_short
        # green candle (for shorts)
        and row[OPEN_IDX] > row[CLOSE_IDX]  # green candle
        and trade.open_rate < row[OPEN_IDX]  # trade-open below open_rate
        and close_rate > row[CLOSE_IDX]  # closes above close
     )
)):

Upvotes: 1

Views: 306

Answers (1)

Ni2Be
Ni2Be

Reputation: 131

If you are in a function, you could use an early return like:

if(trade_dur != 0):
  return

You could then define some bools with appropriate names to make it more readable:

  is_red = row[OPEN_IDX] < row[CLOSE_IDX]
  trade_open_above = trade.open_rate > row[OPEN_IDX]
  closes_below = trade.open_rate > row[OPEN_IDX]
    :
  is_tradable = (is_short and is_red and trade_open_above and closes_below)
             or (not is_short and is_green and trade_open_below and closes_above )

  if(is_tradable) : ...

Upvotes: 1

Related Questions