Help with getting custom indicator to work

Starting out to learn a bit of coding. Started with generating one with ChatGPT. Trying to plot a line with previous one hour candle open. below does not plot anything. Can anyone review and provide feedback? Eventually want to also add previous hour high and low

onst predef = require(“./tools/predef”);

const meta = require(“./tools/meta”);

class PrevHourOpen {

init() {

    this.currentHour = null;

    this.currentHourOpen = null;

    this.prevHourOpen = null;

}



map(d) {



    // On Tradovate Web, timestamp is already a Date object

    const hour = d.timestamp.getHours();



    if (this.currentHour !== hour) {



        // Store completed hour open

        if (this.currentHourOpen !== null) {

            this.prevHourOpen = this.currentHourOpen;

        }



        // Start new hour tracking

        this.currentHour = hour;

        this.currentHourOpen = d.open();

    }



    return {

        prevHour: this.prevHourOpen

    };

}

}

module.exports = {

name: "Prev Hour Open (ETH Web)",

description: "Plots previous hour open for ES/NQ on 5m chart (ETH)",

calculator: PrevHourOpen,

tags: \["My Indicators"\],

params: \[\],

plots: {

    prevHour: {

        title: "Prev Hour Open"

    }

}

};

Yeah. I tried perplexity to help me with tradovate scripts, but I’m very disappointed. Also tradovate’s “help” doesn’t hold what its name promises.

Anyway, I’m not sure what your business logic is, but you may start of with the following template which does draw a line on the chart from “a” to “b”:

const predef = require("./tools/predef");
const meta = require("./tools/meta");
const { px, du, op } = require('./tools/graphics');

class PrevHourOpen {
    init() {
        this.currentHour = null;
        this.currentHourOpen = null;
        this.prevHourOpen = null;
    }

    map(d, i) {

        // On Tradovate Web, timestamp is already a Date object
        const hour = d.timestamp().getHours();

        if (this.currentHour !== hour) {
            // Store completed hour open 
            if (this.currentHourOpen !== null) {
                this.prevHourOpen = this.currentHourOpen;
            }

            // Start new hour tracking
            this.currentHour = hour;
            this.currentHourOpen = d.open();
        }

        let res = {};

        if (this.currentHourOpen && this.prevHourOpen) {
            let items = [{
                tag: 'LineSegments',
                key: 'testLine',
                lines: [{
                    tag: 'Line',
                    a: {
                        x: du(i - 5),
                        y: du(this.prevHourOpen)
                    },
                    b: {
                        x: du(i),
                        y: du(this.currentHourOpen)
                    },
                    infiniteStart: false,
                    infiniteEnd: false
                }],
                lineStyle: {
                    lineWidth: 1,
                    color: "red",
                    lineStyle: 1
                }
            }];

            res = {
                graphics: d.isLast() && {
                    items
                }

            };
        }

        return res;
    }
}

module.exports = {
    name: "Prev Hour Open (ETH Web)",
    description: "Plots previous hour open for ES/NQ on 5m chart (ETH)",
    calculator: PrevHourOpen,
    tags: ["Forum"],
    params: []
};

HTH

Graphity

Hi Graphity, thanks for taking a look at it. It did not work for me. I briefly see a red line once I add it and then it disappears. What I am trying to do is on a lower TF than 1 hour, draw a line on the previous hour candle open like in the attached image line on the (6pm, 7pm and 8pm). I was trying to start slow but eventually also want plot the previous hour candle’s midpoint value as well. Once i get the hang of it, I am planning to do the same for the Four hour candles.

1 Like

Had similar issue with ChatGPT tried the same with Claude AI CoWork and after 4 versions I have gotten exactly what I was after.