0

I've got a function (get_score) that uses variables from other functions(self.__sma) in the same class.

My question: How can I access these variables from the get_score function? (error: global name is not defined).

Also, i heard that using global variables causes a shitstorm(just based from what I read on here). Thanks in advance.

Here is some code:

testlist = ['TREE', 'HAPPY']
Class MyStrategy(strategy.BacktestingStrategy):
    def __init__(self, feed, instrument):
        super(MyStrategy, self).__init__(feed, 1000)
        self.__position = [] #None
        self.__instrument = instrument
        self.setUseAdjustedValues(True)
        self.__prices = feed[instrument].getPriceDataSeries()
        self.__sma = ma.SMA(feed[instrument].getPriceDataSeries(), smaPeriod)

    def get_score(self,banana)
        fruit_lover= self.__sma
        return fruit_lover + banana

    def onBars(self, bars):
        for x in bars.getInstruments():

            list_large = {}
            for y in testlist: 
                list_large.update({y : self.get_score(5)})  
9
  • 1
    I've seen quite a few questions like this, search before asking Commented Nov 26, 2016 at 3:12
  • I did, I am, actually having a hard time with this...a link would be great...found a few pages but its still not working. Commented Nov 26, 2016 at 3:19
  • A function called get_score should get / return a score, not set some variables. Along with that point, {y : self.get_score(5)} is setting y to None Commented Nov 26, 2016 at 3:19
  • sorry, I didint accurately write the code, just edited it. The get_score function does actually return something and "y" is a variable in the testlist list(at the top of the page). Commented Nov 26, 2016 at 3:24
  • Is it possible its the 2 underscores in "self.__sma" that are making it private and therefore inaccessible?? Commented Nov 26, 2016 at 3:51

1 Answer 1

1

With a copy of your script, and a bunch of stubs, and too many syntax corrections:

testlist = ['TREE', 'HAPPY']
class MyStrategy(): #strategy.BacktestingStrategy):  # lowercase class
    def __init__(self, feed, instrument):
        #super(MyStrategy, self).__init__(feed, 1000)
        self.__position = [] #None
        self.__instrument = instrument
        #self.setUseAdjustedValues(True)
        self.__prices = 123 #feed[instrument].getPriceDataSeries()
        self.__sma = 100 # ma.SMA(feed[instrument].getPriceDataSeries(), smaPeriod)

    def get_score(self,banana):   # add :
        fruit_lover= self.__sma
        return fruit_lover + banana

    def onBars(self, bars):
        #for x in bars.getInstruments():
        list_large = {}
        for y in testlist: 
            list_large.update({y : self.get_score(5)})  
        self.list_large = list_large  # save list_large 
s = MyStrategy('feed', 'inst')
print(vars(s))           # look at attributes set in __init__
print(s.get_score(10))   # test get_score by itself
s.onBars('bars')         # test onBars
print(s.list_large)

I get the following run:

0009:~/mypy$ python stack40814540.py 
{'_MyStrategy__position': [], 
 '_MyStrategy__instrument': 'inst', 
 '_MyStrategy__sma': 100, 
 '_MyStrategy__prices': 123}
110
{'TREE': 105, 'HAPPY': 105}

I have no problem accessing __sma attribute in the other methods.

Because the name starts with __ (and not ending __), it is coded as a 'pseudo-private' variable, as can be seen in the vars display. But that doesn't harm access from another method.

For a beginner I'd suggest skipping that feature, and just use names like self.sma. Code will be easier to debug.

Make sure you understand my changes; and run some similar tests.

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

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.