title: Problem with an Alpaca Traiding Bot with Visual Studio Code and Python
body: <p>everyone,</p>
  <p>I've discovered a strange phenomenon and I have no idea what it is.
  I wrote the following little traidingbot in Python:</p>
  <pre><code>import logging
  from time import sleep
  from credentials import *
  import alpaca_trade_api as tradeapi
  import threading
  import pandas as pd
  
  DATA_STREAM = 'alpacadatav1'
  BASE_URL = 'https://paper-api.alpaca.markets'
  DATA_URL = 'wss://data.alpaca.markets'
  API_VERSION = 'v2'
  
  # instantiate REST API
  api = tradeapi.REST(ALPACA_API_KEY,
                      ALPACA_SECRET_KEY,
                      BASE_URL,
                      api_version=API_VERSION)
  
  # init WebSocket
  conn = tradeapi.stream2.StreamConn(
      ALPACA_API_KEY,
      ALPACA_SECRET_KEY,
      base_url=BASE_URL,
      data_url=DATA_URL,
      data_stream=DATA_STREAM,
  )
  
  # init
  logging.basicConfig(
      filename='errlog.log',
      level=logging.WARNING,
      format='%(asctime)s:%(levelname)s:%(message)s',
  )
  
  
  def time_to_market_close():
      clock = api.get_clock()
      return (clock.next_close - clock.timestamp).total_seconds()
  
  
  def wait_for_market_open():
      clock = api.get_clock()
      if not clock.is_open:
          time_to_open = (clock.next_open - clock.timestamp).total_seconds()
          sleep(round(time_to_open))
  
  
  def set_trade_params(df):
      return {
          'high': df.high.tail(10).max(),
          'low': df.low.tail(10).min(),
          'trade_taken': False,
      }
  
  
  def send_order(direction, bar):
      if time_to_market_close() &gt; 120:
          print(f'sent {direction} trade')
          range_size = trade_params['high'] - trade_params['low']
  
          if direction == 'buy':
              sl = bar.high - range_size
              tp = bar.high + range_size
          elif direction == 'sell':
              sl = bar.low + range_size
              tp = bar.low - range_size
  
          api.submit_order(
              symbol='AAPL',
              qty=1,
              side=direction,
              type='market',
              time_in_force='day',
              order_class='bracket',
              stop_loss=dict(stop_price=str(sl)),
              take_profit=dict(limit_price=str(tp)),
          )
  
          return True
  
      wait_for_market_open()
      return False
  
  
  @conn.on(r'^AM.AAPL$')
  async def on_minute_bars(conn, channel, bar):
      if isinstance(candlesticks.df, pd.DataFrame):
          ts = pd.to_datetime(bar.timestamp, unit='ms')
          candlesticks.df.loc[ts] = [
              bar.open, bar.high, bar.low, bar.close, bar.volume
          ]
  
      if not trade_params['trade_taken']:
          if bar.high &gt; trade_params['high']:
              trade_params['trade_taken'] = send_order('buy', bar)
  
          elif bar.low &lt; trade_params['low']:
              trade_params['trade_taken'] = send_order('sell', bar)
  
      if time_to_market_close() &gt; 120:
          wait_for_market_open()
  
  
  @conn.on(r'^trade_updates$')
  async def on_trade_updates(conn, channel, trade):
      # if trade.order['order_type'] != 'market' and trade.order['filled_qty'] == '100':
      if trade.order['order_type'] != 'market' and trade.order[
              'filled_qty'] == '1':
          # trade closed - look for new trade
          trade_params = set_trade_params(candlesticks.df.AAPL)
  
  
  candlesticks = api.get_barset('AAPL', 'minute', limit=10)
  trade_params = set_trade_params(candlesticks.df.AAPL)
  conn.run(['AM.AAPL', 'trade_updates'])
  
  print('Programm finished')
  
  </code></pre>
  <p>During execution I always got the following error in the errlog.log:</p>
  <p>error while consuming ws messages: bracked orders must be entry orders</p>
  <p>I looked for the error everywhere, googled it and found nothing. So I gave the code to a good friend and he ran it on his computer on Linux. Voila! Interestingly, the code worked immediately with no errors. On the other hand, I use a Mac and program Python under Visual Studio Code. Now I thought that the problem was somewhere in the Mac operating system. Again, I looked, found nothing and in desperation I ran the code on my Mac with the IDLE of Python. The code worked under the original Python IDLE !!!!!</p>
  <p>Now I ask if someone can explain this phenomenon to me?</p>
  <p>Greetings
  Patrick</p>