Announcement

Collapse
No announcement yet.

Simple Stop Loss problem

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

  • Simple Stop Loss problem

    Hi,

    I am trying to create the stop loss part of the code for my strategy. To just get the principle working I have opted for a simple 150 points below the entry price. However during backtesting it seems to only work for the first trade and then the next entry does not exit till the close out trade action at the end of the backtest.

    As the entry price is resetting for each trade i cant see why the nStopLevel isnt doing the same. Do i have to declare it as 0/null in the stop loss part of the code.

    Anyone have any ideas?

    PHP Code:
    var nSignal;
    var 
    nTradeEntryPrice null;
    var 
    nStopLevel null;
    var 
    inTrade null;
    var 
    nNewTrade;

    function 
    preMain() {

        
    setPriceStudy(true);
        
    setStudyTitle("Donchian");
        
    setCursorLabelName("20 Day High"0);
        
    setCursorLabelName("10 Day High"1);
        
    setCursorLabelName("20 day Low"2);
        
    setCursorLabelName("10 day Low"3);
        
    setColorPriceBars(true);
        
    setDefaultBarFgColor (Color.blue0);
        
    setDefaultBarFgColor (Color.red1);
        
    setDefaultBarFgColor (Color.blue2);
        
    setDefaultBarFgColor (Color.red3);
        
    setDefaultBarThickness(10);
        
    setDefaultBarThickness(11);
        
    setDefaultBarThickness(12);
        
    setDefaultBarThickness(13);
        
    setDefaultPriceBarColor(Color.black);

    }

    function 
    main() {

        var 
    nDonch_Upper upperDonchian (20);
        var 
    nDonch_Upper2 upperDonchian (10);
        var 
    nDonch_Lower  lowerDonchian (20);
        var 
    nDonch_Lower2 lowerDonchian (10);
        if (
    inTrade == nullinTrade 0;
        

    // identify entry price

    if (nNewTrade == 1) {
        
    nTradeEntryPrice open();
        
    nNewTrade 0;
        
    inTrade 1;
        
    nStopLevel nTradeEntryPrice 150;

    }

    // stop loss/profit exit

    //LONG

    if ((inTrade == 1) && (nSignal 0)) {

    if (
    low() <= nStopLevel) {

    Strategy.doSell("Stop Sell"Strategy.LIMITStrategy.THISBARStrategy.ALLnStopLevel);

    inTrade 0;
            
        }
    }


    //identify entry signal

    if (inTrade == 0) {
    if (
    high() >= nDonch_Upper.getValue(-1)) {
        
    nSignal 1;
        
    nNewTrade 1;
        }
    }


    //execute trade

    if  ((nNewTrade == 1) && (nSignal 0)) {

        
    Strategy.doLong("Long"Strategy.MARKETStrategy.NEXTBAR);
        
    setPriceBarColor(Color.green);

           
    }


    return new Array (
    nDonch_UppernDonch_LowernDonch_Upper2nDonch_Lower2);



  • #2
    thestranger
    As far as I can see it appears to work also on subsequent trades.
    To test the strategy I replaced 150 with 1.5 and ran the efs on a 1 minute chart of ES U6. As you can see in the image the strategy got stopped out multiple times.
    You may want to provide further details (symbol, interval, Time Template used) as to your back test so that someone can try to replicate the issue
    Alex

    Comment


    • #3
      Thanks for responding Alex.

      I've used my own time template which covers the maximum amount of data on $INDU. (Just under 20 years) and a daily interval.

      Comment


      • #4
        thestranger
        The strategy enters a second long trade on 12/18/1987 at a price of 1949.40. That places the stop at 1799.40 which has yet to be breached and since you have no other exit conditions the strategy remains long until the closeout trade on the last (ie current) bar
        Alex

        Comment


        • #5
          thestranger
          One thing you may want to do that will help you visualize what the strategy is doing is to also plot the stops (and profit targets if any) while you are developing a strategy. For a simple example of what I mean see this thread.
          Alex

          Comment


          • #6
            Thanks for the link Alex. I'm glad that the entry strategy is working, as I simply couldnt see where it could have been failing.

            I was intending on using an ATR level as the exit. However I stripped this part out before to simply test if the basic entry was flawed. Now i have added it again for some reason my strategy enters and exits on the same price for 19 trades, it then starts to enter and exit on different levels.

            Any ideas on what I am doing wrong?
            Attached Files

            Comment


            • #7
              thestranger
              There are a few errors in both syntax and logic in the script.
              The first one is that if (atrLevel == null) atrLevel = atr(20,0); is a value and not a series. Also, since you are calling it only once (because of the if(atrLevel==null) condition) that value will be the one of the ATR study when it is first calculated and will remain constant throughout the chart.
              You need to initialize the atrLevel variable as a series and then use its value (through the getValue() method) in your calculations and conditions OR declare the value directly in your conditions and/or calculations. FWIW this is the same error I already described to you in this post.
              Second you omitted to insert a null check for the value of the atrLevel variable. What happens then is that because the length of the ATR study is set to 20 for the first 19 bars in your chart the ATR study returns null while it is priming hence the stop on those bars is the same as the entry price (ie nEntryPrice - null = nEntryPrice). This is why you get 19 trades where the entry and exit prices are identical ie the strategy is stopped out at the same price it went long.
              As I suggested in my previous reply you should plot the stop(s) and/or profit targets (if any) while developing a script so that you can see what is actually happening as the strategy unfolds. If you had done that you would have seen that on the first 19 bars the stop was the same as the Open (ie nEntryPrice) and that after that the stop was at the same price throughout the chart as that of the 20th bar.
              I would also suggest that you get in the habit of using the debugPrintln() function else trying to evaluate what a script is doing equates to guesswork
              Alex

              Comment


              • #8
                Thanks for your help Alex. Hopefully I'll get there in the end.

                Comment

                Working...
                X