Announcement

Collapse
No announcement yet.

Fixed (Non-Trailing) Stop Loss

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

  • Fixed (Non-Trailing) Stop Loss

    Hi all,

    I would like to code a Stop Loss based upon the level at which I opened a trade. I do not want it to trail the price, I just want it to stick at the point at which the trade opened. I would open a trade on the open price of NextBar. How do I then, in successive bars hold that open value as a stop loss ? The only examples I have seen are where the stoploss is modified for each bar, and is thus a local variable. In my thinking I cannot see that I can set the open trade level as a global variable or a local variable. Depending on when/if the stop loss is taken out, the code would have to hold that "trade open" stoploss for different time periods. Do you see my dilemma ?

    Thanks in advance for you help folks.

    Happy Thanksgiving to all !

    Neil.

  • #2
    Neil
    The enclosed efs will go long when prices cross above the moving average on a close basis. As it goes long (on the open of the bar that follows the one that triggers the signal) it sets a Target and Stop at +1.5 and -1.5 respectively from the entry price. These stops remain unchanged throughout the trade. If price reaches either one the trade is closed.
    The efs also paints the bars in blue when long, in lime when closing the trade on the Target and in red when on the Stop (see image)
    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;

    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)+1.5;
                   
    Stop open(0)-1.5;
                   
    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){
                
    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);



    Note: If you want the Target and Stop plots to show only when the strategy is long then replace the following line

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

    with the following section of code

    PHP Code:
    if(Strategy.isLong()==true){
            return new Array (
    Target,vMA.getValue(MAStudy.MA),Stop);
        }else{
            return new Array (
    null,vMA.getValue(MAStudy.MA),null);
        } 

    Comment


    • #3
      Alexis,

      That looks absolutely ideal.

      Thank you very much.

      Neil.

      Comment


      • #4
        Neil
        You are most welcome
        Alex

        Comment

        Working...
        X