Announcement

Collapse
No announcement yet.

Strange Stop-Loss Query

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

  • Strange Stop-Loss Query

    Hi folks,

    I wish to set a stop loss which will set a stop for a long at a certain number of points ABOVE the level at which a trade is entered.

    I know that sounds silly, but I have a good degree of confidence that when my system enters a long, it will not immediately fall below it, (but could do four or five bars later). I thus want to set a stop loss above the entry price (to cover transaction costs etc).

    I guess the way to do this is to get the trade entry price and add x points to it and then trigger the stop loss close once the index trades ABOVE the stop loss number and then falls back below it ( but NOT to trigger a close if if it never trades above it - I have another way of dealing with that).

    I do not want the stop loss to move - it should remain at a static level.

    Can anyone help me with the code for that?

    Many thanks advance.

    Deax

  • #2
    Deax
    The enclosed efs is a revision of an example I set up for you some time ago.
    In this case the Stop does not get triggered until the low of a bar clears the Stop level.
    Alex

    PHP Code:
    var vMA = new MAStudy(340"Close"MAStudy.EXPONENTIAL);

    function 
    preMain() {
        
    setPriceStudy(true);
        
    setStudyTitle("Sample");
        
    setCursorLabelName("Target"0);
        
    setCursorLabelName("MA"1);
        
    setCursorLabelName("Stop"2);
        
    setDefaultBarFgColor(Color.lime0);
        
    setPlotType(PLOTTYPE_FLATLINES,0);
        
    setDefaultBarFgColor(Color.blue1);
        
    setDefaultBarFgColor(Color.red2);
        
    setPlotType(PLOTTYPE_FLATLINES,2);
        
    setColorPriceBars(true)
        
    setDefaultPriceBarColor(Color.black);
    }

    //global variables
    var Target null;
    var 
    Stop  =null;
    var 
    vFlag 0;

    function 
    main() {
       
        if(
    getBarState()==BARSTATE_NEWBAR){
            if(
    close(-2)<vMA.getValue(MAStudy.MA,-2)&&
               
    close(-1)>vMA.getValue(MAStudy.MA,-1)&&
               
    Strategy.isLong()==false){
                   
    Target open(0)+3.5;
                   
    Stop open(0)+1.5;
                   
    vFlag 1;
                   
    Strategy.doLong("Long",Strategy.MARKET,Strategy.THISBAR);
            }
        }
        
        if(
    Strategy.isLong()==true){
            if(
    high(0)>=Target){
                    
    setPriceBarColor(Color.lime);
                    
    Strategy.doSell("Sell",Strategy.STOP,Strategy.THISBAR,null,Target);
            }
            if(
    low(0)>Stop){
                
    vFlag 0;
            }
            if(
    vFlag==0&&low(0)<=Stop){
                
    setPriceBarColor(Color.red)
                
    Strategy.doSell("Sell",Strategy.STOP,Strategy.THISBAR,null,Stop);
            }
            
        }
                
        if(
    Strategy.isLong()==true){
            
    setPriceBarColor(Color.blue);
        }

        return new Array (
    Target,vMA.getValue(MAStudy.MA),Stop);

    Comment


    • #3
      Alexis,

      That looks just perfect - many thanks as ever.

      Deax.

      Comment

      Working...
      X