@run-out Your solution works very nice, but this won't work live, will it? At least it reads in the doc's that there's no live support with oco's, although I see reports that binance api can deal with oco orders. I'm intenting to trade live on a crypto exchange. Do you have experience with oco / live ?
Posts made by stanski
-
RE: Problem with multiple limit orders as take profit
-
RE: Problem fetching execution price
@run-out nonetheless, thank you for sharing the idea! I just made a new post with the initial code with my take profit problem
-
Problem with multiple limit orders as take profit
Hello
I'm trying to set up two TP's with sell-limit orders and one SL with a sell-stop order.
The problem I'm encountering is that the first trades seem to be correct, but later bt creates multiple sell-limit orders without the buy order.
What am I doing wrong?import backtrader as bt from datetime import datetime class SmaCross(bt.Strategy): # list of parameters which are configurable for the strategy params = dict( pfast=100, # period for the fast moving average pslow=50 # period for the slow moving average ) def __init__(self): self.sma0 = bt.ind.SMA(period=800) sma1 = bt.ind.SMA(period=self.p.pfast) # fast moving average sma2 = bt.ind.SMA(period=self.p.pslow) # slow moving average self.crossover = bt.ind.CrossOver(sma1, sma2) # crossover signal 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 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.sell(exectype=bt.Order.Limit, size=1, price=self.data.close[0] * 1.03) self.sell(exectype=bt.Order.Limit, size=1, price=self.data.close[0] * 1.06) self.sell(exectype=bt.Order.Stop, size=2, price=self.data.close[0] * 0.98) self.log('BUY EXECUTED, %.2f' % order.executed.price) elif order.issell(): self.log('SELL EXECUTED, %.2f' % order.executed.price) def next(self): if not self.position: # not in the market if self.crossover > 0 and self.datas[0] < self.sma0: # if fast crosses slow to the upside self.buy_order = self.buy(exectype=bt.Order.Market, size=2) # enter long startcash = 10000 cerebro = bt.Cerebro() # create a "Cerebro" engine instance # Create a data feed data = bt.feeds.GenericCSVData( dataname='data/1inch_perp_28-02_09-03_10S.csv', fromdate=datetime(2021, 3, 7), todate=datetime(2021, 3, 21), timeframe=bt.TimeFrame.Seconds, headers=False, separator=";", volume=5, openinterest=-1, dtformat=('%d.%m.%Y %H:%M:%S'), ) cerebro.adddata(data) # Add the data feed cerebro.addstrategy(SmaCross) # Add the trading strategy cerebro.run() # run it all portvalue = cerebro.broker.getvalue() pnl = portvalue - startcash print('Final Portfolio Value: ${}'.format(portvalue)) print('P/L: ${}'.format(pnl)) cerebro.plot() # and plot it with a single command
2021-03-07, BUY EXECUTED, 38.52 2021-03-07, SELL EXECUTED, 39.67 2021-03-07, SELL EXECUTED, 40.82 2021-03-07, BUY EXECUTED, 40.28 2021-03-07, SELL EXECUTED, 39.52 2021-03-07, BUY EXECUTED, 39.05 2021-03-07, SELL EXECUTED, 38.29 2021-03-07, BUY EXECUTED, 37.97 2021-03-08, SELL EXECUTED, 39.13 2021-03-08, SELL EXECUTED, 40.24 2021-03-08, SELL EXECUTED, 40.27 2021-03-08, SELL EXECUTED, 41.41 2021-03-08, SELL EXECUTED, 41.53 2021-03-08, SELL EXECUTED, 42.74
-
RE: Problem fetching execution price
@run-out well, whatever I do now with
self.datas[0].enter_price
in terms of multiplication or addition, I get aTypeError: unsupported operand type(s) for *: 'float' and 'NoneType'
I'm considering to discard this idea. This is actually an alternative try-out for a percentage-based take profit, which I couldn't get working in another way.
Maybe I should post the initial code in a new post -
RE: Problem fetching execution price
@run-out I've tried it with the attached attribute, but unfortunately, I get an
AttributeError: 'Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst' object has no attribute 'enter_price
-
Problem fetching execution price
Hello everyone,
I'm trying to exit a position if price > executed price + x.
How can I fetch theorder.executed.price
?I tried it with
_orders[data][0]
but it doesn't work:def notify_order(self, order): if order.status in [order.Submitted, order.Accepted]: return if order.status in [order.Completed]: if order.isbuy(): DISTCALC = ((self.ema1[0]-order.executed.price)/order.executed.price*100) self.log('BUY EXECUTED, %.2f' % order.executed.price) self.log('EMA Price, %.2f' % self.ema1[0]) self.log('Price distance to EMA, %.2f' % DISTCALC + str('%')) elif order.issell(): self.log('SELL EXECUTED, %.2f' % order.executed.price) def next(self): if not self.position and self.order==None: if self.distance and self.below800EMA: self.price = self.sma1[0] self.order = self.buy(self.datas[0], exectype=bt.Order.Market, size=1) if self.sma1[0] > self._orders[data][0]+10: self.order = self.close()
The error is:
TypeError: list indices must be integers or slices, not GenericCSVData