Print function excuting when it shouldnt

Questions about MultiCharts and user contributed studies.
User avatar
t-rader
Posts: 139
Joined: 02 Feb 2011
Location: Australia
Has thanked: 11 times
Been thanked: 27 times

Print function excuting when it shouldnt

Postby t-rader » 31 Jul 2011

I have a signal that runs something like this:

Code: Select all

If MarketPosition = 0 Then begin

If condition1 = true and condition 2 = true then begin
buy next bar at market;
print("order executed");
end;

end;

When applied to a chart lets say it takes 10 trades, yet the print will print out hundreds of lines saying 'ordered executed", when it should only print when all conditions are met and thus be equal to the number of trades executed.

Its like its getting into this section of the code even though the conditions have not been met?

Has anyone else had this problem? or is there something wrong with the code?

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

Re: Print function excuting when it shouldnt

Postby TJ » 31 Jul 2011

note: Please use

Code: Select all

tags when posting codes. It makes reading codes easier.
ps. I have added them for you this time.

User avatar
t-rader
Posts: 139
Joined: 02 Feb 2011
Location: Australia
Has thanked: 11 times
Been thanked: 27 times

Re: Print function excuting when it shouldnt

Postby t-rader » 01 Aug 2011

Thanks TJ.

Any idea's about the issue?

User avatar
t-rader
Posts: 139
Joined: 02 Feb 2011
Location: Australia
Has thanked: 11 times
Been thanked: 27 times

Re: Print function excuting when it shouldnt

Postby t-rader » 07 Aug 2011

So nobody's having this issue?

I've also tried it with 'FileAppend' instead of 'print' and get the same issue.

I am using IOG (Intrabar Order Generation) so maybe its something to do with that?

Cheers,

Rick Webber
Posts: 47
Joined: 04 Jan 2008
Has thanked: 21 times
Been thanked: 3 times

Re: Print function excuting when it shouldnt

Postby Rick Webber » 08 Sep 2011

I am also having an issue with this. I am using fileappend to send buy/sell signals using a crossover as a condition.Not using IOG! Same problem. The chart shows a Buy?Sell order correctly but the FileAppend orders continue to fire at the close of every bar even though the condition is now false.

sptrader
Posts: 742
Joined: 09 Apr 2010
Location: Texas
Has thanked: 483 times
Been thanked: 274 times
Contact:

Re: Print function excuting when it shouldnt

Postby sptrader » 08 Sep 2011

Code: Select all

condition1 = average(c,5) crosses above average(c,15);
condition2 = average(c,5) crosses below average(c,20);

If MarketPosition = 0 Then begin

If condition1 = true then begin
buy next bar at market;
print("order executed");
end;
end;
if marketposition = 1 and condition2 then sell next bar at market;
I just tried this code(above) and it works OK in playback mode, I haven't tried it in RT though..

If you still have trouble maybe you could add a flag..ie.

Code: Select all

If condition and condition2 then prntflg = 1 else prntflg = 0; then in your print statement - if prntflg = 1 then print ;
I'll try it in RT tomorrow..

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

Re: Print function excuting when it shouldnt

Postby TJ » 08 Sep 2011

So nobody's having this issue?

I've also tried it with 'FileAppend' instead of 'print' and get the same issue.

I am using IOG (Intrabar Order Generation) so maybe its something to do with that?

Cheers,
IOG would make it print on every tick while the conditions are true.

Erik Pepping
Posts: 74
Joined: 25 Aug 2007
Been thanked: 6 times

Re: Print function excuting when it shouldnt

Postby Erik Pepping » 08 Sep 2011

Market position is not reliable. What I do is with the but or sell set my own variable.

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: Print function excuting when it shouldnt

Postby furytrader » 09 Sep 2011

It may help debugging if, within with your print statement, you include a reference to the date and time of the bar that is triggering the signal ... i.e.,

Code: Select all

print(date,"-",time,"-order executed");
This will let you know if it's printing only on those bars that also triggers the order or whether it's triggering for other bars as well.

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

Re: Print function excuting when it shouldnt

Postby TJ » 09 Sep 2011

I have a signal that runs something like this:

Code: Select all

If MarketPosition = 0 Then begin

If condition1 = true and condition 2 = true then begin
buy next bar at market;
print("order executed");
end;

end;

When applied to a chart lets say it takes 10 trades, yet the print will print out hundreds of lines saying 'ordered executed", when it should only print when all conditions are met and thus be equal to the number of trades executed.

Its like its getting into this section of the code even though the conditions have not been met?

Has anyone else had this problem? or is there something wrong with the code?
for debugging purpose,
you can add this to the print line:

Code: Select all

If MarketPosition = 0 Then begin

If condition1 = true and condition 2 = true then begin
buy next bar at market;
print(currenttime_s, " marketposition=", marketposition, "order executed");
end;

end;

Erik Pepping
Posts: 74
Joined: 25 Aug 2007
Been thanked: 6 times

Re: Print function excuting when it shouldnt

Postby Erik Pepping » 09 Sep 2011

You can also try to use LastBarOnchart. So it never prints with previous bars while loading / plotting the chart.

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

Re: Print function excuting when it shouldnt

Postby TJ » 09 Sep 2011

The OP's issue (and his original question) is, WHY does it print when marketposition was not equals to zero.

He wasn't looking for a work around, but an explanation.

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

Re: Print function excuting when it shouldnt

Postby NW27 » 10 Sep 2011

A few thoughts -
1. Your actual order statement is "buy next bar at market;" Emphasis on the "NEXT BAR"
Ie Buy at Market on the next 5min bar.

2. Your code is working on the current bar, where MarketPosition=0, cond1 & cond2 are true, fine.
Remember that this code is being executed on the current bar and each tick that comes in for this current bar. On a five minute bar, this could be thousands of ticks, thus thousands of loops, thus thousands of Print statements for this current bar.

3. When does MarketPosition change to <> 0 ??? When it receives confirmation of the trade placement from the broker ??? This could take a couple of seconds ???
Ie Order goes out to broker, trade is executed at exchange, broker sends back confirmation of order and position, MarketPosition is now change to reflect the new position.

For your print statement, perhaps it should be more -
If Marketposition<>0 and MarketPosition[1]=0 then
Print("Order Executed");
Ie wait for the change in MarketPosition before doing the print.


Neil.

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

Re: Print function excuting when it shouldnt

Postby NW27 » 10 Sep 2011

Another thought -
"I just tried this code(above) and it works OK in playback mode, I haven't tried it in RT though.."

As an example, the chart you are referring to is a 5min chart.
In playback mode, are you selecting the playback increment size as being 5min bars?
If so in playback mode, your code is only being executed once per bar.
Still working on it being 5min chart, but we set the playback mode to being say 1 minute,
Now the code is executed 5 times within your 5 min bar, potentially creating 5 print statements.

I say potentially because -
At the start of this 5min bar, we will say that Marketposition=0, cond1=true and cond2=false ie MA crossover had not happened yet.
On the 3rd min within this bar the cond2 is now true (cross over happened), so now we get two print statements.

Remember that your code is being checked on every incoming tick. As I said above in my previous post, this could be 1000's of times during the building of the current 5min bar.

Neil.

sptrader
Posts: 742
Joined: 09 Apr 2010
Location: Texas
Has thanked: 483 times
Been thanked: 274 times
Contact:

Re: Print function excuting when it shouldnt

Postby sptrader » 10 Sep 2011

I used a 5 minute chart with playback mode set to "minute by minute" Also tried "Tick by Tick" they BOTH worked correctly, only one print per entry in PE.


Return to “MultiCharts”