Check if currentPrice crossed or touched any of our price levels. If this is the case, we will have one or more price levels appearing between previousPrice and currentPrice OR previousPrice or currentPrice will be equel to a price level. Add the BOLD code to the end of your monitorLevels() method. It must become the last code of monitorLevels(). Study the comments and complete the code # Loop through displayList for i in range(0,len(displayList)): # Test if the first item in the current sub-list contains the text "Price Level" # Tip: Remember that each sub-list is a list within a list (displayList). So you have # to access its items via displayList followed by TWO indexes. . . . # Extract the second item from the current sub-list into a variable called priceLevel priceLevel = . . . # Test if priceLevel is between previousPrice and currentPrice OR # priceLevel == previousPrice OR # priceLevel == currentPrice if( . . . ): # Sound the alarm. Pass in the frequency and duration. if self.currentPrice > self.previousPrice: frequency = 800 duration = 700 else: frequency = 400 duration = 700 winsound.Beep(frequency, duration) # Print the text 'Alarm' with a green background colour, so that the user # can go back into the historical data to see what prices raised the alarm. . . . CODE def monitorLevels(self): threading.Timer(2.0, self.monitorLevels).start() self.previousPrice = self.currentPrice tickObj = self.getBitMexPrice() self.currentPrice = tickObj.price if self.previousPrice == 0.0: self.previousPrice = self.currentPrice print('') print('Price Check at ' + str(datetime.now()) + ' (Press Ctrl+C to stop the monitoring)') print('=================================================================================') displayList = [] for price in self.levelsList: priceLevelLabel = f'Price Level: {price}' displayList.append([str(priceLevelLabel), float(price)]) previousPriceLabel = f'\033[94mPrevious Price: {self.previousPrice}\033[0m' displayList.append([str(previousPriceLabel), float(self.previousPrice)]) if self.currentPrice > self.previousPrice: currentPriceLabel = f'\033[92mCurrent Price: {self.currentPrice}\033[0m' elif self.currentPrice < self.previousPrice: currentPriceLabel = f'\033[93mCurrent Price: {self.currentPrice}\033[0m' else: currentPriceLabel = f'\033[94mCurrent Price: {self.currentPrice}\033[0m' displayList.append([str(currentPriceLabel), float(self.currentPrice)]) displayList.sort(key=lambda x: x[1]) for item in displayList: print(item[0])
Check if currentPrice crossed or touched any of our price levels.
If this is the case, we will have one or more price levels appearing between previousPrice and currentPrice
OR
previousPrice or currentPrice will be equel to a price level.
Add the BOLD code to the end of your monitorLevels() method. It must become the last code of monitorLevels().
Study the comments and complete the code
# Loop through displayList
for i in range(0,len(displayList)):
# Test if the first item in the current sub-list contains the text "Price Level"
# Tip: Remember that each sub-list is a list within a list (displayList). So you have
# to access its items via displayList followed by TWO indexes.
. . .
# Extract the second item from the current sub-list into a variable called priceLevel
priceLevel = . . .
# Test if priceLevel is between previousPrice and currentPrice OR
# priceLevel == previousPrice OR
# priceLevel == currentPrice
if(
. . .
):
# Sound the alarm. Pass in the frequency and duration.
if self.currentPrice > self.previousPrice:
frequency = 800
duration = 700
else:
frequency = 400
duration = 700
winsound.Beep(frequency, duration)
# Print the text 'Alarm' with a green background colour, so that the user
# can go back into the historical data to see what prices raised the alarm.
. . .
CODE
Step by step
Solved in 3 steps with 1 images