For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

Simple Sample code to Backtest an OCO model

  • Hi All,

    Beginner here. This is probably a straightforward task For most of you here.

    I have a pandas data frame which is a time series for currency prices with an extra column indicating when the OCO order should be placed. The extra column is binary e.g 1 means execute OCO order and 0 means do nothing.

    Would anyone be willing to just write some sample code showing how I could create a strategy in backtrader where the OCO order is executed or not based on the column value (1 or 0) in my pandas data frame?

    And then how to plot this??

    Again any help would be much appreciated!!!


  • @J-Amin

    Could someone please kindly respond?

  • Everything is written already, you just need to put it together -

    Docs - Pandas data feed
    Docs - Extending data feed
    Docs - Orders - OCO
    Docs - Plotting

    An the best part for beginner Docs - Quickstart Guide

  • @ab_trader

    Hi, followed the docs and this is the code I’ve got so far:

    from future import (absolute_import, division, print_function,unicode_literals)

    import pandas as pd

    import tkinter

    import matplotlib

    from datetime import date

    from datetime import datetime

    from datetime import timedelta

    import matplotlib.pyplot as plt

    from matplotlib.dates import DateFormatter

    import matplotlib.dates as mdates

    from matplotlib.backends.backend_pdf import PdfPages

    import os

    import sys

    import platform

    import time

    import datetime as dt

    import math

    import numpy as np

    import backtrader as bt

    import argparse

    import backtrader.feeds as btfeeds

    class PandasData(btfeeds.DataBase):

    params = (('datetime',None), ('Open',-1), ('High',-1), ('Low',-1), ('Close',-1), ('Signal',-1) )

    I believe above I’ve correctly fed the pandas dataframe into backtrader but correct me if wrong

    class OCOStrategy(bt.OCOStrategy):

    def notify_order(self, order):
        print('{}: Order ref: {} / Type {} / Status {}'.format(,order.ref, 'Buy' * order.isbuy() or 'Sell',order.getstatusname()))
        if order.status == order.Completed:
            self.holdstart = len(self)
        if not order.alive() and order.ref in self.orefs:
    def __init__(self):
    def next(self):
            return  # pending orders do nothing

    What exactly is wrong with the syntax underneath “def next(self)??”

    At this point I’ve figured out how to run strategies in general, plot etc only thing remaining is to derive this OCO strategy.

    Could you please advise??

    Thanks again!!!

  • @J-Amin

    Under “def init(self):”

    I meant to put[‘Signal’]

  • @J-Amin

    Any response please??

  • @J-Amin

    Any administrators who could respond please??

  • There is no 24/7 service debugging scripts, answering questions and/or writing scripts. You have two options - patiently wait or develop things by yourself.

  • How useless are you?

Log in to reply