Print('Starting Portfolio Value: '.format(cerebro.broker. # or just alpaca_backtrader_api.AlpacaBroker()īACKTEST_START_YEAR, BACKTEST_START_MONTH, BACKTEST_START_DAY),Ĭerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=CHART_RESOLUTION) Store = alpaca_backtrader_api.AlpacaStore(ĭataFactory = store.getdata # or use alpaca_backtrader_api.AlpacaDataĭata = DataFactory(dataname=TICKER_SYMBOL, Size = math.floor((cash * self.p.risk) / data) Raise ValueError('The risk parameter is a percentage which must be'ĭef _getsizing(self, comminfo, cash, data, isbuy): Self.log('SELL CREATE, %.2f' % self.dataclose) # if len(self) >= (self.bar_executed + ): # Keep track of the created order to avoid a 2nd order Self.log('BUY CREATE, %.2f' % self.dataclose) # Simply log the closing price of the series from the reference Self.log('Order Canceled/Margin/Rejected') Self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %Įlif order.status in : # Attention: broker could reject order if not enough cash # Buy/Sell order submitted/accepted to/by broker - Nothing to do Self.buysig = bt.ind.CrossOver(self.kst, 0.00) Self.knowSureThing = KnowSureThing(self.data) # To keep track of pending orders and buy price/commission # change to True if you want to do live paper trading with Alpaca Broker.ĬHART_RESOLUTION = 60 # minutes per candle import alpaca_backtrader_apiĪLPACA_SECRET_KEY = 'IjxUeI0nIDxiS8zcOR/JCDVtQbnGrEU3Q0yvUrFR' Thanks in advance for any help, it seems like some sort of syntax error with the selling but not really sure. I just put it in an indicators folder so I can modify it easily later on. If you want to run it, I think the KST indicator is in backtrader, but if it isn't I can send it. Please let me know if you need anymore info.
I got some of this code from a forum and it looks very complete so I thought it would be good to build on top of it, and it appears you set self.buysig to 1 to buy and -1 to sell, but it doesn't seem to be working this way. Most of the time the problem I end up with is it just sells really fast for every buy. In the example code below, the arduino simulates a coin toss using the function random.I have been messing with this for quite a while, and can't seem to get it to buy/sell when I want it to.
To test my code, I used an Arduino to put some data on the serial port.
Sudo apt-get install python3-serial Generating some fake serial data with an Arduino Digitizing is a four step process: open a graph, set the scale. GetData Graph Digitizer allows to easily get the numbers in such cases. from scanned scientific plots, when data values are not available. It is often necessary to obtain original (x,y) data from graphs, e.g. Installing matplotlib and pyserial on Ubuntu 18 sudo apt-get install python3-matplotlib GetData Graph Digitizer is a program for digitizing graphs and plots.
As it turns out, Matplotlib includes an animation API and a function called FuncAnimation, which can be used to animate data over time (or update a graph with some sensor data over time). A quick search turned up Matplotlib – a MATLAB-like plotting API for Python. I’ve known for a while that Python has an easy-to-use serial library, but I wasn’t sure what kinds of plotting/graphing options might exist for Python. While this is a trivial task in MATLAB or LabVIEW, I wondered if there was a low effort way to do it for free. It’s useful to be able to read and plot serial data in real time (for example, you might want to monitor the output of a laser scanner or IMU).