0

on a dataframe that contains the bitcoin price, I want to measure the strength of buyers and sellers.

For the uninitiated in trading, I use a price representation called 'Japanese candle' which takes into account the opening, closing, highest and lowest price for each line: The shadows are the ends of the candlesticks located between the opening or closing and the top of the candle (for high shadow) and the bottom of the candle (for low shadow).
example: https://en.wikipedia.org/wiki/Candlestick_chart

I want to add to the original dataframe, two new columns that will return for each row :

  • the size of the low shadow IF the price has increased (bullish candle)
  • the size of the high shadow IF the price has gone down (bearish candle)

My basic idea: 1.Create a new object/dataframe 'df_shadow', which contains a function to calculate the size of the shadow, depending on the direction of the prices. 2. Concatenate this 'df_shadow' with the original df that contains the price (Open, Close, High, Low) of the bitcoin to get the 2 new columns.

Unfortunately this doesn't work, certainly because I'm not very experienced in programming yet.

I thought of creating a function:

def low_shadow(x):
  for price in x:
    if x['Close'] > x['Open']:
      return(x['Open']-x['Low'])

then just a for loop:

for price in df['Close']:
  if df['Close']>df['Open']:
    print(df['Open']-df['Low']),
  else:
    print(df['Open']-df['Low'])

then just conditions:

df['low_shadow'] = [if df['Close'] > df['Open']:
                print(df['Open']-df['Low'])]

...but I didn't succeed. In the first 2 cases I get 'ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().' and in the last case 'SyntaxError: invalid syntax' from the for. How to create these 2 columns?

3 Answers 3

2

You are quite close. You can do this in a variety of ways in pandas, but one way would be:

 df['low_shadow'] = df.apply(lambda row: row['Open'] - row['Low']
                             if row['Close'] > row['Open'] 
                             else None, 
                             axis=1)

So what's happening here: df.apply (with second argument axis=1) takes a function and applies it to each row of a data frame, returning a Series that is the result of that function. This Series is then assigned to be a new column in your data frame.

Sign up to request clarification or add additional context in comments.

1 Comment

Votre fonction fonctionne également. Merci
1

If I fully understand your question, I think you can use lambda function.

df['low_shadow'] = df.apply(lambda x: x['Open']-x['Low'] if x['Close'] > x['Open'] else 'enter your value', axis=1)

Please, provide example of data and desired result if this answer doesn't help you.

1 Comment

Your function works perfectly and returns the desired result. Thank you very much.
0

Assuming 'df' is your price dataset with columns 'Open','Low','High','Close', try the following code to calculate low shadow:

def calc_low_shadow(x):
  '''
Function to find low shadow of the candle stick
  '''
  open,close,low = x[0],x[1],x[2]
  if close > open:
    return open-low
  else:
    return close - low
  
df['low_shadow'] = df[['Open','Close','Low']].apply(calc_low_shadow, axis=1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.