Dear all
I want to do short selling. I search a documents to do short selling, but I can`t find it. Could you tell me how to do?
Best posts made by Reo Inoue
-
How to do short selling
Latest posts made by Reo Inoue
-
How to do short selling
Dear all
I want to do short selling. I search a documents to do short selling, but I can`t find it. Could you tell me how to do? -
RE: How to buy maximum stakes?
@ab_trader @backtrader
Thank you for your advises. I try to use BarReplayer_Open , but I can not understand how to use it.
So I use cerebro.broker.set_coc(True), and I can get hopeful output. I put code and output in following.
・Codefrom __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 import backtrader.analyzers as btanalyzers import math import pandas as pd import talib import numpy as np PARAMS = ( ('maperiod', 15), #moving average period ('period', 15), # ('willperiod', 14), #moving average period ('sizer', None), ) class TestStrategy(bt.Strategy): params = PARAMS 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.order = self.order_target_percent(target=0.1) self.buyprice = None self.buycomm = None self.sma = bt.indicators.SimpleMovingAverage(self.datas[0], period=self.params.maperiod) 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 enougth cash if order.status in [order.Completed, order.Canceled, order.Margin]: if order.isbuy(): self.log( 'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.price, order.executed.value, order.executed.comm)) self.buyprice = order.executed.price self.buycomm = order.executed.comm else: # Sell self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.price, order.executed.value, order.executed.comm)) self.bar_executed = len(self) # Write down: no pending order self.order = None def notify_trade(self, trade): if not trade.isclosed: return self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' % (trade.pnl, trade.pnlcomm)) def next(self): # Simply log the closing price of the series from the reference # Check if an order is pending ... if yes, we cannot send a 2nd one if self.order: return # Check if we are in the market if not self.position: # Not yet ... we MIGHT BUY if ... if self.dataclose[0] > self.sma[0]: # BUY, BUY, BUY!!! (with all possible default parameters) self.log('BUY CREATE, %.2f' % self.dataclose[0]) # Keep track of the created order to avoid a 2nd order self.order = self.buy() else: if self.dataclose[0] < self.sma[0]: # SELL, SELL, SELL!!! (with all possible default parameters) self.log('SELL CREATE, %.2f' % self.dataclose[0]) # Keep track of the created order to avoid a 2nd order self.order = self.sell() class LongOnly(bt.Sizer): params = (('stake', 1),) def _getsizing(self, comminfo, cash, data, isbuy): if isbuy: divide = math.floor(cash/data.close[0]) # divide = math.floor(cash/data.open[1]) self.p.stake = divide # print(self.p.stake) # print(math.floor(cash/data.close[0])) return self.p.stake # Sell situation position = self.broker.getposition(data) if not position.size: return 0 # do not sell if nothing is open return self.p.stake if __name__ == '__main__': cerebro = bt.Cerebro() # Add a strategy cerebro.addstrategy(TestStrategy) # Add Sizer cerebro.addsizer(LongOnly) # cerebro.broker.set_coc(True) # because it could have been called from anywhere modpath = os.path.dirname(os.path.abspath(sys.argv[0])) datapath = os.path.join('datas/orcl-1995-2014.txt') # Create a Data Feed data = bt.feeds.YahooFinanceCSVData( dataname=datapath, # Do not pass values before this date fromdate=datetime.datetime(2000, 1, 1), # Do not pass values before this date todate=datetime.datetime(2000, 12, 31), # 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) # In order to buy on open you may want to # bt.filters.BarReplayer_Open(data) cerebro.broker.set_coc(True) # Set the commission cerebro.broker.setcommission(commission=0.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()) # Plot the result cerebro.plot()
・Output
Starting Portfolio Value: 100000.00 2000-01-25, BUY CREATE, 26.61 2000-01-26, BUY EXECUTED, Price: 26.61, Cost: 99973.77, Comm 0.00 2000-01-27, SELL CREATE, 24.43 2000-01-28, SELL EXECUTED, Price: 24.43, Cost: 99973.77, Comm 0.00 2000-01-28, OPERATION PROFIT, GROSS -8190.26, NET -8190.26 2000-02-02, BUY CREATE, 25.61 2000-02-03, BUY EXECUTED, Price: 25.61, Cost: 91786.24, Comm 0.00 2000-02-18, SELL CREATE, 27.61 2000-02-22, SELL EXECUTED, Price: 27.61, Cost: 91786.24, Comm 0.00 2000-02-22, OPERATION PROFIT, GROSS 7168.00, NET 7168.00 2000-02-22, BUY CREATE, 27.97 2000-02-23, BUY EXECUTED, Price: 27.97, Cost: 98957.86, Comm 0.00 2000-03-30, SELL CREATE, 36.98 2000-03-31, SELL EXECUTED, Price: 36.98, Cost: 98957.86, Comm 0.00 2000-03-31, OPERATION PROFIT, GROSS 31877.38, NET 31877.38 2000-04-06, BUY CREATE, 38.75 2000-04-07, BUY EXECUTED, Price: 38.75, Cost: 130820.00, Comm 0.00 2000-04-11, SELL CREATE, 36.48 2000-04-12, SELL EXECUTED, Price: 36.48, Cost: 130820.00, Comm 0.00 2000-04-12, OPERATION PROFIT, GROSS -7663.52, NET -7663.52 2000-04-18, BUY CREATE, 37.22 2000-04-19, BUY EXECUTED, Price: 37.22, Cost: 123160.98, Comm 0.00 2000-04-19, SELL CREATE, 35.16 2000-04-20, SELL EXECUTED, Price: 35.16, Cost: 123160.98, Comm 0.00 2000-04-20, OPERATION PROFIT, GROSS -6816.54, NET -6816.54 2000-04-27, BUY CREATE, 36.45 2000-04-28, BUY EXECUTED, Price: 36.45, Cost: 116348.40, Comm 0.00 2000-05-04, SELL CREATE, 35.01 2000-05-05, SELL EXECUTED, Price: 35.01, Cost: 116348.40, Comm 0.00 2000-05-05, OPERATION PROFIT, GROSS -4596.48, NET -4596.48 2000-05-05, BUY CREATE, 36.22 2000-05-08, BUY EXECUTED, Price: 36.22, Cost: 111774.92, Comm 0.00 2000-05-08, SELL CREATE, 34.10 2000-05-09, SELL EXECUTED, Price: 34.10, Cost: 111774.92, Comm 0.00 2000-05-09, OPERATION PROFIT, GROSS -6542.32, NET -6542.32 2000-05-15, BUY CREATE, 36.31 2000-05-16, BUY EXECUTED, Price: 36.31, Cost: 105226.38, Comm 0.00 2000-05-18, SELL CREATE, 34.45 2000-05-19, SELL EXECUTED, Price: 34.45, Cost: 105226.38, Comm 0.00 2000-05-19, OPERATION PROFIT, GROSS -5390.28, NET -5390.28 2000-05-30, BUY CREATE, 34.98 2000-05-31, BUY EXECUTED, Price: 34.98, Cost: 99832.92, Comm 0.00 2000-06-22, SELL CREATE, 38.43 2000-06-23, SELL EXECUTED, Price: 38.43, Cost: 99832.92, Comm 0.00 2000-06-23, OPERATION PROFIT, GROSS 9846.30, NET 9846.30 2000-06-26, BUY CREATE, 38.99 2000-06-27, BUY EXECUTED, Price: 38.99, Cost: 109678.87, Comm 0.00 2000-06-27, SELL CREATE, 38.78 2000-06-28, SELL EXECUTED, Price: 38.78, Cost: 109678.87, Comm 0.00 2000-06-28, OPERATION PROFIT, GROSS -590.73, NET -590.73 2000-06-28, BUY CREATE, 39.11 2000-06-29, BUY EXECUTED, Price: 39.11, Cost: 109077.79, Comm 0.00 2000-06-29, SELL CREATE, 38.13 2000-06-30, SELL EXECUTED, Price: 38.13, Cost: 109077.79, Comm 0.00 2000-06-30, OPERATION PROFIT, GROSS -2733.22, NET -2733.22 2000-06-30, BUY CREATE, 39.64 2000-07-03, BUY EXECUTED, Price: 39.64, Cost: 106354.12, Comm 0.00 2000-07-03, SELL CREATE, 37.81 2000-07-05, SELL EXECUTED, Price: 37.81, Cost: 106354.12, Comm 0.00 2000-07-05, OPERATION PROFIT, GROSS -4909.89, NET -4909.89 2000-07-20, BUY CREATE, 36.84 2000-07-21, BUY EXECUTED, Price: 36.84, Cost: 101457.36, Comm 0.00 2000-07-21, SELL CREATE, 35.57 2000-07-24, SELL EXECUTED, Price: 35.57, Cost: 101457.36, Comm 0.00 2000-07-24, OPERATION PROFIT, GROSS -3497.58, NET -3497.58 2000-07-25, BUY CREATE, 35.83 2000-07-26, BUY EXECUTED, Price: 35.83, Cost: 97959.22, Comm 0.00 2000-07-27, SELL CREATE, 35.39 2000-07-28, SELL EXECUTED, Price: 35.39, Cost: 97959.22, Comm 0.00 2000-07-28, OPERATION PROFIT, GROSS -1202.96, NET -1202.96 2000-07-31, BUY CREATE, 35.45 2000-08-01, BUY EXECUTED, Price: 35.45, Cost: 96743.05, Comm 0.00 2000-08-01, SELL CREATE, 34.48 2000-08-02, SELL EXECUTED, Price: 34.48, Cost: 96743.05, Comm 0.00 2000-08-02, OPERATION PROFIT, GROSS -2647.13, NET -2647.13 2000-08-03, BUY CREATE, 36.51 2000-08-04, BUY EXECUTED, Price: 36.51, Cost: 94086.27, Comm 0.00 2000-09-08, SELL CREATE, 40.81 2000-09-11, SELL EXECUTED, Price: 40.81, Cost: 94086.27, Comm 0.00 2000-09-11, OPERATION PROFIT, GROSS 11081.10, NET 11081.10 2000-09-28, BUY CREATE, 38.42 2000-09-29, BUY EXECUTED, Price: 38.42, Cost: 105155.54, Comm 0.00 2000-09-29, SELL CREATE, 37.13 2000-10-02, SELL EXECUTED, Price: 37.13, Cost: 105155.54, Comm 0.00 2000-10-02, OPERATION PROFIT, GROSS -3530.73, NET -3530.73 2000-10-19, BUY CREATE, 34.30 2000-10-20, BUY EXECUTED, Price: 34.30, Cost: 101630.90, Comm 0.00 2000-10-30, SELL CREATE, 29.82 2000-10-31, SELL EXECUTED, Price: 29.82, Cost: 101630.90, Comm 0.00 2000-10-31, OPERATION PROFIT, GROSS -13274.24, NET -13274.24 2000-11-17, BUY CREATE, 27.17 2000-11-20, BUY EXECUTED, Price: 27.17, Cost: 88384.01, Comm 0.00 2000-11-20, SELL CREATE, 23.34 2000-11-21, SELL EXECUTED, Price: 23.34, Cost: 88384.01, Comm 0.00 2000-11-21, OPERATION PROFIT, GROSS -12458.99, NET -12458.99 2000-11-30, BUY CREATE, 24.99 2000-12-01, BUY EXECUTED, Price: 24.99, Cost: 75919.62, Comm 0.00 2000-12-14, SELL CREATE, 25.93 2000-12-15, SELL EXECUTED, Price: 25.93, Cost: 75919.62, Comm 0.00 2000-12-15, OPERATION PROFIT, GROSS 2855.72, NET 2855.72 2000-12-15, BUY CREATE, 26.93 2000-12-18, BUY EXECUTED, Price: 26.93, Cost: 78770.25, Comm 0.00 2000-12-20, SELL CREATE, 26.88 2000-12-21, SELL EXECUTED, Price: 26.88, Cost: 78770.25, Comm 0.00 2000-12-21, OPERATION PROFIT, GROSS -146.25, NET -146.25 2000-12-21, BUY CREATE, 27.82 2000-12-22, BUY EXECUTED, Price: 27.82, Cost: 78619.32, Comm 0.00 2000-12-29, SELL CREATE, 27.41 Final Portfolio Value: 77478.72
-
RE: How to buy maximum stakes?
@backtrader
The wrong points are ,BUY CREATED in succession. I put the range in following. I think BUY and SELL is created each other.2000-11-20, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-11-30, BUY CREATE, 24.99 2000-12-01, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-12-01, BUY CREATE, 24.93 2000-12-04, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-12-04, BUY CREATE, 26.58 2000-12-05, BUY EXECUTED, Price: 27.76, Cost: 114149.12, Comm 0.00 2000-12-14, SELL CREATE, 25.93
-
RE: How to buy maximum stakes?
I check the version, and it was old. So I reinstall backtrader==1.9.43.116 and I run my program in following. It is still strange output. Are there any problems?
Starting Portfolio Value: 100000.00 2000-01-25, BUY CREATE, 26.61 2000-01-26, BUY EXECUTED, Price: 26.76, Cost: 99975.36, Comm 0.00 2000-01-27, SELL CREATE, 24.43 2000-01-28, SELL EXECUTED, Price: 24.28, Cost: 99975.36, Comm 0.00 2000-01-28, OPERATION PROFIT, GROSS -9265.28, NET -9265.28 2000-02-02, BUY CREATE, 25.61 2000-02-03, BUY EXECUTED, Price: 26.11, Cost: 90732.25, Comm 0.00 2000-02-18, SELL CREATE, 27.61 2000-02-22, SELL EXECUTED, Price: 27.88, Cost: 90732.25, Comm 0.00 2000-02-22, OPERATION PROFIT, GROSS 6150.75, NET 6150.75 2000-02-22, BUY CREATE, 27.97 2000-02-23, BUY EXECUTED, Price: 28.38, Cost: 96860.94, Comm 0.00 2000-03-30, SELL CREATE, 36.98 2000-03-31, SELL EXECUTED, Price: 37.81, Cost: 96860.94, Comm 0.00 2000-03-31, OPERATION PROFIT, GROSS 32184.59, NET 32184.59 2000-04-06, BUY CREATE, 38.75 2000-04-07, BUY EXECUTED, Price: 39.46, Cost: 129034.20, Comm 0.00 2000-04-11, SELL CREATE, 36.48 2000-04-12, SELL EXECUTED, Price: 36.75, Cost: 129034.20, Comm 0.00 2000-04-12, OPERATION PROFIT, GROSS -8861.70, NET -8861.70 2000-04-18, BUY CREATE, 37.22 2000-04-19, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-04-27, BUY CREATE, 36.45 2000-04-28, BUY EXECUTED, Price: 37.01, Cost: 120171.47, Comm 0.00 2000-05-04, SELL CREATE, 35.01 2000-05-05, SELL EXECUTED, Price: 34.89, Cost: 120171.47, Comm 0.00 2000-05-05, OPERATION PROFIT, GROSS -6883.64, NET -6883.64 2000-05-05, BUY CREATE, 36.22 2000-05-08, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-05-15, BUY CREATE, 36.31 2000-05-16, BUY EXECUTED, Price: 36.60, Cost: 113313.60, Comm 0.00 2000-05-18, SELL CREATE, 34.45 2000-05-19, SELL EXECUTED, Price: 33.94, Cost: 113313.60, Comm 0.00 2000-05-19, OPERATION PROFIT, GROSS -8235.36, NET -8235.36 2000-05-30, BUY CREATE, 34.98 2000-05-31, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-05-31, BUY CREATE, 33.89 2000-06-01, BUY EXECUTED, Price: 34.86, Cost: 105068.04, Comm 0.00 2000-06-22, SELL CREATE, 38.43 2000-06-23, SELL EXECUTED, Price: 38.10, Cost: 105068.04, Comm 0.00 2000-06-23, OPERATION PROFIT, GROSS 9765.36, NET 9765.36 2000-06-26, BUY CREATE, 38.99 2000-06-27, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-06-28, BUY CREATE, 39.11 2000-06-29, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-06-30, BUY CREATE, 39.64 2000-07-03, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-07-20, BUY CREATE, 36.84 2000-07-21, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-07-25, BUY CREATE, 35.83 2000-07-26, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-07-26, BUY CREATE, 36.19 2000-07-27, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-07-31, BUY CREATE, 35.45 2000-08-01, BUY EXECUTED, Price: 35.46, Cost: 114819.48, Comm 0.00 2000-08-01, SELL CREATE, 34.48 2000-08-02, SELL EXECUTED, Price: 34.42, Cost: 114819.48, Comm 0.00 2000-08-02, OPERATION PROFIT, GROSS -3367.52, NET -3367.52 2000-08-03, BUY CREATE, 36.51 2000-08-04, BUY EXECUTED, Price: 36.93, Cost: 111454.74, Comm 0.00 2000-09-08, SELL CREATE, 40.81 2000-09-11, SELL EXECUTED, Price: 40.58, Cost: 111454.74, Comm 0.00 2000-09-11, OPERATION PROFIT, GROSS 11015.70, NET 11015.70 2000-09-28, BUY CREATE, 38.42 2000-09-29, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-10-19, BUY CREATE, 34.30 2000-10-20, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-10-20, BUY CREATE, 33.24 2000-10-23, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-10-23, BUY CREATE, 32.12 2000-10-24, BUY EXECUTED, Price: 33.01, Cost: 122500.11, Comm 0.00 2000-10-30, SELL CREATE, 29.82 2000-10-31, SELL EXECUTED, Price: 30.76, Cost: 122500.11, Comm 0.00 2000-10-31, OPERATION PROFIT, GROSS -8349.75, NET -8349.75 2000-11-17, BUY CREATE, 27.17 2000-11-20, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-11-30, BUY CREATE, 24.99 2000-12-01, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-12-01, BUY CREATE, 24.93 2000-12-04, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-12-04, BUY CREATE, 26.58 2000-12-05, BUY EXECUTED, Price: 27.76, Cost: 114149.12, Comm 0.00 2000-12-14, SELL CREATE, 25.93 2000-12-15, SELL EXECUTED, Price: 27.76, Cost: 114149.12, Comm 0.00 2000-12-15, OPERATION PROFIT, GROSS 0.00, NET 0.00 2000-12-15, BUY CREATE, 26.93 2000-12-18, BUY EXECUTED, Price: 28.29, Cost: 114150.15, Comm 0.00 2000-12-20, SELL CREATE, 26.88 2000-12-21, SELL EXECUTED, Price: 26.23, Cost: 114150.15, Comm 0.00 2000-12-21, OPERATION PROFIT, GROSS -8312.10, NET -8312.10 2000-12-21, BUY CREATE, 27.82 2000-12-22, BUY EXECUTED, Price: 28.65, Cost: 105833.10, Comm 0.00 2000-12-29, SELL CREATE, 27.41 Final Portfolio Value: 101260.49
-
RE: How to buy maximum stakes?
@ab_trader
I refer to https://community.backtrader.com/topic/217/order_target_percent-calculation-problemsreply, and I tried to use BarReplayer_Open. But i get the error following
Traceback (most recent call last): File "test_backtrader_nikkei_WVF2_question.py", line 159, in <module> cerebro.run() File "C:\Users\reoinoue\AppData\Local\Programs\Python\Python35\lib\site-packages\backtrader\cerebro.py", line 810, in run runstrat = self.runstrategies(iterstrat) File "C:\Users\reoinoue\AppData\Local\Programs\Python\Python35\lib\site-packages\backtrader\cerebro.py", line 934, in runstrategies self._runnext(runstrats) File "C:\Users\reoinoue\AppData\Local\Programs\Python\Python35\lib\site-packages\backtrader\cerebro.py", line 1242, in _runnext self._brokernotify() File "C:\Users\reoinoue\AppData\Local\Programs\Python\Python35\lib\site-packages\backtrader\cerebro.py", line 996, in _brokernotify self._broker.next() File "C:\Users\reoinoue\AppData\Local\Programs\Python\Python35\lib\site-packages\backtrader\brokers\bbroker.py", line 863, in next self._try_exec(order) File "C:\Users\reoinoue\AppData\Local\Programs\Python\Python35\lib\site-packages\backtrader\brokers\bbroker.py", line 809, in _try_exec popen = data.tick_open or data.open[0] File "C:\Users\reoinoue\AppData\Local\Programs\Python\Python35\lib\site-packages\backtrader\lineseries.py", line 429, in __getattr__ return getattr(self.lines, name) AttributeError: 'Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst' object has no attribute 'tick_open'
Also, i use the setting as divide = math.floor(cash/data.open[1])
, but it do not work property, I get the following output.Starting Portfolio Value: 100000.00 2000-01-25, BUY CREATE, 26.61 2000-01-26, BUY EXECUTED, Price: 26.76, Cost: 99975.36, Comm 0.00 2000-01-27, SELL CREATE, 24.43 2000-01-28, SELL EXECUTED, Price: 24.28, Cost: 99975.36, Comm 0.00 2000-01-28, OPERATION PROFIT, GROSS -9265.28, NET -9265.28 2000-02-02, BUY CREATE, 25.61 2000-02-03, BUY EXECUTED, Price: 26.11, Cost: 90732.25, Comm 0.00 2000-02-18, SELL CREATE, 27.61 2000-02-22, SELL EXECUTED, Price: 27.88, Cost: 90732.25, Comm 0.00 2000-02-22, OPERATION PROFIT, GROSS 6150.75, NET 6150.75 2000-02-22, BUY CREATE, 27.97 2000-02-23, BUY EXECUTED, Price: 28.38, Cost: 96860.94, Comm 0.00 2000-03-30, SELL CREATE, 36.98 2000-03-31, SELL EXECUTED, Price: 37.81, Cost: 96860.94, Comm 0.00 2000-03-31, OPERATION PROFIT, GROSS 32184.59, NET 32184.59 2000-04-06, BUY CREATE, 38.75 2000-04-07, BUY EXECUTED, Price: 39.46, Cost: 129034.20, Comm 0.00 2000-04-11, SELL CREATE, 36.48 2000-04-12, SELL EXECUTED, Price: 36.75, Cost: 129034.20, Comm 0.00 2000-04-12, OPERATION PROFIT, GROSS -8861.70, NET -8861.70 2000-04-18, BUY CREATE, 37.22 2000-04-19, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-04-27, BUY CREATE, 36.45 2000-04-28, BUY EXECUTED, Price: 37.01, Cost: 120171.47, Comm 0.00 2000-05-04, SELL CREATE, 35.01 2000-05-05, SELL EXECUTED, Price: 34.89, Cost: 120171.47, Comm 0.00 2000-05-05, OPERATION PROFIT, GROSS -6883.64, NET -6883.64 2000-05-05, BUY CREATE, 36.22 2000-05-08, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-05-15, BUY CREATE, 36.31 2000-05-16, BUY EXECUTED, Price: 36.60, Cost: 113313.60, Comm 0.00 2000-05-18, SELL CREATE, 34.45 2000-05-19, SELL EXECUTED, Price: 33.94, Cost: 113313.60, Comm 0.00 2000-05-19, OPERATION PROFIT, GROSS -8235.36, NET -8235.36 2000-05-30, BUY CREATE, 34.98 2000-05-31, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-05-31, BUY CREATE, 33.89 2000-06-01, BUY EXECUTED, Price: 34.86, Cost: 105068.04, Comm 0.00 2000-06-22, SELL CREATE, 38.43 2000-06-23, SELL EXECUTED, Price: 38.10, Cost: 105068.04, Comm 0.00 2000-06-23, OPERATION PROFIT, GROSS 9765.36, NET 9765.36 2000-06-26, BUY CREATE, 38.99 2000-06-27, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-06-28, BUY CREATE, 39.11 2000-06-29, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-06-30, BUY CREATE, 39.64 2000-07-03, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-07-20, BUY CREATE, 36.84 2000-07-21, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-07-25, BUY CREATE, 35.83 2000-07-26, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-07-26, BUY CREATE, 36.19 2000-07-27, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-07-31, BUY CREATE, 35.45 2000-08-01, BUY EXECUTED, Price: 35.46, Cost: 114819.48, Comm 0.00 2000-08-01, SELL CREATE, 34.48 2000-08-02, SELL EXECUTED, Price: 34.42, Cost: 114819.48, Comm 0.00 2000-08-02, OPERATION PROFIT, GROSS -3367.52, NET -3367.52 2000-08-03, BUY CREATE, 36.51 2000-08-04, BUY EXECUTED, Price: 36.93, Cost: 111454.74, Comm 0.00 2000-09-08, SELL CREATE, 40.81 2000-09-11, SELL EXECUTED, Price: 40.58, Cost: 111454.74, Comm 0.00 2000-09-11, OPERATION PROFIT, GROSS 11015.70, NET 11015.70 2000-09-28, BUY CREATE, 38.42 2000-09-29, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-10-19, BUY CREATE, 34.30 2000-10-20, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-10-20, BUY CREATE, 33.24 2000-10-23, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-10-23, BUY CREATE, 32.12 2000-10-24, BUY EXECUTED, Price: 33.01, Cost: 122500.11, Comm 0.00 2000-10-30, SELL CREATE, 29.82 2000-10-31, SELL EXECUTED, Price: 30.76, Cost: 122500.11, Comm 0.00 2000-10-31, OPERATION PROFIT, GROSS -8349.75, NET -8349.75 2000-11-17, BUY CREATE, 27.17 2000-11-20, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-11-30, BUY CREATE, 24.99 2000-12-01, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-12-01, BUY CREATE, 24.93 2000-12-04, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-12-04, BUY CREATE, 26.58 2000-12-05, BUY EXECUTED, Price: 27.76, Cost: 114149.12, Comm 0.00 2000-12-14, SELL CREATE, 25.93 2000-12-15, SELL EXECUTED, Price: 27.76, Cost: 114149.12, Comm 0.00 2000-12-15, OPERATION PROFIT, GROSS 0.00, NET 0.00 2000-12-15, BUY CREATE, 26.93 2000-12-18, BUY EXECUTED, Price: 28.29, Cost: 114150.15, Comm 0.00 2000-12-20, SELL CREATE, 26.88 2000-12-21, SELL EXECUTED, Price: 26.23, Cost: 114150.15, Comm 0.00 2000-12-21, OPERATION PROFIT, GROSS -8312.10, NET -8312.10 2000-12-21, BUY CREATE, 27.82 2000-12-22, BUY EXECUTED, Price: 28.65, Cost: 105833.10, Comm 0.00 2000-12-29, SELL CREATE, 27.41 Final Portfolio Value: 101260.49
How to write my code? I am so sorry to understand what you say exactly.
-
RE: How to buy maximum stakes?
Thank you for your reply.
I want to do that I decide to buy or sell based on close, and I order the maximum size based on next day open price.
I refer to ab_trader link, and I understand BarReplayer_Open is based on only open price.
So I refine the following code based on next open prices. And I have a question. How to get next open price in def _getsizing? Could you teach me?class LongOnly(bt.Sizer): params = (('stake', 1),) def _getsizing(self, comminfo, cash, data, isbuy): if isbuy: divide = math.floor(cash/data.close[0]) self.p.stake = divide return self.p.stake # Sell situation position = self.broker.getposition(data) if not position.size: return 0 # do not sell if nothing is open return self.p.stake
-
RE: How to buy maximum stakes?
I`m sorry to understand hugely difficul. I am not used to use these interface.
I rewrite the code and output following.
I have why are there miss print out?- Code
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 # To keep track of pending orders and buy price/commission self.order = None self.buyprice = None self.buycomm = None self.sma = bt.indicators.SimpleMovingAverage(self.datas[0], period=self.params.maperiod) 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 * * list item # Check if an order has been completed # Attention: broker could reject order if not enougth cash if order.status in [order.Completed, order.Canceled, order.Margin]: if order.isbuy(): self.log( 'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.price, order.executed.value, order.executed.comm)) self.buyprice = order.executed.price self.buycomm = order.executed.comm else: # Sell self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.price, order.executed.value, order.executed.comm)) self.bar_executed = len(self) # Write down: no pending order self.order = None def notify_trade(self, trade): if not trade.isclosed: return self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' % (trade.pnl, trade.pnlcomm)) def next(self): # Simply log the closing price of the series from the reference # Check if an order is pending ... if yes, we cannot send a 2nd one if self.order: return # Check if we are in the market if not self.position: # Not yet ... we MIGHT BUY if ... if self.dataclose[0] > self.sma[0]: # BUY, BUY, BUY!!! (with all possible default parameters) self.log('BUY CREATE, %.2f' % self.dataclose[0]) # Keep track of the created order to avoid a 2nd order self.order = self.buy() else: if self.dataclose[0] < self.sma[0]: # SELL, SELL, SELL!!! (with all possible default parameters) self.log('SELL CREATE, %.2f' % self.dataclose[0]) # Keep track of the created order to avoid a 2nd order self.order = self.sell() class LongOnly(bt.Sizer): params = (('stake', 1),) def _getsizing(self, comminfo, cash, data, isbuy): if isbuy: divide = math.floor(cash/data.close[0]) self.p.stake = divide return self.p.stake # Sell situation position = self.broker.getposition(data) if not position.size: return 0 # do not sell if nothing is open return self.p.stake if name == 'main': cerebro = bt.Cerebro() # Add a strategy cerebro.addstrategy(TestStrategy) # Add Sizer cerebro.addsizer(LongOnly) # because it could have been called from anywhere modpath = os.path.dirname(os.path.abspath(sys.argv[0])) datapath = os.path.join('datas/orcl-1995-2014.txt') # Create a Data Feed data = bt.feeds.YahooFinanceCSVData( dataname=datapath, # Do not pass values before this date fromdate=datetime.datetime(2000, 1, 1), # Do not pass values before this date todate=datetime.datetime(2000, 12, 31), # 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(1000.0) # Set the commission cerebro.broker.setcommission(commission=0.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()) # Plot the result cerebro.plot()
- Output
Starting Portfolio Value: 1000.00 2000-01-25, BUY CREATE, 26.61 2000-01-26, BUY EXECUTED, Price: 26.76, Cost: 990.12, Comm 0.00 2000-01-27, SELL CREATE, 24.43 2000-01-28, SELL EXECUTED, Price: 24.28, Cost: 990.12, Comm 0.00 2000-01-28, OPERATION PROFIT, GROSS -91.76, NET -91.76 2000-02-02, BUY CREATE, 25.61 2000-02-03, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-02-03, BUY CREATE, 26.73 2000-02-04, BUY EXECUTED, Price: 27.17, Cost: 896.61, Comm 0.00 2000-02-18, SELL CREATE, 27.61 2000-02-22, SELL EXECUTED, Price: 27.88, Cost: 896.61, Comm 0.00 2000-02-22, OPERATION PROFIT, GROSS 23.43, NET 23.43 2000-02-22, BUY CREATE, 27.97 2000-02-23, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-02-23, BUY CREATE, 29.73 2000-02-24, BUY EXECUTED, Price: 29.79, Cost: 923.49, Comm 0.00 2000-03-30, SELL CREATE, 36.98 2000-03-31, SELL EXECUTED, Price: 37.81, Cost: 923.49, Comm 0.00 2000-03-31, OPERATION PROFIT, GROSS 248.62, NET 248.62 2000-04-06, BUY CREATE, 38.75 2000-04-07, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-04-07, BUY CREATE, 41.08 2000-04-10, BUY EXECUTED, Price: 41.26, Cost: 1155.28, Comm 0.00 2000-04-11, SELL CREATE, 36.48 2000-04-12, SELL EXECUTED, Price: 36.75, Cost: 1155.28, Comm 0.00 2000-04-12, OPERATION PROFIT, GROSS -126.28, NET -126.28 2000-04-18, BUY CREATE, 37.22 2000-04-19, BUY EXECUTED, Price: 37.07, Cost: 1037.96, Comm 0.00 2000-04-19, SELL CREATE, 35.16 2000-04-20, SELL EXECUTED, Price: 34.80, Cost: 1037.96, Comm 0.00 2000-04-20, OPERATION PROFIT, GROSS -63.56, NET -63.56 2000-04-27, BUY CREATE, 36.45 2000-04-28, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-04-28, BUY CREATE, 37.69 2000-05-01, BUY EXECUTED, Price: 37.66, Cost: 979.16, Comm 0.00 2000-05-04, SELL CREATE, 35.01 2000-05-05, SELL EXECUTED, Price: 34.89, Cost: 979.16, Comm 0.00 2000-05-05, OPERATION PROFIT, GROSS -72.02, NET -72.02 2000-05-05, BUY CREATE, 36.22 2000-05-08, BUY EXECUTED, Price: 35.51, Cost: 887.75, Comm 0.00 2000-05-08, SELL CREATE, 34.10 2000-05-09, SELL EXECUTED, Price: 34.75, Cost: 887.75, Comm 0.00 2000-05-09, OPERATION PROFIT, GROSS -19.00, NET -19.00 2000-05-15, BUY CREATE, 36.31 2000-05-16, BUY EXECUTED, Price: 36.60, Cost: 878.40, Comm 0.00 2000-05-18, SELL CREATE, 34.45 2000-05-19, SELL EXECUTED, Price: 33.94, Cost: 878.40, Comm 0.00 2000-05-19, OPERATION PROFIT, GROSS -63.84, NET -63.84 2000-05-30, BUY CREATE, 34.98 2000-05-31, BUY EXECUTED, Price: 34.54, Cost: 794.42, Comm 0.00 2000-06-22, SELL CREATE, 38.43 2000-06-23, SELL EXECUTED, Price: 38.10, Cost: 794.42, Comm 0.00 2000-06-23, OPERATION PROFIT, GROSS 81.88, NET 81.88 2000-06-26, BUY CREATE, 38.99 2000-06-27, BUY EXECUTED, Price: 38.84, Cost: 893.32, Comm 0.00 2000-06-27, SELL CREATE, 38.78 2000-06-28, SELL EXECUTED, Price: 38.70, Cost: 893.32, Comm 0.00 2000-06-28, OPERATION PROFIT, GROSS -3.22, NET -3.22 2000-06-28, BUY CREATE, 39.11 2000-06-29, BUY EXECUTED, Price: 38.69, Cost: 889.87, Comm 0.00 2000-06-29, SELL CREATE, 38.13 2000-06-30, SELL EXECUTED, Price: 37.90, Cost: 889.87, Comm 0.00 2000-06-30, OPERATION PROFIT, GROSS -18.17, NET -18.17 2000-06-30, BUY CREATE, 39.64 2000-07-03, BUY EXECUTED, Price: 38.25, Cost: 841.50, Comm 0.00 2000-07-03, SELL CREATE, 37.81 2000-07-05, SELL EXECUTED, Price: 36.22, Cost: 841.50, Comm 0.00 2000-07-05, OPERATION PROFIT, GROSS -44.66, NET -44.66 2000-07-20, BUY CREATE, 36.84 2000-07-21, BUY EXECUTED, Price: 36.51, Cost: 839.73, Comm 0.00 2000-07-21, SELL CREATE, 35.57 2000-07-24, SELL EXECUTED, Price: 36.36, Cost: 839.73, Comm 0.00 2000-07-24, OPERATION PROFIT, GROSS -3.45, NET -3.45 2000-07-25, BUY CREATE, 35.83 2000-07-26, BUY EXECUTED, Price: 35.28, Cost: 811.44, Comm 0.00 2000-07-27, SELL CREATE, 35.39 2000-07-28, SELL EXECUTED, Price: 35.42, Cost: 811.44, Comm 0.00 2000-07-28, OPERATION PROFIT, GROSS 3.22, NET 3.22 2000-07-31, BUY CREATE, 35.45 2000-08-01, BUY EXECUTED, Price: 35.46, Cost: 851.04, Comm 0.00 2000-08-01, SELL CREATE, 34.48 2000-08-02, SELL EXECUTED, Price: 34.42, Cost: 851.04, Comm 0.00 2000-08-02, OPERATION PROFIT, GROSS -24.96, NET -24.96 2000-08-03, BUY CREATE, 36.51 2000-08-04, BUY EXECUTED, Price: 36.93, Cost: 812.46, Comm 0.00 2000-09-08, SELL CREATE, 40.81 2000-09-11, SELL EXECUTED, Price: 40.58, Cost: 812.46, Comm 0.00 2000-09-11, OPERATION PROFIT, GROSS 80.30, NET 80.30 2000-09-28, BUY CREATE, 38.42 2000-09-29, BUY EXECUTED, Price: 38.36, Cost: 882.28, Comm 0.00 2000-09-29, SELL CREATE, 37.13 2000-10-02, SELL EXECUTED, Price: 37.60, Cost: 882.28, Comm 0.00 2000-10-02, OPERATION PROFIT, GROSS -17.48, NET -17.48 2000-10-19, BUY CREATE, 34.30 2000-10-20, BUY EXECUTED, Price: 34.06, Cost: 851.50, Comm 0.00 2000-10-30, SELL CREATE, 29.82 2000-10-31, SELL EXECUTED, Price: 30.76, Cost: 851.50, Comm 0.00 2000-10-31, OPERATION PROFIT, GROSS -82.50, NET -82.50 2000-11-17, BUY CREATE, 27.17 2000-11-20, BUY EXECUTED, Price: 22.93, Cost: 664.97, Comm 0.00 2000-11-20, SELL CREATE, 23.34 2000-11-21, SELL EXECUTED, Price: 23.39, Cost: 664.97, Comm 0.00 2000-11-21, OPERATION PROFIT, GROSS 13.34, NET 13.34 2000-11-30, BUY CREATE, 24.99 2000-12-01, BUY EXECUTED, Price: 24.87, Cost: 795.84, Comm 0.00 2000-12-14, SELL CREATE, 25.93 2000-12-15, SELL EXECUTED, Price: 27.76, Cost: 795.84, Comm 0.00 2000-12-15, OPERATION PROFIT, GROSS 92.48, NET 92.48 2000-12-15, BUY CREATE, 26.93 2000-12-18, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-12-18, BUY CREATE, 30.18 2000-12-19, BUY EXECUTED, Price: 30.00, Cost: 900.00, Comm 0.00 2000-12-20, SELL CREATE, 26.88 2000-12-21, SELL EXECUTED, Price: 26.23, Cost: 900.00, Comm 0.00 2000-12-21, OPERATION PROFIT, GROSS -113.10, NET -113.10 2000-12-21, BUY CREATE, 27.82 2000-12-22, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-12-22, BUY CREATE, 30.06 2000-12-26, BUY EXECUTED, Price: 29.70, Cost: 772.20, Comm 0.00 2000-12-29, SELL CREATE, 27.41 Final Portfolio Value: 739.73
-
RE: How to buy maximum stakes?
Thank you for your reply.
I write the code following.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 math # Import the backtrader platform import backtrader as bt # Create a Stratey class TestStrategy(bt.Strategy): params = ( ('maperiod', 15), ('period', 15), ) 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.sma = bt.indicators.SimpleMovingAverage( self.datas[0], period=self.params.maperiod) 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 enougth cash if order.status in [order.Completed, order.Canceled, order.Margin]: if order.isbuy(): self.log( 'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.price, order.executed.value, order.executed.comm)) self.buyprice = order.executed.price self.buycomm = order.executed.comm else: # Sell self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.price, order.executed.value, order.executed.comm)) self.bar_executed = len(self) # Write down: no pending order self.order = None def notify_trade(self, trade): if not trade.isclosed: return self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' % (trade.pnl, trade.pnlcomm)) def next(self): # Simply log the closing price of the series from the reference # Check if an order is pending ... if yes, we cannot send a 2nd one if self.order: return # Check if we are in the market if not self.position: # Not yet ... we MIGHT BUY if ... if self.dataclose[0] > self.sma[0]: # BUY, BUY, BUY!!! (with all possible default parameters) self.log('BUY CREATE, %.2f' % self.dataclose[0]) # Keep track of the created order to avoid a 2nd order self.order = self.buy() else: if self.dataclose[0] < self.sma[0]: # SELL, SELL, SELL!!! (with all possible default parameters) self.log('SELL CREATE, %.2f' % self.dataclose[0]) # Keep track of the created order to avoid a 2nd order self.order = self.sell() class LongOnly(bt.Sizer): params = (('stake', 1),) def _getsizing(self, comminfo, cash, data, isbuy): if isbuy: divide = math.floor(cash/data.close[0]) self.p.stake = divide return self.p.stake # Sell situation position = self.broker.getposition(data) if not position.size: return 0 # do not sell if nothing is open return self.p.stake if __name__ == '__main__': cerebro = bt.Cerebro() # Add a strategy cerebro.addstrategy(TestStrategy) # Add Sizer cerebro.addsizer(LongOnly) # because it could have been called from anywhere modpath = os.path.dirname(os.path.abspath(sys.argv[0])) datapath = os.path.join('datas/orcl-1995-2014.txt') # Create a Data Feed data = bt.feeds.YahooFinanceCSVData( dataname=datapath, # Do not pass values before this date fromdate=datetime.datetime(2000, 1, 1), # Do not pass values before this date todate=datetime.datetime(2000, 12, 31), # 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(1000.0) # Set the commission cerebro.broker.setcommission(commission=0.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()) # Plot the result cerebro.plot()
And I get the output following. I think it is strange.
For example, Date 2000-02-02 is make "BUY CREATE", but "BUY EXECUTED" shows "Price: 0.00, Cost: 0.00, Comm 0.00".
So, why it can not make excuted an order? How to understand these outputs?Starting Portfolio Value: 1000.00 2000-01-25, BUY CREATE, 26.61 2000-01-26, BUY EXECUTED, Price: 26.76, Cost: 990.12, Comm 0.00 2000-01-27, SELL CREATE, 24.43 2000-01-28, SELL EXECUTED, Price: 24.28, Cost: 990.12, Comm 0.00 2000-01-28, OPERATION PROFIT, GROSS -91.76, NET -91.76 2000-02-02, BUY CREATE, 25.61 2000-02-03, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-02-03, BUY CREATE, 26.73 2000-02-04, BUY EXECUTED, Price: 27.17, Cost: 896.61, Comm 0.00 2000-02-18, SELL CREATE, 27.61 2000-02-22, SELL EXECUTED, Price: 27.88, Cost: 896.61, Comm 0.00 2000-02-22, OPERATION PROFIT, GROSS 23.43, NET 23.43 2000-02-22, BUY CREATE, 27.97 2000-02-23, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-02-23, BUY CREATE, 29.73 2000-02-24, BUY EXECUTED, Price: 29.79, Cost: 923.49, Comm 0.00 2000-03-30, SELL CREATE, 36.98 2000-03-31, SELL EXECUTED, Price: 37.81, Cost: 923.49, Comm 0.00 2000-03-31, OPERATION PROFIT, GROSS 248.62, NET 248.62 2000-04-06, BUY CREATE, 38.75 2000-04-07, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-04-07, BUY CREATE, 41.08 2000-04-10, BUY EXECUTED, Price: 41.26, Cost: 1155.28, Comm 0.00 2000-04-11, SELL CREATE, 36.48 2000-04-12, SELL EXECUTED, Price: 36.75, Cost: 1155.28, Comm 0.00 2000-04-12, OPERATION PROFIT, GROSS -126.28, NET -126.28 2000-04-18, BUY CREATE, 37.22 2000-04-19, BUY EXECUTED, Price: 37.07, Cost: 1037.96, Comm 0.00 2000-04-19, SELL CREATE, 35.16 2000-04-20, SELL EXECUTED, Price: 34.80, Cost: 1037.96, Comm 0.00 2000-04-20, OPERATION PROFIT, GROSS -63.56, NET -63.56 2000-04-27, BUY CREATE, 36.45 2000-04-28, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-04-28, BUY CREATE, 37.69 2000-05-01, BUY EXECUTED, Price: 37.66, Cost: 979.16, Comm 0.00 2000-05-04, SELL CREATE, 35.01 2000-05-05, SELL EXECUTED, Price: 34.89, Cost: 979.16, Comm 0.00 2000-05-05, OPERATION PROFIT, GROSS -72.02, NET -72.02 2000-05-05, BUY CREATE, 36.22 2000-05-08, BUY EXECUTED, Price: 35.51, Cost: 887.75, Comm 0.00 2000-05-08, SELL CREATE, 34.10 2000-05-09, SELL EXECUTED, Price: 34.75, Cost: 887.75, Comm 0.00 2000-05-09, OPERATION PROFIT, GROSS -19.00, NET -19.00 2000-05-15, BUY CREATE, 36.31 2000-05-16, BUY EXECUTED, Price: 36.60, Cost: 878.40, Comm 0.00 2000-05-18, SELL CREATE, 34.45 2000-05-19, SELL EXECUTED, Price: 33.94, Cost: 878.40, Comm 0.00 2000-05-19, OPERATION PROFIT, GROSS -63.84, NET -63.84 2000-05-30, BUY CREATE, 34.98 2000-05-31, BUY EXECUTED, Price: 34.54, Cost: 794.42, Comm 0.00 2000-06-22, SELL CREATE, 38.43 2000-06-23, SELL EXECUTED, Price: 38.10, Cost: 794.42, Comm 0.00 2000-06-23, OPERATION PROFIT, GROSS 81.88, NET 81.88 2000-06-26, BUY CREATE, 38.99 2000-06-27, BUY EXECUTED, Price: 38.84, Cost: 893.32, Comm 0.00 2000-06-27, SELL CREATE, 38.78 2000-06-28, SELL EXECUTED, Price: 38.70, Cost: 893.32, Comm 0.00 2000-06-28, OPERATION PROFIT, GROSS -3.22, NET -3.22 2000-06-28, BUY CREATE, 39.11 2000-06-29, BUY EXECUTED, Price: 38.69, Cost: 889.87, Comm 0.00 2000-06-29, SELL CREATE, 38.13 2000-06-30, SELL EXECUTED, Price: 37.90, Cost: 889.87, Comm 0.00 2000-06-30, OPERATION PROFIT, GROSS -18.17, NET -18.17 2000-06-30, BUY CREATE, 39.64 2000-07-03, BUY EXECUTED, Price: 38.25, Cost: 841.50, Comm 0.00 2000-07-03, SELL CREATE, 37.81 2000-07-05, SELL EXECUTED, Price: 36.22, Cost: 841.50, Comm 0.00 2000-07-05, OPERATION PROFIT, GROSS -44.66, NET -44.66 2000-07-20, BUY CREATE, 36.84 2000-07-21, BUY EXECUTED, Price: 36.51, Cost: 839.73, Comm 0.00 2000-07-21, SELL CREATE, 35.57 2000-07-24, SELL EXECUTED, Price: 36.36, Cost: 839.73, Comm 0.00 2000-07-24, OPERATION PROFIT, GROSS -3.45, NET -3.45 2000-07-25, BUY CREATE, 35.83 2000-07-26, BUY EXECUTED, Price: 35.28, Cost: 811.44, Comm 0.00 2000-07-27, SELL CREATE, 35.39 2000-07-28, SELL EXECUTED, Price: 35.42, Cost: 811.44, Comm 0.00 2000-07-28, OPERATION PROFIT, GROSS 3.22, NET 3.22 2000-07-31, BUY CREATE, 35.45 2000-08-01, BUY EXECUTED, Price: 35.46, Cost: 851.04, Comm 0.00 2000-08-01, SELL CREATE, 34.48 2000-08-02, SELL EXECUTED, Price: 34.42, Cost: 851.04, Comm 0.00 2000-08-02, OPERATION PROFIT, GROSS -24.96, NET -24.96 2000-08-03, BUY CREATE, 36.51 2000-08-04, BUY EXECUTED, Price: 36.93, Cost: 812.46, Comm 0.00 2000-09-08, SELL CREATE, 40.81 2000-09-11, SELL EXECUTED, Price: 40.58, Cost: 812.46, Comm 0.00 2000-09-11, OPERATION PROFIT, GROSS 80.30, NET 80.30 2000-09-28, BUY CREATE, 38.42 2000-09-29, BUY EXECUTED, Price: 38.36, Cost: 882.28, Comm 0.00 2000-09-29, SELL CREATE, 37.13 2000-10-02, SELL EXECUTED, Price: 37.60, Cost: 882.28, Comm 0.00 2000-10-02, OPERATION PROFIT, GROSS -17.48, NET -17.48 2000-10-19, BUY CREATE, 34.30 2000-10-20, BUY EXECUTED, Price: 34.06, Cost: 851.50, Comm 0.00 2000-10-30, SELL CREATE, 29.82 2000-10-31, SELL EXECUTED, Price: 30.76, Cost: 851.50, Comm 0.00 2000-10-31, OPERATION PROFIT, GROSS -82.50, NET -82.50 2000-11-17, BUY CREATE, 27.17 2000-11-20, BUY EXECUTED, Price: 22.93, Cost: 664.97, Comm 0.00 2000-11-20, SELL CREATE, 23.34 2000-11-21, SELL EXECUTED, Price: 23.39, Cost: 664.97, Comm 0.00 2000-11-21, OPERATION PROFIT, GROSS 13.34, NET 13.34 2000-11-30, BUY CREATE, 24.99 2000-12-01, BUY EXECUTED, Price: 24.87, Cost: 795.84, Comm 0.00 2000-12-14, SELL CREATE, 25.93 2000-12-15, SELL EXECUTED, Price: 27.76, Cost: 795.84, Comm 0.00 2000-12-15, OPERATION PROFIT, GROSS 92.48, NET 92.48 2000-12-15, BUY CREATE, 26.93 2000-12-18, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-12-18, BUY CREATE, 30.18 2000-12-19, BUY EXECUTED, Price: 30.00, Cost: 900.00, Comm 0.00 2000-12-20, SELL CREATE, 26.88 2000-12-21, SELL EXECUTED, Price: 26.23, Cost: 900.00, Comm 0.00 2000-12-21, OPERATION PROFIT, GROSS -113.10, NET -113.10 2000-12-21, BUY CREATE, 27.82 2000-12-22, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2000-12-22, BUY CREATE, 30.06 2000-12-26, BUY EXECUTED, Price: 29.70, Cost: 772.20, Comm 0.00 2000-12-29, SELL CREATE, 27.41 Final Portfolio Value: 739.73
-
How to buy maximum stakes?
Dear all
I want to do buy max stakes what I can buy by my assets. I write codes following, but Sell and Buy are outpuy many times and it is strange. Are there any misses?class TestStake(bt.Sizer): params = (('stake', 1),) def _getsizing(self, comminfo, cash, data, isbuy): if isbuy: divide = math.floor(cash/data.close[0]) self.p.stake = divide return self.p.stake # Sell situation position = self.broker.getposition(data) if not position.size: return 0 # do not sell if nothing is open return self.p.stake ... ... ... cerebro.addsizer(TestStake)
-
Question: How to decide buy and sell order for once?
Dear all
I make the strategy to use RSI. I make easy example code to make sure the behavior follows.if self.RSI[0] < -80, I buy. if self.dataclose[0] > -20, I sell. And I check the command prompt, there are many sell order. 2014-02-24, SELL CREATE, 14837.68 2014-02-25, SELL EXECUTED, Price: 15002.51, Cost: 14803.64, Comm 0.00 2014-03-06, SELL CREATE, 15134.75 2014-03-07, SELL EXECUTED, Price: 15280.76, Cost: 14933.76, Comm 0.00 ....
I want to do buy and next time should be sell. How to do these?