anfederico / clairvoyant

Software designed to identify and monitor social/historical cues for short term stock movement
MIT License
2.41k stars 771 forks source link

NameError: name 'backtester' is not defined #28

Closed arambula1980 closed 5 years ago

arambula1980 commented 6 years ago

Hi Federico,

I am new to Python, and I am trying the second example , Simulate a Trading Strategy , and I am getting the following Error:

Traceback (most recent call last): File "stock6.py", line 60, in simulation = backtester.Simulation(features, trainStart, trainEnd, testStart, testEnd, buyThreshold, sellThreshold, continuedTraining) NameError: name 'backtester' is not defined

If I try to edit it and use backtest.simulation I get the same error NameError: name 'backtest' is not defined

Is there a workaround for this ?

jordan-green-zz commented 6 years ago

Hi arambula,

I assume that you've copied the second code snippet from the readme page into 'stock6.py' NameError: name 'backtest' is not defined is telling you that you have not created an object called backtest. This object is defined just after the imports and parameters are set in the first code snippet in the readme page. Try copying that snippet in before your code in stock6.py and then delete whatever you don't want/need.

I hope that helps

arambula1980 commented 6 years ago

Hi Jordan,

Thanks for your quick reply.

Yes, Here is my code up to this point :

from clairvoyant.engine import Backtest
import pandas as pd

features  = ["x1", "x2", "x3"]   # Financial indicators of choice (
trainStart = 0               # Start of training period
trainEnd   = 551            # End of training period
testStart  = 552            # Start of testing period
testEnd    = 723            # End of testing period
buyThreshold  = 0.40        # Confidence threshold for predicting buy (default = 0.65) 
sellThreshold = 0.40       # Confidence threshold for predicting sell (default = 0.65)
continuedTraining = False    # Continue training during testing period? (default = false)

# Initialize backtester
backtest = Backtest(features, trainStart, trainEnd, testStart, testEnd, buyThreshold, sellThreshold, 
continuedTraining)

# A little bit of pre-processing
df = pd.read_csv("stock1.MX.csv", date_parser=['date'])
df = df.fillna(0)
df = df.round(3)

def logic(account, today, prediction, confidence):

    if prediction == 1:
        Risk         = 0.30
        EntryPrice   = today['close']
        EntryCapital = account.BuyingPower*Risk
        if EntryCapital >= 0:
            account.EnterPosition('Long', EntryCapital, EntryPrice)

    if prediction == -1:
        ExitPrice = today['close']
        for Position in account.Positions:  
            if Position.Type == 'Long':
                account.ClosePosition(Position, 1.0, ExitPrice)

simulation = backtest.Simulation(features, trainStart, trainEnd, testStart, testEnd, buyThreshold, 
sellThreshold, continuedTraining)
simulation.start(data, 1000, logic, kernel='rbf', C=1, gamma=10)
simulation.statistics()
simulation.chart('stock1')`

Not sure why I am getting the error, since the first example runs fine. And with the second example, backtest is there as well.

Any ideas ?

Thanks.

jordan-green-zz commented 6 years ago

Hi,

Your code runs fine beyond that point on my system - I do get an error that Simulation is not defined but that is due to the back you have referenced it as backtest.Simulation and I believe Simulation is actually a class within clairvoyant.engine. ("I've actually not used this software myself beyond getting it running - I'm just starting to play with it a little so I might not be right there).

You should be able to import it like so: from clairvoyant.engine import Backtest, Simulation

Your code runs into an index problem then, which may be your code or my data (I threw a random datafile in), but I don't have time to look through that I'm afraid.

As for your import problem - do you have more than one version of python on your machine? IE do you have both python 2.7 and python3 (this is common) as they will have different install locations for packages.

Check which python you're using by typing which python (on unix) and ensure it's the same one you used when you ran the first script. Or, you can try reinstalling the package: pip install --upgrade --force-reinstall clairvoyant

I hope this helps, and good luck learning python!

mattpetersen commented 5 years ago

Glad it seems to be working :) Can we close this issue as it's not actually a bug?

arambula1980 commented 5 years ago

Yes, lets close it ... Thanks !!

santosh1383 commented 3 years ago

What did you...i am still getting error : NameError: name 'backtester' is not defined

santosh1383 commented 3 years ago

or another error: AttributeError: 'Backtest' object has no attribute 'Simulation'