Trailing Stop Order

Questions about MultiCharts and user contributed studies.
evdl
Posts: 399
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 84 times
Been thanked: 123 times

Trailing Stop Order

Postby evdl » 02 May 2012

I am trying to get a fixed trailing stop. When a certain amount of profit is reached, the normal stoploss will be replaced by the trailingstoploss. I got this to work, but it will not maintain the reached stoploss value, once the profit is declining. For example: trailingstop trigger is a profit of 100. The trailingstop must trail the profit by 250 from the trigger. If the profit is 100 (is also the trigger in this example), the stoploss must be -150. If the profit is 500, the stoploss must be 250.

In the following code the trailing will happen, but once the profit is 500 and the stoploss is 250 and the profit is falling to 400, the stoploss will be 150 and not maintain the 250. I am not using the setstoploss or setdollartrailing. I coded the amounts per contract.

Code: Select all

[IntrabarOrderGeneration = True]

inputs: StockAmt (5000),
    ProfittargetAmt (500),
    StoplossAmt (500),
    TrailingTrigger (100),
    TrailingAmt (250),
            
variables:  Stoploss_Fixed (0),
       Profit_Fixed (0),
       Trailing_Value(0),
       TrailingStop_Trigger (False),
       Brokerposition(0),
           
   Profit_Fixed = ProfittargetAmt/StockAmt;
   Stoploss_Fixed = StoplossAmt/StockAmt;
   Trailing_value = ((openpositionprofit/stockamt)-(TrailingAmt/Stockamt));
   
//TEST conditions   
   Condition1 = price > price[1];
      
   
//Entry Conditions
If (barstatus(1) = 2)then begin;

   if Condition1 and marketposition  = 0 then begin;
      Buy ("Long") next bar at market;
      end;
   
End;

//Exit Conditions ---------------------------------------------------------------------------------------
//Stoploss and profit target

If marketposition = 0 then TrailingStop_Trigger = false;
If openpositionprofit >= Trailingtrigger and (trailingtrigger <>0) then trailingstop_trigger = true;
   
//Profit target fixed
//Long exit:   
   If marketposition = 1 then
   Sell ("CloseProfitLong") next bar Round((avgEntryPrice + Profit_Fixed),2) limit;
   end;
//Short exit
   If marketposition = -1 then
   BuytoCover ("CloseProfitShort") next bar Round((avgEntryPrice - Profit_Fixed),2) limit;
   end;
   
//Stoploss fixed --------------------------------------------------------------------------------------------
//Long exit:
   if marketposition = 1 then
   Sell ("StopLossLong") next bar Round((avgEntryPrice - Stoploss_Fixed),2) stop;
   end;
//Short exit:   
   if marketposition = -1 then
   Buytocover ("StopLossshort") next bar Round((avgEntryPrice + Stoploss_Fixed),2) stop;
   end;
   
//Trailingstoploss -----------------------------------------------------
//Long exit:   
   if (Trailingstop_Trigger=true) and (StoplossAmt > 0) and TrailingAmt > 0 then begin;
   if marketposition = 1 then
   Sell ("TrailingStopLong") next bar Round((avgEntryPrice + Trailing_Value),2) stop;
   end;
//Short exit:
   if (Trailingstop_Trigger=true) and (StoplossAmt > 0) and TrailingAmt > 0 then begin;
   if marketposition = -1 then
   BuytoCover ("TrailingStopShort") next bar Round((avgEntryPrice - Trailing_value),2) stop;
   end;


I tried all sorts of combinations but I can't get it to work, anyone have an idea?

User avatar
TJ
Posts: 6584
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 970 times
Been thanked: 1907 times

Re: Trailing Stop Order

Postby TJ » 02 May 2012

evdl wrote:I am trying to get a fixed trailing stop....

I tried all sorts of combinations but I can't get it to work, anyone have an idea?


Draw a flow chart, then match your code to the chart.

There are no short cuts.
These users thanked the author TJ for the post:
JoshM

escamillo
Posts: 197
Joined: 25 Mar 2011
Has thanked: 22 times
Been thanked: 55 times

Re: Trailing Stop Order

Postby escamillo » 02 May 2012

I am not really sure as I cannot quite follow your code but:
1). it might be better to have your Entry, Stop and (profit) Exits in separate Strat docs.
2). you might try Stops in the following format, if indeed you want one or the other to be active at a time:

Code: Select all

//Long STOP:
if marketposition = 1 then
begin
if Trailingstop_Trigger=false then
Sell ("StopLossLong") next bar Round((avgEntryPrice - Stoploss_Fixed),2) stop
else
if (Trailingstop_Trigger=true) and (StoplossAmt > 0) and TrailingAmt > 0 then
Sell ("TrailingStopLong") next bar Round((avgEntryPrice + Trailing_Value),2) stop;
end; //if marketposition = 1 then

User avatar
JoshM
Posts: 2089
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1525 times
Been thanked: 1485 times
Contact:

