Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Simple Sample code to Backtest an OCO model

    General Code/Help
    2
    9
    457
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J Amin
      J Amin last edited by

      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!!!

      Thanks

      J Amin 1 Reply Last reply Reply Quote 0
      • J Amin
        J Amin @J Amin last edited by

        @J-Amin

        Could someone please kindly respond?

        1 Reply Last reply Reply Quote 0
        • A
          ab_trader last edited by

          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

          • If my answer helped, hit reputation up arrow at lower right corner of the post.
          • Python Debugging With Pdb
          • New to python and bt - check this out
          J Amin 1 Reply Last reply Reply Quote 0
          • J Amin
            J Amin @ab_trader last edited by

            @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(self.data.datetime.date(0),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:
            
                    self.orefs.remove(order.ref)
            
            
            
            def __init__(self):
            
               self.data.close=df_cur['Signal']
            
            
            
            def next(self):
            
                if self.data.signal=1
            
                    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 1 Reply Last reply Reply Quote 0
            • J Amin
              J Amin @J Amin last edited by

              @J-Amin

              Under “def init(self):”

              I meant to put self.data.signal=df_cur[‘Signal’]

              J Amin 1 Reply Last reply Reply Quote 0
              • J Amin
                J Amin @J Amin last edited by

                @J-Amin

                Any response please??

                J Amin 1 Reply Last reply Reply Quote 0
                • J Amin
                  J Amin @J Amin last edited by

                  @J-Amin

                  Any administrators who could respond please??

                  1 Reply Last reply Reply Quote 0
                  • A
                    ab_trader last edited by

                    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.

                    • If my answer helped, hit reputation up arrow at lower right corner of the post.
                    • Python Debugging With Pdb
                    • New to python and bt - check this out
                    1 Reply Last reply Reply Quote 0
                    • J Amin
                      J Amin last edited by

                      How useless are you?

                      1 Reply Last reply Reply Quote -2
                      • 1 / 1
                      • First post
                        Last post
                      Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors