Navigation

    Backtrader Community

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

    Sasha

    @Sasha

    0
    Reputation
    164
    Profile views
    21
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    Sasha Unfollow Follow

    Latest posts made by Sasha

    • RE: How to execute(buy or sell) create and complete operations at one time?

      Thanks a lot
      I need to be careful )

      posted in General Code/Help
      S
      Sasha
    • RE: How to execute(buy or sell) create and complete operations at one time?

      @ab_trader thanks, but I do not benefit from this because the prices are different and I want to buy a stock that day at the same price

      2018-02-20, BUY CREATE AAPL, price = 171.85, qty = 26
      2018-02-20, BUY COMPLETE, 178.27
      I want to buy and complete stock with a price 171.85, not 178.27

      posted in General Code/Help
      S
      Sasha
    • How to execute(buy or sell) create and complete operations at one time?

      For instance,
      2018-02-20, BUY CREATE AAPL, price = 171.85
      2018-02-21, BUY COMPLETE, 178.13
      171.85 and 178.13 are the close price of each day.
      It is daily trading. Complete operation is performed the next day, in 2018-02-21.
      I want to create and complete the operation in 2018-02-20.
      I mean that the action was created and completed at a price of 171.85.
      Is it possible to perform such an operation?
      In short, I want to buy and sell shares in the market the same day when a signal comes to me, and not the next day as in the document

      import datetime  # For datetime objects
      import os  # To manage paths
      import sys  # To find out the script name (in argv[0])
      
      # Import the backtrader platform
      import backtrader as bt
      import scipy.stats
      
          
      # Create a Stratey
      class TestStrategy(bt.Strategy):
      
          lines = ('correlation',)
          params = dict(
              period20=20,
              period10=10,
              stake=10,
              qty1=0,
              qty2=0,
              status=0,
              portfolio_value=10000,
          )
      
          
          def log(self, txt, dt=None):
              ''' Logging function for this strategy'''
              dt = dt or self.datas[0].datetime.date(0)
              print('%s, %s' % (dt.isoformat(), txt))
      
          
          def notify_order(self, order):
              if order.status in [bt.Order.Submitted, bt.Order.Accepted]:
                  return  # Await further notifications
      
              if order.status == order.Completed:
                  if order.isbuy():
                      buytxt = 'BUY COMPLETE, %.2f' % order.executed.price
                      self.log(buytxt)
                  else:
                      selltxt = 'SELL COMPLETE, %.2f' % order.executed.price
                      self.log(selltxt)
                  
              elif order.status in [order.Expired, order.Canceled, order.Margin]:
                  self.log('%s ,' % order.Status[order.status])
                  pass  # Simply log
      
              # Allow new orders
              self.orderid = None
          
          def __init__(self):
              # Keep a reference to the "close" line in the data[0] dataseries
              
              self.orderid = None
              self.qty1 = self.p.qty1
              self.qty2 = self.p.qty2
              self.status = self.p.status
              self.portfolio_value = self.p.portfolio_value
              self.profit_fix = 30
              self.profit = 0 
              self.cash_aapl = 0
              self.cash_msft = 0
              self.init_close1 = 0
              self.init_close2 = 0
              
              
          def next(self):
              if self.orderid:
                  return
              print('*'*20)
              print('Status is ', self.status)
      
              c10, p = scipy.stats.pearsonr(self.data0.get(size=self.p.period10),
                                           self.data1.get(size=self.p.period10))
      
              c20, p = scipy.stats.pearsonr(self.data0.get(size=self.p.period20),
                                           self.data1.get(size=self.p.period20))
              c10, c20 = round(c10, 2), round(c20, 2)
              self.log('Close, {}'.format(self.datas[0].close[0]))
              self.log('Close, {}'.format(self.datas[1].close[0]))    
              print('corr10: {}, corr20: {}'.format(c10, c20))
              # Step 2: Check conditions for LONG & place the order
              # Checking the condition for LONG
      
              
              if self.status == 1:
                  self.cash_aapl = round((self.data0.close[0] - self.init_close1)*self.qty1,2)
                  self.cash_msft = round((self.init_close2 - self.data1.close[0])*self.qty2,2)
                  self.profit = round((self.cash_aapl + self.cash_msft),2)
                  
                  print('Profit stock1: {}'.format(self.cash_aapl))
                  print('Profit stock2: {}'.format(self.cash_msft))
                  print('Total profit: {}'.format(self.profit))
                     
              
              if (c10 > 0.7 and c20 > 0.7) and (self.status != 1):
      
                  # Calculating the number of shares for each stock
                  value = 0.45 * self.portfolio_value  # Divide the cash equally
                  x = int(value / (self.data0.close))  # Find the number of shares for Stock1
                  y = int(value / (self.data1.close))  # Find the number of shares for Stock2
      
                  
                  print('portfolio_value: {}'.format(self.portfolio_value))
                  
                  # Place the order
                  self.log('BUY CREATE %s, price = %.2f, qty = %d' % ("AAPL", self.data0.close[0], x))
                  self.buy(data=self.data0, size=(x))  
      
                  self.log('SELL CREATE %s, price = %.2f, qty = %d' % ("MSFT", self.data1.close[0], y))
                  self.sell(data=self.data1, size=(y))  
      
                  
                  self.qty1 = x  # The new open position quantity for Stock1 is x shares
                  self.qty2 = y  # The new open position quantity for Stock2 is y shares
                  self.init_close1 = self.data0.close[0]
                  self.init_close2 = self.data1.close[0]
                  self.status = 1  # The current status is "long the spread"
                  
              elif (self.profit > self.profit_fix) and self.status == 1:
                  print('profit: {}, profit_fix: {}'.format(self.profit, self.profit_fix))
                  # Placing the order
                  self.log('SELL CREATE %s, price = %.2f, qty = %d' % ("AAPL", self.data0.close[0], self.qty1))
                  self.sell(data=self.data0, size=(self.qty1))  
                  
                  self.log('BUY CREATE %s, price = %.2f, qty = %d' % ("MSFT", self.data1.close[0], self.qty2))
                  self.buy(data=self.data1, size=(self.qty2))  
      
                  # Updating the counters with new value
                  self.portfolio_value+=self.profit
                  self.qty1, self.qty2 = 0, 0
                  
                  self.cash_aapl, self.cash_msft, self.profit = 0, 0, 0
                  
                  self.status = 0 
      
          def stop(self):
              print('==================================================')
              print('Starting Value - %.2f' % self.broker.startingcash)
              print('Ending   Value - %.2f' % self.broker.getvalue())
              print('==================================================')
              
              
      if __name__ == '__main__':
          # Create a cerebro entity
          cerebro = bt.Cerebro()
      
          # Add a strategy
          cerebro.addstrategy(TestStrategy)
          
          
          def read_data(file_name):    
              datapath = os.path.abspath(os.getcwd() + '/' + file_name)
              return bt.feeds.YahooFinanceCSVData(
                      dataname=datapath,
                      # Do not pass values before this date
                      #fromdate=datetime.datetime(2018, 1, 1),
                      # Do not pass values before this date
                      #todate=datetime.datetime(2019, 1, 1),
                      # Do not pass values after this date
                      reverse=False)
          
          # Create a Data Feed
          data1 = read_data('aapl_1819.csv')
          cerebro.adddata(data1, name='AAPL')
          
          data2 = read_data('msft_1819.csv')
          cerebro.adddata(data2, 'MSFT')
          
          # Set our desired cash start
          cerebro.broker.setcash(100000.0)
      
          # Print out the starting conditions
          print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
      
          # Run over everything
          cerebro.run()
      
          # Print out the final result
          print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
      

      Output:

      Status is  0
      2018-02-20, Close, 171.85
      2018-02-20, Close, 92.72
      corr10: 0.9, corr20: 0.73
      portfolio_value: 10000
      2018-02-20, BUY CREATE AAPL, price = 171.85, qty = 26
      2018-02-20, SELL CREATE MSFT, price = 92.72, qty = 48
      2018-02-21, BUY COMPLETE, 178.13
      2018-02-21, SELL COMPLETE, 95.61
      ********************
      Status is  1
      2018-02-21, Close, 171.07
      2018-02-21, Close, 91.49
      corr10: 0.93, corr20: 0.76
      Profit stock1: -20.28
      Profit stock2: 59.04
      Total profit: 38.76
      profit: 38.76, profit_fix: 30
      2018-02-21, SELL CREATE AAPL, price = 171.07, qty = 26
      2018-02-21, BUY CREATE MSFT, price = 91.49, qty = 48
      2018-02-22, SELL COMPLETE, 177.95
      2018-02-22, BUY COMPLETE, 94.96
      posted in General Code/Help
      S
      Sasha
    • RE: problem with live trading(delayed data)

      @ab_trader but I just want to check my algorithm on paper trading

      posted in General Code/Help
      S
      Sasha
    • RE: Problem with YahooFinanceData

      @backtrader How to check algorithm for history data with minutes timeframe?

      posted in General Code/Help
      S
      Sasha
    • RE: problem with live trading(delayed data)

      @ab_trader said in problem with live trading(delayed data):

      Buy subscription for market data from Interactive Brokers.

      Is there another way to get permission? )

      posted in General Code/Help
      S
      Sasha
    • Problem with YahooFinanceData

      I am new to backtrader
      I want to reciev data with timeframe=bt.TimeFrame.Minutes

      from __future__ import (absolute_import, division, print_function,
                              unicode_literals)
      
      import datetime  # For datetime objects
      import os.path  # To manage paths
      import sys  # To find out the script name (in argv[0])
      
      # Import the backtrader platform
      import backtrader as bt
      
      
      # Create a Stratey
      class TestStrategy(bt.Strategy):
      
          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):
              # Keep a reference to the "close" line in the data[0] dataseries
              self.dataclose = self.datas[0].close
      
          def next(self):
              # Simply log the closing price of the series from the reference
              self.log('Close, %.2f' % self.dataclose[0])
      
              if self.dataclose[0] < self.dataclose[-1]:
                  # current close less than previous close
      
                  if self.dataclose[-1] < self.dataclose[-2]:
                      # previous close less than the previous close
      
                      # BUY, BUY, BUY!!! (with all possible default parameters)
                      self.log('BUY CREATE, %.2f' % self.dataclose[0])
                      self.buy()
      
      
      if __name__ == '__main__':
          # Create a cerebro entity
          cerebro = bt.Cerebro()
      
          # Add a strategy
          cerebro.addstrategy(TestStrategy)
      
          # Datas are in a subfolder of the samples. Need to find where the script is
          # because it could have been called from anywhere
      
          # Create a Data Feed
          data = bt.feeds.YahooFinanceData(
              dataname='AAPL',
              # Do not pass values before this date
              fromdate=datetime.datetime(2016, 1, 1),
              # Do not pass values before this date
              todate=datetime.datetime(2016, 2, 1),
              timeframe = bt.TimeFrame.Minutes,
              # Do not pass values after this date
              reverse=False)
      
          # Add the Data Feed to Cerebro
          cerebro.adddata(data)
      
          # Set our desired cash start
          cerebro.broker.setcash(100000.0)
      
          # Print out the starting conditions
          print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
      
          # Run over everything
          cerebro.run()
      
          # Print out the final result
          print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
      

      I get an error.What is the error?

      KeyError                                  Traceback (most recent call last)
      <ipython-input-6-0bc68b8d700a> in <module>()
           68 
           69     # Run over everything
      ---> 70     cerebro.run()
           71 
           72     # Print out the final result
      
      ~\Anaconda3\envs\env_zipline\lib\site-packages\backtrader\cerebro.py in run(self, **kwargs)
         1125             # let's skip process "spawning"
         1126             for iterstrat in iterstrats:
      -> 1127                 runstrat = self.runstrategies(iterstrat)
         1128                 self.runstrats.append(runstrat)
         1129                 if self._dooptimize:
      
      ~\Anaconda3\envs\env_zipline\lib\site-packages\backtrader\cerebro.py in runstrategies(self, iterstrat, predata)
         1208                 if self._exactbars < 1:  # datas can be full length
         1209                     data.extend(size=self.params.lookahead)
      -> 1210                 data._start()
         1211                 if self._dopreload:
         1212                     data.preload()
      
      ~\Anaconda3\envs\env_zipline\lib\site-packages\backtrader\feed.py in _start(self)
          201 
          202     def _start(self):
      --> 203         self.start()
          204 
          205         if not self._started:
      
      ~\Anaconda3\envs\env_zipline\lib\site-packages\backtrader\feeds\yahoo.py in start(self)
          347 
          348     def start(self):
      --> 349         self.start_v7()
          350 
          351         # Prepared a "path" file -  CSV Parser can take over
      
      ~\Anaconda3\envs\env_zipline\lib\site-packages\backtrader\feeds\yahoo.py in start_v7(self)
          319         }
          320 
      --> 321         urlargs.append('interval={}'.format(intervals[self.p.timeframe]))
          322         urlargs.append('events=history')
          323         urlargs.append('crumb={}'.format(crumb))
      
      
      
      posted in General Code/Help
      S
      Sasha
    • RE: Problem with multiple stocks.

      @backtrader Thanks. You helped me a lot

      ----------------------------------------
      2016-02-16, BUY CREATE LABU in long , 25.4, SMA: 23.916
      2016-02-16, Number of stocks: 1850.0
      2016-02-17, BUY EXECUTED, Price: 28.60, Cost: 52910.00, Comm 0.00
      2016-02-17, Cash 47090.00, Value 100000.00
      ----------------------------------------
      2016-02-17, SELL CREATE LABU in long, 28.60, SMA: 24.13, DIFF: 12.60
      2016-02-17, Number of stocks 1850.00
      2016-02-18, SELL EXECUTED, Price: 25.68, Profit 47508.00, Comm 0.00
      2016-02-18, OPERATION PROFIT, GROSS -5402.00, NET -5402.00, SUM -5402.00
      ----------------------------------------
      2016-02-18, BUY CREATE LABU in long , 25.68, SMA: 24.016
      2016-02-18, Number of stocks: 1731.0
      2016-02-19, BUY EXECUTED, Price: 26.96, Cost: 46667.76, Comm 0.00
      2016-02-19, Cash 47930.24, Value 94598.00
      ----------------------------------------
      2016-02-19, BUY CREATE TQQQ in long , 13.8, SMA: 12.718
      2016-02-19, Number of stocks: 1632.0
      2016-02-22, BUY EXECUTED, Price: 14.45, Cost: 23582.40, Comm 0.00
      2016-02-22, Cash 24347.84, Value 96052.04
      2016-02-22, Cash 24347.84, Value 96052.04
      ----------------------------------------
      2016-02-22, SELL CREATE LABU in long, 27.80, SMA: 24.14, DIFF: 8.26
      2016-02-22, Number of stocks 1632.00
      2016-02-23, SELL EXECUTED, Price: 24.20, Profit 39494.40, Comm 0.00
      2016-02-23, Cash 63842.24, Value 88645.40
      ----------------------------------------
      2016-02-23, SELL CREATE LABU in long, 24.20, SMA: 24.47, DIFF: 2320.00
      2016-02-23, Number of stocks 1632.00
      2016-02-24, SELL EXECUTED, Price: 24.76, Profit 40408.32, Comm 0.00
      2016-02-24, OPERATION PROFIT, GROSS -4722.12, NET -4722.12, SUM -10124.12
      2016-02-24, Cash 104250.56, Value 89369.96
      

      The problem was with the size of stocks.

      posted in General Code/Help
      S
      Sasha
    • RE: Problem with multiple stocks.

      @backtrader said in Problem with multiple stocks.:

      you only have SELL orders. How do you expect a trade to be closed if you don't buy what you have sold?
      Sorry, output was not correct.
      Correct output:

      2016-02-16, BUY CREATE LABU in long , 25.4, SMA: 23.916
      2016-02-16, Number of stocks: 1850.0
      2016-02-17, BUY EXECUTED, Price: 28.60, Cost: 52910.00, Comm 0.00
      2016-02-17, Cash 47090.00, Value 100000.00
      2016-02-17, SELL CREATE LABU in long, 28.60, SMA: 24.13, DIFF: 12.60
      2016-02-18, SELL EXECUTED, Price: 25.68, Profit 47508.00, Comm 0.00
      2016-02-18, OPERATION PROFIT, GROSS -5402.00, NET -5402.00, SUM -5402.00
      2016-02-18, BUY CREATE LABU in long , 25.68, SMA: 24.016
      2016-02-18, Number of stocks: 1731.0
      2016-02-19, BUY EXECUTED, Price: 26.96, Cost: 46667.76, Comm 0.00
      2016-02-19, Cash 47930.24, Value 94598.00
      2016-02-19, BUY CREATE TQQQ in long , 13.8, SMA: 12.718
      2016-02-19, Number of stocks: 1632.0
      2016-02-22, BUY EXECUTED, Price: 14.45, Cost: 23582.40, Comm 0.00
      2016-02-22, Cash 24347.84, Value 96052.04
      2016-02-22, Cash 24347.84, Value 96052.04
      2016-02-22, SELL CREATE LABU in long, 27.80, SMA: 24.14, DIFF: 8.26
      2016-02-23, SELL EXECUTED, Price: 24.20, Profit 39494.40, Comm 0.00
      2016-02-23, Cash 63842.24, Value 88645.40
      2016-02-23, SELL CREATE LABU in long, 24.20, SMA: 24.47, DIFF: 2320.00
      2016-02-24, SELL EXECUTED, Price: 24.76, Profit 40408.32, Comm 0.00
      2016-02-24, OPERATION PROFIT, GROSS -4722.12, NET -4722.12, SUM -10124.12
      2016-02-24, Cash 104250.56, Value 89369.96
      2016-02-24, SELL CREATE LABU in long, 24.76, SMA: 24.83, DIFF: 2376.00
      2016-02-25, SELL EXECUTED, Price: 24.24, Profit 39559.68, Comm 0.00
      2016-02-25, Cash 143810.24, Value 90787.28
      2016-02-25, SELL CREATE LABU in long, 24.24, SMA: 25.12, DIFF: 2324.00
      2016-02-26, SELL EXECUTED, Price: 25.52, Profit 41648.64, Comm 0.00
      2016-02-26, Cash 185458.88, Value 86687.12
      2016-02-26, SELL CREATE LABU in long, 25.52, SMA: 25.59, DIFF: 2452.00
      2016-02-29, SELL EXECUTED, Price: 23.52, Profit 38384.64, Comm 0.00
      2016-02-29, Cash 223843.52, Value 95660.96
      2016-02-29, SELL CREATE LABU in long, 23.52, SMA: 25.67, DIFF: 2252.00
      Start capital: 100000.00
      Final Portfolio Value: 95660.96
      
      

      I have both buy and sell orders.
      This code will work correctly if I use only one stock. If I use for several stocks, there is a problem with sell orders. Above it can be seen that until the stock TQQQ occurs, the LABU is trading successfully. When two stocks are combined, an error occurs.
      I realized that after the SELL the trade does not close, I can not understand why it is re-executed. What do I need to fix in the code?

      posted in General Code/Help
      S
      Sasha
    • Problem with multiple stocks.

      Hi, I am new at Backtrader.
      I have two stocks and use a simple algorithm in the long position
      If I buy and sell in that order
      self.buy(first_stock)
      self.sell(first_stock)
      self.buy(second_stock)
      self.sell(second_stock)

      The code works correctly and after each sell operation notify_trade(self, trade) function is called and print profit etc.
      I have problem,when this situation happens
      self.buy(first_stock)
      self.buy(second_stock)
      self.sell(first_stock)
      self.sell(second_stock)
      In this situation notify_trade(self, trade) function is not called

      from __future__ import (absolute_import, division, print_function,
                              unicode_literals)
      
      import datetime  # For datetime objects
      import os 
      
      
      # Import the backtrader platform
      import backtrader as bt
      import numpy as np
      
      # Create a Stratey
      class TestStrategy(bt.Strategy):
          params = (
              ('maperiod', 10),
              ('oneplot', True),
              #('order_pct', round(0.95/len(self.datas),2)),
          )
      
          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):
              
              # Keep a reference to the "close" line in the data[0] dataseries
              self.dataclose = self.datas[0].close
      
              # To keep track of pending orders and buy price/commission
              self.order = None
              self.buyprice = None
              self.buycomm = None
              
              self.buy_price = [1,1]
              self.entry = None
              self.sum = 0
              self.pos = [None, None]
              self.trading = False
              self.lose = 0
              
              self.order_pct = round(0.95/len(self.datas),2)
              
              # self.number_stake = 100
              # Add a MovingAverageSimple indicator
              self.sma = []
              
              for i, d in enumerate(self.datas):
                  self.sma.append(bt.indicators.SimpleMovingAverage(
                      self.datas[i], period=self.params.maperiod))    
                      
          def next(self):
              for i, d in enumerate(self.datas):
                  #self.log('{}, {}'.format(d._name, self.buy_price[i]))
                  dt, dn = self.datetime.date(), d._name
                  pos = self.getposition(d).size
                   
                  day_close = d.close[0]
                  date = d.datetime.date
                  symbol = d._name
                  
                  if self.order:
                      return
      
                  if not pos:
                      if (day_close > self.sma[i][0]) :
      
                          # BUY, BUY, BUY!!! (with all possible default parameters)
                          diff = ((day_close/self.sma[i][0])-1)*100
                          
                          self.log("BUY CREATE {} in long , {}, SMA: {}".format(
                                    d._name,
                                    day_close, 
                                    self.sma[i][0]))
                          
                          # Keep track of the created order to avoid a 2nd order
                          amount_to_invest = (self.order_pct * self.broker.cash)
                          self.size = amount_to_invest // day_close
      
                          self.order = self.buy(data=d, exectype=bt.Order.Close, size=self.size)
      
                          self.buy_price[i] = day_close
      
                          self.log(('Number of stocks: {}').format(self.size))
      
                  else:
                      if ((day_close > self.buy_price[i] * 1.06) or
                          (day_close < self.buy_price[i] * 0.97)) :
                          # SELL, SELL, SELL!!! (with all possible default parameters)
                          
                          self.log('SELL CREATE %s in long, %.2f, SMA: %.2f, DIFF: %.2f' % 
                                   (d._name,
                                    day_close, 
                                    self.sma[i][0], 
                                    ( (day_close/self.buy_price[i])-1)*100 ))
      
                          # Keep track of the created order to avoid a 2nd order
                          self.order = self.sell(data=d, exectype=bt.Order.Close, size=self.size)
                          self.buy_price[i] = 1
                              
                          
          def notify_order(self, order):
              if order.status in [order.Submitted, order.Accepted]:
                  # Buy/Sell order submitted/accepted to/by broker - Nothing to do
                  return
      
              # Check if an order has been completed
              # Attention: broker could reject order if not enough cash
              if order.status in [order.Completed]:
                  # order.executed.value = order.executed.price * self.p.stake
                  if order.isbuy():
                      self.log(
                          'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
                          (order.executed.price,
                           order.executed.price * self.size,
                           order.executed.comm))
      
                      self.buyprice = order.executed.price
                      self.buycomm = order.executed.comm
                  else:  # Sell
                      self.log('SELL EXECUTED, Price: %.2f, Profit %.2f, Comm %.2f' %
                               (order.executed.price,
                                #order.executed.value,
                                order.executed.price * self.size,
                                order.executed.comm))
      
                  self.bar_executed = len(self)
              elif order.status in [order.Canceled, order.Margin, order.Rejected]:
                  self.log('Order Canceled/Margin/Rejected')
      
              # Write down: no pending order
              self.order = None
      
          def notify_trade(self, trade):
              if not trade.isclosed:
                  return
              
              self.sum+=trade.pnlcomm
              
              
              self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f, SUM %.2f' %
                       (trade.pnl, trade.pnlcomm, self.sum))
              
          def notify_cashvalue(self,cash, value):
              for i in self.buy_price:
                  if i != 1 or self.trading:
                      self.log('Cash %.2f, Value %.2f' % (cash, value))
      
      
      if __name__ == '__main__':
          
          def read_data(file_name):    
              datapath = os.path.abspath(os.getcwd() + '/datas/' + file_name)
              return bt.feeds.YahooFinanceCSVData(
                      dataname=datapath,
                      # Do not pass values before this date
                      fromdate=datetime.datetime(2016, 2, 1),
                      # Do not pass values before this date
                      todate=datetime.datetime(2016, 3, 1),
                      # Do not pass values after this date
                      reverse=False)
          
              
          # Create a cerebro entity
          cerebro = bt.Cerebro()
      
              
          # Add a strategy
          strategy = TestStrategy
          cerebro.addstrategy(strategy)
      
          # Datas are in a subfolder of the samples. Need to find where the script is
          # because it could have been called from anywhere
      
          data0 = read_data('labu.csv')
          data1 = read_data('TQQQ.csv')
          data2 = read_data('SOXL.csv')
          
          # Add the Data Feed to Cerebro
          cerebro.adddata(data0, name='LABU')
          
          data1.plotinfo.plotmaster = data0
          cerebro.adddata(data1, name='TQQQ')
      
      #     data2.plotinfo.plotmaster = data0
      #     cerebro.adddata(data2, name='data2')
      
          #cerebro.addobserver(bt.observers.DrawDown)
          #cerebro.adddata(get_data(datapath0))
          # Set our desired cash start
          cerebro.broker.setcash(100000.0)
      
          # Add a FixedSize sizer according to the stake
          cerebro.addsizer(bt.sizers.FixedSize)
      
          # Set the commission
          cerebro.broker.setcommission(commission=0.00)
      
          start_value = cerebro.broker.getvalue()
          # Run over everything
          results = cerebro.run()
          strat = results[0]
      
          # Print out the starting conditions
          print('Start capital: %.2f' % start_value)
          # Print out the final result
          print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
          
      
      

      Output:

      2016-02-17, SELL CREATE labu in long, 28.60, SMA: 24.13, DIFF: 12.60
      2016-02-18, SELL EXECUTED, Price: 25.68, Profit 47508.00, Comm 0.00
      2016-02-18, OPERATION PROFIT, GROSS -5402.00, NET -5402.00, SUM -5402.00
      2016-02-18, BUY CREATE 0 in long , 25.68, SMA: 24.016
      2016-02-18, Number of stocks: 1731.0
      2016-02-19, BUY EXECUTED, Price: 26.96, Cost: 46667.76, Comm 0.00
      2016-02-19, Cash 47930.24, Value 94598.00
      2016-02-19, BUY CREATE 1 in long , 13.8, SMA: 12.718
      2016-02-19, Number of stocks: 1632.0
      2016-02-22, BUY EXECUTED, Price: 14.45, Cost: 23582.40, Comm 0.00
      2016-02-22, Cash 24347.84, Value 96052.04
      2016-02-22, Cash 24347.84, Value 96052.04
      2016-02-22, SELL CREATE labu in long, 27.80, SMA: 24.14, DIFF: 8.26
      2016-02-23, SELL EXECUTED, Price: 24.20, Profit 39494.40, Comm 0.00
      2016-02-23, Cash 63842.24, Value 88645.40
      2016-02-23, SELL CREATE labu in long, 24.20, SMA: 24.47, DIFF: 2320.00
      2016-02-24, SELL EXECUTED, Price: 24.76, Profit 40408.32, Comm 0.00
      2016-02-24, OPERATION PROFIT, GROSS -4722.12, NET -4722.12, SUM -10124.12
      2016-02-24, Cash 104250.56, Value 89369.96
      2016-02-24, SELL CREATE labu in long, 24.76, SMA: 24.83, DIFF: 2376.00
      2016-02-25, SELL EXECUTED, Price: 24.24, Profit 39559.68, Comm 0.00
      2016-02-25, Cash 143810.24, Value 90787.28
      2016-02-25, SELL CREATE labu in long, 24.24, SMA: 25.12, DIFF: 2324.00
      2016-02-26, SELL EXECUTED, Price: 25.52, Profit 41648.64, Comm 0.00
      2016-02-26, Cash 185458.88, Value 86687.12
      2016-02-26, SELL CREATE labu in long, 25.52, SMA: 25.59, DIFF: 2452.00
      2016-02-29, SELL EXECUTED, Price: 23.52, Profit 38384.64, Comm 0.00
      2016-02-29, Cash 223843.52, Value 95660.96
      2016-02-29, SELL CREATE labu in long, 23.52, SMA: 25.67, DIFF: 2252.00
      
      posted in General Code/Help
      S
      Sasha