Navigation

    Backtrader Community

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

    bgrosso

    @bgrosso

    1
    Reputation
    9
    Profile views
    9
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    bgrosso Unfollow Follow

    Best posts made by bgrosso

    • Updating Stop Loss based on current profit

      Hi,

      I am currently issuing a bracket order as follows:

      self.buy_bracket(exectype=bt.Order.Market,
                                       size=qty,
                                       limitprice=cash*3*self.p.risk / qty + self.data.close[0],
                                       stopprice=stop_price,
                                       )
      

      I wish to update the stop order issue based on the profit of the current trade. There is only one trade open at any given moment. I tried to use the trail order and trailpercent method but it doesn't seem to work.

      In general, here's what I would like the system to do:

      if profit > 20, stop loss should be at break-even
      if profit > 40, stop loss should be at 20
      ... and so on.

      Any clever ideas on how to implement it?

      posted in General Code/Help
      B
      bgrosso

    Latest posts made by bgrosso

    • RE: Updating Stop Loss based on current profit

      Cool, thanks!

      posted in General Code/Help
      B
      bgrosso
    • Updating Stop Loss based on current profit

      Hi,

      I am currently issuing a bracket order as follows:

      self.buy_bracket(exectype=bt.Order.Market,
                                       size=qty,
                                       limitprice=cash*3*self.p.risk / qty + self.data.close[0],
                                       stopprice=stop_price,
                                       )
      

      I wish to update the stop order issue based on the profit of the current trade. There is only one trade open at any given moment. I tried to use the trail order and trailpercent method but it doesn't seem to work.

      In general, here's what I would like the system to do:

      if profit > 20, stop loss should be at break-even
      if profit > 40, stop loss should be at 20
      ... and so on.

      Any clever ideas on how to implement it?

      posted in General Code/Help
      B
      bgrosso
    • RE: Update / Amend Existing Orders

      any update on the amend functionality?

      posted in General Code/Help
      B
      bgrosso
    • RE: Stochastic strategy with TP and Stop Loss. How to reverse position? Why is the commission not calculated?

      Maybe @backtrader can shed some light? Thanks in advance!

      posted in General Code/Help
      B
      bgrosso
    • RE: Stochastic strategy with TP and Stop Loss. How to reverse position? Why is the commission not calculated?

      Thank you @ab_trader. I could try that for the parent orders and logically it makes sense.

      Now, I am also using the stop-loss price to determine the size of the operation:

      qty = math.floor(cash * self.p.risk / (self.data.close[0] - stop_price))

      If I am only executing the order in the next bar, then:

      1. The stop price can't be known upfront;
      2. Hence, the size can't be determined;
      3. As a result, I can't send the parent order.

      Is there a way to:

      1. First, calculate the signals for entry with past data (all periods before n);
      2. Issue the orders within the bar with time-frame n, using all the considerations above, namely: stop-loss and size calculations.
      posted in General Code/Help
      B
      bgrosso
    • RE: Stochastic strategy with TP and Stop Loss. How to reverse position? Why is the commission not calculated?

      That's helpful. Curious about the reasons for stocklike to be equal to True.

      How could I connect it to the order execution price?

      posted in General Code/Help
      B
      bgrosso
    • RE: Stochastic strategy with TP and Stop Loss. How to reverse position? Why is the commission not calculated?

      See also with reversal entries:

              else:
                  if self.position.size < 0:
                      if self.buysell_sig < 0:
                          self.close(price=self.data.close[0])
                          stop_price = (self.data.close[0] * (1 - self.p.stop_dist))
                          qty = math.floor(cash * self.p.risk / (self.data.close[0] - stop_price))
                          self.buy_bracket(exectype=bt.Order.Market, price=self.data.close[0], size=qty, limitprice=cash * 3 * self.p.risk / qty + self.data.close[0], stopprice=stop_price)
                  if self.position.size > 0:
                      if self.buysell_sig > 0:
                          self.close(price=self.data.close[0])
                          stop_price = (self.data.close[0] * (1 + self.p.stop_dist))
                          qty = math.floor(cash * self.p.risk / (self.data.close[0] - stop_price))
                          self.sell_bracket(exectype=bt.Order.Market, price=self.data.close[0], size=qty, limitprice=self.data.close[0] - cash * 3 * self.p.risk / -qty,stopprice=stop_price)
      
      posted in General Code/Help
      B
      bgrosso
    • RE: Stochastic strategy with TP and Stop Loss. How to reverse position? Why is the commission not calculated?

      Regarding point 3, see below the output. Curious to understand why to I get a 180EUR profit and open 2 long positions before, while it should open only one. Thank you.

      [*********************100%***********************]  1 of 1 completed
      Starting Portfolio Value: 1000.00
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-01-29, Status 2: Ref: 1, Size: -1815, Cash 939.6362307959258, Price: 1.10198
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-01-29, Status 2: Ref: 2, Size: 1815, Cash 939.6362307959258, Price: 1.113
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-01-29, Status 2: Ref: 3, Size: 1815, Cash 939.6362307959258, Price: 1.06892
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Completed
      2020-01-29, Status 4: Ref: 1, Size: -1815, Price: 1.10229
      Created: 2020-01-28 00:00:00 Price: 1.101976990699768 Size: -1815
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-01-31, Status 2: Ref: 4, Size: 1815, Cash 998.9408569389731, Price: 1.10169
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Completed
      2020-01-31, Status 4: Ref: 4, Size: 1815, Price: 1.10288
      Created: 2020-01-30 00:00:00 Price: 1.1016855239868164 Size: 1815
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY TRADE  --------------------------------
      2020-01-31 00:00:00, Close Price: 1.102292776107788, Profit, Gross -1.06, Net -1.06, Commission 0
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-02-07, Status 2: Ref: 5, Size: -1816, Cash 938.1268314247249, Price: 1.10023
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-02-07, Status 2: Ref: 6, Size: 1816, Cash 938.1268314247249, Price: 1.11123
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-02-07, Status 2: Ref: 7, Size: 1816, Cash 938.1268314247249, Price: 1.06723
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Completed
      2020-02-07, Status 4: Ref: 5, Size: -1816, Price: 1.0981
      Created: 2020-02-06 00:00:00 Price: 1.1002310514450073 Size: -1816
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-02-21, Status 2: Ref: 8, Size: 1816, Cash 1034.05017326171, Price: 1.08101
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Completed
      2020-02-21, Status 4: Ref: 8, Size: 1816, Price: 1.07877
      Created: 2020-02-20 00:00:00 Price: 1.081009864807129 Size: 1816
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY TRADE  --------------------------------
      2020-02-21 00:00:00, Close Price: 1.0981045961380005, Profit, Gross 35.11, Net 35.11, Commission 0
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-02-24, Status 2: Ref: 9, Size: -1918, Cash 970.0995138183629, Price: 1.0788
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-02-24, Status 2: Ref: 10, Size: 1918, Cash 970.0995138183629, Price: 1.08959
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-02-24, Status 2: Ref: 11, Size: 1918, Cash 970.0995138183629, Price: 1.04645
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Completed
      2020-02-24, Status 4: Ref: 9, Size: -1918, Price: 1.08386
      Created: 2020-02-21 00:00:00 Price: 1.078799843788147 Size: -1918
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-02-25, Status 2: Ref: 12, Size: 1918, Cash 1032.0428424466008, Price: 1.08391
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Completed
      2020-02-25, Status 4: Ref: 12, Size: 1918, Price: 1.0849
      Created: 2020-02-24 00:00:00 Price: 1.0839051008224487 Size: 1918
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY TRADE  --------------------------------
      2020-02-25 00:00:00, Close Price: 1.0838581323623657, Profit, Gross -2.01, Net -2.01, Commission 0
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Completed
      2020-02-26, Status 4: Ref: 10, Size: 1918, Price: 1.08959
      Created: 2020-02-21 00:00:00 Price: 1.0895878422260286 Size: 1918
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Canceled
      2020-02-26, Status 5: Ref: 11, Size: 1918, Price: 1.04645
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Completed
      2020-03-02, Status 4: Ref: 2, Size: 1815, Price: 1.113
      Created: 2020-01-28 00:00:00 Price: 1.1129967606067657 Size: 1815
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Canceled
      2020-03-02, Status 5: Ref: 3, Size: 1815, Price: 1.06892
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Completed
      2020-03-02, Status 4: Ref: 6, Size: 1816, Price: 1.11123
      Created: 2020-02-06 00:00:00 Price: 1.1112333619594574 Size: 1816
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Canceled
      2020-03-02, Status 5: Ref: 7, Size: 1816, Price: 1.06723
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-03-09, Status 2: Ref: 13, Size: -5549, Cash 1222.3058167335987, Price: 1.12238
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Completed
      2020-03-09, Status 4: Ref: 13, Size: -5549, Price: 1.13862
      Created: 2020-03-06 00:00:00 Price: 1.1223849058151245 Size: -5549
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY TRADE  --------------------------------
      2020-03-09 00:00:00, Close Price: 1.1043284172299832, Profit, Gross 190.26, Net 190.26, Commission 0
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-03-26, Status 2: Ref: 14, Size: 2262, Cash 1147.275600510342, Price: 1.08026
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-03-26, Status 2: Ref: 15, Size: -2262, Cash 1147.275600510342, Price: 1.06946
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-03-26, Status 2: Ref: 16, Size: -2262, Cash 1147.275600510342, Price: 1.11269
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Completed
      2020-03-26, Status 4: Ref: 14, Size: 2262, Price: 1.08876
      Created: 2020-03-25 00:00:00 Price: 1.080263614654541 Size: 2262
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Completed
      2020-03-30, Status 4: Ref: 16, Size: -2262, Price: 1.11354
      Created: 2020-03-25 00:00:00 Price: 1.1126855196076868 Size: -2262
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Canceled
      2020-03-30, Status 5: Ref: 15, Size: -2262, Price: 1.06946
      --------------------------------  NOTIFY TRADE  --------------------------------
      2020-03-30 00:00:00, Close Price: 1.0887553691864014, Profit, Gross 56.05, Net 56.06, Commission 0
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-04-03, Status 2: Ref: 17, Size: -2335, Cash 1199.7929186572526, Price: 1.09536
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-04-03, Status 2: Ref: 18, Size: 2335, Cash 1199.7929186572526, Price: 1.10632
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-04-03, Status 2: Ref: 19, Size: 2335, Cash 1199.7929186572526, Price: 1.06251
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Completed
      2020-04-03, Status 4: Ref: 17, Size: -2335, Price: 1.08439
      Created: 2020-04-02 00:00:00 Price: 1.0953621864318848 Size: -2335
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-04-09, Status 2: Ref: 20, Size: 2335, Cash 1274.3188852390074, Price: 1.08951
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Completed
      2020-04-09, Status 4: Ref: 20, Size: 2335, Price: 1.08612
      Created: 2020-04-08 00:00:00 Price: 1.0895144939422607 Size: 2335
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY TRADE  --------------------------------
      2020-04-09 00:00:00, Close Price: 1.084386944770813, Profit, Gross -4.04, Net -4.04, Commission 0
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-04-15, Status 2: Ref: 21, Size: -2334, Cash 1195.904667003107, Price: 1.0923
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-04-15, Status 2: Ref: 22, Size: 2334, Cash 1195.904667003107, Price: 1.10322
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-04-15, Status 2: Ref: 23, Size: 2334, Cash 1195.904667003107, Price: 1.05954
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Completed
      2020-04-15, Status 4: Ref: 21, Size: -2334, Price: 1.09824
      Created: 2020-04-14 00:00:00 Price: 1.0922993421554565 Size: -2334
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-04-16, Status 2: Ref: 24, Size: 2334, Cash 1292.6543446055591, Price: 1.09854
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Completed
      2020-04-16, Status 4: Ref: 24, Size: 2334, Price: 1.09038
      Created: 2020-04-15 00:00:00 Price: 1.098538875579834 Size: 2334
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY TRADE  --------------------------------
      2020-04-16 00:00:00, Close Price: 1.0982372760772705, Profit, Gross 18.33, Net 18.34, Commission 0
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-04-17, Status 2: Ref: 25, Size: -2371, Cash 1214.1025976356648, Price: 1.09051
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-04-17, Status 2: Ref: 26, Size: 2371, Cash 1214.1025976356648, Price: 1.10142
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-04-17, Status 2: Ref: 27, Size: 2371, Cash 1214.1025976356648, Price: 1.0578
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Completed
      2020-04-17, Status 4: Ref: 25, Size: -2371, Price: 1.08601
      Created: 2020-04-16 00:00:00 Price: 1.0905101299285889 Size: -2371
      --------------------------------------------------------------------------------
      ----SUMMARY----
      Final Portfolio Value: $1293.5143458184384
      P/L: $293.5143458184384
      
      Process finished with exit code 0
      
      posted in General Code/Help
      B
      bgrosso
    • Stochastic strategy with TP and Stop Loss. How to reverse position? Why is the commission not calculated?

      Hi all,

      I am following a simple crossover stochastic strategy. I go both long and short depending on the signal given. Every time I enter the market, I issue a bracket order: one order at the current price, one order for a stop loss, and one order for trial profit.

      The size is calculated based on risk I want to take, ie, 2% of the cash in the broker at any given time. The stop loss is defined as 1% of the distance of the market price order and the trial profit is 3x the risk assumed (3 x cash in the broker x 2%).

      The strategy seems to be working fine, but one trade records -1.97 EUR profit. That's not expected, as we either get to the trial profit price or we are kicked out with the assumed risk.

      Here's the code I am working on:

      # Import the backtrader platform
      import backtrader as bt
      import backtrader.indicators as btind
      import datetime as dt
      import pandas as pd
      import yfinance as yf
      from pandas_datareader import data as pdr
      import math
      
      #importing data
      yf.pdr_override()
      startyear = 2020
      startmonth = 1
      startday = 1
      start = dt.datetime(startyear, startmonth, startday)
      #endyear = 2020
      #endmonth = 4
      #endday = 16
      #end = dt.datetime(endyear, endmonth, endday)
      end = dt.datetime.now()
      asset = "EURUSD=X"
      df = pdr.get_data_yahoo(asset, start, end)
      
      #set up commission scheme
      startcash = 1000.0
      
      class forexSpreadCommisionScheme(bt.CommInfoBase):
          params = (
              ('commtype', bt.CommInfoBase.COMM_PERC),
              ('stocklike', False)
              )
      
          def _getcommission(self, size, price, pseudoexec):
              return size * price * self.p.commission
      
      comminfo = forexSpreadCommisionScheme(
          commission=0.0035,
          leverage=30
      )
      
      
      #Defining strategy
      
      class BaseStrategy(bt.Strategy):
      
          params = (('risk', 0.02),  # risk 2%
                    ('stop_dist', 0.01))  # stoploss distance 1%
      
          def log(self, txt, dt=None):
              ''' Logging function fot this strategy'''
              dt = dt or self.datas[0].datetime.date(0)
              print('%s, %s' % (dt.isoformat(), txt))
      
      
          def __init__(self):
              self.bbands = bt.indicators.BollingerBands()
              self.sto = btind.Stochastic()
              percD = self.sto.lines.percD
              percK = self.sto.lines.percK
              self.buysell_sig = btind.CrossOver(percD, percK)
      
          def next(self):
              cash = self.broker.get_cash()
              self.dt = self.data.datetime.date()
      
              if not self.position:
      
                  if self.buysell_sig < 0:
                      stop_price = (self.data.close[0] * (1 - self.p.stop_dist))
                      qty = math.floor(cash * self.p.risk / (self.data.close[0] - stop_price))
                      self.buy_bracket(exectype=bt.Order.Market, price=self.data.close[0], size=qty, limitprice= cash*3*self.p.risk / qty + self.data.close[0], stopprice=stop_price)
      
      
                  elif self.buysell_sig > 0:
                      stop_price = (self.data.close[0] * (1 + self.p.stop_dist))
                      qty = math.floor(cash * self.p.risk / (self.data.close[0] - stop_price))
                      self.sell_bracket(exectype=bt.Order.Market, price=self.data.close[0], size=qty, limitprice= self.data.close[0] - cash*3*self.p.risk / -qty, stopprice=stop_price)
      
              '''else:
                  if self.position.size < 0:
                      if self.buysell_sig < 0:
                          self.order_target_size(price=self.data.close[0])
                  if self.position.size > 0:
                      if self.buysell_sig > 0:
                          self.order_target_size(price=self.data.close[0])'''
      
          def notify_order(self, order):
              date = self.data.datetime.datetime().date()
      
              if order.status == order.Accepted:
                  print('-' * 32, ' NOTIFY ORDER ', '-' * 32)
                  print('{} Order Accepted'.format(order.info['name']))
                  print('{}, Status {}: Ref: {}, Size: {}, Cash {}, Price: {}'.format(
                      date,
                      order.status,
                      order.ref,
                      order.size,
                      self.broker.get_cash(),
                      'NA' if not order.price else round(order.price, 5)
                  ))
      
              if order.status == order.Completed:
                  print('-' * 32, ' NOTIFY ORDER ', '-' * 32)
                  print('{} Order Completed'.format(order.info['name']))
                  print('{}, Status {}: Ref: {}, Size: {}, Price: {}'.format(
                      date,
                      order.status,
                      order.ref,
                      order.size,
                      'NA' if not order.executed.price else round(order.executed.price, 5)
                  ))
                  print('Created: {} Price: {} Size: {}'.format(bt.num2date(order.created.dt), order.created.price,
                                                                order.created.size))
                  print('-' * 80)
      
              if order.status == order.Canceled:
                  print('-' * 32, ' NOTIFY ORDER ', '-' * 32)
                  print('{} Order Canceled'.format(order.info['name']))
                  print('{}, Status {}: Ref: {}, Size: {}, Price: {}'.format(
                      date,
                      order.status,
                      order.ref,
                      order.size,
                      'NA' if not order.price else round(order.price, 5)
                  ))
      
              if order.status == order.Rejected:
                  print('-' * 32, ' NOTIFY ORDER ', '-' * 32)
                  print('WARNING! {} Order Rejected'.format(order.info['name']))
                  print('{}, Status {}: Ref: {}, Size: {}, Price: {}'.format(
                      date,
                      order.status,
                      order.ref,
                      order.size,
                      'NA' if not order.price else round(order.price, 5)
                  ))
                  print('-' * 80)
      
          def notify_trade(self, trade):
              date = self.data.datetime.datetime()
              if trade.isclosed:
                  print('-' * 32, ' NOTIFY TRADE ', '-' * 32)
                  print('{}, Close Price: {}, Profit, Gross {}, Net {}, Commission {}'.format(
                      date,
                      trade.price,
                      round(trade.pnl, 2),
                      round(trade.pnlcomm, 2),
                      round(trade.commission), 4))
                  print('-' * 80)
      
      if __name__ == '__main__':
      
          # Create a cerebro entity
          cerebro = bt.Cerebro()
      
          cerebro.addstrategy(BaseStrategy)
      
          data = bt.feeds.PandasData(dataname=df)
      
          # Add the Data Feed to Cerebro
          cerebro.adddata(data)
      
          # Set our desired cash start
          cerebro.broker.setcash(startcash)
      
          # Set the commission
          cerebro.broker.addcommissioninfo(comminfo)
      
          # Print out the starting conditions
          print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
      
          # Run over everything
          cerebro.run()
      
          portvalue = cerebro.broker.getvalue()
          pnl = portvalue - startcash
      
          # Print out the final result
          print('----SUMMARY----')
          print('Final Portfolio Value: ${}'.format(portvalue))
          print('P/L: ${}'.format(pnl))
      
          cerebro.plot()
      

      Output:

      [*********************100%***********************]  1 of 1 completed
      Starting Portfolio Value: 1000.00
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-01-29, Status 2: Ref: 1, Size: -1815, Cash 939.6362307959258, Price: 1.10198
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-01-29, Status 2: Ref: 2, Size: 1815, Cash 939.6362307959258, Price: 1.113
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-01-29, Status 2: Ref: 3, Size: 1815, Cash 939.6362307959258, Price: 1.06892
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Completed
      2020-01-29, Status 4: Ref: 1, Size: -1815, Price: 1.10229
      Created: 2020-01-28 00:00:00 Price: 1.101976990699768 Size: -1815
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Completed
      2020-03-02, Status 4: Ref: 2, Size: 1815, Price: 1.113
      Created: 2020-01-28 00:00:00 Price: 1.1129967606067657 Size: 1815
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Canceled
      2020-03-02, Status 5: Ref: 3, Size: 1815, Price: 1.06892
      --------------------------------  NOTIFY TRADE  --------------------------------
      2020-03-02 00:00:00, Close Price: 1.102292776107788, Profit, Gross -19.43, Net -19.43, Commission 0
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-03-09, Status 2: Ref: 4, Size: -1748, Cash 922.3972951478748, Price: 1.12238
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-03-09, Status 2: Ref: 5, Size: 1748, Cash 922.3972951478748, Price: 1.13361
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-03-09, Status 2: Ref: 6, Size: 1748, Cash 922.3972951478748, Price: 1.08873
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Completed
      2020-03-09, Status 4: Ref: 4, Size: -1748, Price: 1.13862
      Created: 2020-03-06 00:00:00 Price: 1.1223849058151245 Size: -1748
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Completed
      2020-03-10, Status 4: Ref: 5, Size: 1748, Price: 1.13974
      Created: 2020-03-06 00:00:00 Price: 1.1336087548732758 Size: 1748
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Canceled
      2020-03-10, Status 5: Ref: 6, Size: 1748, Price: 1.08873
      --------------------------------  NOTIFY TRADE  --------------------------------
      2020-03-10 00:00:00, Close Price: 1.1386150121688843, Profit, Gross -1.97, Net -1.97, Commission 0
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-03-26, Status 2: Ref: 7, Size: 1811, Cash 918.5275701915566, Price: 1.08026
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-03-26, Status 2: Ref: 8, Size: -1811, Cash 918.5275701915566, Price: 1.06946
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-03-26, Status 2: Ref: 9, Size: -1811, Cash 918.5275701915566, Price: 1.11269
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Completed
      2020-03-26, Status 4: Ref: 7, Size: 1811, Price: 1.08876
      Created: 2020-03-25 00:00:00 Price: 1.080263614654541 Size: 1811
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Completed
      2020-03-30, Status 4: Ref: 9, Size: -1811, Price: 1.11354
      Created: 2020-03-25 00:00:00 Price: 1.1126854207606627 Size: -1811
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Canceled
      2020-03-30, Status 5: Ref: 8, Size: -1811, Price: 1.06946
      --------------------------------  NOTIFY TRADE  --------------------------------
      2020-03-30 00:00:00, Close Price: 1.0887553691864014, Profit, Gross 44.88, Net 44.88, Commission 0
      --------------------------------------------------------------------------------
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-04-03, Status 2: Ref: 10, Size: -1869, Cash 960.5889084148374, Price: 1.09536
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-04-03, Status 2: Ref: 11, Size: 1869, Cash 960.5889084148374, Price: 1.10632
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-04-03, Status 2: Ref: 12, Size: 1869, Cash 960.5889084148374, Price: 1.06251
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Completed
      2020-04-03, Status 4: Ref: 10, Size: -1869, Price: 1.08439
      Created: 2020-04-02 00:00:00 Price: 1.0953621864318848 Size: -1869
      --------------------------------------------------------------------------------
      ----SUMMARY----
      Final Portfolio Value: $1018.1813883709875
      P/L: $18.18138837098752
      

      My questions are:

      1. Why am I getting a -1.97 EUR profit?
      2. Why is the commission not being calculated?
      3. I was trying to code a reversal without success. A reversal would happen if we are long or short in the market, we get a signal to go instead short or long, and we want to close the existing position while opening a bracket order in the other direction. I got more orders than expected while doing this. See also my code in the snippet. Any idea what is might not be working? Thanks.
      posted in General Code/Help
      B
      bgrosso