How to set StopLoss and ProfitTarget based on atr  [SOLVED]

Questions about MultiCharts and user contributed studies.
dario
Posts: 31
Joined: 01 Dec 2015
Has thanked: 7 times
Been thanked: 1 time

How to set StopLoss and ProfitTarget based on atr

Postby dario » 08 Dec 2015

this is my code :

Code: Select all

Inputs: wf(0.5), perc(0.5), portionTakeProfit(2.5);
Vars: indecisionBar(false),closeOnUpperPerc(false),closeOnLowerPerc(false),
buyCondition(false), sellShortCondition(false),

indecisionBar = absvalue(Open - Close) < (wf * Range);
closeOnUpperPerc = Close > Low + (perc * Range);
closeOnLowerPerc = Close < High - (perc * Range);

buyCondition = indecisionBar AND closeOnUpperPerc;
sellShortCondition = indecisionBar AND closeOnLowerPerc;

if(buyCondition AND MarketPosition = 0) then
begin
buy 10 contracts next bar High data2 stop; // BUY ON ASK PRICE
sell("Stop Loss") next bar (High - (averagetruerange(2)/3)) stop; //STOP LOSS
sell next bar (High + (averagetruerange(2)/2.5)) limit; //PROFIT TARGET
end;
but the Stop Loss and Profit target don't work... How can I insert Stop Loss and Profit Target based on atr value??

data1 : eur/usd weekly bid price
data2 : eur/usd weekly ask price

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: How to set StopLoss and ProfitTarget based on atr

Postby TJ » 08 Dec 2015

this is my code :

Code: Select all

Inputs: wf(0.5), perc(0.5), portionTakeProfit(2.5);
Vars: indecisionBar(false),closeOnUpperPerc(false),closeOnLowerPerc(false),
buyCondition(false), sellShortCondition(false),

indecisionBar = absvalue(Open - Close) < (wf * Range);
closeOnUpperPerc = Close > Low + (perc * Range);
closeOnLowerPerc = Close < High - (perc * Range);

buyCondition = indecisionBar AND closeOnUpperPerc;
sellShortCondition = indecisionBar AND closeOnLowerPerc;

if(buyCondition AND MarketPosition = 0) then
begin
buy 10 contracts next bar High data2 stop; // BUY ON ASK PRICE
sell("Stop Loss") next bar (High - (averagetruerange(2)/3)) stop; //STOP LOSS
sell next bar (High + (averagetruerange(2)/2.5)) limit; //PROFIT TARGET
end;
but the Stop Loss and Profit target don't work... How can I insert Stop Loss and Profit Target based on atr value??

data1 : eur/usd weekly bid price
data2 : eur/usd weekly ask price
take a look at your logic in detail...

ask yourself,
when you want to trigger these 2 lines, will MarketPosition = 0 ?

Code: Select all

sell("Stop Loss") next bar (High - (averagetruerange(2)/3)) stop; //STOP LOSS
sell next bar (High + (averagetruerange(2)/2.5)) limit; //PROFIT TARGET

dario
Posts: 31
Joined: 01 Dec 2015
Has thanked: 7 times
Been thanked: 1 time

Re: How to set StopLoss and ProfitTarget based on atr

Postby dario » 08 Dec 2015

not sure :)
but I want that the stopLoss and TakeProfit are set right after my buy order is filled so...if I write

Code: Select all

if(MarketPosition = 1) then
begin
sell("Stop Loss") next bar (High - (averagetruerange(2)/3)) stop; //STOP LOSS
sell next bar (High + (averagetruerange(2)/2.5)) limit; //PROFIT TARGET
end;
1)this code is evaluated on close of next weekly bar (that is too late) ..this is a problem
2)the stop and limit orders are set for the next bar...this is another problem.

How can I solve these 2 problems?

thank you.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: How to set StopLoss and ProfitTarget based on atr

Postby TJ » 08 Dec 2015

not sure :)
but I want that the stopLoss and TakeProfit are set right after my buy order is filled so...if I write

Code: Select all

if(MarketPosition = 1) then
begin
sell("Stop Loss") next bar (High - (averagetruerange(2)/3)) stop; //STOP LOSS
sell next bar (High + (averagetruerange(2)/2.5)) limit; //PROFIT TARGET
end;
1)this code is evaluated on close of next weekly bar (that is too late) ..this is a problem
2)the stop and limit orders are set for the next bar...this is another problem.

How can I solve these 2 problems?

thank you.
You might think you are trading a Weekly bar,
but in reality you are trading intra week,

ie. your coding must cater to your intra week methodology.

ie. trying to fit an intra week method into a weekly chart is not going to produce easy results.



Look up these keywords. They might be useful to your application:

SetBreakEven
SetDollarTrailing
SetPercentTrailing
SetProfitTarget
SetStopLoss
SetExitonClose

dario
Posts: 31
Joined: 01 Dec 2015
Has thanked: 7 times
Been thanked: 1 time

Re: How to set StopLoss and ProfitTarget based on atr

Postby dario » 08 Dec 2015

Ok , I solved the problem in this way :

Code: Select all

stopLossAmout = averagetruerange(atrPeriod);
takeProfitAmout = averagetruerange(atrPeriod);
stopLossAmout = (stopLossAmout * PriceScale); //Stop Loss in pips
takeProfitAmout = (takeProfitAmout * PriceScale); //Take Profit in pips

setprofittarget_pt(takeProfitAmout);
setstoploss_pt(stopLossAmout);
is a robust solution, in your opinion?

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: How to set StopLoss and ProfitTarget based on atr  [SOLVED]

Postby TJ » 08 Dec 2015

::

is a robust solution, in your opinion?
The devil is in the details.

Writing a strategy is a thousands iterations of refinements repeated thousands of times.


Return to “MultiCharts”