keep orders open for set number of bars

Questions about MultiCharts and user contributed studies.
kagein
Posts: 55
Joined: 12 Oct 2017
Has thanked: 16 times
Been thanked: 10 times

keep orders open for set number of bars

Postby kagein » 31 Oct 2017

I'm trying to figure out how i can keep an order open for set number of bars. At the moment the code below is what i can come up with.
The trade_on variable doesn't seem to be resetting, so after an entry, buy stops are still placed even though the long condition is false.

Code: Select all

if long_condition then begin
bar_number = BarNumber;
entryprice = high;
trade_on = true;
end;

if trade_on and (BarNumber - bar_number) <=Bars_valid then
buy next bar at entryprice stop;

if marketposition = 1 then trade_on = false;
if (BarNumber - bar_number) > Bars_valid then trade_on= false
Any help would be greatly appreciated.

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

Re: keep orders open for set number of bars

Postby TJ » 31 Oct 2017

I'm trying to figure out how i can keep an order open for set number of bars. At the moment the code below is what i can come up with.
The trade_on variable doesn't seem to be resetting, so after an entry, buy stops are still placed even though the long condition is false.

Code: Select all

if long_condition then begin
bar_number = BarNumber;
entryprice = high;
trade_on = true;
end;

if trade_on and (BarNumber - bar_number) <=Bars_valid then
buy next bar at entryprice stop;

if marketposition = 1 then trade_on = false;
if (BarNumber - bar_number) > Bars_valid then trade_on= false
Any help would be greatly appreciated.

When long_condition = False
do you have a routine to turn trade_on = False ?

kagein
Posts: 55
Joined: 12 Oct 2017
Has thanked: 16 times
Been thanked: 10 times

Re: keep orders open for set number of bars

Postby kagein » 31 Oct 2017

i thought the code below would turn trade_on to false. So 1. when an order is placed above a high and i'm filled. 2. When bars between time order is placed is greater than bars_valid.

Code: Select all

if marketposition = 1 then trade_on = false;
if (BarNumber - bar_number) > Bars_valid then trade_on= false

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

Re: keep orders open for set number of bars

Postby TJ » 31 Oct 2017

i thought the code below would turn trade_on to false. So 1. when an order is placed above a high and i'm filled. 2. When bars between time order is placed is greater than bars_valid.

Code: Select all

if marketposition = 1 then trade_on = false;
if (BarNumber - bar_number) > Bars_valid then trade_on= false

Read my question again carefully.
I asked a specific question.
You have given a reply, but you have not addressed my question. You circumvented it.

User avatar
ABC
Posts: 718
Joined: 16 Dec 2006
Location: www.abctradinggroup.com
Has thanked: 125 times
Been thanked: 408 times
Contact:

Re: keep orders open for set number of bars

Postby ABC » 01 Nov 2017

TJ,

as far as I can tell from the code snippet posted, the trade_on reset to false is not done within the "if long_condition then begin" conditional statement. So for the snippet posted the code has a routine to "to turn trade_on = False" in case long_condition = False.

However one could only guess from the code snippet where the problem is (maybe long_condition remains true and thus trade_on remains true, too). Kagein you might want to post full working code to demonstrate the problem, as this should show where the problem is.

Regards,

ABC

kagein
Posts: 55
Joined: 12 Oct 2017
Has thanked: 16 times
Been thanked: 10 times

Re: keep orders open for set number of bars

Postby kagein » 01 Nov 2017

This is essentially shows the issue im having

Code: Select all

inputs:
bars_valid(10),
target(10),
stoploss(15);

vars :
entry_price(0),
long_condition(false),
bar_number(0),
trade_on(false);

long_condition = high > high[1] and low < low[1];

if long_condition = true then begin
bar_number = BarNumber;
entry_price = high;
trade_on = true;
end;

if trade_on and (BarNumber - bar_number) <=Bars_valid then
buy next bar at entry_price stop;

if marketposition = 1 then trade_on = false;
if (BarNumber - bar_number) > Bars_valid then trade_on= false;

setstoploss_pt(stoploss);
setprofittarget_pt(target);

User avatar
ABC
Posts: 718
Joined: 16 Dec 2006
Location: www.abctradinggroup.com
Has thanked: 125 times
Been thanked: 408 times
Contact:

Re: keep orders open for set number of bars

Postby ABC » 01 Nov 2017

kagein,

according to your code anytime long_condition is true (even while you are in a trade), trade_on will become true and could allow an order.

Regards,

ABC

kagein
Posts: 55
Joined: 12 Oct 2017
Has thanked: 16 times
Been thanked: 10 times

Re: keep orders open for set number of bars

Postby kagein » 03 Nov 2017

yes this is the case, because i want to have the order open for a set number of bars even if the next bar is false. I want to limit the trades within the bars_valid. So i thought that once i put trade_on = false when a trade is initiated it will limit the number of trades till another bar satisfies the long condition.

Jad
Posts: 92
Joined: 15 Jun 2014
Has thanked: 13 times
Been thanked: 21 times

Re: keep orders open for set number of bars

Postby Jad » 03 Nov 2017

When the long condition is first satisfied, your variable 'trade_on' is reset to false on every tick - but only until the next bar (which is also when your order is filled). The variable 'trade_on' then no longer resets to false on every tick but instead, resets to the state it was last set (in this case, 'true').

So, even though the long condition is no longer valid on the following bar, the code following if trade_on and (BarNumber - bar_number) <=Bars_valid is still going to be executed.

Because that is performed before you check marketposition, you will send another order before you reset 'trade_on' to false.
However, this will again be reset automatically on each tick - but now to 'true' - until the next bar. Only then will it reset to false etc. etc.

You could either change the order of your 'IF' statements or consider using 'IntraBarPersist' for some of your variables.


Return to “MultiCharts”