ddsaif / Roshan_tasks

0 stars 0 forks source link

Wednesday same task as yesterday #2

Open ddsaif opened 2 weeks ago

ddsaif commented 2 weeks ago

Work on yesterday's task complete it ASAP.

  1. Change the ladder Model as provided in the repository. (these are the original fields as used in the table)
  2. update the table by replacing the values as received from the req body.
  3. After updating the ladder table with the value then create a buy and a sell order as discussed yesterday.
roshanK1998 commented 2 weeks ago

Started working on the same.

roshanK1998 commented 2 weeks ago

I tried to work on the code and the response which, I am receiving right now is this { "message": "Error updating ladder", "error": { "name": "SequelizeDatabaseError", "parent": { "code": "ER_BAD_FIELD_ERROR", "errno": 1054, "sqlState": "42S22", "sqlMessage": "Unknown column 'id' in 'field list'", "sql": "INSERT INTO ordersnewtable_1 (id,order_type,order_status,order_user_id,order_position_id,order_automated,order_open_price,order_trading_mode,order_units,order_closed_units,order_cash_gain,order_realized_profit,order_exchange,order_ticker_id,order_stock_name,order_follow_up,createdAt,updatedAt) VALUES (DEFAULT,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);", "parameters": [ "BUY", "OPEN", "721", 788, true, "NaN", "SIMULATION", 12, 0, 0, 0, "BSE", 9418, "TCS", "'Buy&Sell'", "2024-11-06 05:29:28", "2024-11-06 05:29:28" ] }, "original": { "code": "ER_BAD_FIELD_ERROR", "errno": 1054, "sqlState": "42S22", "sqlMessage": "Unknown column 'id' in 'field list'", "sql": "INSERT INTO ordersnewtable_1 (id,order_type,order_status,order_user_id,order_position_id,order_automated,order_open_price,order_trading_mode,order_units,order_closed_units,order_cash_gain,order_realized_profit,order_exchange,order_ticker_id,order_stock_name,order_follow_up,createdAt,updatedAt) VALUES (DEFAULT,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);", "parameters": [ "BUY", "OPEN", "721", 788, true, "NaN", "SIMULATION", 12, 0, 0, 0, "BSE", 9418, "TCS", "'Buy&Sell'", "2024-11-06 05:29:28", "2024-11-06 05:29:28" ] }, "sql": "INSERT INTO ordersnewtable_1 (id,order_type,order_status,order_user_id,order_position_id,order_automated,order_open_price,order_trading_mode,order_units,order_closed_units,order_cash_gain,order_realized_profit,order_exchange,order_ticker_id,order_stock_name,order_follow_up,createdAt,updatedAt) VALUES (DEFAULT,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);", "parameters": [ "BUY", "OPEN", "721", 788, true, "NaN", "SIMULATION", 12, 0, 0, 0, "BSE", 9418, "TCS", "'Buy&Sell'", "2024-11-06 05:29:28", "2024-11-06 05:29:28" ] } }

I have not mentioned id anywhere in the code. Not able to understand from where its taking the field id

ddsaif commented 2 weeks ago

keep updating your code as well so i can review it.

roshanK1998 commented 2 weeks ago

okay, I will push the code

ddsaif commented 2 weeks ago

what is logging in console

roshanK1998 commented 2 weeks ago

