Easy Strategy: Begginer RSI
-
Hello,
I'm begginer with this library. I've developed this code... It's a simply RSI.
But it doesn't function.My cerebro is with this code:
from __future__ import (absolute_import, division, print_function,unicode_literals) #from backtrader_plotting import Bokeh import datetime from plot import * from MyStrategy import * from TestStrategy import* #Create a cerebro entity cerebro = bt.Cerebro() #Add a my strategy cerebro.addstrategy(MyStrategy) # Add TestStrategy #cerebro.addstrategy(TestStrategy) #Set our desired cash start cerebro.broker.set_cash(1000) cerebro.broker.setcommission(commission=0.001) data = bt.feeds.GenericCSVData( dataname='BTCUSDT.csv', fromdate=datetime.datetime(2019, 1, 1), todate=datetime.datetime(2020, 12, 31), nullvalue=0.0, dtformat=('%Y-%m-%d'), tmformat=('%H:%M:%S'), date=0, time=6, high=2, low=3, open=1, close=4, volume=5, openinterest=-1, ) #Add Data Feed to Cerebro cerebro.adddata(data) #Set our desired cash start cerebro.broker.set_cash(1000.0) #Set the comission cerebro.broker.setcommission(commission=0.001) # 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())
And MyStrategy is with:
import backtrader as bt import numpy as np class MyStrategy (bt.Strategy): #Parametros de los indicadores params = (('BBandsperiod', 20),('devfactor',2), ('RSIperiod',14)) 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 __init__(self): #keep a reference to the close self.dataclose = self.datas[0].close #add a BBand and RSI self.rsi=bt.indicators.RSI_SMA(period=self.p.RSIperiod,plotname='mysma') self.bband = bt.indicators.BollingerBands(period=self.p.BBandsperiod, devfactor=self.p.devfactor) def next(self): if self.rsi[0] < 30: self.log('BUY CREATE, %.2f' % self.dataclose[0]) self.buy() if self.rsi[0] >70: self.log('SELL CREATE, %.2f' % self.dataclose[0]) self.sell()
If someone can explain me why it doesn't function... I will be very grateful.
P.S: Sorry for my English
-
It show me:
... 2020-09-05, SELL CREATE, 10243.70 2020-09-05, BUY CREATE, 10061.94 2020-09-05, BUY CREATE, 10099.79 2020-09-05, BUY CREATE, 10033.66 2020-09-05, BUY CREATE, 9898.82 2020-09-06, SELL CREATE, 10256.06 2020-09-06, SELL CREATE, 10217.32 Final Portfolio Value: 134084.88
-
But with only buying, it happens:
2020-09-05, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2020-09-05, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2020-09-05, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2020-09-05, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2020-09-05, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2020-09-05, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 2020-09-05, BUY EXECUTED, Price: 0.00, Cost: 0.00, Comm 0.00 Final Portfolio Value: 1000.00
-
@Petter_Axcell said in Easy Strategy: Begginer RSI:
But with only buying, it happens:
Your code has no means to print this.
@Petter_Axcell said in Easy Strategy: Begginer RSI:
If someone can explain me why it doesn't function... I will be very grateful.
First, your code is functioning, it prints logs. Second, if somehow it doesn't, show us what did you do to debug your own code before post the question on the community forum?
To avoid duplicate steps. -
@Petter_Axcell said in Easy Strategy: Begginer RSI:
2020-09-05, BUY CREATE, 10061.94
The log shows the
close
price IIUC:self.log('BUY CREATE, %.2f' % self.dataclose[0])
you initialize your cash with just 1000:
cerebro.broker.set_cash(1000)
seems not to be enough to satisfy the above
buy
order. -
Thanks,
I knew about the close prices but I didn't know about the quantity of purchase