Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9149173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:26:28+00:00 2026-06-17T11:26:28+00:00

I am researching/backtesting a trading system. I have a Pandas dataframe containing OHLC data

  • 0

I am researching/backtesting a trading system.

I have a Pandas dataframe containing OHLC data and have added several calculated columns which identify price patterns that I will use as signals to initiate positions.

I would now like to add a further column that will keep track of the current net position. I have tried using df.apply(), but passing the dataframe itself as the argument instead of the row object, as with the latter I seem to be unable to look back at previous rows to determine whether they resulted in any price patterns:

open_campaigns = []
Campaign = namedtuple('Campaign', 'open position stop')

def calc_position(df):
  # sum of current positions + any new positions

  if entered_long(df):
    open_campaigns.add(
        Campaign(
            calc_long_open(df.High.shift(1)), 
            calc_position_size(df), 
            calc_long_isl(df)
        )
    )

  return sum(campaign.position for campaign in open_campaigns)

def entered_long(df):
  return buy_pattern(df) & (df.High > df.High.shift(1))

df["Position"] = df.apply(lambda row: calc_position(df), axis=1)

However, this returns the following error:

ValueError: ('The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()', u'occurred at index 1997-07-16 08:00:00')

Rolling window functions would seem to be the natural fit, but as I understand it, they only act on a single time series or column, so wouldn’t work either as I need to access the values of multiple columns at multiple timepoints.

How should I in fact be doing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:26:29+00:00Added an answer on June 17, 2026 at 11:26 am

    This problem has its roots in NumPy.

    def entered_long(df):
      return buy_pattern(df) & (df.High > df.High.shift(1))
    

    entered_long is returning an array-like object. NumPy refuses to guess if an array is True or False:

    In [48]: x = np.array([ True,  True,  True], dtype=bool)
    
    In [49]: bool(x)
    
    ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
    

    To fix this, use any or all to specify what you mean for an array to be True:

    def calc_position(df):
      # sum of current positions + any new positions
    
      if entered_long(df).any():  # or .all()
    

    The any() method will return True if any of the items in entered_long(df) are True.
    The all() method will return True if all the items in entered_long(df) are True.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am researching filling out a form programatically. I have seen this question which
In researching how to convert a NameValueCollection to a querystring, I have come across
I'm researching options to synchronize external data to a Category and Keyword list. I
I have been researching for over an hour and finding great samples of how
I have been researching all over the internet about this, and unfortunately cannot find
While researching how to create custom compound views in Android, I have come across
I have been researching the possibility of scheduling an automatic back up of a
I've been researching this problem all day long and no success. I have a
Been researching how to send data from one child view to another child view.
In researching forcing files to be written as quickly as possible, I have seen

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.