Hi - I am working in Future mode on daily data and I want to be able to set a guaranteed max stop loss (which is available in spread-bet brokers in the UK). For example : if loss is greater than or equal to 60 points then trigger stop at 60 points. Therefore I should not see any losses greater than 60 points.
The code I currently has, shows losses of greater than 60 points, I suspect this is because there has been a large gap up/down in price data.
The code is : ApplyStop(stopTypeLoss , stopModePoint, 60, 2);
You should use a for loop to force the sellprice to you guaranteed stop price.
You can easily modify the trailing stop example from amibroker knowledge base to a guaranteed stop.
Suggest you have a go and post some code if you need help.
Also It would help to know the instrument you are using and timeframe ie EOD or Intraday eg 15 mins.
@easypipsy
I found an easier way. Ive use a simple MACross System to test it on DJ Index and it seems t work ok
//GuaranteedStop Code for simple MA Cross over
//BackTest will Display Trades as Long (maxloss) in the trade list
//Tested on YM DJ Futures or DJ Index $5 per point
//Normal Entry and Exit on Close 0 Delay
//Exit price on Guaranteed Stop set to Entry Price - GuaranteedStopPoints otherwise it is close price of exit bar.
Guaranteed_Stop_Points = 60;//Param("Guaranteed_Stop_Points", 60, 1, 100, 1);//Remove // if you want to use Param.
//System code//insert your buy /sell code here
Buy = Cross( Close, MA( Close, 50 ));
//Sell = Cross( MA( Close, 50 ), Close ) ); ;
Sell = Cross( MA( Close, 50 ),Close);
buy = ExRem( buy, sell );
sell = ExRem( sell, buy );
//##################################
ApplyStop(0,2,Guaranteed_Stop_Points,1,Volatile = False,ReEntryDelay = 0, ValidFrom = 0, ValidTo = -1);
IIf(Sell == 2,SellPrice = BuyPrice - Guaranteed_Stop_Points, SellPrice =Close);
SetPositionSize(1,spsShares);
Hi Richard - Thanks for taking the time to respond with the code.
I've tried it - but still get losses greater than 60 points. (note: i'm using your code at the moment for the buy/sell signals)
wondering what I'm missing here.
Excerpt of trade which showed the 164 point loss:
Excerpt of analysis of the trades including the 164 point loss at top of list.
I can see that the market gapped which could explain the loss greater than 60 points. But my broker offers guaranteed stops - so even in this circumstance I should only see a 60 point loss.
@easypipsy, I believe that the ApplyStop() function will always exit at a price that is within the range of the exit bar, i.e. Low <= exit price <= High, and will also take opening gaps into account. If you want to exit outside the Low-High range, then you will need to do a couple of things:
Disable AmiBroker's price bound checking so that AmiBroker will execute the trade at any price that you specify:
SetOption("PriceBoundChecking", False);
Use an alternative to ApplyStop() similar to the one @C_M suggested (although I don't understand the need for the sellprice<high+1000 clause) or else write your own stop loss functionality in a Custom Back Test (CBT), which is what I normally do. The former is easier, but assumes that there is no ambiguity about when a position is entered. For example, if there is contention for capital, then a particular symbol may not actually enter a trade on the first occurrence of an entry signal.
Thanks folks really appreciate your help - got it to work using the following code:
SetOption("PriceBoundChecking", False);
x = Valuewhen(Buy,Close);
SellPrice = IIf (SellPrice < x-60, x-60, SellPrice); // Sets loss to maximum of 60 points