Skip to content

How use pandas dataframe own inside strategy. #585

@mablue

Description

@mablue

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()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions