<commit_before>import { WS }                from 'Services';
import {
    createMarkerSpotEntry,
    createMarkerSpotExit,
    createMarkerSpotMiddle,
    createMarkerExpiry,
    createMarkerPurchaseTime,
    createMarkerStartTime,
} from './chart-marker-helpers';

export const createChartTickMarkers = (SmartChartStore, contract_info) => {
    const tick_marker_handler = tickMarker.getInstance(SmartChartStore, contract_info);

    if (contract_info.exit_tick_time) {
        tick_marker_handler.addSpotsFromHistory();
        tick_marker_handler.addLines();
    } else {
        // TODO: implement middle tick markers for ongoing contracts
        // tick_marker_handler.addOnGoingMarkers();
    }
};

export const destroyChartTickMarkers = () => {
    tickMarker.destroyInstance();
};

const zip = (arr, ...arrs) => arr.map((val, idx) => arrs.reduce((a, curr) => [...a, curr[idx]], [val]));

const makeTickArr = (price_arr, times_arr) =>
    zip(price_arr, times_arr).reduce((acc, tick) => [...acc, { price: tick[0], time: tick[1] }], []);

const fnForEach = (fn) => ([...args]) => args.forEach(fn);

const tickMarker = (function () {
    let instance;

    const tickMarkerHandler = (SmartChartStore, { ...contract_info }) => {
        const ticks_history_req = {
            ticks_history: contract_info.underlying,
            start        : contract_info.entry_tick_time,
            end          : contract_info.exit_tick_time,
            count        : (contract_info.tick_count + 1), // add 1 to prevent for 1-tick contracts from returning 5000 ticks
        };
        const added_ticks = [];

        const addMarkerFromContract = (createMarkerFn, tick, idx) => {
            const align_label = labelTopOrBottom(tick, idx);
            const marker_config = createMarkerFn(contract_info, align_label);

            if (marker_config) SmartChartStore.createMarker(marker_config);
        };

        const labelTopOrBottom = (tick, idx) => {
            let align_label = 'top';
            if (tick && tick.price) {
                if (idx > 0) {
                    const prev_tick = added_ticks[idx - 1];

                    if (+tick.price < +prev_tick.price) align_label = 'bottom';
                    if (+tick.price === +prev_tick.price) align_label = prev_tick.align_label;
                }
                added_ticks.push({ ...tick, align_label });
            }
            return align_label;
        };

        const addMarkerFromTick = (createMarkerFn, tick, idx) => {
            const label_position = labelTopOrBottom(tick, idx);
            const marker_config = createMarkerFn(tick, idx, label_position);

            if (marker_config) SmartChartStore.createMarker(marker_config);
        };

        const isContractTick = tick =>
            +tick.time >= +contract_info.entry_tick_time && +tick.time <= +contract_info.exit_tick_time;

        const isMiddleTick = tick =>
            +tick.time > contract_info.entry_tick_time && tick.time < contract_info.exit_tick_time;

        const isEntryTick = tick => +tick.time === +contract_info.entry_tick_time;

        const isExitTick = tick => +tick.time === +contract_info.exit_tick_time;

        const addTickToChart = (tick, idx) => {
            const is_entry  = idx === 0 && isEntryTick(tick);
            const is_exit   = isExitTick(tick);
            const is_middle = isMiddleTick(tick);

            if (is_entry) addMarkerFromContract(createMarkerSpotEntry, tick, idx);
            if (is_exit) addMarkerFromContract(createMarkerSpotExit, tick, idx);
            if (is_middle) addMarkerFromTick(createMarkerSpotMiddle, tick, idx);
        };
<commit_msg>cleanup, fix naming<commit_after>import { WS }                from 'Services';
import {
    createMarkerSpotEntry,
    createMarkerSpotExit,
    createMarkerSpotMiddle,
    createMarkerExpiry,
    createMarkerPurchaseTime,
    createMarkerStartTime,
} from './chart-marker-helpers';

export const createChartTickMarkers = (SmartChartStore, contract_info) => {
    const tick_marker_handler = tickMarker.getInstance(SmartChartStore, contract_info);

    if (contract_info.exit_tick_time) {
        tick_marker_handler.addSpotsFromHistory();
        tick_marker_handler.addLines();
    } else {
        // TODO: implement middle tick markers for ongoing contracts
        // tick_marker_handler.addMarkerSpotsFromStream();
    }
};

export const destroyChartTickMarkers = () => {
    tickMarker.destroyInstance();
};

const zip = (arr, ...arrs) => arr.map((val, idx) => arrs.reduce((a, curr) => [...a, curr[idx]], [val]));

const makeTickArr = (price_arr, times_arr) =>
    zip(price_arr, times_arr).reduce((acc, tick) => [...acc, { price: tick[0], time: tick[1] }], []);

const fnForEach = (fn) => ([...args]) => args.forEach(fn);

const tickMarker = (function () {
    let instance;

    const tickMarkerHandler = (SmartChartStore, { ...contract_info }) => {
        const ticks_history_req = {
            ticks_history: contract_info.underlying,
            start        : contract_info.entry_tick_time,
            end          : contract_info.exit_tick_time,
            count        : (contract_info.tick_count + 1), // add 1 to prevent for 1-tick contracts from returning 5000 ticks
        };
        const ticks_added_to_chart = [];

        const addMarkerFromContract = (createMarkerFn, tick, idx) => {
            const align_label = labelTopOrBottom(tick, idx);
            const marker_config = createMarkerFn(contract_info, align_label);

            if (marker_config) SmartChartStore.createMarker(marker_config);
        };

        const labelTopOrBottom = (tick, idx) => {
            let align_label = 'top';
            if (tick && tick.price) {
                if (idx > 0 && ticks_added_to_chart.length > 1) {
                    const prev_tick = ticks_added_to_chart[idx - 1];

                    if (+tick.price < +prev_tick.price) align_label = 'bottom';
                    if (+tick.price === +prev_tick.price) align_label = prev_tick.align_label;
                }
                ticks_added_to_chart.push({ ...tick, align_label });
            }
            return align_label;
        };

        const addMarkerFromTick = (createMarkerFn, tick, idx) => {
            const label_position = labelTopOrBottom(tick, idx);
            const marker_config = createMarkerFn(tick, idx, label_position);

            if (marker_config) SmartChartStore.createMarker(marker_config);
        };

        const isContractTick = tick =>
            +tick.time >= +contract_info.entry_tick_time && +tick.time <= +contract_info.exit_tick_time;

        const isMiddleTick = tick =>
            +tick.time > contract_info.entry_tick_time && tick.time < contract_info.exit_tick_time;

        const isEntryTick = tick => +tick.time === +contract_info.entry_tick_time;

        const isExitTick = tick => +tick.time === +contract_info.exit_tick_time;

        const addTickToChart = (tick, idx) => {
            const is_entry  = idx === 0 && isEntryTick(tick);
            const is_exit   = isExitTick(tick);
            const is_middle = isMiddleTick(tick);

            if (is_entry) addMarkerFromContract(createMarkerSpotEntry, tick, idx);
            if (is_exit) addMarkerFromContract(createMarkerSpotExit, tick, idx);
            if (is_middle) addMarkerFromTick(createMarkerSpotMiddle, tick, idx);
        };

{'fraction_non_alphanumeric': 0.11595183331078339, 'fraction_numerical': 0.0033824922202678933, 'mean_word_length': 2.5675675675675675, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6963151', 'n_tokens_mistral': 2181, 'n_tokens_neox': 2086, 'n_words': 444}