Announcement

Collapse
No announcement yet.

How to put line or image at mouse cursor continuously while the cursor is moving?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to put line or image at mouse cursor continuously while the cursor is moving?

    I just would like to have the mouse cursor area look different depending on what is being done in the efs. eg grabbing my soft stop line, onLButtondown, and moving it up a bit to a different price.

  • #2
    Hello diesel,
    I put together an eSignal backtest code program to allow you to experiment with moving the stop with a mouse click. If you put this on a one-minute chart, it will give you lots of opportunity to experiment with it and gain confidence that it’s doing what you want before you put it into a real-time streaming program. Some of the key points you’d have to consider for an actual real-time trading program are as follows:

    You need to declare both your nBuyStop and nSellStop globally…i.e., outside of main. Your program can then assign a value to the stops which will then be triggered based upon your program: Trade.sellLimit(nBuyStop, …).

    The way I wrote it, the mouse barIndex has to be above the closing price (or streaming tick close) to move the nBuyStop. If it’s below the close, it’ll move the nSellStop. The mouse bar index itself doesn’t matter, it could be -100, if you’re below the closing price of that bar, it will reset the nSellStop to the yValue of the mouse rounded to the proper tick. Instead of the bar’s close, you could use the bar’s high or low.

    Since it would be a pain to try to get the mouse at an exact proper tick for the symbol that you’re using, I’m including a function that will round the price to a proper tick price based on the symbol for your order. The tick size can be determined dynamically with Symbol.minTick() if you’re logged into your broker, otherwise you can just enter the tick size as I’ve done here.

    I have the program printing to the “Formula Output” window so that you can also see how the stops are changed from the old stop value to the new value. If the markets aren’t active, i.e., like in an overnight session, there can be a delay in seeing the line for the stop change on the chart as it waits for the next streaming tick, but once that happens, you’ll see the line move to the new stop, nonetheless, as you’ll see in the “Formula Output” window, the stop itself has been updated in real-time.

    Along with the code below, I'm going to include a Notepad file of the code, since sometimes it doesn't paste too good here...so far, the forum isn't letting me upload, I'll try after posting.


    // 2023.05.09 - The only purpose of this program is to practice dynamically moving a stop price.

    "use strict" // I use this to insure that I don't have any undeclared variables.
    var xSMA_5 = null;
    var xSMA_10 = null;
    var nBuyStop = null;
    var nSellStop = null;
    var bOkToGoLong = true;
    var bOkToGoShort = true;

    function preMain(){
    setPriceStudy(true);
    setStudyTitle("MoveStop");
    setCursorLabelName("MA_5", 0);
    setPlotType(PLOTTYPE_LINE, 0);
    setDefaultBarFgColor(Color.white, 0);
    setCursorLabelName("MA_10", 1);
    setPlotType(PLOTTYPE_LINE, 1);
    setDefaultBarFgColor(Color.purple, 1);
    Trade.setLiveTrading(true); // To use minTick() below, you must have this and be logged into your broker.
    }

    function main(){
    if(getBarState() == BARSTATE_ALLBARS){
    var nSMA_5 = null;
    var nSMA_10 = null;
    xSMA_5 = sma(5);
    xSMA_10 = sma(10);
    }
    nSMA_5 = xSMA_5.getValue(0);
    nSMA_10 = xSMA_10.getValue(0);

    if(bOkToGoShort && Strategy.isShort() == false && nSMA_5 < nSMA_10){
    if(Strategy.isLong() == true){
    Strategy.doSell("CloseLong", Strategy.LIMIT, Strategy.THISBAR, 1, close(0));
    removeLine("SellStop");
    debugPrintln("Close Long at: \t" + close(0).toFixed(2) + '\t ' + getValue("time", 0).ToShortTime(true, true, false, false));
    }
    Strategy.doShort("GoShort", Strategy.LIMIT, Strategy.THISBAR, 1, close(0));
    nBuyStop = close(0) + 5;
    bOkToGoLong = true;
    drawLineAbsolute(3, nBuyStop, -10, nBuyStop, PS_SOLID, 1, Color.yellow, "BuyStop");
    debugPrintln("Go Short at: " + close(0).toFixed(2) + "\tBuyStop: " + nBuyStop.toFixed(2) + '\t ' + getValue("time", 0).ToShortTime(true, true, false, false));
    }
    if(bOkToGoLong && Strategy.isLong() == false && nSMA_5 > nSMA_10){
    if(Strategy.isShort() == true){
    Strategy.doCover("CoverShort", Strategy.LIMIT, Strategy.THISBAR, 1, close(0));
    removeLine("BuyStop");
    debugPrintln("Cover Short\t" + close(0).toFixed(2) + '\t ' + getValue("time", 0).ToShortTime(true, true, false, false));
    }
    Strategy.doLong("GoLong", Strategy.LIMIT, Strategy.THISBAR, 1, close(0));
    nSellStop = close(0) - 5;
    bOkToGoShort = true;
    drawLineAbsolute(3, nSellStop, -10, nSellStop, PS_SOLID, 1, Color.red, "SellStop");
    debugPrintln("Go Long at: " + close(0).toFixed(2) + "\tSellStop: " + nSellStop.toFixed(2) + '\t ' + getValue("time", 0).ToShortTime(true, true, false, false));
    }
    if(close(0) >= nBuyStop && Strategy.isShort() == true){
    Strategy.doCover("CoverShort", Strategy.LIMIT, Strategy.THISBAR, 1, nBuyStop);
    removeLine("BuyStop");
    bOkToGoShort = false; // Stop program from re-entering shorts until after it goes long.
    debugPrintln("***Stop Hit...Cover Short\t" + nBuyStop.toFixed(2) + '\t ' + getValue("time", 0).ToShortTime(true, true, false, false));
    }
    if(close(0) <= nSellStop && Strategy.isLong() == true){
    Strategy.doSell("CloseLong", Strategy.LIMIT, Strategy.THISBAR, 1, nSellStop);
    removeLine("SellStop");
    bOkToGoLong = false; // Stop program from re-entering longs until after it goes short.
    debugPrintln("***Stop Hit...Sell Long\t" + nSellStop.toFixed(2) + '\t ' + getValue("time", 0).ToShortTime(true, true, false, false));
    }
    return [nSMA_5, nSMA_10];
    }

    function onLButtonDown(barIndex, yValue, x, y){ // You must hold the shift key down for this to work.
    var nOldBuyStop = null;
    var nOldSellStop = null;
    var nMinTick = 0.25; // Symbol.minTick(); To use this, you must be logged into your broker or the program will crash.

    if(yValue > getValue("Close", barIndex)){ // To be true, your mouse must be above the close at the bar index where your mouse is.
    nOldBuyStop = nBuyStop;
    nBuyStop = GetPrice(true, yValue, nMinTick);
    drawLineAbsolute(3, nBuyStop, -10, nBuyStop, PS_SOLID, 1, Color.yellow, "BuyStop");
    debugPrintln(nBuyStop.toFixed(2) + '\t' + nOldBuyStop.toFixed(2));
    }else{
    nOldSellStop = nSellStop;
    nSellStop = GetPrice(false, yValue, nMinTick);
    drawLineAbsolute(3, nSellStop, -10, nSellStop, PS_SOLID, 1, Color.red, "SellStop");
    debugPrintln(nSellStop.toFixed(2) + '\t' + nOldSellStop.toFixed(2));
    }
    }

    function RoundPrice(nPrice, nTick){
    return Math.floor(nPrice) + Math.ceil((nPrice - Math.floor(nPrice)) / nTick) * nTick; // Round Up
    // return Math.floor(nPrice) + Math.floor((nPrice - Math.floor(nPrice)) / nTick) * nTick; // Round Down
    }
    function GetPrice(bBuyer, nPrice, nTick){ // Round nPrice up or down to a proper tick based on if it's a buy or a sell
    if(bBuyer) return Math.floor(nPrice) + Math.ceil((nPrice - Math.floor(nPrice)) / nTick) * nTick; // Round Up
    else return Math.floor(nPrice) + Math.floor((nPrice - Math.floor(nPrice)) / nTick) * nTick; // Round Down
    }

    Date.prototype.ToShortTime = function(bShowingSeconds, bShowingDate, bExcelDate, bExcelDateAndTime){
    var dTimeString = null;
    var years = this.getFullYear();
    var months = this.getMonth() + 1;
    var days = this.getDate();
    var hours = this.getHours();
    var minutes = this.getMinutes();
    var seconds = this.getSeconds();
    if(bShowingDate && bShowingSeconds) dTimeString = ('0' + months).slice(-2) + "." + ('0' + days).slice(-2) + " " +
    ('0' + hours).slice(-2) + ":" + ('0' + minutes).slice(-2) + ":" + ('0' + seconds).slice(-2);
    else if(bShowingDate) dTimeString = ('0' + months).slice(-2) + "." + ('0' + days).slice(-2) + " " +
    ('0' + hours).slice(-2) + ":" + ('0' + minutes).slice(-2);
    else if(bShowingSeconds) dTimeString = ('0' + hours).slice(-2) + ":" + ('0' + minutes).slice(-2) +
    ":" + ('0' + seconds).slice(-2);
    else if(bExcelDate) dTimeString = months + '/' + days + '/' + years;
    else if(bExcelDateAndTime) dTimeString = months + '/' + days + '/' + years + ' ' + ('0' + hours).slice(-2) + ":" +
    ('0' + minutes).slice(-2) + ":" + ('0' + seconds).slice(-2);
    else dTimeString = ('0' + hours).slice(-2) + ":" + ('0' + minutes).slice(-2);
    return dTimeString;
    }?

    Happy Trading!
    LetUsLearn
    ?

    Comment

    Working...
    X