Discover Refinitiv
MyRefinitiv Refinitiv Perspectives Careers
Created with Sketch.
All APIs Questions & Answers  Register |  Login
Ask a question
  • Questions
  • Tags
  • Badges
  • Unanswered
Search:
  • Home /
  • Datastream /
avatar image
Question by Aleniles · Sep 17, 2021 at 06:11 PM · datastream-apidsws-apidatastreambacktesting

Datastream Data For BackTrader

Hello, is there someone that coud help to feed Datastream data into Backtrader using the following example ?

from datetime import datetime

import backtrader as bt



# Create a subclass of Strategy to define the indicators and logic



class SmaCross(bt.Strategy):

    # list of parameters which are configurable for the strategy

    params = dict(

        pfast=10,  # period for the fast moving average

        pslow=30   # period for the slow moving average

    )



    def __init__(self):

        sma1 = bt.ind.SMA(period=self.p.pfast)  # fast moving average

        sma2 = bt.ind.SMA(period=self.p.pslow)  # slow moving average

        self.crossover = bt.ind.CrossOver(sma1, sma2)  # crossover signal



    def next(self):

        if not self.position:  # not in the market

            if self.crossover > 0:  # if fast crosses slow to the upside

                self.buy()  # enter long



        elif self.crossover < 0:  # in the market & cross to the downside



            self.close()  # close long position



# create a "Cerebro" engine instance

cerebro = bt.Cerebro()  



# Create a data feed

data = ds.get_data(tickers='@MSFT', fields=['PO','PH','PL','P','RI','VO'], start='-5Y', end='0D', freq='D')

cerebro.adddata(data)  # Add the data feed



cerebro.addstrategy(SmaCross)  # Add the trading strategy

cerebro.run()  # run it all

cerebro.plot()  # and plot it with a single command


The cerebro.adddata(data) # Add the data feed gives an error.

Any help would be highly appreciated !

People who like this

0 Show 0
Comment
10 |1500 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

3 Replies

  • Sort: 
avatar image
REFINITIV
Best Answer
Answer by Jirapongse · Sep 20, 2021 at 02:43 AM

@anilo

You need to use the bt.feeds.PandasData method instead.

The code looks like this.

datads = ds.get_data(tickers='@MSFT', fields=['PO','PH','PL','P','VO'], start='-5Y', end='0D', freq='D')
datads.columns = datads.columns.droplevel()
datads.index = pd.to_datetime(datads.index, format='%Y-%m-%d')
datads = datads.rename(columns={"PO": "Open", 
                       "PH":"High",
                       "PL":"Low",
                       "P":"Close",
                       "VO": "Volume"})


data = bt.feeds.PandasData(dataname=datads.dropna())

cerebro.adddata(data)


Comment

People who like this

0 Show 0 · Share
10 |1500 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

avatar image
Answer by Aleniles · Sep 20, 2021 at 09:18 AM

Great!


Many thanks.


However there is now another error related to the plot, since I have the

Javascript Error: IPython is not defined


https://newbedev.com/javascript-error-ipython-is-not-defined-in-jupyterlab


Or maybe is due to the fact that Pandas needs to have a specific column format ? See https://www.backtrader.com/docu/pandas-datafeed/pandas-datafeed/

Comment

People who like this

0 Show 1 · Share
10 |1500 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

avatar image
REFINITIV
Jirapongse ♦♦ · Sep 20, 2021 at 10:52 AM 0
Share

@anilo

I have followed the steps mentioned in the stackoverflow.

Then, I ran the following code.

%matplotlib widget
cerebro.plot(iplot=False)

The plot is shown properly.

avatar image
Answer by Aleniles · Sep 20, 2021 at 12:18 PM

Cool! Now it works!

Many thanks!

Comment

People who like this

0 Show 0 · Share
10 |1500 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Watch this question

Add to watch list
Add to your watch list to receive emailed updates for this question. Too many emails? Change your settings >
8 People are following this question.

Related Questions

Can I use Datastream API (DSWS) to collect historical constituent data?

Why does this DDL request fail validation?

Excluding holidays within ds.get_data call

In Datastream/Worldscope, does Item "Total Intangible Assets" (WC02649) include Total (accumulated) goodwill?

Is Datastream code (DSCD) a unique and permanent identifier?

  • Copyright
  • Cookie Policy
  • Privacy Statement
  • Terms of Use
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Alpha
  • App Studio
  • Block Chain
  • Bot Platform
  • Connected Risk APIs
  • DSS
  • Data Fusion
  • Data Model Discovery
  • Datastream
  • Eikon COM
  • Eikon Data APIs
  • Electronic Trading
    • Generic FIX
    • Local Bank Node API
    • Trading API
  • Elektron
    • EMA
    • ETA
    • WebSocket API
  • Intelligent Tagging
  • Legal One
  • Messenger Bot
  • Messenger Side by Side
  • ONESOURCE
    • Indirect Tax
  • Open Calais
  • Open PermID
    • Entity Search
  • Org ID
  • PAM
    • PAM - Logging
  • ProView
  • ProView Internal
  • Product Insight
  • Project Tracking
  • RDMS
  • Refinitiv Data Platform
    • Refinitiv Data Platform Libraries
  • Rose's Space
  • Screening
    • Qual-ID API
    • Screening Deployed
    • Screening Online
    • World-Check One
    • World-Check One Zero Footprint
  • Side by Side Integration API
  • TR Knowledge Graph
  • TREP APIs
    • CAT
    • DACS Station
    • Open DACS
    • RFA
    • UPA
  • TREP Infrastructure
  • TRKD
  • TRTH
  • Thomson One Smart
  • Transactions
    • REDI API
  • Velocity Analytics
  • Wealth Management Web Services
  • Workspace SDK
    • Element Framework
    • Grid
  • World-Check Data File
  • 中文论坛
  • Explore
  • Tags
  • Questions
  • Badges