Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. KT
    3. Posts
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    K
    • Profile
    • Following 0
    • Followers 1
    • Topics 14
    • Posts 35
    • Best 1
    • Controversial 0
    • Groups 0

    Posts made by KT

    • Broker with different multiplier for different dataset

      Hi! I am currently coding a strategy that will be applied simultaneously to different datasets. I would like to check how to set broker with unique multiplier for each datasets.

      Also, when I try to plot the data, it gives an error as there are many plots to be plotted. Is it possible if I only plot the equity line.

      Thanks for helping :)

      posted in General Discussion
      K
      KT
    • RE: Commission

      @backtrader do you mean the name of the data or the name of the commission? Thanks :)

      posted in Indicators/Strategies/Analyzers
      K
      KT
    • Commission

      Hi @backtrader ! Just wanna check whether it is possible to add two commission scheme for two different datasets. Let say commission scheme one has multiplier of 1 for self.data0 and commission scheme two has multiplier of 2 for self.data1 and we are trading using these two datasets simulataneously.

      Thanks!!

      posted in Indicators/Strategies/Analyzers
      K
      KT
    • Closing All Orders

      Hi! How do I close all the orders before the end of the session?

      I found out that there are multiple ways to execute it such as self.close() and using Order.Close. However, I don't know how to code it or where to execute it.

      Thanks for the help :)

      posted in General Discussion
      K
      KT
    • RE: Order

      @Curtis-Miller Thanks!! I will check the PyFolio

      posted in General Discussion
      K
      KT
    • Order Validity

      Hi!! Is it possible to set the validity of an order until the next bar instead of using dates? If yes, how do I do it?

      Thanks :)

      posted in General Discussion
      K
      KT
    • RE: Order

      Thanks @backtrader . For the order's opening price and closing price, how do i check the value of the opening and closing price?

      Also, for the statistics that are provided by backtrader, can we check for the following info too ? The info includes
      largest profit trade, largest loss trade, average profit trade, average loss trade, maximum number of consecutive wins, maximum number of consecutive loss, and number of trades.

      posted in General Discussion
      K
      KT
    • RE: Order

      Thanks @backtrader !! Is it possible to access the opening price and closing price of an order?

      posted in General Discussion
      K
      KT
    • RE: Order

      Thanks @backtrader ! For the case of multiple data feeds, is it possible to refer to the trade.pnl of each dataset? (Maybe sth like Trade.pnl(self.data1))

      posted in General Discussion
      K
      KT
    • Order

      Hi! I would like to clarify for the definition of order.executed.value and how the trade.pnl is calculated in backtrader.

      If I am using multiple datasets, is it possible if I calculate trade.pnl for each datasets? And if I am backtesting for FX, how do i obtain the net profit in terms of pips?

      Thankss :)

      posted in General Discussion
      K
      KT
    • Feeding Multiple Data Feeds in Indicators

      Hi! I would like to ask how to add multiple data feeds into indicators. I am using StandardDeviation indicator and instead of using multiple indicators, I am planning to input 3 data into the indicator so that the indicator can give me 3 sd for different data. Thanks :)

      posted in General Discussion
      K
      KT
    • RE: Strategy with Signals

      Thanks @backtrader

      posted in Indicators/Strategies/Analyzers
      K
      KT
    • Modify Order

      Hi! I would like to check how to modify the order that has been submitted but not executed. I would like to change the stopping price of sell_bracket order after it has been submitted.

      posted in General Discussion
      K
      KT
    • Strategy with Signals

      I read through "strategy with signals" section of the documentation. However, I don't really understand how to code using those signals.

      Let say I would like to have a strategy that :

      1. goes into a long position if today's closing price is lower than yesterday's
      2. goes into a short position if today's closing price is higher than yesterday's

      How do I use the signal? Do I still have to use self.sell and self.buy? Thankss :)

      posted in Indicators/Strategies/Analyzers
      K
      KT
    • RE: Plot Indicator on Master Data

      Thanks @backtrader

      posted in Indicators/Strategies/Analyzers
      K
      KT
    • Plot Indicator on Master Data

      Hi! I would like to plot an indicator on hourly data but the indicator is calculated based on daily data.

      I tried using the following code but I am unable to see the indicator

      self.indicatorname.plotinfo.plotmaster =True
      self.indicatorname.plotinfo.plotylimited = False
      

      This is the full code.

      from __future__ import (absolute_import, division, print_function,
                              unicode_literals)
      import os.path
      import argparse
      import datetime
      import math
      import backtrader as bt
      import backtrader.feeds as btfeeds
      import backtrader.plot as plt
      import backtrader.indicators as btinds
      
      class Donchian(btinds.PeriodN):
          lines = ('maxi','mini',)
          params = (('period',6),)
          
          def __init__(self):
              self.lines.maxi = btinds.Highest(self.data.high, period=self.p.period)
              self.lines.mini = btinds.Lowest(self.data.low, period=self.p.period)
      
      class St(bt.Strategy):
          
          def log(self, txt, dt=None):
              ''' Logging function fot this strategy'''
              dt = dt or self.data0.datetime.date(0)
              dt1 = self.data0.datetime.time(0)
              print('%s,%s, %s' % (dt.isoformat(),dt1.isoformat(), txt))
              
          def __init__(self):
              self.channel = Donchian(self.data1)
              self.channel.plotinfo.subplot=False
              self.channel.plotinfo.plotmaster = True
              self.buysignal = 0
              self.sellsignal = 0
              self.dayhigh = 0
              self.daylow = 0
              
          def notify_order(self, order):
              if order.status in [order.Submitted, order.Accepted]:
                  return
              
              if order.status in [order.Completed]:
                  if order.isbuy():
                      self.log(
                          'BUY EXECUTED, Price: %.5f, Cost: %.5f' %
                          (order.executed.price,
                           order.executed.value))
                      
                      self.buyprice = order.executed.price
                      
                  else:  # Sell
                      self.log('SELL EXECUTED, Price: %.5f, Cost: %.5f' %
                               (order.executed.price,
                                order.executed.value))
              
                  self.bar_executed = len(self)
                  
              elif order.status in [order.Canceled, order.Margin, order.Rejected]:
                  self.log('Order Canceled/Margin/Rejected')
              
              self.order = None
              
          def notify_trade(self, trade):
              if not trade.isclosed:
                  return
      
              self.log('OPERATION PROFIT, GROSS %.5f, NET %.5f' %
                       (trade.pnl, trade.pnlcomm))
              
          def next(self):
              txt = ','.join(
                  ['%04d' % len(self.data0),
                   '%04d' % len(self.data1),
                   self.data0.datetime.date(0).isoformat(),
                   self.data0.datetime.time(0).isoformat(),
                   self.data1.datetime.date(0).isoformat(),
                   'Open : %.5f' % self.data0.open[0],
                   'Close : %.5f' % self.data0.close[0],
                   'Buysignal = %d' % self.buysignal,
                   'Sellsignal = %d' % self.sellsignal,
                   'Current Low = %.5f' % self.daylow,
                   'Current High = %.5f' % self.dayhigh]) 
              print(txt)
              
              if self.buysignal >0 :
                  if self.data0.datetime.time(0)==datetime.time(0,0,0):
                      self.buysignal = 0
                      self.sellsignal = 0
      
              if self.sellsignal >0 :
                  if self.data0.datetime.time(0)==datetime.time(0,0,0):
                      self.buysignal = 0
                      self.sellsignal = 0
                      
              if self.buysignal == 0 :
                      
                      if self.data0.close[0] < self.channel.lines.mini[0]:
                          self.buysignal = 1
                          self.daylow = self.channel.lines.mini[0]
                          print("-----------breaking low----------------")
                          print(self.data0.datetime.time(0),"Current Low : %.5f" % self.channel.mini[0])
                  
              elif self.buysignal == 1:
                      
                      if self.data0.close[0] > self.daylow:
                          print('Current Low : %5f' % self.daylow)
                          self.log('BUY CREATE, %.5f' % self.data0.close[0])
                          self.order=self.buy(data=self.data0)
                          self.buysignal = 0
              
              if self.sellsignal==0:
                      
                      if self.data0.close[0] > self.channel.lines.maxi[0]:
                          self.sellsignal=1
                          self.dayhigh = self.channel.lines.maxi[0]
                          print("------------breaking high----------------")
                          print(self.data0.datetime.time(0),"Current High : %.5f" % self.channel.lines.maxi[0])
                  
              elif self.sellsignal ==1:
                  
                  if self.data0.close[0] < self.dayhigh:
                      print('Current High : %5f' % self.dayhigh)
                      self.log('SELL CREATE, %.5f' % self.data0.close[0])
                      self.order = self.sell(data=self.data0)
                      self.sellsignal=0
      def runstrat():
          args = parse_args()
      
          cerebro = bt.Cerebro()
          modpath = 'd:\\I - TradersGPS\\'
          datapath0 = os.path.join(modpath, 'GBPUSD_H1_UTC+2_00.csv')
          datapath1 = os.path.join(modpath, 'GBPUSD_D1_UTC+2_00.csv')    
          data0 = btfeeds.GenericCSVData(dataname=datapath0,
                                         timeframe=bt.TimeFrame.Minutes,
                                         compression = 60,
                                        fromdate = datetime.datetime(2016,10,21),
                                        todate=datetime.datetime(2016,11,15),                                  
                                        nullvalue=0.0,
                                        dtformat=('%Y.%m.%d'),
                                        tmformat=('%H:%M'),
                                        datetime=0,
                                        time=1,
                                        open=2,
                                        high=3,
                                        low=4,
                                        close=5,
                                        volume=6,
                                        openinterest=-1)
          data1 = btfeeds.GenericCSVData(dataname=datapath1,
                                         timeframe = bt.TimeFrame.Days,
                                        fromdate =datetime.datetime(2016,10,21),
                                        todate= datetime.datetime(2016,11,15),
                                        nullvalue=0.0,
                                        dtformat=('%Y.%m.%d'),
                                        tmformat=('%H:%M'),
                                        datetime=0,
                                        time=1,
                                        open=2,
                                        high=3,
                                        low=4,
                                        close=5,
                                        volume=6,
                                        openinterest=-1)
          cerebro.adddata(data0,name ="Hourly Data")
          cerebro.adddata(data1,name="Daily Data")
      
          cerebro.addstrategy(St)
          cerebro.broker.setcash(100000.0)
          print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
          cerebro.run(stdstats=False)
          print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
          cerebro.plot()
      
      posted in Indicators/Strategies/Analyzers
      K
      KT
    • RE: Cancel Order

      Thanks @backtrader !

      posted in General Code/Help
      K
      KT
    • RE: Cancel Order

      How do i cancel those orders that are still alive? For example order 582. Thanks @backtrader :)

      posted in General Code/Help
      K
      KT
    • RE: Cancel Order

      Thanks @backtrader for the clarification. With regards to what you said, that means I cannot cancel the order once the instance has changed. For example, I can no longer cancel order 581 and 582 after the order 583 is executed.

      2017-04-11 : Order ref: 580 / Type Sell / Status Submitted
      2017-04-11 : Order ref: 580 / Type Sell / Status Accepted
      2017-04-11 : Order ref: 580 / Type Sell / Status Completed
      2017-04-11, SELL EXECUTED, Reference : 580, Price: 1.24116, Cost: -1.24116,Position : -1
      2017-04-11: Oref 581 / Buy Stop at 1.2535716000000001
      2017-04-11: Oref 582 / Buy Limit at 1.2287484
      0002,2017-04-11,Open : 1.24116,High : 1.24944,Low : 1.24035,Close : 1.24893,Position : -1
      2017-04-12 : Order ref: 581 / Type Buy / Status Submitted
      2017-04-12 : Order ref: 582 / Type Buy / Status Submitted
      2017-04-12 : Order ref: 581 / Type Buy / Status Accepted
      2017-04-12 : Order ref: 582 / Type Buy / Status Accepted
      2017-04-12 : Order ref: 581 / Type Buy / Status Completed
      2017-04-12, BUY EXECUTED, Ref : 581, Price: 1.25357, Cost: -1.24116, Position : 0
      2017-04-12, OPERATION PROFIT, GROSS -0.01241, NET -0.01241
      0003,2017-04-12,Open : 1.24891,High : 1.25483,Low : 1.24801,Close : 1.25387,Position : 0
      2017-04-12: Oref 583 / Sell at 1.25387
      2017-04-13 : Order ref: 583 / Type Sell / Status Submitted
      2017-04-13 : Order ref: 583 / Type Sell / Status Accepted
      2017-04-13 : Order ref: 583 / Type Sell / Status Completed
      
      posted in General Code/Help
      K
      KT
    • RE: Cancel Order

      Sorry @backtrader . I couldn't figure out how to remove the orders, with reference code in self.orefs, that have been submitted, accepted but not executed.

      I tried to remove it in next(self) section using self.cancel(self.order[oref] but the order here is not indexable.

      Starting Portfolio Value: 100000.00
      0050,2017-03-17,01:00:00,Open : 1.23558,High : 1.23559,Low : 1.23451,Close : 1.23498
      2017-03-17 01:00:00  : Oref 294 / Sell at 1.23552
      2017-03-17 01:00:00  : Oref 295 / Sell Take Profit at 1.23107
      2017-03-17 01:00:00  : Oref 296 / Sell Stop Loss at 1.23997
      2017-03-17 02:00:00: Order ref: 294 / Type Sell / Status Submitted
      2017-03-17 02:00:00: Order ref: 295 / Type Buy / Status Submitted
      2017-03-17 02:00:00: Order ref: 296 / Type Buy / Status Submitted
      2017-03-17 02:00:00: Order ref: 294 / Type Sell / Status Accepted
      2017-03-17 02:00:00: Order ref: 295 / Type Buy / Status Accepted
      2017-03-17 02:00:00: Order ref: 296 / Type Buy / Status Accepted
      2017-03-17 02:00:00: Order ref: 294 / Type Sell / Status Completed
      

      An example would be removing order ref 295 and 296. I couldn't remove those orders. Thankss!!

      posted in General Code/Help
      K
      KT
    • 1
    • 2
    • 1 / 2