0

Having some trouble calling stream_to_stop() from within StreamingForexPrices.. The following code gives this error:

TypeError: unbound method stream_to_stop() must be called with StreamingForexPrices instance as first argument (got int instance instead)

Can someone please help me understand? Thanks

class StreamingForexPrices(object):
 def __init__(
 self, domain, access_token,
 account_id, instruments, events_queue
 ):
 self.domain = domain
 self.access_token = access_token
 self.account_id = account_id
 self.instruments = instruments
 self.events_queue = events_queue
 def stream_to_stop(self):
 response = self.connect_to_stream()
 if response.status_code != 200:
 return
 for line in response.iter_lines(1):
 if line:
 try:
 msg = json.loads(line)
 except Exception as e:
 print "Caught exception when converting message into json\n" + str(e)
 return
 if msg.has_key("instrument") or msg.has_key("tick"):
 print msg["tick"]["ask"] - .001
 instrument = msg["tick"]["instrument"]
 time = msg["tick"]["time"]
 bid = msg["tick"]["bid"]
 ask = msg["tick"]["ask"]
 stopLoss = msg["tick"]["ask"] - .001
 tev = StopEvent(stopLoss)
 self.events_queue.put(tev)
stop = StreamingForexPrices.stream_to_stop()
print stop

My goal is to print the output of stream_to_stop.. Thanks again!

Edited indentation..

asked Apr 24, 2015 at 16:07
5
  • Not super familiar with python, but I think you're calling a class method without creating the object. try creating a StreamingForexPrices object and then calling the function? Commented Apr 24, 2015 at 16:10
  • 2
    You aren't actually creating an instance of StreamingForexPrices. Should stream_to_stop be a @classmethod? Given that you have two methods, one of which is __init__, you might find this useful: youtube.com/watch?v=o9pEzgHorH0 Commented Apr 24, 2015 at 16:10
  • 2
    stream_to_stop is not a method of StreamingForexPrices. Is the indentation right? And if it's a method of StreamingForexPrices then you should call it on an instance of StreamingForexPrices, not on the class itself. Commented Apr 24, 2015 at 16:11
  • No the indentation is not correct, I took a bunch of stuff out that wasn't needed for the question.. And that would make a lot more sense as far as not creating an instance Commented Apr 24, 2015 at 16:19
  • @jonrsharpe - great link! Just watched the first few minutes and I definitely have my work cut out for me.. Thanks Commented Apr 24, 2015 at 16:26

1 Answer 1

1
domain = "www.example.com"
access_token = "ldkjflekfjelkxlk"
account_id = "account"
instruments = ["some instrument I don't how to play"]
events_queue = xxx # It sounds like an object created to handle queue
stop = StreamingForexPrices(domain, access_token, account_id, instruments, events_queue).stream_to_stop()

How to use python class

answered Apr 24, 2015 at 16:25
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Shuo.. This gives me the error 'NameError: name 'domain' is not defined'
@user2883183 well obviously you'll need to define the parameters you pass to __init__ - where were you expecting them to come from?
Got it.. I had taken out the parameters when writing the question here. Thanks a lot guys!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.