Dynamic stoploss

Questions about MultiCharts and user contributed studies.
evdl
Posts: 401
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 85 times
Been thanked: 124 times

Dynamic stoploss

Postby evdl » 05 Mar 2012

Hi,
I am trying to apply a dynamic stoploss to my automatic trading strategy. The BollingBand width is used for setting the stoploss. I got this to work. But the problem I am having is that I only want to dynamicly determine what the stoploss must be, at the pricelevel at which the position is taking.

As it is now the stoploss is changing whenever the bollingband width is changing. How can I change in Easy Language that it will use the dynamic determined stoploss, as an fixed stoploss once the position is taken.

My code for the stoploss:

Code: Select all

inputs:RiskRewardAmt(2)
StockAmt(2500)
Variables: Stopvalue(0)

Stopvalue = (StockAmt * (Topband-Bottomband))/RiskRewardAmt);

SetStopLoss(Stopvalue);
Hope someone can give me a idea how to achief this. Thanks!

escamillo
Posts: 203
Joined: 25 Mar 2011
Has thanked: 23 times
Been thanked: 56 times

Re: Dynamic stoploss

Postby escamillo » 05 Mar 2012

See if this works:
How can I change in Easy Language that it will use the dynamic determined stoploss, as an fixed stoploss once the position is taken.

inputs:RiskRewardAmt(2)
StockAmt(2500)
Variables: Stopvalue(0)

if <your buy condition> then
Once Stopvalue = (StockAmt * (Topband-Bottomband))/RiskRewardAmt);

SetStopLoss(Stopvalue);

evdl
Posts: 401
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 85 times
Been thanked: 124 times

Re: Dynamic stoploss

Postby evdl » 05 Mar 2012

Hi Escamillo,

That is a really fast reply! Thanks.

I just tried your suggestion. But that results unfortunatly in no stoploss at all. I put the stoploss code in the condition. Do you have any other suggestions?

escamillo
Posts: 203
Joined: 25 Mar 2011
Has thanked: 23 times
Been thanked: 56 times

Re: Dynamic stoploss

Postby escamillo » 05 Mar 2012

Do you have any other suggestions?
Maybe try it with (where MP = MarketPosition):

if MP[1] = 0 and MP[0] <> 0 then
Once Stopvalue = (StockAmt * (Topband-Bottomband))/RiskRewardAmt);

That method works for me on creating a value when an order is filled. That is all that I can think of for now. Someone hopefully will have an idea. Good luck.

evdl
Posts: 401
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 85 times
Been thanked: 124 times

Re: Dynamic stoploss

Postby evdl » 05 Mar 2012

Tried your last suggestion. I put the code in the condition and I also tried it, out of the condition. But the stoploss is not applied.

Thanks for your suggestions.

Does someone have any idea how to solve this?

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: Dynamic stoploss

Postby vking » 05 Mar 2012

I can think of this(not tested):

Code: Select all

// If Flat reset StopValue to '0'
if MP[0]=0 and StopValue<>0 then begin
StopValue=0;
end;

// If Not Flat and Current StopValue=0 then Set the StopValue to required Value
if MP[0]<>0 and StopValue=0 then begin
Stopvalue = (StockAmt * (Topband-Bottomband))/RiskRewardAmt);
end;

evdl
Posts: 401
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 85 times
Been thanked: 124 times

Re: Dynamic stoploss

Postby evdl » 06 Mar 2012

Hi Vking,

Changed the strategy, but can't get the dynamic stoploss to hold the pricevalue of the moment the position is taken. If I put the code in the condition, then there is no stoploss applied. If I put the code outside the condition, the stoploss will change every bar according to the bandwidth at that moment. The code "once"in front of it, will does not work and will result in no stoploss.

But thanks for your suggestion.

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: Dynamic stoploss

Postby vking » 06 Mar 2012

