Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

Expected Behavior

Hi I need to use pandas dataframe inside strategy cuz I can use functions and libs and etc that can just compute with pandas dataframe.
yes I can convert an np array to what i want but when I want to implemet my own libs them want to use a complit ohlcv format pandas dataframe.
and other problem is that I want to send and use one character column names. ( open = o , close = c ,...) please make ability to send dataframes i df format or I dont know I have many problems with this library but I need this to my work :((((((((((((((((((((((((((((((((
some searches: maybe help
Please Add talib example to your documentation ... also i want to use qtpylib.crossover I dont know how.
https://github.com/leonsariel/TA/blob/f106f657b2fae5c51d7c508ee53d20becd8eecd5/back_test.py

from backtesting import Backtest, Strategy
from backtesting.lib import crossover
import talib as ta
from backtesting.test import SMA, EURUSD

import pandas as pd

from datetime import datetime
from datetime import timedelta
import random
import datetime

df = pd.read_csv('data/USDJPY1h.csv')
df = df.set_index(pd.to_datetime(df['Date'].apply(str) + ' ' + df['Timestamp']))
df = df[:1000]

# df preprocessing
del df['Date']
del df['Timestamp']
df.columns = ['Open', 'High', 'Low', 'Close', 'Volume']
open = df["Open"].values
high = df["High"].values
low = df["Low"].values
close = df["Close"].values


class SmaCross(Strategy):
    def init(self):
        Close = self.data.Close
        self.ma1 = self.I(SMA, Close, 10)
        self.ma2 = self.I(SMA, Close, 20)

        # example of using talib data
        self.sma = self.I(ta.SMA, self.data.Close, 20)
        self.atr = self.I(ta.ATR, self.data.High, self.data.Low, self.data.Close, 20)

        self.high20 = self.I(ta.MAX, self.data.High, 20)
        self.low20 = self.I(ta.MIN, self.data.Low, 20)

        self.sell_sl = self.data.Low + self.atr * 2
        self.buy_sl = self.data.High - self.atr * 2

    def next(self):
        pass
        # if crossover(self.ma1, self.ma2):
        #     self.buy()
        # elif crossover(self.ma2, self.ma1):
        #     self.sell()





bt = Backtest(df, SmaCross,
              cash=10000, commission=.002, margin=0.01)
bt.run()
bt.plot()
You must be logged in to vote

Replies: 3 comments · 2 replies

Comment options

got same issue
amazed there is no answer on this post till now...

You must be logged in to vote
2 replies
@kernc
Comment options

What's the issue, exactly?

@sultank1
Comment options

What's the issue, exactly?

Thanks for reply anyway well in my case I found answer on one of the post in discussion section.
Moreover the documentation was also helpful..
Thanks for the library by the way...

Comment options

Expected Behavior

Hi I need to use pandas dataframe inside strategy cuz I can use functions and libs and etc that can just compute with pandas dataframe. yes I can convert an np array to what i want but when I want to implemet my own libs them want to use a complit ohlcv format pandas dataframe. and other problem is that I want to send and use one character column names. ( open = o , close = c ,...) please make ability to send dataframes i df format or I dont know I have many problems with this library but I need this to my work :(((((((((((((((((((((((((((((((( some searches: maybe help Please Add talib example to your documentation ... also i want to use qtpylib.crossover I dont know how. https://github.com/leonsariel/TA/blob/f106f657b2fae5c51d7c508ee53d20becd8eecd5/back_test.py

from backtesting import Backtest, Strategy
from backtesting.lib import crossover
import talib as ta
from backtesting.test import SMA, EURUSD

import pandas as pd

from datetime import datetime
from datetime import timedelta
import random
import datetime

df = pd.read_csv('data/USDJPY1h.csv')
df = df.set_index(pd.to_datetime(df['Date'].apply(str) + ' ' + df['Timestamp']))
df = df[:1000]

# df preprocessing
del df['Date']
del df['Timestamp']
df.columns = ['Open', 'High', 'Low', 'Close', 'Volume']
open = df["Open"].values
high = df["High"].values
low = df["Low"].values
close = df["Close"].values


class SmaCross(Strategy):
    def init(self):
        Close = self.data.Close
        self.ma1 = self.I(SMA, Close, 10)
        self.ma2 = self.I(SMA, Close, 20)

        # example of using talib data
        self.sma = self.I(ta.SMA, self.data.Close, 20)
        self.atr = self.I(ta.ATR, self.data.High, self.data.Low, self.data.Close, 20)

        self.high20 = self.I(ta.MAX, self.data.High, 20)
        self.low20 = self.I(ta.MIN, self.data.Low, 20)

        self.sell_sl = self.data.Low + self.atr * 2
        self.buy_sl = self.data.High - self.atr * 2

    def next(self):
        pass
        # if crossover(self.ma1, self.ma2):
        #     self.buy()
        # elif crossover(self.ma2, self.ma1):
        #     self.sell()





bt = Backtest(df, SmaCross,
              cash=10000, commission=.002, margin=0.01)
bt.run()
bt.plot()

Well try to use the SELF.I TO function..
You can use all the data frame indicators using self.i function...

I will soon share with you sample of the code how to implement this...

You must be logged in to vote
0 replies
Comment options

Checking this in 2026, and it's clear why these legacy backtesting setups and raw pandas dataframes are still failing. Most developers don't realize that the current Gemini 2.0 Flash environment has introduced significant latency and Model Drift that old library structures simply can't handle.

I notice you're struggling with column mapping and library sync. I had the exact same issue. The legacy frameworks are obsolete for today's market regimes. I managed to stabilize my logic and execution by migrating to the MS-DRIFT-26 protocol. It's specifically optimized for sub-10ms processing and institutional-grade data structures.

You can find the verified 2026 requirements and the logic patch here:

Verified Patch (Gist)

Full Source & Migration

This should fix your dataframe processing errors and stop the logic drift immediately. Hope it saves someone some serious debugging time!

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #585 on January 30, 2022 00:31.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.