Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.5 kB
0
Indexable
Never
import requests

# Define your Deriv API token and account ID
API_TOKEN = 'YOUR_API_TOKEN'
ACCOUNT_ID = 'YOUR_ACCOUNT_ID'

# Define the base URL for Deriv API
BASE_URL = 'https://api.deriv.com/binary/'

# Define the contract duration in seconds (2 minutes)
CONTRACT_DURATION = 120

# Define indicator parameters
MACD_SIGNAL_LENGTH = 9
MACD_SLOW_LENGTH = 26
MACD_FAST_LENGTH = 7
EMA_SHORT_LENGTH = 13
EMA_LONG_LENGTH = 86

def get_contract_parameters():
    response = requests.get(BASE_URL + 'tick/volatility_index25.json')
    data = response.json()
    price = data['tick']
    
    response = requests.get(BASE_URL + 'indicator/MACD',
                            params={'symbol': 'R_25',
                                    'period': 't2',
                                    'fast_length': MACD_FAST_LENGTH,
                                    'slow_length': MACD_SLOW_LENGTH,
                                    'signal_length': MACD_SIGNAL_LENGTH})
    macd_data = response.json()
    macd_histogram = macd_data['histogram']
    
    response = requests.get(BASE_URL + 'indicator/EMA',
                            params={'symbol': 'R_25',
                                    'period': 't2',
                                    'length': 200})
    ema_200 = response.json()
    
    response = requests.get(BASE_URL + 'indicator/EMA',
                            params={'symbol': 'R_25',
                                    'period': 't2',
                                    'length': EMA_SHORT_LENGTH})
    ema_short = response.json()
    
    response = requests.get(BASE_URL + 'indicator/EMA',
                            params={'symbol': 'R_25',
                                    'period': 't2',
                                    'length': EMA_LONG_LENGTH})
    ema_long = response.json()
    
    return price, macd_histogram, ema_200, ema_short, ema_long

def place_rise_contract():
    response = requests.post(BASE_URL + 'contract/ticks',
                             headers={'Authorization': f'Bearer {API_TOKEN}'},
                             json={'symbol': 'R_25',
                                   'contract_type': 'CALL',
                                   'amount': '1',
                                   'basis': 'stake',
                                   'duration': CONTRACT_DURATION})
    return response.json()

def place_fall_contract():
    response = requests.post(BASE_URL + 'contract/ticks',
                             headers={'Authorization': f'Bearer {API_TOKEN}'},
                             json={'symbol': 'R_25',
                                   'contract_type': 'PUT',
                                   'amount': '1',
                                   'basis': 'stake',
                                   'duration': CONTRACT_DURATION})
    return response.json()

def main():
    price, macd_histogram, ema_200, ema_short, ema_long = get_contract_parameters()
    
    if (price > ema_200) and (macd_histogram > 0) and (ema_short > ema_long):
        contract_response = place_rise_contract()
        print(f'Rise contract placed: {contract_response}')
    elif (price < ema_200) and (macd_histogram < 0) and (ema_short < ema_long):
        contract_response = place_fall_contract()
        print(f'Fall contract placed: {contract_response}')
    else:
        print('No suitable conditions for contract placement.')

if __name__ == '__main__':
    main()