evdl - Can you post the code sample including the buy entry ( you can take out the stuff that you don't want to post) but a sample. That should give an idea on why this is not working for you.

Thanks.

evdl
Posts: 401
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 85 times
Been thanked: 124 times

Re: Dynamic stoploss

Postby evdl » 07 Mar 2012

Hi Vking,

This is my code. This is without the suggestions made in this thread. This code works and gives me a dynamic profit target and dynamic stoploss.

Code: Select all

[IntrabarOrderGeneration = FALSE]

inputs:Begin_time (900),
End_time (1730),
Length (20),
NumStdDevs (2),
Price (Close),
RiskRewardAmt (2),
StockAmt (5000),
ProfittargetAmt (0),
StoplossAmt (0),
TrailingTrigger (100),
TrailingAmt (75);

variables: MidBand (0), StdDev (0), TopBand (0), BottomBand (0),Stopvalue (0), profitvalue (0),VolumeAvg(0), MP(0);

MidBand = AverageFC (Price, Length);
StdDev = StandardDev (Price, Length, 1) * NumStdDevs;
TopBand = MidBand + StdDev;
BottomBand = MidBand - StdDev;
Stopvalue = (StockAmt * (Value6/100))/RiskRewardAmt;
Profitvalue = (StockAmt * (Value6/100)*0.75);
MP = Marketposition;

if TopBand <> BottomBand then begin

Value6 = (Topband - BottomBand)* 100;

If time > begin_time and time < end_time then begin
if Condition1 and Condition5 and Condition7 and condition10 and condition11 then
if MP = 0 then
Buy ("Long") next bar at market;
end;

if ProfitTargetAmt > 0 then
SetProfitTarget(ProfitTargetAmt);

if StopLossAmt > 0 then
SetStopLoss(StopLossAmt);

If StopLossAmt = 0 then
SetStopLoss(Stopvalue);

if TrailingAmt > 0 and openpositionprofit >=TrailingTrigger then
SetDollartrailing(trailingAmt);

If ProfitTargetAmt = 0 then
SetProfitTarget(profitvalue);
End;

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: Dynamic stoploss

Postby vking » 07 Mar 2012

Can you try this(moved couple of lines - haven't tested):

Code: Select all

[IntrabarOrderGeneration = FALSE]

inputs:Begin_time (900),
End_time (1730),
Length (20),
NumStdDevs (2),
Price (Close),
RiskRewardAmt (2),
StockAmt (5000),
ProfittargetAmt (0),
StoplossAmt (0),
TrailingTrigger (100),
TrailingAmt (75);

variables: MidBand (0), StdDev (0), TopBand (0), BottomBand (0),Stopvalue (0), profitvalue (0),VolumeAvg(0), MP(0);

MidBand = AverageFC (Price, Length);
StdDev = StandardDev (Price, Length, 1) * NumStdDevs;
TopBand = MidBand + StdDev;
BottomBand = MidBand - StdDev;
//Stopvalue = (StockAmt * (Value6/100))/RiskRewardAmt;
//Profitvalue = (StockAmt * (Value6/100)*0.75);
MP = Marketposition;

if TopBand <> BottomBand then begin

Value6 = (Topband - BottomBand)* 100;

If time > begin_time and time < end_time then begin
if Condition1 and Condition5 and Condition7 and condition10 and condition11 then
if MP = 0 then begin
Buy ("Long") next bar at market;
Stopvalue = (StockAmt * (Value6/100))/RiskRewardAmt;
Profitvalue = (StockAmt * (Value6/100)*0.75);
end;
end;

if ProfitTargetAmt > 0 then
SetProfitTarget(ProfitTargetAmt);

if StopLossAmt > 0 then
SetStopLoss(StopLossAmt);

If StopLossAmt = 0 then
SetStopLoss(Stopvalue);

if TrailingAmt > 0 and openpositionprofit >=TrailingTrigger then
SetDollartrailing(trailingAmt);

If ProfitTargetAmt = 0 then
SetProfitTarget(profitvalue);
End;

evdl
Posts: 401
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 85 times
Been thanked: 124 times

Re: Dynamic stoploss

Postby evdl » 08 Mar 2012

Hello Vking,

The last suggestion worked! I did put the profitvalue outside the condition so it will use the current bandwidth for profit taking. (Is good for breakouts in the right direction). And the stoploss value is the value when the position was taken.

Thanks for this and your time.

vking
Posts: 235
Joined: 21 May 2009
Has thanked: 51 times
Been thanked: 41 times

Re: Dynamic stoploss

Postby vking » 08 Mar 2012

Thanks for the update evdl :)


Return to “MultiCharts”