# Websockets don't pass data to while loop

**URL:** <https://forum.alpaca.markets/t/websockets-dont-pass-data-to-while-loop/4638>\
**Category:** Alpaca Integration Applications\
**Created:** [February 25, 2021, 3:35pm UTC](https://forum.alpaca.markets/t/websockets-dont-pass-data-to-while-loop/4638 "2021-02-25T15:35:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex.meketer](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/alex.meketer/32/1337_2.png) [@alex.meketer](https://forum.alpaca.markets/u/alex.meketer)\
**Post date:** [February 25, 2021, 3:35pm UTC](https://forum.alpaca.markets/t/websockets-dont-pass-data-to-while-loop/4638/1 "2021-02-25T15:35:04Z")

</div>

Hello, copied this pairs trading example [Pairs Trading - Strategy Deployment Guide](https://alpaca.markets/learn/pairs-trading/) and changed websockets from T to Minute streaming. Script seems to be working the first loop, but then in 1 minute when data arrives from Websocket it does not pass it to while not trade\_taken. Please help me to understand how to pass data to while not trade\_taken:

> ```
> import threading
> 
> ```
> 
> import websocket  
> import websockets  
> from time import sleep
> 
> import alpaca\_trade\_api as tradeapi  
> import pandas as pd
> 
> base\_url = ‘[https://paper-api.alpaca.markets](https://paper-api.alpaca.markets)’  
> data\_url = ‘wss://data.alpaca.markets’  
> trade\_taken = False  
> count = 0  
> count1 = 0
> 
> # instantiate REST API
> 
> api = tradeapi.REST(‘PK5XL4LYUFJNDGXU8BNW’,  
> ‘CvIaAkLxB2AcXZRwbsgV6hXy3grTLLRjVo6SHYMx’, base\_url=base\_url, api\_version=‘v2’)
> 
> # init WebSocket
> 
> conn = tradeapi.stream2.StreamConn(  
> ‘PK5XL4LYUFJNDGXU8BNW’, ‘CvIaAkLxB2AcXZRwbsgV6hXy3grTLLRjVo6SHYMx’, data\_url=data\_url, data\_stream=‘alpacadatav1’  
> )
> 
> def wait\_for\_market\_open():  
> clock = api.get\_clock()  
> if not clock.is\_open:  
> time\_to\_open = clock.next\_open - clock.timestamp  
> sleep\_time = round(time\_to\_open.total\_seconds())  
> print(“Sleep time:”, sleep\_time)  
> sleep(sleep\_time)  
> return clock
> 
> # define websocket callbacks
> 
> data\_df = None
> 
> @conn.on(r’^AM.V$')  
> async def on\_second\_bars\_V(conn, channel, bar):  
> print('Visa: ', bar.close)  
> if data\_df is not None:  
> data\_df.v[-1] = bar.close  
> print("DatadfV: ", data\_df.v[-1])
> 
> @conn.on(r’^AM.MA$')  
> async def on\_second\_bars\_MA(conn, channel, bar):  
> print('MA: ', bar.close)  
> if data\_df is not None:  
> data\_df.ma[-1] = bar.close  
> print("DatadfMA: ", data\_df.ma[-1])
> 
> # start WebSocket in a thread
> 
> streams = [‘AM.V’, ‘AM.MA’]  
> ws\_thread = threading.Thread(target=conn.run, daemon=True, args=(streams,))  
> ws\_thread.start()
> 
> # main script executes forever
> 
> while True:  
> count1 += 1  
> print("Main Count1: ", count1)  
> clock = wait\_for\_market\_open()  
> ma = api.get\_barset(‘MA’, ‘1Min’, limit=26)  
> v = api.get\_barset(‘V’, ‘1Min’, limit=26)  
> # print(ma)  
> # print(v)  
> data\_df = pd.concat(  
> [ma.df.MA.close, v.df.V.close],  
> axis=1,  
> join=‘inner’,  
> keys=[‘ma’, ‘v’],  
> )  
> # print(“DataDF”, data\_df)  
> data\_df.v[-1] = 0  
> data\_df.ma[-1] = 0  
> # print(data\_df.v)
> 
> ```
> spread_df = data_df.pct_change()
> # print(spread_df)
> spread_df = spread_df[:-1]
> spread_df['spread'] = spread_df.ma - spread_df.v
> max_divergence = spread_df.spread.tail(20).abs().max()
> # print("Max Divergence:", max_divergence)
> 
> # Check if websockets are streaming
> while data_df.v[-1] == 0 or data_df.ma[-1] == 0:
> print("Data is not coming in?", data_df.v[-1])
> sleep(1)
> 
> # Not in trade. Looking for arbitrage diviations
> while not trade_taken:
> # check for trade opportunities
> spread_df = data_df.pct_change()[:2]
> print("Spread_df: ", spread_df)
> # count += 1
> # print(count)
> # print("Spr ", spread_df)
> # print(".................")
> spread_df['spread'] = spread_df.ma - spread_df.v
> print("Max Divergence1:", max_divergence)
> print("abs(spread_df.spread[-1]: ", abs(spread_df.spread[-1]))
> print("abs(spread_df.spread[0]: ", abs(spread_df.spread[0]))
> # print("abs(spread_df.spread[1]: ", abs(spread_df.spread[1]))
> if abs(spread_df.spread[-1]) > max_divergence:
> print("Inside the trade")
> # there is a trade opportunuty - calculate position sizing
> acct = api.get_account()
> print("acct", acct)
> acct_size = float(acct.equity)
> print("Acc Size:", acct_size)
> ma_size = round(data_df.v[-1])
> v_size = round(data_df.ma[-1])
> 
> if spread_df.spread[-1] < 0:
> # ma - v is negative -> Long ma short v
> long_ma = True
> ma_side = 'buy'
> v_side = 'sell'
> 
> else:
> # ma - v is positive -> Short ma long v
> long_ma = False
> ma_side = 'sell'
> v_side = 'buy'
> 
> # submit order
> api.submit_order(
> symbol='MA',
> qty=ma_size,
> side=ma_side,
> time_in_force='gtc',
> type='market',
> )
> 
> api.submit_order(
> symbol='V',
> qty=v_size,
> side=v_side,
> time_in_force='gtc',
> type='market',
> )
> print("trade taken")
> trade_taken = True
> stop_loss = (max_divergence + 10) * -1
> print("StopLoss:", stop_loss)
> take_profit = max_divergence + 3
> print("TakeProfit:", take_profit)
> break
> 
> sleep(1)
> 
> # # check if the market is still open
> if pd.Timestamp.now(tz='America/Los_Angeles') > clock.next_close:
> trade_taken = False
> break
> 
> # In a trade - check for exit
> while trade_taken:
> print("In trade")
> pnl = data_df.ma[-1] * ma_size - data_df.v[-1] * v_size
> print("PNL:", pnl)
> if not long_ma:
> pnl *= -1 # inverse the p&l calculation
> 
> if pnl < stop_loss or pnl > take_profit:
> # Either stop or take profit hit - close trade
> api.close_position('MA')
> api.close_position('V')
> trade_taken = False
> break
> 
> if pd.Timestamp.now(tz='America/Los_Angeles') > clock.next_close:
> break
> 
> ```

---

<div class="post-metadata">

**Author:** ![ChuckT](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/chuckt/32/2188_2.png) [@ChuckT](https://forum.alpaca.markets/u/ChuckT)\
**Post date:** [February 25, 2021, 6:17pm UTC](https://forum.alpaca.markets/t/websockets-dont-pass-data-to-while-loop/4638/2 "2021-02-25T18:17:54Z")

</div>

> [@alex.meketer](#):
>
> `data_df`

Personally I find this example to to unnecessarily complicated. Try using the technique put forth by parttimelarry at this YouTube video: [Part Time Larry (Web Sockets on Alpaca)](https://youtu.be/Mv6c_9FqNx4)

Part Time Larry uses a much simpler structure where you set up a simple listener that receives the stream as a message. What I did notice though, is that when the message comes in you need to assign it to another variable in order to work with the data because the socket reserves the message variable for additional incoming messages.

---

<div class="post-metadata">

**Author:** ![alex.meketer](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/alex.meketer/32/1337_2.png) [@alex.meketer](https://forum.alpaca.markets/u/alex.meketer)\
**Post date:** [February 26, 2021, 7:38pm UTC](https://forum.alpaca.markets/t/websockets-dont-pass-data-to-while-loop/4638/3 "2021-02-26T19:38:10Z")

</div>

Thank you. Will check!

---

<div class="post-metadata">

**Author:** ![Ray\_Riordan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/ray_riordan/32/1844_2.png) [@Ray\_Riordan](https://forum.alpaca.markets/u/Ray_Riordan)\
**Post date:** [June 25, 2021, 4:33pm UTC](https://forum.alpaca.markets/t/websockets-dont-pass-data-to-while-loop/4638/4 "2021-06-25T16:33:42Z")

</div>

I have tried using the code Part Time Larry demonstrated. Unfortunately, I can’t get the stream.

I am able to run both v1 and v2 through the Command Line without a problem. However, my PyCharm IDE won’t stream.

Here is the code I am using from Stream.py:

import config  
import websocket  
import json

def on\_open(ws):  
print(“opened”)  
auth\_data = {  
“action”: “authenticate”,  
“data”: {“action”: “auth”, “key”: “_ **", “secret”: "** _\*\*”}  
}

```
ws.send(json.dumps(auth_data))

listen_message = {"action": "subscribe", "trades": ["AAPL"], "quotes": ["AMD", "CLDR"], "bars": ["*"]}

ws.send(json.dumps(listen_message))

```

def on\_message(ws,message):  
print(“received a message”)  
print(message)

def on\_close(ws):  
print(“closed connection”)

socket = “wss://stream.data.alpaca.markets/v2/iex”

ws = websocket.WebSocketApp(socket, on\_open=on\_open, on\_message=on\_message, on\_close=on\_close)

ws.run\_forever()

I receive the following message in the terminal:

“Process finished with exit code 0”

But no stream. Should I be looking somewhere else for the stream or should it pop up in the terminal? I have the same issue with v1 and v2.  
"
