How to get a value for an ATR stop?

Questions about MultiCharts and user contributed studies.
Erik1966
Posts: 7
Joined: 24 Oct 2014
Has thanked: 1 time

How to get a value for an ATR stop?

Postby Erik1966 » 09 Dec 2014

Hello,

I am trying to get a limit stop order using averagetruerange in my signal.
This is how i get the value:

Code: Select all

value1= entryprice - averagetruerange (atrlength)*NumATRs ;
value2= entryprice + averagetruerange (atrlength)*NumATRs ;
And this is how i currently use it in my signal:

Code: Select all

[IntrabarOrderGeneration=true]
if marketposition = -1 and price > value2

then
begin
buytocover ("rsishortstop") next bar;
end;
The position is now closed at the bar that opens above the entryprice+averagetruerange but what i realy want is a limit stop order at the exact sum of entryprice+averagetruerange.

How can i get a value from an indicator to use it for limit orders?

Thanks,
Erik

MultiCharts Version 9.0 Release (Build 10252)

User avatar
ABC
Posts: 631
Joined: 16 Dec 2006
Has thanked: 119 times
Been thanked: 355 times
Contact:

Re: How to get a value for an ATR stop?

Postby ABC » 09 Dec 2014

Erik1966,

how about using a stop order to accomplish what you have in mind?

Something along the lines of:

Code: Select all

[IntrabarOrderGeneration=true]
if marketposition = -1
then
buytocover ("rsishortstop") next bar value2 stop ;
Regards,
ABC

Erik1966
Posts: 7
Joined: 24 Oct 2014
Has thanked: 1 time

Re: How to get a value for an ATR stop?

Postby Erik1966 » 09 Dec 2014

Erik1966,

how about using a stop order to accomplish what you have in mind?

Something along the lines of:

Code: Select all

[IntrabarOrderGeneration=true]
if marketposition = -1
then
buytocover ("rsishortstop") next bar value2 stop ;
Regards,
ABC
Hi ABC,
Thank you for your reply.

The buytocover is still at the beginning of a new bar, i want the buytocover immediatly at market when the price hits the sum entryprice+averagetruerange.

Example:if the price is 100 and the value from atrlength (14) is 10 multiplied by NumATRs (1.5), then i want buytocover or setstoploss fixed at 115.

Regards

User avatar
ABC
Posts: 631
Joined: 16 Dec 2006
Has thanked: 119 times
Been thanked: 355 times
Contact:

Re: How to get a value for an ATR stop?

Postby ABC » 09 Dec 2014

Erik1966,

I am not sure if I follow you about what you mean with "still at open of the next bar". With intrabar order generation an order for "next bar" will be issued the next tick. So the order will be send immediately when MarketPosition = -1.
If you are doing a backtest and as you are working with intrabar order generation make sure to enable the bar magnifier with the highest resolution possible.

It might make sense though to prevent the price from changing after the placement, unless you want it to be updated when the ATR value changes.

Regards,
ABC

Erik1966
Posts: 7
Joined: 24 Oct 2014
Has thanked: 1 time

Re: How to get a value for an ATR stop?

Postby Erik1966 » 09 Dec 2014

ABC,

When you look at my example the stop i want is 115 but the next bar can open at 118 or higher and my trade wil be closed at 118 or higher while my stop is 115.
I don't want to wait for the next bar.
So how can i set the stoploss at 115 using averagetruerange?

Regards,
Erik

User avatar
ABC
Posts: 631
Joined: 16 Dec 2006
Has thanked: 119 times
Been thanked: 355 times
Contact:

Re: How to get a value for an ATR stop?

Postby ABC » 10 Dec 2014

Erik1966,

declare a variable and make it intrabarpersist to store the value of your stop price instead of using value2. Besides that use print statements in your code to print the values of the stop at each bar to the output. This way you can track down the values used in your code and know why the stop was executed only on the next bar.

Regards,
ABC

Erik1966
Posts: 7
Joined: 24 Oct 2014
Has thanked: 1 time

Re: How to get a value for an ATR stop?

Postby Erik1966 » 10 Dec 2014

Erik1966,

declare a variable and make it intrabarpersist to store the value of your stop price instead of using value2. Besides that use print statements in your code to print the values of the stop at each bar to the output. This way you can track down the values used in your code and know why the stop was executed only on the next bar.

Regards,
ABC
Thanks, i'm going to learn how to do that.
The code you provided closes trades intrabar.
My first code works as it should work.

Regards,
Erik

User avatar
ABC
Posts: 631
Joined: 16 Dec 2006
Has thanked: 119 times
Been thanked: 355 times
Contact:

Re: How to get a value for an ATR stop?

Postby ABC » 10 Dec 2014

Erik1966,

your requestion is a bit confusing for me, maybe you can try to elaborate a bit.
- You want to use values from an indicator in a strategy. Why not compute the value in the strategy exactly.

- I misread that you asked for a stop limit order. The code I provided sends a simple stop order. You can modify it by adding the limit price:
buytocover ("rsishortstop") next bar StopPrice stop LimitPrice limit;

Where StopPrice and LimitPrice would be variables storing the prices for the stop and limit.

- When do you want to send the order? What price should be used i.e. use the value of Variable2 at the time of the entry or at the time of the order (or from another time).

- Do you want to update the order price while you are in the position?

Answering those questions will help me and others reading this thread getting a better understanding of what is required coding wise.

Regards,

ABC

Erik1966
Posts: 7
Joined: 24 Oct 2014
Has thanked: 1 time