Re: Trailing Stop Order

Postby JoshM » 02 May 2012

Did something went wrong when copy-pasting the code here to the forum? I'm asking because there are some formatting errors in the code. For example, no ";" after the inputs and variables section..

Code: Select all

variables:  Stoploss_Fixed (0),
       Profit_Fixed (0),
       Trailing_Value(0),
       TrailingStop_Trigger (False),
       Brokerposition(0),


.. a ";" after a begin statement..

Code: Select all

if Condition1 and marketposition  = 0 then begin;


Code: Select all

If (barstatus(1) = 2)then begin;

evdl
Posts: 399
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 84 times
Been thanked: 123 times

Re: Trailing Stop Order

Postby evdl » 02 May 2012

Thanks escamillo for your reply.

I will try to rewrite the code a bit with the else statement. The switching to trailingstop instead of the fixed stop value is working. The problem I am having is that it will trail the openpositionprofit but not hold the trailing value. The purpose of trailing the profit, is it will set the stoploss everytime a bit higher when the profit is increasing. Now the trailing stop is increasing when the profit increases(that's good), but also declines when the profit is declining (that's not good).

So the question is, how to hold the value if the profit is not increasing and when the profit is increasing hold the new higher value of the trailingstop.

User avatar
JoshM
Posts: 2089
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1525 times
Been thanked: 1485 times
Contact:

Re: Trailing Stop Order

Postby JoshM » 02 May 2012

evdl wrote:The problem I am having is that it will trail the openpositionprofit but not hold the trailing value.

Have you tried IntraBarPersist in combination with ..

evdl wrote:Now the trailing stop is increasing when the profit increases(that's good), but also declines when the profit is declining (that's not good).

So the question is, how to hold the value if the profit is not increasing and when the profit is increasing hold the new higher value of the trailingstop.

MaxList?
These users thanked the author JoshM for the post:
evdl

evdl
Posts: 399
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 84 times
Been thanked: 123 times

Re: Trailing Stop Order

Postby evdl » 02 May 2012

Hello JoshM,

Yes, I did copy only the bits of the code that's relevant to the issue and also to keep it readable, I hope. There are more variabeles and conditions, so I cut out some ";" by accident.

Addition to my last post: When I used the setdollartrailing reserved word and used the trailingstop-trigger to set to false and true. It is holding the stoploss value. So I suspect, that is build in the trading engine of MC. But now with using the sell and buytocover statements, it needs additional coding to get the same result.

I will have a look at the maxlist reserved word. Thanks.

escamillo
Posts: 197
Joined: 25 Mar 2011
Has thanked: 22 times
Been thanked: 55 times

Re: Trailing Stop Order

Postby escamillo » 02 May 2012

As you said, "it will not maintain the reached stoploss value, once the profit is declining." because 'openpositionprofit' changes in your statement:
Trailing_value = ((openpositionprofit/stockamt)-(TrailingAmt/Stockamt));

You should define (do your calculations for) Trailing_Value separately for long and short positions (behind if MP = 1 and if MP = -1) and then for a long position, after you calculate Trailing_Value, if you want to lock in its highest value:

Code: Select all

if Trailing_Value < Trailing_Value[1] then Trailing_Value = Trailing_Value[1] ;
[sidenote: a prior TJ reprimand for not using a code box made me remember to use one this time - nice TJ!]
These users thanked the author escamillo for the post (total 2):
TJevdl

User avatar
TJ
Posts: 6584
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 970 times
Been thanked: 1907 times

Re: Trailing Stop Order

Postby TJ » 02 May 2012

escamillo wrote:...
[sidenote: a prior TJ reprimand for not using a code box made me remember to use one this time - nice TJ!]


No reprimands... just a friendly reminder; You want to do everything possible to make it easy for people to help you.

evdl
Posts: 399
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 84 times
Been thanked: 123 times

Re: Trailing Stop Order

Postby evdl » 04 May 2012

I have solved the issue. Big thank you to escamillo and joshm for pointing me in the right direction.

The place in the code of the trailingvalue calculations is very important. I did place the calculation of the trailingvalue before the condition. Otherwise it would not work. (maybe something to do with the avgentryprice, that needs to be determined first?)

And in the condition I use the maxlist and minlist reserve word.

For who is interested in the code, here it is:
[IntrabarOrderGeneration = True]

inputs: StockAmt (5000),
ProfittargetAmt (500),
StoplossAmt (500),
TrailingTrigger (100),
TrailingAmt (250);

variables: Stoploss_Fixed (0),
Profit_Fixed (0),
Trailing_Value(0),
TrailingStop_Trigger (False),
Brokerposition(0);

//Calculation for targets per contract
Profit_Fixed = ProfittargetAmt/StockAmt;
Stoploss_Fixed = StoplossAmt/StockAmt;
Trailing_value = (openpositionprofit-TrailingAmt)/stockamt;

