if not order: if strategy.signal == 'buy': price = candle.close - (candle.close * order_diff / 100) elif strategy.signal == 'sell': price = candle.close + (candle.close * order_diff / 100) else: price = candle.close order = Order(candle.end, order_valid_time, price, strategy.signal) # Check order status if order: if order.status == 'ACTIVE': order.check_status(candle) if order.status == 'FILLED': if order.side == 'buy': logging.info('Open long position @ ' + str(order.price) + " diff: " + str(strategy.up_percent * 100)) pos.open('btcusd', order.price, start_margin / float(order.price)) plot_buy_index.append(datetime.fromtimestamp(float(candle.start / 1000))) plot_buy_value.append(order.price) if order.side == 'sell': logging.info('Open short position @ ' + str(order.price) + " diff: " + str(strategy.down_percent * 100)) pos.open('btcusd', order.price, -start_margin / float(order.price)) plot_sell_index.append(datetime.fromtimestamp(float(candle.start / 1000))) plot_sell_value.append(order.price) # trailing_profit_pct = (strategy.down_percent * 100 + order_diff) / 4 # stop_pct = trailing_profit_pct * 2 order = None elif order.status == 'CANCELED': order = None check_position(candle, trade) candle = OHLCV()
plot_macd_short = [] plot_macd_long = [] for trade in trade_cursor: # Add trade to OHLCV candle candle.add_trade(trade['time'], trade['size'], trade['price'], trade['side']) # print(candle.start, candle.end, candle.open, candle.high, candle.low, candle.close, candle.volume) # Check order status if order: if order.status == 'ACTIVE': order.check_status(candle) if order.status == 'FILLED': if order.side == 'buy': logging.info('Open long position @ ' + str(order.price) + " diff: " + str(strategy.up_percent * 100)) pos.open('btcusd', order.price, margin / float(order.price)) plot_buy_index.append( datetime.fromtimestamp(float(candle.start / 1000))) plot_buy_value.append(order.price) if order.side == 'sell': logging.info('Open short position @ ' + str(order.price) + " diff: " + str(strategy.down_percent * 100)) pos.open('btcusd', order.price, -margin / float(order.price)) plot_sell_index.append( datetime.fromtimestamp(float(candle.start / 1000))) plot_sell_value.append(order.price) trailing_profit_pct = (strategy.down_percent * 100 + order_diff) / 4 stop_pct = trailing_profit_pct * 2 order = None elif order.status == 'CANCELED':