PS C:\Users\HP\Desktop\ladder> node server.js Server is running on http://localhost:3000 Database synced ladder details here ---- Ladder { dataValues: { lad_id: 1283, lad_user_id: 721, lad_position_id: 788, lad_ticker: 'TCS', lad_name: 1, lad_status: 'ACTIVE', lad_ticker_id: 9418, lad_exchange: 'BSE', lad_trading_mode: 'SIMULATION', lad_cash_allocated: 995000, lad_cash_gain: '-837271.73', lad_cash_left: 995000, lad_last_trade_price: 0, lad_last_trade_order_price: '3420.94', lad_minimum_price: 0, lad_extra_cash_generated: '4568.02', lad_realized_profit: '2199.42', lad_initial_buy_quantity: 133, lad_default_buy_sell_quantity: 3, lad_target_price: 10000, lad_num_of_steps_above: 44, lad_num_of_steps_below: 0, lad_cash_needed: '155636.54', lad_recent_trade_id: 2584, createdAt: '03-11-2024 17:40', updatedAt: '03-11-2024 17:45', lad_deleted: false, lad_initial_buy_price: 5000, lad_current_quantity: 175, lad_definition_id: 1389, lad_initial_buy_executed: true, lad_step_size: '112.79', lad_extra_cash_left: 0, lad_reinvest_extra_cash: false, lad_unsold_stock_cash_gain: '-839471.15', target_price_multiplier: 2, continue_trading_after_hitting_target_price: true, lad_recent_order_id: '' }, _previousDataValues: { lad_id: 1283, lad_user_id: 721, lad_position_id: 788, lad_ticker: 'TCS', lad_name: 1, lad_status: 'ACTIVE', lad_ticker_id: 9418, lad_exchange: 'BSE', lad_trading_mode: 'SIMULATION', lad_cash_allocated: 995000, lad_cash_gain: '-837271.73', lad_cash_left: 995000, lad_last_trade_price: 0, lad_last_trade_order_price: '3420.94', lad_minimum_price: 0, lad_extra_cash_generated: '4568.02', lad_realized_profit: '2199.42', lad_initial_buy_quantity: 133, lad_default_buy_sell_quantity: 3, lad_target_price: 10000, lad_num_of_steps_above: 44, lad_num_of_steps_below: 0, lad_cash_needed: '155636.54', lad_recent_trade_id: 2584, createdAt: '03-11-2024 17:40', updatedAt: '03-11-2024 17:45', lad_deleted: false, lad_initial_buy_price: 5000, lad_current_quantity: 175, lad_definition_id: 1389, lad_initial_buy_executed: true, lad_step_size: '112.79', lad_extra_cash_left: 0, lad_reinvest_extra_cash: false, lad_unsold_stock_cash_gain: '-839471.15', target_price_multiplier: 2, continue_trading_after_hitting_target_price: true, lad_recent_order_id: '' }, uniqno: 1, _changed: Set(0) {}, _options: { isNewRecord: false, _schema: null, _schemaDelimiter: '', raw: true, attributes: [ 'lad_id', 'lad_user_id', 'lad_position_id', 'lad_ticker', 'lad_name', 'lad_status', 'lad_ticker_id', 'lad_exchange', 'lad_trading_mode', 'lad_cash_allocated', 'lad_cash_gain', 'lad_cash_left', 'lad_last_trade_price', 'lad_last_trade_order_price', 'lad_minimum_price', 'lad_extra_cash_generated', 'lad_realized_profit', 'lad_initial_buy_quantity', 'lad_default_buy_sell_quantity', 'lad_target_price', 'lad_num_of_steps_above', 'lad_num_of_steps_below', 'lad_cash_needed', 'lad_recent_trade_id', 'createdAt', 'updatedAt', 'lad_deleted', 'lad_initial_buy_price', 'lad_current_quantity', 'lad_definition_id', 'lad_initial_buy_executed', 'lad_step_size', 'lad_extra_cash_left', 'lad_reinvest_extra_cash', 'lad_unsold_stock_cash_gain', 'target_price_multiplier', 'continue_trading_after_hitting_target_price', 'lad_recent_order_id' ] }, isNewRecord: false } Error updating ladder: Error at Query.run (C:\Users\HP\Desktop\ladder\node_modules\sequelize\lib\dialects\mysql\query.js:52:25) at C:\Users\HP\Desktop\ladder\node_modules\sequelize\lib\sequelize.js:315:28 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async MySQLQueryInterface.insert (C:\Users\HP\Desktop\ladder\node_modules\sequelize\lib\dialects\abstract\query-interface.js:308:21) at async Order.save (C:\Users\HP\Desktop\ladder\node_modules\sequelize\lib\model.js:2490:35) at async Order.create (C:\Users\HP\Desktop\ladder\node_modules\sequelize\lib\model.js:1362:12) at async withdrawOrAddFundsToLadder (C:\Users\HP\Desktop\ladder\controller\addSubstractController.js:98:13) { name: 'SequelizeDatabaseError', parent: Error: Unknown column 'id' in 'field list' at Packet.asError (C:\Users\HP\Desktop\ladder\node_modules\mysql2\lib\packets\packet.js:738:17) at Prepare.execute (C:\Users\HP\Desktop\ladder\node_modules\mysql2\lib\commands\command.js:29:26) at Connection.handlePacket (C:\Users\HP\Desktop\ladder\node_modules\mysql2\lib\connection.js:481:34) at PacketParser.onPacket (C:\Users\HP\Desktop\ladder\node_modules\mysql2\lib\connection.js:97:12) at PacketParser.executeStart (C:\Users\HP\Desktop\ladder\node_modules\mysql2\lib\packet_parser.js:75:16) at Socket. (C:\Users\HP\Desktop\ladder\node_modules\mysql2\lib\connection.js:104:25) at Socket.emit (node:events:519:28) at addChunk (node:internal/streams/readable:559:12) at readableAddChunkPushByteMode (node:internal/streams/readable:510:3) at Readable.push (node:internal/streams/readable:390:5) { code: 'ER_BAD_FIELD_ERROR', errno: 1054, sqlState: '42S22', sqlMessage: "Unknown column 'id' in 'field list'", sql: 'INSERT INTO ordersnewtable_1 (id,order_type,order_status,order_user_id,order_position_id,order_automated,order_open_price,order_trading_mode,order_units,order_closed_units,order_cash_gain,order_realized_profit,order_exchange,order_ticker_id,order_stock_name,order_follow_up,createdAt,updatedAt) VALUES (DEFAULT,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);', parameters: [ 'BUY', 'OPEN', '721', 788, true, 'NaN', 'SIMULATION', 12, 0, 0, 0, 'BSE', 9418, 'TCS', "'Buy&Sell'", '2024-11-06 05:47:39', '2024-11-06 05:47:39' ] }, original: Error: Unknown column 'id' in 'field list' at Packet.asError (C:\Users\HP\Desktop\ladder\node_modules\mysql2\lib\packets\packet.js:738:17) at Prepare.execute (C:\Users\HP\Desktop\ladder\node_modules\mysql2\lib\commands\command.js:29:26) at Connection.handlePacket (C:\Users\HP\Desktop\ladder\node_modules\mysql2\lib\connection.js:481:34) at PacketParser.onPacket (C:\Users\HP\Desktop\ladder\node_modules\mysql2\lib\connection.js:97:12) at PacketParser.executeStart (C:\Users\HP\Desktop\ladder\node_modules\mysql2\lib\packet_parser.js:75:16) at Socket. (C:\Users\HP\Desktop\ladder\node_modules\mysql2\lib\connection.js:104:25) at Socket.emit (node:events:519:28) at addChunk (node:internal/streams/readable:559:12) at readableAddChunkPushByteMode (node:internal/streams/readable:510:3) at Readable.push (node:internal/streams/readable:390:5) { code: 'ER_BAD_FIELD_ERROR', errno: 1054, sqlState: '42S22', sqlMessage: "Unknown column 'id' in 'field list'", sql: 'INSERT INTO ordersnewtable_1 (id,order_type,order_status,order_user_id,order_position_id,order_automated,order_open_price,order_trading_mode,order_units,order_closed_units,order_cash_gain,order_realized_profit,order_exchange,order_ticker_id,order_stock_name,order_follow_up,createdAt,updatedAt) VALUES (DEFAULT,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);', parameters: [ 'BUY', 'OPEN', '721', 788, true, 'NaN', 'SIMULATION', 12, 0, 0, 0, 'BSE', 9418, 'TCS', "'Buy&Sell'", '2024-11-06 05:47:39', '2024-11-06 05:47:39' ] }, sql: 'INSERT INTO ordersnewtable_1 (id,order_type,order_status,order_user_id,order_position_id,order_automated,order_open_price,order_trading_mode,order_units,order_closed_units,order_cash_gain,order_realized_profit,order_exchange,order_ticker_id,order_stock_name,order_follow_up,createdAt,updatedAt) VALUES (DEFAULT,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);', parameters: [ 'BUY', 'OPEN', '721', 788, true, 'NaN', 'SIMULATION', 12, 0, 0, 0, 'BSE', 9418, 'TCS', "'Buy&Sell'", '2024-11-06 05:47:39', '2024-11-06 05:47:39' ] }

