Separate Stops Long v Short

I appreciate the great contributions on this board and wondering if someone could help with a stop loss question.

Is there’s a way to have separate long and short stops for a single strategy that trades long and short, but models differently from each side.

The following code compiles, but I’m certain it’s not doing what I want it to…which is to have a separate stoploss for longs vs shorts, a separate max bar stop for longs vs shorts, and a separate target profit stop for longs vs shorts.

Is it possible to do this?

Thank you for your consideration and advice.


LongStopLoss = ApplyStop(stopTypeLoss, stopModePercent, 5);
ShortStopLoss = ApplyStop(stopTypeLoss, stopModePercent, 8);

LongMaxBars = ApplyStop(stopTypeNBar, stopModeBars, 95);
ShortMaxBars = ApplyStop(stopTypeNBar, stopModeBars, 5);

LongTargetProfit = ApplyStop(stopTypeProfit, stopModePercent, 100);
ShortTargetProfit = ApplyStop(stopTypeProfit, stopModePercent, 5);

Buy = (buy condition);

Sell = (sell condition)
 OR ConditionalStopLoss = IIf(Short, ShortStopLoss, LongStopLoss)
 OR ConditiionalMaxBars = IIf(Short, ShortMaxBars, LongMaxBars)
 OR ConditionalTagetProfit = IIf(Short, ShortTargetProfit, LongTargetProfit);


Short = (Short Condition);
Cover = (Cover Condition)
 OR ConditionalStopLoss = IIf(Short, ShortStopLoss, LongStopLoss)
 OR ConditiionalMaxBars = IIf(Short, ShortMaxBars, LongMaxBars)
 OR ConditionalTagetProfit = IIf(Short, ShortTargetProfit, LongTargetProfit);

Try this:

StopLoss = IIf(BuyPriceBuy > 0,5,IIf(ShortPriceShort > 0,8,Null));
ApplyStop(stopTypeLoss,stopModePercent, Stoploss, false );

1 Like

Thanks @Joachim108. Much appreciated.

Sorry, there is missing something…
How to type "*"
It has to be between Buy and Buyprice and also between Short and Shortprice!

Moderator comment: You did not read “How to use this site” If you read that you would know that you should use CODE TAGS for code. If you are copy-pasting codes without proper tags the formula gets screwed because * alone means ITALIC text ! So read “how to use this site”

@seaworthy There is some similar ideas in this post that should also help you. Define when you are Long (or Short) and then your stop amount.

http://www.amibroker.com/kb/2014/10/17/using-price-levels-with-applystop-function/

stopAmount = IIf( IsLong, stopAmountLong, stopAmountShort );
ApplyStop( stopTypeLoss, stopModePoint, stopAmount, True );
1 Like

Thanks @portfoliobuilder. I had been looking at that article previously.

The bit that was throwing my off was the “-TradeDelay” as I’ve only used numbers in that context (Ref(array, -somenumber).

“IsLong = Ref( Buy, -TradeDelay );”

Does this reference the previous instance of a Buy or something else?

@seaworthy yes I think you can ignore that part of the example and build off of your own trading rules. From that article there are only parts of it that apply to what you want.

Using some (or all depending on your strategy) of the below code after you have written your own rules might help get you where you want to be.

Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);
Short = ExRem(Short, Cover);
Cover = ExRem(Cover, Short);
InLong = Flip(Buy, Sell);
InShort = Flip(Short, Cover);
InTrade = InLong OR InShort;

And I think you will need two “ApplyStop” statements:
stopTypeProfit for the profit target
stopTypeTrailing for the trailing stop-loss.

Good luck and l’d be interested to see your solution (positive thinking that you will both create and then share your solution :slightly_smiling_face: )

2 Likes

I’d like to share it…but I’m still struggling…not getting a solution yet.

Hi try this code for alternating different profit target paramaters for Long and Short signals

TradeDelay = 1;
SetTradeDelays( TradeDelay, TradeDelay, TradeDelay, TradeDelay ); // set trade delays by your trading system
BuyPrice = SellPrice = ShortPrice = CoverPrice = O; // set fill to open 

IsLong = Ref(Buy,-TradeDelay);
IsShort = Ref(Short,-TradeDelay);

Profit_ATR_Short =  Optimize("Profit_T_mul_Short", 1,0.5,10,1); 
Profit_ATR_Long =  Optimize("Profit_T_mul_Long", 2,0.5,10,1); 

alt_stop = IIf(IsLong,Profit_ATR_Long,IIf(IsShort,Profit_ATR_Short,0));
ApplyStop( stopTypeProfit, stopModePoint, alt_stop* ATR( 20 ) , True );



2 Likes

Could we also use the BarsSince command to do this?

		inLong = BarsSince(Buy) < Min(BarsSince(Sell), BarsSince(Short));
		inShort = BarsSince(Short) < Min(BarsSince(Buy),BarsSince(Cover));

You could also use this to use different leverage for longs and shorts, right?