All files / test/generators generateTrade.ts

100% Statements 9/9
100% Branches 2/2
100% Functions 1/1
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29    1x 4x 4x 4x 4x 4x 4x 4x                                   1x  
import {ChangeType, ITrade, OrderType, StreamType} from '../../src/interfaces';
 
const generateTrade = (code: string): ITrade => {
  const now = Date.now();
  const randomVolume = Math.floor(Math.random() * 1000);
  const randomPrice = Math.floor(Math.random() * 10000);
  const randomType = Math.random() < 0.5 ? 'ASK' : 'BID';
  const randomChange = Math.floor(Math.random() * 3);
  const randomSequentialId = Math.floor(Math.random() * 10000);
  return {
    type: 'trade',
    code: code,
    timestamp: now,
    trade_date: new Date(now).toISOString().substring(0, 10),
    trade_time: new Date(now).toISOString().substring(11, 19),
    trade_timestamp: now,
    trade_price: randomPrice,
    trade_volume: randomVolume,
    ask_bid: randomType as OrderType,
    prev_closing_price: randomPrice - randomChange,
    change: ['RISE', 'EVEN', 'FALL'][randomChange] as ChangeType,
    change_price: randomChange,
    sequential_id: randomSequentialId,
    stream_type: 'REALTIME' as StreamType,
  };
};
 
export default generateTrade;