it's impossible, use ta-lib ...
Best posts made by dmitry.prokopiev
-
RE: Abnormal behavior of the CrossOver class
@vladisld Uh ... Thnx, I forgot about the lot size.
-
RE: Attributes position in Strategy - how to set value correctly?
@run-out thnx, I understand
i misunderstood the order creation and interaction with a broker class
-
RE: Attributes position in Strategy - how to set value correctly?
@run-out in github, i searched and found an example (https://github.com/bartosh/backtrader/blob/ccxt/backtrader/brokers/ccxtbroker.py)
def _submit(self, owner, data, exectype, side, amount, price, params): order_type = self.order_types.get(exectype) _order = self.store.create_order(symbol=data.symbol, order_type=order_type, side=side, amount=amount, price=price, params=params) order = CCXTOrder(owner, data, amount, _order) self.notify(order) return order
Why is the order being re-created?
Latest posts made by dmitry.prokopiev
-
RE: Attributes position in Strategy - how to set value correctly?
@run-out thnx, I understand
i misunderstood the order creation and interaction with a broker class
-
RE: Attributes position in Strategy - how to set value correctly?
@run-out in github, i searched and found an example (https://github.com/bartosh/backtrader/blob/ccxt/backtrader/brokers/ccxtbroker.py)
def _submit(self, owner, data, exectype, side, amount, price, params): order_type = self.order_types.get(exectype) _order = self.store.create_order(symbol=data.symbol, order_type=order_type, side=side, amount=amount, price=price, params=params) order = CCXTOrder(owner, data, amount, _order) self.notify(order) return order
Why is the order being re-created?
-
RE: Attributes position in Strategy - how to set value correctly?
@run-out How to tracking position - understandable.
But, if implemented broker class is, how to correctly set position (size, upclosed, upopened)?
-
RE: Attributes position in Strategy - how to set value correctly?
@run-out thnx the answer :)
In documentation for the Strategy class, section Member Attributes has:
....
position: actually a property which gives the current position for data0.
Methods to retrieve all possitions are available (see the reference)This attribute, instance of class Position.
When the broker (class broker) receives an order to open a position, this attribute must be correctly set. -
RE: Attributes position in Strategy - how to set value correctly?
Addition, notify_trade sending only after call order.execute method. Its correctly.
How correctly setting self.position (upopened, size ... etc)?
I'm still :) looking in source ... -
Attributes position in Strategy - how to set value correctly?
I made my class broker, orders are sent and executed - ok.
But, how to set the value of attributes position correctly?
i see several methods:- using notify_trade, and thus set the value attributes position
- using self.data[0], and thus set the value attributes position
But how do it right in the broker class?
-
RE: Abnormal behavior of the CrossOver class
@vladisld Uh ... Thnx, I forgot about the lot size.
-
RE: Abnormal behavior of the CrossOver class
@vladisld thnx, worked.
But I don't understand what the relationship is cash on the broker and not open BUY position ...
Could you tell me why this is so ?! Or what to read in the documentation?
-
Abnormal behavior of the CrossOver class
Hi All,
Can someone comments, I don't understand behavior of the class CrossOver.
Simple strategy:
if EMA (30) Сrosses UP WMA(30) - buy
if EMA (30) Сrosses Down WMA(30) - sellimport sys import backtrader as bt from datetime import datetime, timedelta import backtrader.indicators as btind import backtrader.feeds as btfeed from backtrader_plotting import Bokeh from backtrader_plotting.schemes import Tradimo import argparse import logging logger = logging.getLogger(__name__) logging.basicConfig(stream=sys.stderr, level=logging.DEBUG, format='%(asctime)s [%(levelname)s][module: %(module)s.py] %(funcName)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S' ) class MyTradimo(Tradimo): def _set_params(self): super()._set_params() # Disable the output of Volume on the displayed chart self.volume=False class CommFixed(bt.CommInfoBase): params = ( ('stocklike', False), ('commtype', bt.CommInfoBase.COMM_FIXED), ) class TestedStrategy(bt.SignalStrategy): def __init__(self): self.order = None self.ema = btind.ExponentialMovingAverage(self.data.close) self.lwma = btind.WeightedMovingAverage(self.data.close) self.buysell = btind.CrossOver(self.lwma, self.ema) def next(self): if self.buysell < 0: self.sell(exectype=bt.Order.Market) elif self.buysell > 0: self.buy(exectype=bt.Order.Market) def parse_args(pargs=None): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='Sample for Signal concepts') parser.add_argument('--data', required=False, default='csv/BTCUSD_M5_2021_01_28__21_35-2021_02_02__21_25.csv', help='Specific data to be read in') parser.add_argument('--fromdate', required=False, default=None, help='Starting date in YYYY-MM-DD[THH:MM:SS] format') if pargs is not None: return parser.parse_args(pargs) return parser.parse_args() def runstrat(args=None): args = parse_args(args) if args.fromdate: fmtstr = '%Y-%m-%d' dtsplit = args.fromdate.split('T') if len(dtsplit) > 1: fmtstr += 'T%H:%M:%S' start_date = datetime.strptime(args.fromdate, fmtstr) else: start_date = datetime(2021,1,31,23,00) end_date = datetime(2021,2,1,18,00) logger.info("Start: {} End: {}".format(start_date, end_date)) data = btfeed.GenericCSVData( dataname='BTCUSD_M5_2021_01_28__21_40-2021_02_02__21_35.csv', dtformat=('%Y.%m.%d %H:%M:%S'), timeframe=bt.TimeFrame.Minutes, compression=5, fromdate=start_date, todate=end_date, ) cerebro = bt.Cerebro(stdstats=False) cerebro.adddata(data) cerebro.addstrategy(TestedStrategy) cerebro.broker.setcash(1000) cerebro.broker.addcommissioninfo(CommFixed, "CommFixed") cerebro.addobserver(bt.observers.BuySell) cerebro.run() b = Bokeh(style='bar', tabs='multi', scheme=MyTradimo()) cerebro.plot(b) if __name__ == '__main__': runstrat()
We are doing two tests:
- BTC, M5, Start: 2021-01-31 22:35:00 End: 2021-02-01 18:00:00
- BTC, M5, Start: 2021-01-31 22:40:00 End: 2021-02-01 18:00:00
We see such results:
-
OK
-
Where is BUY?!
It looked more complicated in the code - I simplified everything as much as possible for an example.