Announcement

Collapse
No announcement yet.

setStop screwing up rest of efs code

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

  • setStop screwing up rest of efs code

    Hi, I am new to coding but have made an efs script that was working fine.
    I wanted it to only initiate trades during regular trading hours and close out any open position by the end of the day.

    I did this by declaring a time variable:
    var time = (getHour()*100 + getMinute());

    Then adding this code to the main function where the rest of the trading instructions are:

    if (Strategy.isLong() == true && (time >= 1600 || time <= 930))
    {
    Strategy.doSell("CoverLT",Strategy.MARKET,Strategy .THISBAR);
    }

    This worked perfectly fine and functioned the way i wanted it to.

    After this I added this line to my code with the rest of my entry condition right under the line Strategy.doLong("Long Signal", Strategy.MARKET, Strategy.NEXTBAR);:

    Strategy.setStop(close(0)- 0.1);

    After I added this I wanted it to have a 10c stop from the close of the entry candle, and exit no matter what by 16:00.

    Now when I run the back test it holds position over night and many trades are over a 10c loss......

    I have tried a few variations and nothing I do seems to work. I am pretty new to coding so I could be looking over something simple.
    Any help here would be greatly appreciated.

  • #2
    Hi,

    Strategy.setStop(close(0)- 0.1);
    This line of code continuously sets the stop to the current close so the close in the Strategy.setStop property changes to the close of each bar as it is processed for historical bars and each new tick for real time bars.

    After I added this I wanted it to have a 10c stop from the close of the entry candle, and exit no matter what by 16:00.
    I don't know what unit of measure 10c represents (though I expect its 0.1) but to do what you want you have to set a global variable to the close of the entry candle within the conditional that identifies the entry candle then use that variable instead of close(0) in the Strategy.setStop property.

    Wayne
    Last edited by waynecd; 10-08-2013, 05:53 PM.

    Comment

    Working...
    X