For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
my code does not buy and sell
-
this is my code
this code run ok with 1h time frame, but does not buy and sell with 5m time frame
data 1h and 5m saved on csv# -*- coding: utf-8 -*- """ Created on Tue Jul 14 10:03:06 2020 @author: mahmoudr """ from __future__ import (absolute_import, division, print_function, unicode_literals) # import datetime from datetime import datetime import os.path import sys import backtrader as bt import backtrader.indicators as btind import backtrader.feeds as btfeeds import pdb class TestStrategy(bt.Strategy): params = (('period20', 20), ('period50', 50), ('macd1', 12), ('macd2', 26), ('macdsig', 9), ('rsiperiod', 14), ('period_stoch', 14), ('d_fast', 3), ('d_slow', 3)) 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.dataclose = self.datas[0].close self.datalow = self.datas[0].low self.datahigh = self.datas[0].high self.macd = btind.MACD(self.dataclose, period_me1=self.params.macd1, period_me2=self.params.macd2, period_signal=self.params.macdsig) self.order = None self.buyprice = None self.buycomm = None 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 if order.status in [order.Completed]: 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) elif order.status in [order.Canceled, order.Margin, order.Rejected]: self.log('Order Canceled/Margin/Rejected') 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): last_low = self.datalow[0] last_high = self.datahigh[0] last_close = self.dataclose[0] last_macd = self.macd.macd[0] last_signal = self.macd.signal[0] self.log('Close, %.2f' % self.dataclose[0]) # 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 last_macd > last_signal and last_ema_fast>last_ema_slow: #if last_plusDI > last_minusDI and last_plusDI>20: if last_macd > last_signal: self.log('BUY CREATE, %.2f' % self.dataclose[0]) else: # Already in the market ... we might sell #if last_psar>last_close and last_last_psar>last_last_close: if last_macd < last_signal : # if last_signal>last_macd or last_d>last_k or last_MA_20 > last_close : # SELL, SELL, SELL!!! (with all possible default parameters) self.log('SELL CREATE, %.2f' % self.dataclose[0]) 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='BTC-USD', fromdate=datetime(2020, 1, 1),todate=datetime(2020, 6, 1)) data = btfeeds.GenericCSVData( ##for minutes dataname='Binance-BTCUSDT-5m.csv', timeframe=bt.TimeFrame.Minutes, compression=5, dtformat=('%Y-%m-%d %H:%M:%S'), #for m or h nullvalue=0.0, datetime=0, Open=1, High=2, Low=3, Close=4, Volume=5, openinterest=-1 ) # Add the Data Feed to Cerebro cerebro.adddata(data) # Set our desired cash start cerebro.broker.setcash(10000.0) # Set the commission - 0.1% ... divide by 100 to remove the % cerebro.broker.setcommission(commission=0.001) # Print out the starting conditions print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) # Run over everything cerebro.run() cerebro.plot() # Print out the final result print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
-
There is no one to help me?
-
you dont have any self.order = self.buy() or self.sell in your next section ? Please add them and give it a try..
-
@rajanprabu can you please send me a simple code to test
-
three example codes are here..