Passing definition parameters - strange results?

tw391819tw391819 Posts: 27
edited July 2017 in Support

Hi,
I am learning Python and CQ API at the same time, and have run across something I don't understand...

Below is the simple code to show the problem.

The function "mean2", which ignores the unexpected first parameter, will work; but I don't understand where the first parameter is coming from, and will this always be the case, so I can always disregard that first parameter?

Thank you for any assistance!
-Tom

Code

TW Functions

# TW - Calculate mean of list 
def mean(numbers):  #Intended code
    return sum(float(i) for i in numbers)/len(numbers)

def mean2(numbers,other): # Created for debugging
    print 'numbers: ' + str(numbers) +'\n'
    print 'other: ' + str(other)+'\n'
    return sum(float(i) for i in other)/len(other)

TW Strategy

# called at the beginning of each instance      
def on_start(self, md, order, service, account):
    print('On Start: Current Symbols:\t' + self.symbol + '\n') # Breadcrumb to see where we are

    self.bar_data_5d = md.bar.daily(start=-5) # Get 5 days of data
    print 'count of highs: ' + str(len(self.bar_data_5d.high)) # Verify how many are returned

# Brute force calculation
    average_highs = (self.bar_data_5d.high[0]+self.bar_data_5d.high[1]+self.bar_data_5d.high[2]+self.bar_data_5d.high[3]+self.bar_data_5d.high[4])/5
    print 'average_highs1: '+ str(average_highs)

# More elegant calculation for generalized use
    average_highs2 = sum(float(i) for i in self.bar_data_5d.high)/len(self.bar_data_5d.high)
    print 'average_highs2: '+ str(average_highs2)

# Now lets use a function 
    print 'mean_Func:' + str(self.mean2(self.bar_data_5d.high))  
   # Get Strange error that 2 parameters were passed to function !!??!!
# Created debug version of funciton - it created output, below
########### Console Output

On Strategy_Start: ...

On Start: Current Symbols: SPY

count of highs: 5
average_highs1: 242.682000732
average_highs2: 242.682000732
numbers: ' CQ80b5f3a805994ab3b218ce8771378606 object at 0x7f1c090a99d0' <-- Where does this come from (originally enclosed in angle brackets)?

other: [ 242.71000671 243.38000488 243.00999451 242.02999878 242.27999878] <-- this is all I expected to be passed!

mean_Func:242.682000732
On Finish: Current Symbols: SPY

On Strategy_Finish: ...

Comments

  • aj165602aj165602 Posts: 105

    Function mean2 does not ignore the first parameter, in the sense that it casts that parameter to a string and then prints it.

  • tw391819tw391819 Posts: 27

    Thank you, AJ,
    I added the first parameter and printed it in order to get visibility into the issue I was having.

    In calling the original function, self.mean(self.bar_data_5d.high), I intended to send it 1 parameter -- the list of self.bar_data_5d.high. However there was an unexpected parameter included, and that caused an error message indicating that 2 parameters were sent to the function when only one was expected. So I added a 2nd parameter and printed them both out to see what they were (in mean2).

    To make it work, I have the function accept 2 parameters, I ignore the first which I don't care about and process the second as intended. It is working fine now, but I am curious about where the unexpected first parameter is coming from.

    Thank you!
    -Tom

  • aj165602aj165602 Posts: 105
    edited July 2017

    Hi Tom,

    Try this simpler way of doing it:

    def mean2(d):
    return sum(d[0:]) / len(d)

    print str(mean2(d))

    You shouldn't need to use a loop.

  • aj165602aj165602 Posts: 105

    ...or

    self.bar_data_5d.mean()

  • tw391819tw391819 Posts: 27

    Hi AJ... Cool, thanks! I'll give those a try.

  • tw391819tw391819 Posts: 27
    edited July 2017

    I found the resolution to this problem -- newbie user error!
    I learned that when defining a method in python, you have to include self as the first parameter, like this:

    def mean(self, numbers):
    return sum(float(i) for i in numbers)/len(numbers)

  • aj165602aj165602 Posts: 105
    edited July 2017

    Hi Tom,

    You'll also notice that static methods have cls rather than self in their signatures.

    You may find it useful to define global parameters, in which case you can then access them using:
    self.__class__.

    Hope that's helpful. It took me a while to work it out!

    Best,
    Antony

  • tw391819tw391819 Posts: 27

    Thank you, Antony!

Sign In or Register to comment.