deriv-com / deriv-api

Websocket API for deriv.app
MIT License
56 stars 97 forks source link

Buy contract is not updating #135

Open bidreshivaprasad opened 2 years ago

bidreshivaprasad commented 2 years ago

As you can see below @ buy_contract_request ----> " amount ": lot, this is not updating in loop As you can see below on the 8th line ---> let lot = 0.35; this value changes on every loop When I run this code it should increase the amount on every loop eg:- amount : 0.35 , amount : 0.77 ,amount: 1.5 But it's not increasing the amount in loop, It just taking trade like , amount : 0.35, amount : 0.35,amount : 0.35

const WebSocket = require('ws'); const DerivAPIBasic = require('@deriv/deriv-api/dist/DerivAPIBasic'); const app_id = 1089; // Replace with your app_id or leave as 1089 for testing. const connection = new WebSocket(wss://ws.binaryws.com/websockets/v3?app_id=${app_id}); const api = new DerivAPIBasic({ connection }); const token = 'xxxxxxxxxxx';// ----->API token

let trade_status = 0;

let lot = 0.35;

const buy_contract_request = { "buy": 1, "subscribe": 1, "price": 1000, "parameters": { "amount": lot, // this is not updating "duration": 1, "basis": "stake", "symbol": "R_10", "currency": "USD", "duration_unit": "t", "contract_type": "CALL" } }

const buyContractResponse = async (res) => { const data = JSON.parse(res.data); const is_sold = data.proposal_open_contract?.is_sold;

if (data.error !== undefined) {
    console.log('Error : %o', data.error.message);
    connection.removeEventListener('message', buyContractResponse, false);
    //await api.disconnect();
}

if (data.msg_type === 'buy') {
    console.log(`Contract Id ${data.buy.contract_id} \n`);
    console.log(`Details ${data.buy.longcode} \n`);
}

if (data.msg_type === 'proposal_open_contract') {
    // If `is_sold` is true it means our contract has finished and we can see if we won or not.
    if (is_sold) {
        const contract_status = data.proposal_open_contract.status;
        const contract_profit = data.proposal_open_contract.profit;
        console.log(`Profit ${contract_profit} \n`);
        console.log(`Contract ${contract_status} \n`);
        connection.removeEventListener('message', buyContractResponse, false);
        //await api.disconnect();
        trade_status = 0;
        get_all = lot * 2.5 ;
        lot = get_all.toFixed(2);
        console.log(lot)
    } else {
        // We can track the status of our contract as updates to the spot price occur.
        let entry_spot = 0; 
        const entry_tick = data.proposal_open_contract.entry_tick;
        const current_spot = data.proposal_open_contract.current_spot;
        if (typeof (entry_tick) !== 'undefined') entry_spot = entry_tick;
        //console.log(`Entry spot ${entry_spot} \n`);
       // console.log(`Current spot ${current_spot} \n`);
        //console.log(`Difference ${current_spot - entry_spot} \n`);
    }
}

}

const buyContract = async () => { await api.authorize(token); await api.buy(buy_contract_request); connection.addEventListener('message', buyContractResponse);

}

setInterval(function () { if (trade_status === 0){

buyContract();
trade_status = 1;

}}, 1);