Announcement

Collapse
No announcement yet.

Plotting okay, trades don't work

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

  • Plotting okay, trades don't work

    Hi:

    The following Strategy plots 2 times the avrg range for 21 bars,
    above or below the bar. When the upper plot is crossed, the plot reverses and a sell signal should be trigered, and when the bottom plot is crossed, the plot reverses again and a
    buy signal is trigered. The ploting is working properly but the
    trigering of the trades is not working. I am getting zero trades.
    Could you please help me with this problem.

    Thank you very much
    sol123


    var xbuysel = 1;
    var buysel = 1;
    var nstop = 0;
    var ostop = 0;
    var xnstop = 0;
    var xostop = 0;
    var newtrade = 0;
    var entpric = 0;

    function preMain(){
    setPriceStudy(true);
    setStudyTitle("Sell");
    setCursorLabelName("Range",0);
    setPlotType(PLOTTYPE_DOT);

    }


    function main(mult, xmult, proftar){
    if(mult == null){
    mult = 2;
    }
    if(xmult == null){
    xmult = 2;
    }
    if(proftar == null){
    proftar = 10;
    }
    var rng = 0;
    var xrng = 0;
    var sb2 = 0;
    var length = 21;
    rng = 0;
    xrng = 0;
    sb2 = 1;

    for(i = -length + 1; i < 0; i++)
    rng += high(i) - low(i);

    rng /= length;
    xrng = rng;
    rng *= mult;
    xrng *= xmult;

    if(buysel == 1){
    nstop = low() - rng;
    }
    if(buysel == 2){
    nstop = high() + rng;
    }

    if(buysel == 1 && nstop > ostop){
    ostop = nstop;
    }
    if(buysel == 2 && nstop < ostop){
    ostop = nstop;
    }

    if(xbuysel == 1){
    xnstop = low() - xrng;
    }
    if(xbuysel == 2){
    xnstop = high() + xrng;
    }
    if(xbuysel == 1 && xnstop > xostop){
    xostop = xnstop;
    }
    if(xbuysel == 2 && xnstop < xostop){
    xostop = xnstop;
    }
    if (newtrade == 1){
    entpric = open();
    }

    if (Strategy.isInTrade() == true && (Strategy.isShort() == true)) {
    if (low() <= (entpric - proftar)) {
    Strategy.doCover("Short PT1 Exit", Strategy.STOP, Strategy.THISBAR,
    Strategy.getDefaultLotSize(), (entpric - proftar));

    }
    }
    if (Strategy.isInTrade() == true && (Strategy.isLong() == true)) {
    if (high() >= (entpric + proftar)) {
    Strategy.doSell("Long PT1 Exit", Strategy.STOP, Strategy.THISBAR,
    Strategy.getDefaultLotSize(), (entpric + proftar));

    }
    }
    if (Strategy.isInTrade() == true && (Strategy.isShort() == true)) {
    if (high() >= (xostop)) {
    Strategy.doCover("Short stp Exit", Strategy.STOP, Strategy.THISBAR,
    Strategy.getDefaultLotSize(), (xostop));

    }
    }
    if (Strategy.isInTrade() == true && (Strategy.isLong() == true)) {
    if (low() <= (xostop)) {
    Strategy.doSell("Long stp Exit", Strategy.STOP, Strategy.THISBAR,
    Strategy.getDefaultLotSize(), (xostop));

    }
    }

    newtrade = 0;

    if(buysel == 1 && low() < ostop){
    buysel = 2;
    nstop = high() + rng;
    ostop = nstop;
    Strategy.doshort("GO SHORT", Strategy.MARKET, Strategy.NEXTBAR);
    newtrade = 1;
    }

    if(buysel == 2 && high() > ostop){
    buysel = 1;
    nstop = low() - rng;
    ostop = nstop;
    Strategy.dolong("GO LONG", Strategy.MARKET, Strategy.NEXTBAR);
    newtrade = 1;
    }

    if(xbuysel == 1 && low() < xostop){
    xbuysel = 2;
    xnstop = high() + xrng;
    xostop = xnstop;
    }

    if(xbuysel == 2 && high() > xostop){
    xbuysel = 1;
    xnstop = low() - xrng;
    xostop = xnstop;
    }
    setBarThickness(2);

    if(buysel == 1){
    setBarFgColor(Color.red);
    return ostop;
    }
    else{
    setBarFgColor(Color.lime);
    return ostop;
    }

    }

  • #2
    Hello Sol,

    JavaScript is case sensitive. To get your formula going, you just need to change .dolong to .doLong and .doshort to .doShort. You should see some trades in the Strategy Analyzer with the modified formula below. Note also that I changed length to nlength. "length" is a property name associated with an array object. It wasn't causing a problem for you in this case, but its a good habit to make sure you always use a unique naming convention to avoid any problems with reserved words and such.

    sol123_Sell.efs
    Jason K.
    Project Manager
    eSignal - an Interactive Data company

    EFS KnowledgeBase
    JavaScript for EFS Video Series
    EFS Beginner Tutorial Series
    EFS Glossary
    Custom EFS Development Policy

    New User Orientation

    Comment

    Working...
    X