in this tutorial I will show you how to use BinaryOptionsToolsV2
How to install BinaryOptionsToolsV2
to install bo2 – (BinaryOptionsToolsV2), you can use two methods, here are the two options:
- using PyPi
- building from source
How to install using PyPi
to install using PyPi, you need to first make sure you have python3 installed in your system.
then, in the terminal run the following command:
pip install binaryoptionstoolsv2==0.1.6a3
How to use the python version for BinaryOptionsToolsV2
so here is how to use the library
Check rather a trade was profitable or not
here is the code to accomplish this:
from BinaryOptionsToolsV2.pocketoption import PocketOptionAsync import asyncio # Main part of the code async def main(ssid: str): # The api automatically detects if the 'ssid' is for real or demo account api = PocketOptionAsync(ssid) (buy_id, _) = await api.buy(asset="EURUSD_otc", amount=1.0, time=15, check_win=False) (sell_id, _) = await api.sell(asset="EURUSD_otc", amount=1.0, time=300, check_win=False) print(buy_id, sell_id) # This is the same as setting checkw_win to true on the api.buy and api.sell functions buy_data = await api.check_win(buy_id) print(f"Buy trade result: {buy_data['result']}\nBuy trade data: {buy_data}") sell_data = await api.check_win(sell_id) print(f"Sell trade result: {sell_data['result']}\nSell trade data: {sell_data}") if __name__ == '__main__': ssid = input('Please enter your ssid: ') asyncio.run(main(ssid))
What’s going on in this code
This Python script demonstrates how to use the PocketOptionAsync
class to perform automated trading actions on the PocketOption platform. Let’s go step-by-step:
1. Import Statements
from BinaryOptionsToolsV2.pocketoption import PocketOptionAsync import asyncio
– PocketOptionAsync
: A class used to interact with PocketOption’s platform asynchronously.
– asyncio
: Python’s built-in async framework for handling asynchronous operations.
2. Define the Main Async Function
async def main(ssid: str):
This function defines the core logic. It accepts the user’s session ID (ssid
) to authenticate the API.
3. Authenticate and Connect
api = PocketOptionAsync(ssid)
The API automatically identifies whether the session is for a demo or real account.
4. Execute Trades
(buy_id, _) = await api.buy(asset="EURUSD_otc", amount=1.0, time=15, check_win=False) (sell_id, _) = await api.sell(asset="EURUSD_otc", amount=1.0, time=300, check_win=False)
This sends two trades:
- A buy trade with 15 seconds duration.
- A sell trade with 5 minutes (300 seconds) duration.
The check_win=False
means the win/loss result is not returned immediately.
5. Print Trade IDs
print(buy_id, sell_id)
Logs the unique IDs of the trades for later reference.
6. Check Results
buy_data = await api.check_win(buy_id) print(f"Buy trade result: {buy_data['result']}\nBuy trade data: {buy_data}") sell_data = await api.check_win(sell_id) print(f"Sell trade result: {sell_data['result']}\nSell trade data: {sell_data}")
After the trades expire, these lines check and print the outcome: win, lose, or draw, along with full trade data.
7. Main Entry Point
if __name__ == '__main__': ssid = input('Please enter your ssid: ') asyncio.run(main(ssid))
This is the entry point of the script. It asks the user for their session ID and starts the async process.
How to check your balance in BinaryOptionsToolsV2
here is the code example:
from BinaryOptionsToolsV2.pocketoption import PocketOptionAsync import asyncio # Main part of the code async def main(ssid: str): # The api automatically detects if the 'ssid' is for real or demo account api = PocketOptionAsync(ssid) await asyncio.sleep(5) balance = await api.balance() print(f"Balance: {balance}") if __name__ == '__main__': ssid = input('Please enter your ssid: ') asyncio.run(main(ssid))
This script connects to the PocketOption platform and fetches your current account balance using asynchronous Python code. Here’s what each part does:
1. Import Required Modules
from BinaryOptionsToolsV2.pocketoption import PocketOptionAsync import asyncio
– PocketOptionAsync
provides async access to PocketOption’s API.
– asyncio
is used to handle asynchronous operations.
2. Define the Main Function
async def main(ssid: str):
The main
function is asynchronous and takes the session ID (ssid
) as input. This ID is needed to authenticate and fetch account data.
3. Create API Instance
api = PocketOptionAsync(ssid)
Initializes the API client using the session ID. It will automatically detect if the session is for a demo or real account.
4. Wait Before Request
await asyncio.sleep(5)
Introduces a 5-second delay. This can help avoid API rate limits or give the server time to prepare a response.
5. Fetch Balance
balance = await api.balance() print(f"Balance: {balance}")
Calls the balance()
method to retrieve the current account balance and prints it to the console.
6. Script Entry Point
if __name__ == '__main__': ssid = input('Please enter your ssid: ') asyncio.run(main(ssid))
When the script runs directly, it asks the user to input their session ID, then starts the async function to check their balance.
Get Candles in BinaryOptionsToolsV2
from BinaryOptionsToolsV2.pocketoption import PocketOptionAsync import pandas as pd import asyncio # Main part of the code async def main(ssid: str): # The api automatically detects if the 'ssid' is for real or demo account api = PocketOptionAsync(ssid) await asyncio.sleep(5) # Candñes are returned in the format of a list of dictionaries times = [ 3600 * i for i in range(1, 11)] time_frames = [ 1, 5, 15, 30, 60, 300] for time in times: for frame in time_frames: candles = await api.get_candles("EURUSD_otc", 60, time) # print(f"Raw Candles: {candles}") candles_pd = pd.DataFrame.from_dict(candles) print(f"Candles: {candles_pd}") if __name__ == '__main__': ssid = input('Please enter your ssid: ') asyncio.run(main(ssid))
This Python script asynchronously fetches historical candlestick data from the PocketOption platform and formats it using pandas for easier analysis. Let’s go through the code step-by-step:
1. Import Statements
from BinaryOptionsToolsV2.pocketoption import PocketOptionAsync import pandas as pd import asyncio
– PocketOptionAsync
: Provides async access to PocketOption.
– pandas
: Used to format and manipulate data as dataframes.
– asyncio
: Enables asynchronous execution in Python.
2. Define the Main Async Function
async def main(ssid: str):
The core function of the script. It uses the user’s ssid
to connect and authenticate.
3. Create API Instance
api = PocketOptionAsync(ssid)
Initializes the PocketOption client using the session ID, automatically selecting real or demo mode.
4. Wait Before Execution
await asyncio.sleep(5)
Waits for 5 seconds before starting API calls. This can help avoid API throttling or timeouts.
5. Setup Time Ranges
times = [ 3600 * i for i in range(1, 11)] time_frames = [ 1, 5, 15, 30, 60, 300]
– times
: A list of time offsets (in seconds), from 1 hour to 10 hours.
– time_frames
: Common candle durations in seconds (e.g., 1-minute, 5-minute, etc.).
6. Nested Loop for Fetching Candles
for time in times: for frame in time_frames: candles = await api.get_candles("EURUSD_otc", 60, time) candles_pd = pd.DataFrame.from_dict(candles) print(f"Candles: {candles_pd}")
– For each combination of time and frame, the script requests 60 candlesticks of the EURUSD_otc
asset.
– Converts the result (a list of dictionaries) into a pandas DataFrame.
– Prints the structured DataFrame to the console for inspection or later analysis.
7. Script Entry Point
if __name__ == '__main__': ssid = input('Please enter your ssid: ') asyncio.run(main(ssid))
Prompts the user for a session ID and runs the main asynchronous logic using asyncio.run
.