Jump to content

Adjust stopLevel using streaming api data

Recommended Posts

Hi, I am trying to update the stops for an existing order using the streaming API.  Working / fledgling code so far:

import time

from ig_streaming_client import IgStreamingSession
from lightstreamer_client import LightstreamerSubscription
api_key = ""; account_id = ''; rest_api_username = ""; rest_api_password = ""

session = IgStreamingSession(api_key, account_id, rest_api_username, rest_api_password)

subscription = LightstreamerSubscription('MERGE',
                                         ['UPDATE_TIME', 'BID', 'OFFER'])

print(subscription.addlistener(lambda item: print(item['values']['OFFER']))) #####thisone



This successfully prints out the latest offer prices for 10 seconds. However, I am struggling to figure out how to take the real time offer values from the streaming data and check against my current order stopLevel and change the stopLevel if the streaming offer price goes below my threshold.

For the #####thisone line above, I am needing something like this (58000 is my threshold):

if subscription.addlistener(lambda item: item['values']['OFFER']) < 58000:
    ig_service.update_open_position(limit_level = None, stop_level = 58100, deal_id = dealId)

Massive gap in my streaming api knowledge I accept but any help would be appreciated.  There does not seem to be much out there that uses streaming data and "does stuff" with the outputs, apart from displaying in an html of course.

Edited by wki
Link to comment

ok I think I have figured out how to extract the value from lightstreamer and then do something with it.  I pass the streaming item through a function that extracts the appropriate value from the item, adds this value to a list, looks at the last item of that list and does something based on that value. Seems to work ok...

lst = []
def add2list(item):
    val = float(item['values']['OFFER'])
    if lst[-1] < 58000:
        return print('below',lst[-1])
        return print('above',lst[-1])

session = IgStreamingSession(api_key, account_id, rest_api_username, rest_api_password)

subscription = LightstreamerSubscription('MERGE',
                                         ['UPDATE_TIME', 'BID', 'OFFER'])
subscription.addlistener(lambda item: add2list(item))

Link to comment
  • 9 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • image.png

  • Posts

    • Is there any update on A Dividend Reinvestment Plan ?    Thanks  
    • How Merlin Is Addressing Bitcoin Scalability With A Unique Layer 2 Approach If you've been around the crypto world for a while, you're likely familiar with the limitations of Bitcoin's Layer 1 and its scalability issues. Over time, we've seen various innovative proposals like Wormhole's unique approach to address these limitations. In this context, Merlin Chain caught my attention. It's a project that aims to tackle Bitcoin's scalability challenges by offering a Layer 2 solution built on the ZK-Rollups model. This translates to faster and cheaper transactions while maintaining security through a decentralized oracle network with anti-fraud measures. Developed by the well-established Bitmap Tech team, Merlin Chain boasts a market cap exceeding $500 million. Their key objective has always been to empower existing Bitcoin assets by enabling them to interact seamlessly on the Layer 2 network. I believe backing from 20+ prominent VC firms like OKX Ventures and ViaBTC suggests confidence in Merlin Chain's future. MERL was launched as the first token on Bitget's pre-market trade platform and had a commendable price action. Is Merlin Chain's Layer 2 solution a long-term solution to this issue? Let's hear your candid opinions in the comments section below! Cheers! #Bitcoin #btc #halving #transactions #merlin #merl #bitget #web3 #wormhole
    • As the crypto sector undergoes transformation, Asia emerges as a formidable player alongside the established giants of the US and Europe. With significant investments and a growing user base, the region's potential for dominance is unmistakable. Leading this movement is Dubai, pioneering crypto adoption with its favorable regulations, attracting crypto exchanges focused on the region. A standout in this field is Bitget, Asia's largest web3 wallet provider, boasting a user base of over 19 million. Through regular events, Bitget cultivates community engagement, recognizing and rewarding the trust and loyalty of its users. One such event, the ongoing "Bitget Super Savings," offers over $20,000 worth of hot tokens across various categories, providing users with an opportunity to enrich their crypto portfolios. For those in South Asia, Bitget extends an invitation to participate in this ongoing event. Seize the chance to earn free hot tokens before the event concludes on April 26th. Good luck!
  • Create New...