Re: How to get a value for an ATR stop?

Postby Erik1966 » 11 Dec 2014

ABC,
Thank you for your input,
- When do you want to send the order?
Let's name this stoplossorder from now on.
The stoplossorder must be send at the same time or direct after the entry.
This is the entry:

Code: Select all

then
begin
sellshort ("rsisell") next bar open;
end;
What price should be used i.e. use the value of Variable2 at the time of the entry or at the time of the order (or from another time).
The price used must be the price at the time of the entry (entryprice). Value2 is the price where the stoplossorder must be placed.
I think that the stoplossorder only can be calculated after the entry because only then the entryprice can be used to calculate the sum of value2.
- Do you want to update the order price while you are in the position?
No, the stoplossorder does not update or trail the position, it is a fixed stoploss.

This is my takeprofit:

Code: Select all

[IntrabarOrderGeneration=False]
if marketposition = -1 and CloseD (0) < entryprice

then
begin
buytocover ("rsishortprofit") next bar;
end;
When the position is closed by this takeprofit then the stoplossorder must be removed/deleted for it has no further use.

Regards,
Erik

User avatar
TJ
Posts: 7210
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 991 times
Been thanked: 2052 times

Re: How to get a value for an ATR stop?

Postby TJ » 11 Dec 2014

ABC,
Thank you for your input,
::
This is my takeprofit:

Code: Select all

[IntrabarOrderGeneration=False]
if marketposition = -1 and CloseD (0) < entryprice

then
begin
buytocover ("rsishortprofit") next bar;
end;
When the position is closed by this takeprofit then the stoplossorder must be removed/deleted for it has no further use.

Regards,
Erik
Just want to make sure we are on the same page and do not have misunderstandings,

[IntrabarOrderGeneration=False] is a Strategy Statement, it is for the whole strategy, not just a section of the code. {edited}

You declare IntrabarOrderGeneration at the beginning of your code. It is a one time declaration.

User avatar
ABC
Posts: 631
Joined: 16 Dec 2006
Has thanked: 119 times
Been thanked: 355 times
Contact:

Re: How to get a value for an ATR stop?

Postby ABC » 11 Dec 2014

Erik1966,

it's probably best if you post the whole code with entries and exits. You can change the entry conditions to something else to protect your logic. This way we can all see the same thing and as TJ has pointed out changing the IntrabarOrderGeneration in your code several times will not give you the result you are hoping for. Maybe there are other things in the code that create issues and are related.

Regards,
ABC

Erik1966
Posts: 7
Joined: 24 Oct 2014
Has thanked: 1 time

Re: How to get a value for an ATR stop?

Postby Erik1966 » 11 Dec 2014

Hi ABC and TJ,

This is a part of the code.
It's my first code ever so i am curious what your findings are.

Code: Select all

[IntrabarOrderGeneration = false ]
Inputs:
Length ( 20 ),
atrlength(14), NumATRs( 1 );

value1= entryprice - averagetruerange (atrlength)*NumATRs ;
value2= entryprice + averagetruerange (atrlength)*NumATRs ;
//------------------------long buy-------------------------------------------------------------//


if Average (close, length)> average (close, length) yesterday


then
begin
buy("rsilongbuy") next bar open;

end;
//--------------------------long stoploss------------------------------------------------------//

If MarketPosition = 1
then
begin
sell ("rsilongstop") next bar value1 stop;
end;

//-------------------------long takeprofit------------------------------------------------------//

if marketposition = 1 and CloseD (0) > entryprice
then
begin
sell ("rsilongprofit")this bar;

end;
//-----------------------short sell------------------------------------------------------------//


if Average (close, length)< average (close, length) yesterday


then
begin
sellshort("rsisell") next bar open;
end;
//-------------------------short stoploss------------------------------------------------------//

if marketposition = -1

then
begin
buytocover ("rsishortstop") next bar value2 stop ;
end;
//-------------------------short takeprofit----------------------------------------------------//

if marketposition = -1 and CloseD (0) < entryprice

then
begin
buytocover ("rsishortprofit") this bar;
end;
Regards,
Erik
Last edited by Erik1966 on 11 Dec 2014, edited 1 time in total.

User avatar
TJ
Posts: 7210
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 991 times
Been thanked: 2052 times

Re: How to get a value for an ATR stop?

Postby TJ » 11 Dec 2014

Hi ABC and TJ,

This is a part of the code.
It's my first code ever so i am curious what your findings are.
::
Regards,
Erik
Have you read and understood my post #10 ?

Erik1966
Posts: 7
Joined: 24 Oct 2014
Has thanked: 1 time

Re: How to get a value for an ATR stop?

Postby Erik1966 » 11 Dec 2014

Hi ABC and TJ,

This is a part of the code.
It's my first code ever so i am curious what your findings are.
::
Regards,
Erik
Have you read and understood my post #10 ?
TJ,

Yes i did read your post and learned that IntrabarOrderGeneration is a global statement.

I had removed most entryrules but forgot to remove the IntrabarOrderGeneration statements, i have adjusted the code now.

User avatar
Henry MultiСharts
Posts: 9110
Joined: 25 Aug 2011
Has thanked: 1258 times
Been thanked: 2927 times

Re: How to get a value for an ATR stop?

Postby Henry MultiСharts » 17 Dec 2014

Erik1966,

Please study the following article to understand how orders are filled in backtesting: How Signals are Calculated.


Return to “MultiCharts”