ddsaif commented 2 weeks ago

check your order table is properly available in your database or not

roshanK1998 commented 2 weeks ago

I have resolve the issue the response which i am getting is this { "message": "Ladder updated successfully.", "ladder": { "lad_id": 1283, "lad_user_id": 721, "lad_position_id": 788, "lad_ticker": "TCS", "lad_name": 1, "lad_status": "ACTIVE", "lad_ticker_id": 9418, "lad_exchange": "BSE", "lad_trading_mode": "SIMULATION", "lad_cash_allocated": 995000, "lad_cash_gain": "-837271.73", "lad_cash_left": 995000, "lad_last_trade_price": 0, "lad_last_trade_order_price": "3420.94", "lad_minimum_price": 0, "lad_extra_cash_generated": "4568.02", "lad_realized_profit": "2199.42", "lad_initial_buy_quantity": 133, "lad_default_buy_sell_quantity": 3, "lad_target_price": 10000, "lad_num_of_steps_above": 44, "lad_num_of_steps_below": 0, "lad_cash_needed": "155636.54", "lad_recent_trade_id": 2584, "createdAt": "03-11-2024 17:40", "updatedAt": "03-11-2024 17:45", "lad_deleted": false, "lad_initial_buy_price": 5000, "lad_current_quantity": 175, "lad_definition_id": 1389, "lad_initial_buy_executed": true, "lad_step_size": "112.79", "lad_extra_cash_left": 0, "lad_reinvest_extra_cash": false, "lad_unsold_stock_cash_gain": "-839471.15", "target_price_multiplier": 2, "continue_trading_after_hitting_target_price": true, "lad_recent_order_id": "" } }

