Python Sync Examples
This directory contains synchronous examples using BinaryOptionsToolsV2.
Examplesโ
Basic Exampleโ
File: get_balance.py
from BinaryOptionsToolsV2.pocketoption import PocketOption
# Main part of the code
def main(ssid: str):
# Use context manager for automatic connection and cleanup
with PocketOption(ssid) as api:
balance = api.balance()
print(f"Balance: {balance}")
if __name__ == "__main__":
ssid = input("Please enter your ssid: ")
main(ssid)
Check Trade Resultโ
File: check_win.py
import time
from BinaryOptionsToolsV2.pocketoption import PocketOption
def main(ssid: str):
with PocketOption(ssid) as api:
# Place a trade first
buy_id, buy = api.buy(
asset="EURUSD_otc", amount=1.0, time=60, check_win=False
)
print(f"Trade placed: {buy_id}")
# Wait for trade to complete
time.sleep(65)
# Check result
result = api.check_win(buy_id)
if result.get("profit", 0) > 0:
print(f"WIN! Profit: ${result['profit']:.2f}")
else:
print(f"LOSS! Loss: ${abs(result.get('profit', 0)):.2f}")
if __name__ == "__main__":
ssid = input("Please enter your ssid: ")
main(ssid)
Get Historical Candlesโ
File: get_candles.py
from BinaryOptionsToolsV2.pocketoption import PocketOption
def main(ssid: str):
with PocketOption(ssid) as api:
# Get last 100 candles with 60-second period
candles = api.get_candles("EURUSD_otc", 60, 100)
print(f"Retrieved {len(candles)} candles")
for candle in candles[:5]: # Show first 5
print(f" Time: {candle.get('time')}, Close: {candle.get('close')}")
if __name__ == "__main__":
ssid = input("Please enter your ssid: ")
main(ssid)
Subscribe to Real-time Dataโ
File: subscribe_symbol.py
import time
from BinaryOptionsToolsV2.pocketoption import PocketOption
def main(ssid: str):
with PocketOption(ssid) as api:
# Subscribe to 60-second candles
subscription = api.subscribe("EURUSD_otc", 60)
print("Subscribed to EURUSD_otc")
# Iterate over candles (iterator)
for candle in subscription:
print(f"Candle: {candle}")
time.sleep(1) # Process candles
if __name__ == "__main__":
ssid = input("Please enter your ssid: ")
main(ssid)
Place a Tradeโ
File: trade.py
from BinaryOptionsToolsV2.pocketoption import PocketOption
# Main part of the code
def main(ssid: str):
# Use context manager for automatic connection and cleanup
with PocketOption(ssid) as api:
(buy_id, buy) = api.buy(
asset="EURUSD_otc", amount=1.0, time=60, check_win=False
)
print(f"Buy trade id: {buy_id}\nBuy trade data: {buy}")
(sell_id, sell) = api.sell(
asset="EURUSD_otc", amount=1.0, time=60, check_win=False
)
print(f"Sell trade id: {sell_id}\nSell trade data: {sell}")
if __name__ == "__main__":
ssid = input("Please enter your ssid: ")
main(ssid)
Raw Handler Usageโ
File: raw_send.py
import json
from BinaryOptionsToolsV2.pocketoption import PocketOption
from BinaryOptionsToolsV2.validator import Validator
def main(ssid: str):
with PocketOption(ssid) as api:
# Create validator for balance messages
validator = Validator.contains('"balance"')
# Create raw handler
handler = api.create_raw_handler(validator)
# Send custom message
handler.send_text('42["getBalance"]')
# Wait for response
response = handler.wait_next()
data = json.loads(response)
print(f"Balance: {data.get('balance', 'N/A')}")
if __name__ == "__main__":
ssid = input("Please enter your ssid: ")
main(ssid)
Get Payout Informationโ
File: payout.py
from BinaryOptionsToolsV2.pocketoption import PocketOption
def main(ssid: str):
with PocketOption(ssid) as api:
# Get payout for asset
payout = api.payout("EURUSD_otc")
print(f"Payout for EURUSD_otc: {payout * 100}%")
if __name__ == "__main__":
ssid = input("Please enter your ssid: ")
main(ssid)
Login with Email and Passwordโ
File: login_with_email_and_password.py
from BinaryOptionsToolsV2.pocketoption.tools.login import login
from BinaryOptionsToolsV2.pocketoption import PocketOption
def main(email: str, password: str):
# Login to get SSID
ssid = login(email, password)
print(f"SSID: {ssid}")
# Use SSID with client
with PocketOption(ssid) as api:
balance = api.balance()
print(f"Balance: {balance}")
if __name__ == "__main__":
email = input("Email: ")
password = input("Password: ")
main(email, password)
Comprehensive Demoโ
File: comprehensive_demo.py (async version available)
# Note: Comprehensive demo is available in async examples
# See examples/python/async/comprehensive_demo.py
Trading Strategy Exampleโ
File: strategy_example.py (async version available)
# Note: Strategy example is available in async examples
# See examples/python/async/strategy_example.py
Running Examplesโ
cd examples/python/sync
python get_balance.py
python trade.py
python get_candles.py
python subscribe_symbol.py
python raw_send.py
python payout.py
python login_with_email_and_password.py
Key Conceptsโ
Context Managerโ
All examples use the context manager pattern:
with PocketOption(ssid) as api:
# API is automatically connected and cleaned up
Initialization Waitโ
The synchronous client handles initialization internally, but you can add a small delay:
with PocketOption(ssid) as api:
import time
time.sleep(2) # Optional, for stability
Demo vs Real Accountโ
if not api.is_demo():
print("WARNING: Using REAL account!")
Proper Cleanupโ
The context manager handles cleanup automatically, but you can also call:
api.shutdown()
Sync vs Asyncโ
| Feature | Sync | Async |
|---|---|---|
| Syntax | with PocketOption(ssid) | async with PocketOptionAsync(ssid) |
| Methods | api.balance() | await api.balance() |
| Sleep | time.sleep() | await asyncio.sleep() |
| Iteration | for candle in subscription | async for candle in subscription |
Use async for:
- High-frequency trading
- Multiple concurrent operations
- Better performance with many subscriptions
Use sync for:
- Simple scripts
- Single operations
- Easier debugging
Build strategies fasterโ
- โจ ChipaEditor โ AI-powered algorithmic trading strategy builder: describe your edge, get a working CHTL strategy, backtest it on historical data, deploy it to a live broker. Free tier, browser and Android. (A trading platform โ not a general-purpose code editor.)
- ๐ ChipaX โ hybrid crypto exchange: perpetuals, spot and margin, with demo mode.
- Chipa Ecosystem overview โ how these fit together with this library.