0% found this document useful (0 votes)
3 views

Import Websocket

This document outlines a Python script that connects to the Deriv WebSocket API to receive market tick data and analyze it for trading patterns. It tracks the last three digits of ticks, identifies the most common digit, and places trades based on this analysis. The script includes functions for handling WebSocket events, analyzing patterns, and executing trades using the Deriv API token.

Uploaded by

munenejames2121
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Import Websocket

This document outlines a Python script that connects to the Deriv WebSocket API to receive market tick data and analyze it for trading patterns. It tracks the last three digits of ticks, identifies the most common digit, and places trades based on this analysis. The script includes functions for handling WebSocket events, analyzing patterns, and executing trades using the Deriv API token.

Uploaded by

munenejames2121
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import websocket

import json
import time
from collections import Counter

# Your Deriv API token (replace with your actual token)


API_TOKEN = "YOUR_DERIV_API_TOKEN"
DERIV_API_URL = "wss://ws.binaryws.com/websockets/v3?app_id=1089"

# Function to connect to Deriv WebSocket


def on_message(ws, message):
data = json.loads(message)
if "tick" in data:
last_tick = str(data["tick"]["quote"])[-3:] # Extract last 3 digits
tick_history.append(last_tick)
if len(tick_history) > 100:
tick_history.pop(0)
analyze_patterns()

def on_error(ws, error):


print(f"WebSocket error: {error}")

def on_close(ws, close_status_code, close_msg):


print("WebSocket closed. Reconnecting...")
time.sleep(5)
start_websocket()

def on_open(ws):
print("Connected to Deriv WebSocket!")
ws.send(json.dumps({"ticks": "R_100"})) # Subscribe to market ticks

def start_websocket():
ws = websocket.WebSocketApp(
DERIV_API_URL, on_message=on_message, on_error=on_error, on_close=on_close
)
ws.on_open = on_open
ws.run_forever()

# Function to analyze patterns and place trades


def analyze_patterns():
if len(tick_history) < 10:
return

digit_counts = Counter(tick_history)
most_common = digit_counts.most_common(1)
if most_common:
predicted_digit = most_common[0][0]
print(f"Most common pattern: {predicted_digit}")
place_trade(predicted_digit)

# Function to place a trade using the Deriv API


def place_trade(predicted_digit):
trade_data = {
"buy": 1,
"parameters": {
"amount": 1,
"basis": "stake",
"contract_type": "DIGITMATCH",
"currency": "USD",
"duration": 1,
"duration_unit": "t",
"symbol": "R_100",
"barrier": predicted_digit
},
"authorize": API_TOKEN
}

ws = websocket.WebSocket()
ws.connect(DERIV_API_URL)
ws.send(json.dumps({"authorize": API_TOKEN})) # Authenticate
ws.send(json.dumps(trade_data)) # Send trade request
response = ws.recv()
print("Trade Response:", response)
ws.close()

# Initialize tick history


tick_history = []

# Start WebSocket connection


start_websocket()

You might also like