简体   繁体   中英

How to call a Function in a python

There is a function def on_ticks(ws,ticks) and what this function does is gets the ticks value of the stocks from market:

def on_ticks(ws,ticks):
    for tick in ticks:
    data = tick  # all the incoming tick data stored in variable data.

Now i need to create some strategy and i define a new function for the strategy def calculate_orb()

def calculate_orb():

Since in this function i need the tick data from the function on_ticks , so i need to know how to call on_ticks function in the calculate_orb() function so that i can make my calculation according to the tick data received from market

so the complete code would look like below:

def on_ticks(ws,ticks):
    for tick in ticks:
    data = tick 

def calculate_orb():
    on_ticks()

Try consuming the return values of your on_ticks() function into your calculate_orb funtion:

def on_ticks(ws,ticks):
    for tick in ticks:
        data = tick 

def calculate_orb():
    for data in on_ticks(value_for_ws, value_for_ticks):
        # TODO

First of all, that is a syntax error here, because there is nothing in the for loop:

def on_ticks(ws,ticks):
    for tick in ticks:
    data = tick 

Then, if you fix it by moving the data = tick line into the loop, there is a problem that data is overwritten in each step of the loop, so only the last one remains:

def on_ticks(ws,ticks):
    for tick in ticks:
        data = tick # overwrites the previous value in data

Then there is the main problem that the data is not really stored anywhere, except in variable data , which is in the local scope of the function. Since it is not used at all before the end of the function, the variable just disappears along with the data as soon as the function ends, so it's useless.

To actually use the data outsie of the function, one would have to return it:

def on_ticks(ws,ticks):
    for tick in ticks:
        data = tick # overwrites the previous value in data
    return data

Now, if ou actually want all of the data, and not just the last one, you need to collect it in a list and retunr that:

def on_ticks(ws,ticks):
    list_of_data = []
    for tick in ticks:
        list_of_data.append(tick)
    return list_of_data

Then you can use it as:

def calculate_orb():
    list_of_data = on_ticks(ws, ticks) # note that you need to pass the arguments

This is still useless, because the returned list_of_data actually contains the same data as the ticks , which was passed to the function, but I guess I explained what was wrong with the original code ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM