Here is some code I have been working on. I will go into detail on the code a little later. But this is how you monitor re-entering at a better price after taking a profit. The problem with taking profits on longer term trend following systems is that the logic that got you into the position is probably still true and you will notice your algorithm will re-enter in the same direction. So you need to inform your algorithm not to re-enter until a certain condition is met. In this example, I only re-enter at a better price if the condition that got me into the trade is still valid.
Inputs: swingHiStrength(2),swingLowStrength(2),numDaysToLookBack(30),stopAmt$(500),profitAmt$(1000),getBackInAfterProfAmt$(250);
vars: mp(0),longProfTaken(false),shortProfTaken(false);
mp = marketPosition;
if not(longProfTaken) and mp <> 1 then Buy("BreakOut-B") next bar at highest(h[1],20) on a stop;
if not(shortProfTaken) and mp <>-1 then SellShort("BreakOut-S") next bar at lowest(l[1],20) on a stop;
//If mp[0] = 0 and mp[1] = 1 then print(date," ",exitPrice(1)," ",entryPrice(1));
If longProfTaken then
Begin
If c < exitPrice(1) - getBackInAfterProfAmt$/bigPointValue then
begin
longProfTaken = false;
If mp <> -1 then buy("longRe-Entry") next bar at open;
end;
end;
If shortProfTaken then
Begin
If c > exitPrice(1) + getBackInAfterProfAmt$/bigPointValue then
begin
shortProfTaken = false;
If mp <>-1 then sellShort("shortRe-Entry") next bar at open;
end;
end;
If mp = 1 and c > entryPrice + profitAmt$/bigPointValue then
begin
sell this bar on close;
longProfTaken = true;
end;
If mp =-1 and c < entryPrice - profitAmt$/bigPointValue then
begin
buyToCover this bar on close;
shortProfTaken = true;
end;
If mp = -1 then longProfTaken = false;
If mp = 1 then shortProfTaken = false;
//if mp = 1 then setStopLoss(stopAmt$);
//if mp = 1 then setProfitTarget(profitAmt$);
Re-Entering At Better Price After Profit
Backtesting with [Trade Station,Python,AmiBroker, Excel]. Intended for informational and educational purposes only!