Announcement

Collapse
No announcement yet.

strategy entering multiple trades

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

  • strategy entering multiple trades

    This is supposed to be a "simple" macd crossover with a trailing stop. I can't figure out why it's entering more than one trade at a time. I've used the same code in other strategies a long time ago without any problems but having problems now.


    var vMACD4_13 = new MACDStudy(4, 13, 1, "Close", false);
    var vMACD28_91 = new MACDStudy(28, 91, 7, "Close", false);
    var vLastAlert = -1;
    var vtime;
    var nStopPrice;
    var nLastRawTime = 0;
    var BLBarOffset = 0;


    function preMain() {

    setColorPriceBars(true);
    setPriceStudy(true);
    setStudyTitle("MACD XOVER-BT2b8");

    }

    function main() {
    vtime = (getHour()*100)+ getMinute();

    if (getValue("rawtime", 0) != nLastRawTime) { // Bar Counter function.
    nLastRawTime = getValue("rawtime",0);
    BLBarOffset += 1;
    }

    if (Strategy.isInTrade()) { // Draw Current Stop Level on Chart.
    drawShapeRelative(4, nStopPrice, Shape.DIAMOND, null, Color.green, Shape.ONTOP, BLBarOffset+"lFSX");
    }

    if (vMACD4_13.getValue(MACDStudy.MACD) > 0) //Color bars.
    setPriceBarColor(Color.RGB(0,0,255));
    else if (vMACD4_13.getValue(MACDStudy.MACD) < 0)
    setPriceBarColor(Color.RGB(255,0,0));

    if ((Strategy.isLong() == true) && (vtime >= 1600)) { //EOD Long Exit
    Strategy.doSell("EOD Exit", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT);
    drawShapeRelative(0, high()+1, Shape.SQUARE, "", Color.RGB(0,0,255), Shape.LEFT);

    }

    if ((Strategy.isShort() == true) && (vtime >= 1600)) { //EOD Short Exit
    Strategy.doCover("EOD Exit", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT);
    drawShapeRelative(0, low()-1, Shape.SQUARE, "", Color.RGB(255,0,0), Shape.LEFT);
    }

    if ((Strategy.isLong() == true) && (low() <= nStopPrice)) { //Exit long if stop hit.
    Strategy.doSell("LongStop", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT);
    drawTextRelative(0, high()+.75, "StopXt", Color.RGB(0,0,0), Color.RGB(255,255,255), Text.ONTOP, "Arial", 8, BLBarOffset+"tag");

    }

    if ((Strategy.isShort() == true) && (high() >= nStopPrice)) { //Exit short if stop hit.
    Strategy.doCover("ShortStop", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT);
    drawTextRelative(0, low()-.75, "StopXt", Color.RGB(0,0,0), Color.RGB(255,255,255), Text.ONTOP, "Arial", 8, BLBarOffset+"tag");

    }

    if (Strategy.isLong() == true) { // Adjust trailing stop....
    if (vMACD4_13.getValue(MACDStudy.MACD) > 0 &&
    vMACD4_13.getValue(MACDStudy.MACD, -1) < 0)
    nStopPrice = (lowest(7, low())-.25);
    }

    if (Strategy.isShort() == true) { // Adjust trailing stop....
    if (vMACD4_13.getValue(MACDStudy.MACD) < 0 &&
    vMACD4_13.getValue(MACDStudy.MACD, -1) > 0)
    nStopPrice = (highest(7, high())+.25);
    }


    if (!Strategy.isLong()) { // Go long if not in already...
    if (vMACD4_13.getValue(MACDStudy.MACD) > 0 &&
    vMACD4_13.getValue(MACDStudy.MACD, -1) < 0 &&
    vMACD28_91.getValue(MACDStudy.MACD) > 0 &&
    vtime > 930 &&
    vtime < 1600 ) {
    Strategy.doLong("Trend Long", Strategy.CLOSE, Strategy.THISBAR);
    drawShapeRelative(0, low()-1, Shape.UPARROW, "", Color.RGB(0,0,255), Shape.LEFT);
    nStopPrice = (lowest(7, low())-.25);

    }
    }

    return;

    }
    Attached Files

  • #2
    Hello mgin,

    There is a logic problem being created with the "LongStop" condition. Modify this code block to use a LIMIT exit like below, which is the proper way to handle this type of stop logic. This will resolve the issue. Also make note of the check for the open of the bar triggering the stop. This should also be done to ensure a realistic price for nStopPrice. Make the same change to your "ShortStop" condition as well.

    PHP Code:
    // long stop
        
    if ((Strategy.isLong() == true) && (low(0) <= nStopPrice)) { //Exit long if stop hit.
            
    if (open(0) < nStopPricenStopPrice open(0);
            
    Strategy.doSell("LongStop"Strategy.LIMITStrategy.THISBARnStopPrice); 
            
    drawTextRelative(0high()+.75"StopXt"Color.RGB(0,0,0), Color.RGB(255,255,255), Text.ONTOP"Arial"8BLBarOffset+"tag");        
        } 
    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


    • #3
      Thanks for the response Jason. I'll try what you suggested.

      Comment

      Working...
      X