//TEST conditions
Condition1 = price > price[1];

//Entry Conditions
If (barstatus(1) = 2)then begin;

if Condition1 and marketposition = 0 then begin;
Buy ("Long") next bar at market;
end;

End;

//Exit Conditions ---------------------------------------------------------------------------------------
//Stoploss, profit target, trailing stop

//this to activate the trailing stop instead of the normal stoploss:
If marketposition = 0 then TrailingStop_Trigger = false;
If openpositionprofit >= Trailingtrigger and (trailingtrigger <>0) then trailingstop_trigger = true;

//Calculations for the exitstatements
Profit_Fixed_Long = round2fraction(avgentryprice + Profit_Fixed);
Profit_Fixed_Short = round2fraction(avgentryprice - Profit_Fixed);
Profit_variabel_long = round2fraction(avgentryprice + Profit_Variabel);
Profit_Variabel_Short = round2fraction(avgentryprice - Profit_Variabel);
Stoploss_Fixed_Long = round2fraction(avgentryprice - Stoploss_Fixed);
Stoploss_Fixed_Short = round2fraction(avgentryprice + Stoploss_Fixed);
Trailing_value_Long = round2fraction(avgentryprice + trailing_value);
Trailing_value_Short = round2fraction(avgentryprice - trailing_value);

//Profit target fixed
//Long exit:
if ProfitTargetAmt > 0 then begin;
If marketposition = 1 then
Sell ("CloseProfitLong") next bar Profit_Fixed_Long limit;
end;
//Short exit
if ProfitTargetAmt > 0 then begin;
If marketposition = -1 then
BuytoCover ("CloseProfitShort") next bar Profit_Fixed_Short limit;
end;

//Stoploss fixed --------------------------------------------------------------------------------------------
//Long exit:
if (TrailingStop_Trigger=false) and StopLossAmt > 0 then begin;
if marketposition = 1 then
Sell ("StopLossLong") next bar Stoploss_Fixed_Long stop;
end;
//Short exit:
if (TrailingStop_Trigger=false) and StopLossAmt > 0 then begin;
if marketposition = -1 then
Buytocover ("StopLossshort") next bar Stoploss_Fixed_Short stop;
end;

//Trailingstop with activation on certain trailingamount set in the inputs--------------------------
//Long exit:
if (Trailingstop_Trigger=true) and (StoplossAmt > 0) and TrailingAmt > 0 then begin;
if marketposition = 1 then
Trailing_value_long = round2fraction((maxlist(trailing_value_long,trailing_value_long[1])));
Sell ("TrailingStopLong") next bar Trailing_value_long stop;
end;
//Short exit:
If (Trailingstop_Trigger=true) and (StoplossAmt > 0) and TrailingAmt > 0 then begin;
if marketposition = -1 then
Trailing_value_short = round2fraction((minlist(trailing_value_short,trailing_value_short[1])));
BuytoCover ("TrailingStopShort") next bar Trailing_value_short stop;
end;


I also used the round2fraction function to round up the values to the settings in quotemanager, otherwise price variation could cause errors at your broker.
These users thanked the author evdl for the post (total 2):
TJJoshM

User avatar
MAtricks
Posts: 789
Joined: 09 Apr 2012
Has thanked: 286 times
Been thanked: 279 times

Re: Trailing Stop Order

Postby MAtricks » 04 May 2012

I like the idea.

If I'm not mistaken these variables are missing:

Code: Select all

Variables:
Profit_Fixed_Long(0),
Profit_Fixed_Short(0),
Profit_variable_long(0),
Profit_Variable_Short(0),
Stoploss_Fixed_Long(0),
Stoploss_Fixed_Short(0),
Trailing_value_Long(0),
Trailing_value_Short(0),
price(close) ;


I'm still confused as to what

Code: Select all

Profit_Variable
is?

evdl
Posts: 399
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 84 times
Been thanked: 123 times

Re: Trailing Stop Order

Postby evdl » 05 May 2012

Yes, you are right. The code is part of a bigger strategy. So I missed some variabels when copying. This code can be incorporated in your own strategy. The profit_variable is a profit target exit based on the bandwidth of the bollinger bands. But I don't use that anymore, and the calculating was left by accident in the code. So that is of no use in this code example. Sorry for the mixup.

NW27
Posts: 177
Joined: 25 Dec 2010
Has thanked: 40 times
Been thanked: 83 times

Re: Trailing Stop Order

Postby NW27 » 14 May 2012

Hi,

Perhaps you may want to take a look at the Exit signal suite I have uploaded
http://www.multicharts.com/discussion/viewtopic.php?f=5&t=10377

Neil.
These users thanked the author NW27 for the post:
evdl

evdl
Posts: 399
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 84 times
Been thanked: 123 times

Re: Trailing Stop Order

Postby evdl » 15 May 2012

Thanks Neil.

You have set the bar for programming exits ;)


Return to “MultiCharts”