Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buy contract is not updating #135

Open
bidreshivaprasad opened this issue Aug 24, 2022 · 0 comments
Open

Buy contract is not updating #135

bidreshivaprasad opened this issue Aug 24, 2022 · 0 comments

Comments

@bidreshivaprasad
Copy link

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant