[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Stoploss that switchess to trailing stop at a given profit level



PureBytes Links

Trading Reference Links

With the irreplacable help of this forum I have figured out how to 
do this this weekend (see "why doesn't this work" thread.) I've 
explored the results over 1 years worth of data (1 hour bars) and it 
works. I wanted to make this easy to find in the archives in case 
any other non-programmer types are out there trying to do something 
similar. Here it is.

James

//This shows how to do a stoploss that switches to a trailing stop 
once a target profit level has been reached.
//This code only works for systems that give one buy signal per long 
trade (and one short signal per short trade).
//This code uses the fixed SL for the bar where the trade opens. It 
will not switch to the trailing stop loss until (at least) the 
second bar. 
//The powerbot trading robot (www.fabrefactum.com) can use these 
types of stops, so this may be useful if you are backtesting a 
system for that platform. 

//First put in your basic trading system. This is just an example.
Buy = Cross(EMA(Close,12),EMA(Close,26));
Short = Cross(EMA(Close,26),EMA(Close,12));
Sell = Short;
Cover = Buy;

//Now set your stoploss level. It is up front to make it easy to 
optimize. This code works for a  "point" SL level. If you want to 
use other types of SL levels (percent, etc.) you need to alter the 
applystop commands.
stoploss=0.005;

//Now set the profit level (in points for this code) where you want 
to switch to a trailing stop. It is up front to make it easy to 
optimize.
triggerfortrail=0.003;

//Same for the trailing stop level.
trailingstop=0.002;

//Are you currently in a buy?
Buyarea=BarsSince(Buy)<BarsSince(Short);

//Are you currently in a short?
Shortarea=BarsSince(Short)<BarsSince(Buy);

//Is there a buy signal at this moment?
Currentbuy=Ref(Buy,0);

//Is there a short signal at this moment?
Currentshort=Ref(Short,0);

//What is the price where the buy occured?
priceatbuy=ValueWhen(Buy,BuyPrice,1);

//What is the price where the short occured?
priceatshort=ValueWhen(Short,ShortPrice,1);

//If the highest high since you entered the buy is greater than or 
equal to your trigger level ... then do not use the fixed stoploss. 
Otherwise use the fixed stoploss (at the stoploss level.)
//Conversely, if the lowest low since you entered the sell is less 
than or equal to your trigger level ... then do not use the fixed 
stoploss. Otherwise use the fixed stoploss (at the stoploss level.)
//The buyarea/shortarea and currentbuy/currentshort parameters are 
needed to ensure the stoplosses are triggered at only the correct 
places.
Var1=IIf(((HHV(High,BarsSince(Buy))>=priceatbuy+triggerfortrail) AND 
Currentbuy<1 AND Buyarea>0) OR ((LLV(Low,BarsSince(Short))
<=priceatshort-triggerfortrail) AND Currentshort<1 AND 
Shortarea>0),0,stoploss);

//If the highest high since you entered the buy is greater than or 
equal to your trigger level ... then use the trailing stop (at the 
trailingstop level). Otherwise do not use the trailing stop. 
//Conversely, if the lowest low since you entered the sell is less 
than or equal to your trigger level ... then use the trailing stop 
(at the trailingstop level). Otherwise do not use the trailing stop.
//The buyarea/shortarea and currentbuy/currentshort parameters are 
needed to ensure the stoplosses are triggered at only the correct 
places.
Var2=IIf(((HHV(High,BarsSince(Buy))>=priceatbuy+0.003) AND 
Currentbuy<1 AND Buyarea>0) OR ((LLV(Low,BarsSince(Short))
<=priceatshort-0.003) AND Currentshort<1 AND 
Shortarea>0),trailingstop,0);

//Change the applystop functions as needed. Volitility must 
be "True" for this code to work as described. When the amount 
(Var1/Var2) is 0 amibroker effectively ignores that stoploss.
ApplyStop(0,2,Var1,True,True);
ApplyStop(2,2,Var2,True,True);





------------------------ Yahoo! Groups Sponsor ---------------------~-->
Buy Ink Cartridges or Refill Kits for your HP, Epson, Canon or Lexmark
Printer at MyInks.com. Free s/h on orders $50 or more to the US & Canada.
http://www.c1tracking.com/l.asp?cid=5511
http://us.click.yahoo.com/mOAaAA/3exGAA/qnsNAA/GHeqlB/TM
---------------------------------------------------------------------~->

Send BUG REPORTS to bugs@xxxxxxxxxxxxx
Send SUGGESTIONS to suggest@xxxxxxxxxxxxx
-----------------------------------------
Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx 
(Web page: http://groups.yahoo.com/group/amiquote/messages/)
--------------------------------------------
Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/