Please, Check the response and do let me know weather it is correct or not...

roshanK1998 commented 2 weeks ago

I have updated the code as well.

ddsaif commented 2 weeks ago

send request body as well

roshanK1998 commented 2 weeks ago

This is the request body, I am using

{

"lad_id": 1283,
"new_stocks_to_buy": 14, 
"new_cash_needed": 123.23,
"new_order_size": 12,
"new_step_size":213

}

ddsaif commented 2 weeks ago

send the table on whatsapp as csv

roshanK1998 commented 2 weeks ago

Okay

ddsaif commented 2 weeks ago

Nothing is updated in ladder table and there are no order created in the orderTable as well

ddsaif commented 2 weeks ago

in ladder table cash needed, order size and step size should be updated for that ladder

ddsaif commented 2 weeks ago

And 2 orders should be created in the order table for that ladder id of that user

roshanK1998 commented 2 weeks ago

okay..I will try to make it

ddsaif commented 2 weeks ago

Explain to me what does the function do or what are we trying to do here? (It is important for us that we know that we are on the right track, so we know we are building correct thing and don't need to redo things)

roshanK1998 commented 2 weeks ago

Actually, I have discussed this thing with pervez sir yesterday, I want to know the overview of the project. Like what are we doing and on what part i am working on. So he told me to ask you this things. Whenever you are free can you arrange a call on Google meet to make me understand all this things.

roshanK1998 commented 2 weeks ago

Now the orders are getting created. But as you said the orders are wrong can you please check the code in your machine once. Its getting difficult for me can you please do it and make me understand that. I will push the code please check it.

ddsaif commented 2 weeks ago

start fresh, make only orders buy or sell with the order size as units and save it in database and nothing else

roshanK1998 commented 2 weeks ago

I have added new file as TestwithdrawOrAddFundsToLadder. Once check will that logic works

roshanK1998 commented 2 weeks ago

const sequelize = require('../config/database'); const Ladder = require('../model/ladderModel'); const dd_orders = require('../model/orderModel');

const withdrawOrAddFundsToLadder = async (req, res) => { const transaction = await sequelize.transaction(); try { const lad_user_id = req.params.id; const { lad_id, new_cash_allocated, new_cash_needed, new_order_size, new_step_size, order_type } = req.body;

    if (!lad_id || !lad_user_id) {
        return res.status(400).json({ message: "lad_id and lad_user_id are required." });
    }

    const ladder = await Ladder.findOne({ where: { lad_id, lad_user_id } });

    if (!ladder) {
        return res.status(404).json({ message: "Ladder not found for the specified user." });
    } 

    ladder.cash_needed = new_cash_needed;          
    ladder.order_size = new_order_size;           
    ladder.step_size = new_step_size;            

    await ladder.save({ transaction });

    const currentPrice = ladder.currentPrice;

    if (!ladder.lad_position_id) {
        return res.status(400).json({ message: "Ladder position ID cannot be null." });
    }

    const orderData = {
        order_type: order_type === "SELL" ? "SELL" : "BUY", 
        order_status: "OPEN",
        order_user_id: lad_user_id,
        order_position_id: ladder.lad_position_id,
        order_automated: true,
        order_open_price: order_type === "SELL" ? currentPrice + ladder.step_size : currentPrice - ladder.step_size,
        order_trading_mode: ladder.lad_trading_mode,
        order_units: new_order_size,
        order_closed_units: 0,
        order_cash_gain: 0,
        order_realized_profit: 0,
        order_exchange: ladder.lad_exchange,
        order_ticker_id: ladder.lad_ticker_id,
        order_stock_name: ladder.lad_ticker,
        order_follow_up: "Buy&Sell",
    };

    await dd_orders.create(orderData, { transaction });

    await transaction.commit();

    res.status(200).json({ message: "Order created successfully", ladder });
} catch (error) {

    await transaction.rollback();
    console.error("Error updating ladder and creating order:", error);
    res.status(500).json({ message: "Error updating ladder and creating order", error });
}

};

module.exports = { withdrawOrAddFundsToLadder, };

please checkout this

start fresh, make only orders buy or sell with the order size as units and save it in database and nothing else

ddsaif commented 2 weeks ago

try console.log the currentPrice

roshanK1998 commented 2 weeks ago

its getting Current Price: undefined

roshanK1998 commented 2 weeks ago

What will be my today's task?