Signal Entering One Bar Too Late?  [SOLVED]

Questions about MultiCharts and user contributed studies.
dwitkin
Posts: 36
Joined: 18 Apr 2013
Has thanked: 41 times
Been thanked: 11 times

Signal Entering One Bar Too Late?

Postby dwitkin » 27 May 2013

I'm still new to MultiCharts and EasyLanguage, so hopefully someone can give me a hand with this. I'm trying to code a simple entry signal, but my entries aren't occurring when I would expect them too. They seem to be occurring at least one bar later than I'd expect. Suggestions / help is greatly appreciated.

Here's the "trigger" logic.
Trigger an entry when...
(1) today's close is greater than yesterday's high and
(2) today's close is greater than today's open and
(3) today's low is greater than yesterday's low.

When these conditions are met, enter at a stop price of today's high + $0.05.

The attached screen shot shows an example when the entry should have occurred at the trigger price of $11.74 on bar 3426 (April 22), which hit a high price of $11.75, but instead the entry triggered at the open on the subsequent bar (April 23).

Here's my current code, which isn't working as expected. A bunch of the code enclosed within "{Debugging Code}" start/end tags and can be ignored.

Code: Select all

[ProcessMouseEvents=true]

variables: Close_GT_Yes_High( false ), // Today's close is Greater Than (GT) yesterday's high
Close_GT_Open( false ), // Today's close is GT today's open
Low_GT_Yes_Low( false ), // Today's low is GT yesterday's low
BuyTrigger(false), // Are all UE conditions true?
TriggerBarHigh(0), // High price of the 'triger' bar
TriggerBuyPrice(0); // $0.05 above trigger bar high


Close_GT_Yes_High = Close > high[1];
Close_GT_Open = Close > Open;
Low_GT_Yes_Low = Low > Low[1];

{Debugging Code}
{The CheckCommentary condition only evaluates the commentary on the currently selected bar;
allows for much faster execution}
If CheckCommentary Then Begin
{BarStatus=2 condition only provides commentary on the close value; if IOG is enables
and this condition is not enabled, the commentary evaluates 4 times - once for
open, high, low and close}
If barstatus=2 then begin
Commentary("Section 01: After Initial varible assignment / inspection", newline);
Commentary("Current Bar Number: ", Currentbar:4:0, newline);
Commentary("Close_GT_Yes_High: ", Close_GT_Yes_High, newline);
Commentary("Close_GT_Open: ", Close_GT_Open, newline);
Commentary("Low_GT_Yes_Low: ", Low_GT_Yes_Low, newline);
Commentary("Section 01: End", newline, newline);
end;
End;
{End Debugging Code}


if Close_GT_Yes_High and
Close_GT_Open and
Low_GT_Yes_Low then
begin
BuyTrigger = true;
TriggerBarHigh = high;
TriggerBuyPrice = TriggerBarHigh + 0.05;
{Debugging Code}
If CheckCommentary Then Begin
If barstatus=2 then begin
Commentary("Section 02: Conditions for buy met", newline);
Commentary("Current Bar Number: ", Currentbar:4:0, newline);
Commentary("BuyTrigger: ", BuyTrigger, " ", newline);
Commentary("TriggerBarHigh: ", TriggerBarHigh , " ", newline);
Commentary("TriggerBuyPrice: ", TriggerBuyPrice , " " , newline);
Commentary("Section 02: End", newline, newline);
end;
end;
{End Debugging Code}
end
else
begin
BuyTrigger = false;
TriggerBarHigh = 0;
TriggerBuyPrice = 0;
{Debugging Code}
If CheckCommentary Then Begin
If barstatus=2 then begin
Commentary("Section 03: Conditions for buy NOT met", newline);
Commentary("Current Bar Number: ", Currentbar:4:0, newline);
Commentary("BuyTrigger: ", BuyTrigger, " ", newline);
Commentary("TriggerBarHigh: ", TriggerBarHigh , " ", newline);
Commentary("TriggerBuyPrice: ", TriggerBuyPrice , " ", newline);
Commentary("Section 03: End", newline, newline);
end;
end;
{End Debugging Code}
end;

if BuyTrigger then
begin
Buy ( "DAW_UE" ) next bar at TriggerBuyPrice stop ;
BuyTrigger= false ;
end ;
Attachments
2013-05-27 1335 Multicharts UE Signal Troubleshooting.png
(159.92 KiB) Downloaded 816 times

dwitkin
Posts: 36
Joined: 18 Apr 2013
Has thanked: 41 times
Been thanked: 11 times

Re: Signal Entering One Bar Too Late?

Postby dwitkin » 27 May 2013

Almost forgot to mention that I did enable "Allow Intrabar Order Generation" via the entry signal dialog box.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Signal Entering One Bar Too Late?

Postby Henry MultiСharts » 28 May 2013

Hello dwitkin,

Which version and build number of MultiCharts do you use?
Please attach a screenshot of Format->Strategy properties->Backtesting tab.

segut
Posts: 14
Joined: 01 Nov 2012
Has thanked: 5 times

Re: Signal Entering One Bar Too Late?

Postby segut » 28 May 2013

I am new too with this but I had similar problems before. You require that

"When these conditions are met, enter at a stop price of today's high + $0.05."

This has top execute at next bar based on the previous bar close because if it is executed at current bar it will assume that you know the high before the close of bar.

I don't know if that is the problem you had.

dwitkin
Posts: 36
Joined: 18 Apr 2013
Has thanked: 41 times
Been thanked: 11 times

Re: Signal Entering One Bar Too Late?

Postby dwitkin » 30 May 2013

Henry,

I'm using:
MultiCharts64 Version 8.5 Release (Build 6851)

Attached is the screen shot you asked for.


That said, something has changed; the entries are now different than what was shown on screen shot I provided in the prior email. Even so, there are still problems.

For example, see the attached screenshot. It shows my setup conditions were met on 2/27/2013. My entry was put in at a stop of $11.41. The 2/28/2013 bar shows the position was entered, even though the price never reached $11.41 on that date. I don't understand how that can be...

I'm re-attaching the code because I may have changed something...

Code: Select all

[ProcessMouseEvents=true]
[IntrabarOrderGeneration = true]

variables: Close_GT_Yes_High( false ), // Today's close is Greater Than (GT) yesterday's high
Close_GT_Open( false ), // Today's close is GT today's open
Low_GT_Yes_Low( false ), // Today's low is GT yesterday's low
BuyTrigger(false), // Are all UE conditions true?
TriggerBarHigh(0), // High price of the 'triger' bar
TriggerBuyPrice(0); // $0.05 above trigger bar high


Close_GT_Yes_High = Close > high[1];
Close_GT_Open = Close > Open;
Low_GT_Yes_Low = Low > Low[1];

{Debugging Code}
{The CheckCommentary condition only evaluates the commentary on the currently selected bar;
allows for much faster execution}
If CheckCommentary Then Begin
{BarStatus=2 condition only provides commentary on the close value; if IOG is enables
and this condition is not enabled, the commentary evaluates 4 times - once for
open, high, low and close}
If barstatus=2 then begin
Commentary("Section 01: After Initial varible assignment / inspection", newline);
Commentary("Current Bar Number: ", Currentbar:4:0, newline);
Commentary("Close_GT_Yes_High: ", Close_GT_Yes_High, newline);
Commentary("Close_GT_Open: ", Close_GT_Open, newline);
Commentary("Low_GT_Yes_Low: ", Low_GT_Yes_Low, newline);
Commentary("Section 01: End", newline, newline);
end;
End;
{End Debugging Code}


if Not(BuyTrigger) and // Only evaluate if a buy-stop is not already triggered
Close_GT_Yes_High and
Close_GT_Open and
Low_GT_Yes_Low then
begin
BuyTrigger = true;
TriggerBarHigh = high;
TriggerBuyPrice = TriggerBarHigh + 0.05;
{Debugging Code}
If CheckCommentary Then Begin
If barstatus=2 then begin
Commentary("Section 02: Conditions for buy met", newline);
Commentary("Current Bar Number: ", Currentbar:4:0, newline);
Commentary("BuyTrigger: ", BuyTrigger, " ", newline);
Commentary("TriggerBarHigh: ", TriggerBarHigh , " ", newline);
Commentary("TriggerBuyPrice: ", TriggerBuyPrice , " " , newline);
Commentary("Section 02: End", newline, newline);
end;
end;
{End Debugging Code}
end
else
begin
TriggerBarHigh = 0;
TriggerBuyPrice = 0;
{Debugging Code}
If CheckCommentary Then Begin
If barstatus=2 then begin
Commentary("Section 03: Conditions for buy NOT met", newline);
Commentary("Current Bar Number: ", Currentbar:4:0, newline);
Commentary("BuyTrigger: ", BuyTrigger, " ", newline);
Commentary("TriggerBarHigh: ", TriggerBarHigh , " ", newline);
Commentary("TriggerBuyPrice: ", TriggerBuyPrice , " ", newline);
Commentary("Section 03: End", newline, newline);
end;
end;
{End Debugging Code}
end;

if BuyTrigger then
begin
Buy ( "DAW_UE" ) next bar at TriggerBuyPrice stop ;
If marketposition <> 0 then
BuyTrigger= false ;
end ;
Hello dwitkin,

Which version and build number of MultiCharts do you use?
Please attach a screenshot of Format->Strategy properties->Backtesting tab.
Attachments
2013-05-30 1000 UE Signal Troubleshooting 002.png
New UE Entry Issues
(169.29 KiB) Downloaded 789 times
2013-05-30 0931 Backtest Settings.png
Backtest Settings Window
(40.51 KiB) Downloaded 817 times

dwitkin
Posts: 36
Joined: 18 Apr 2013
Has thanked: 41 times
Been thanked: 11 times

Re: Signal Entering One Bar Too Late?

Postby dwitkin » 30 May 2013

Henry,

At least some part of the issue is due to IOG. When IOG is set to TRUE, the entry prices are not the stop entry prices; entries appear to occur before the stop price is reached. This is shown in the example / screen shot showing the 2/27/2013 trigger and 2/28/2013 trade I previously attached.

When IOG is set to FALSE (see new screen shot attached; the only change is IOG was set to FALSE in the code), the entry price on this trade is $11.13 on the subsequent bar. Any idea why? If I'm reading the documentation correctly, the execution should still be able to take place at any price within a bar even with IOG off.

Let me know your thoughts / suggestions. Thanks.
Attachments
2013-05-30 1024 Feb 27 Trade With IOG False.png
(170.77 KiB) Downloaded 803 times

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Signal Entering One Bar Too Late?

Postby Henry MultiСharts » 30 May 2013

Hello dwitkin,

BuyTrigger, TriggerBarHigh and TriggerBuyPrice variables are reset intrabar. These variables should be intrabarpersist in order to save their values intrabar.
Also when prices are reset to zero your execution condition is still set as True which do not look right. Please check the attached modified code that should work as expected.

Code: Select all

once cleardebug;
[ProcessMouseEvents=true]
[IntrabarOrderGeneration = true]

variables: Close_GT_Yes_High( false ), // Today's close is Greater Than (GT) yesterday's high
Close_GT_Open( false ), // Today's close is GT today's open
Low_GT_Yes_Low( false ), // Today's low is GT yesterday's low
intrabarpersist BuyTrigger(false), // Are all UE conditions true?
intrabarpersist TriggerBarHigh(0), // High price of the 'triger' bar
intrabarpersist TriggerBuyPrice(0); // $0.05 above trigger bar high


Close_GT_Yes_High = Close > high[1];
Close_GT_Open = Close > Open;
Low_GT_Yes_Low = Low > Low[1];

{Debugging Code}
{The CheckCommentary condition only evaluates the commentary on the currently selected bar;
allows for much faster execution}
If CheckCommentary Then Begin
{BarStatus=2 condition only provides commentary on the close value; if IOG is enables
and this condition is not enabled, the commentary evaluates 4 times - once for
open, high, low and close}
If barstatus=2 then begin
Commentary("Section 01: After Initial varible assignment / inspection", newline);
Commentary("Current Bar Number: ", Currentbar:4:0, newline);
Commentary("Close_GT_Yes_High: ", Close_GT_Yes_High, newline);
Commentary("Close_GT_Open: ", Close_GT_Open, newline);
Commentary("Low_GT_Yes_Low: ", Low_GT_Yes_Low, newline);
Commentary("Section 01: End", newline, newline);
end;
End;
{End Debugging Code}


if Not(BuyTrigger) and // Only evaluate if a buy-stop is not already triggered
Close_GT_Yes_High and
Close_GT_Open and
Low_GT_Yes_Low then
begin
BuyTrigger = true;
TriggerBarHigh = high;
TriggerBuyPrice = TriggerBarHigh + 0.05;
{Debugging Code}
If CheckCommentary Then Begin
If barstatus=2 then begin
Commentary("Section 02: Conditions for buy met", newline);
Commentary("Current Bar Number: ", Currentbar:4:0, newline);
Commentary("BuyTrigger: ", BuyTrigger, " ", newline);
Commentary("TriggerBarHigh: ", TriggerBarHigh , " ", newline);
Commentary("TriggerBuyPrice: ", TriggerBuyPrice , " " , newline);
Commentary("Section 02: End", newline, newline);
end;
end;
{End Debugging Code}
end
else
begin
TriggerBarHigh = 0;
TriggerBuyPrice = 0;
BuyTrigger= false ;

{Debugging Code}
If CheckCommentary Then Begin
If barstatus=2 then begin
Commentary("Section 03: Conditions for buy NOT met", newline);
Commentary("Current Bar Number: ", Currentbar:4:0, newline);
Commentary("BuyTrigger: ", BuyTrigger, " ", newline);
Commentary("TriggerBarHigh: ", TriggerBarHigh , " ", newline);
Commentary("TriggerBuyPrice: ", TriggerBuyPrice , " ", newline);
Commentary("Section 03: End", newline, newline);
end;
end;
{End Debugging Code}
end;

if BuyTrigger then
begin
Buy ( "DAW_UE" ) next bar at TriggerBuyPrice stop ;
If marketposition <> 0 then
BuyTrigger= false ;
end ;

dwitkin
Posts: 36
Joined: 18 Apr 2013
Has thanked: 41 times
Been thanked: 11 times

Re: Signal Entering One Bar Too Late?  [SOLVED]

Postby dwitkin » 04 Jun 2013

Henry,

Thanks for the suggestions. I tried your revised code, but the results were still not as I expected so I went back to the drawing board and re-worked the logic. Here's the working code in case it will help others. It seems to work without the intrabarpersist statements and with IOG off.

Code: Select all

once cleardebug;
[ProcessMouseEvents=true]
[IntrabarOrderGeneration = false]

inputs: UESetup_DaysToPersist(2); {UESetup_DaysToPersist - After the UE setup is true,
the UE may not hit the stop price and enter right away.
This defines the number of bars we will wait for the
stop price to be hit before resetting the UE setup conditions.
If set to 2, the UE setup will reset on the 3rd bar.}


variables: Close_GT_Yes_High( false ), // Today's close is Greater Than (GT) yesterday's high
Close_GT_Open( false ), // Today's close is GT today's open
Low_GT_Yes_Low( false ), // Today's low is GT yesterday's low
BuyTrigger(false), // Are all UE conditions true?
TriggerBarHigh(0), // High price of the 'triger' bar
TriggerBuyPrice(0), // $0.05 above trigger bar high
UESetup_Days_Count(0), // Count # of bars since the UE setup occurred
BuyTriggerBar(0), // Bar when UE setup conditions were triggered
BarsSinceTrigger(0); // Counter from the bar when a new entry is triggered


BarsSinceTrigger = currentbar - BuyTriggerBar;
if BarsSinceTrigger < 0 then
BarsSinceTrigger = 0;


{Debugging Code}
If CheckCommentary and barstatus=2 Then Begin
Commentary ( "<font size=", doublequote, "-3", doublequote, ">") ;
Commentary("Section 01: After Initial varible assignment / inspection", newline);
Commentary("Current Bar Number: ", Currentbar:4:0, newline);
Commentary("Market Position: ", MarketPosition:1:0, newline);
Commentary("BuyTrigger: ", BuyTrigger, " ", newline);
Commentary("BuyTriggerBar: ", BuyTriggerBar:1:0, " ", newline);
Commentary("TriggerBarHigh: ", TriggerBarHigh , " ", newline);
Commentary("TriggerBuyPrice: ", TriggerBuyPrice , " " , newline);
Commentary("UESetup_DaysToPersist: ", UESetup_DaysToPersist:1:0, " " , newline);
Commentary("Bars Since Trigger: ", (currentbar - BuyTriggerBar):1:0, " " , newline);
Commentary("Section 01: End", newline, newline);
Commentary("</font>");
End;
{End Debugging Code}


If MarketPosition <> 0 then
{Already in a position, so re-set variables; no need to evaluate UE entry conditions}
begin
BuyTrigger= false ;
BuyTriggerBar = 0;
BarsSinceTrigger = 0;
TriggerBarHigh = 0;
TriggerBuyPrice = 0;
{Debugging Code}
If CheckCommentary and barstatus=2 Then Begin
Commentary ( "<font size=", doublequote, "-3", doublequote, ">") ;
Commentary("Section 02: Already in a position", newline);
Commentary("Current Bar Number: ", Currentbar:4:0, newline);
Commentary("Market Position: ", MarketPosition:1:0, newline);
Commentary("BuyTrigger: ", BuyTrigger, " ", newline);
Commentary("BuyTriggerBar: ", BuyTriggerBar:1:0, " ", newline);
Commentary("TriggerBarHigh: ", TriggerBarHigh , " ", newline);
Commentary("TriggerBuyPrice: ", TriggerBuyPrice , " " , newline);
Commentary("Bars Since Trigger: ", (currentbar - BuyTriggerBar):1:0, " " , newline);
Commentary("Section 02: End", newline, newline);
Commentary("</font>");
End;
{End Debugging Code}
end;

If MarketPosition = 0 then
{NOT already in a position}
Begin
{Debugging Code}
If CheckCommentary and barstatus=2 Then Begin
Commentary ( "<font size=", doublequote, "-3", doublequote, ">") ;
Commentary("Section 03: NOT Already in a position", newline);
Commentary("Current Bar Number: ", Currentbar:4:0, newline);
Commentary("Market Position: ", MarketPosition:1:0, newline);
Commentary("BuyTrigger: ", BuyTrigger, " ", newline);
Commentary("BuyTriggerBar: ", BuyTriggerBar:1:0, " ", newline);
Commentary("TriggerBarHigh: ", TriggerBarHigh , " ", newline);
Commentary("TriggerBuyPrice: ", TriggerBuyPrice , " " , newline);
Commentary("Bars Since Trigger: ", (currentbar - BuyTriggerBar):1:0, " " , newline);
Commentary("Section 03: End", newline, newline);
Commentary("</font>");
End;
{End Debugging Code}


if BuyTrigger then
{A buy was triggered on a prior bar}
begin
{Debugging Code}
If CheckCommentary and barstatus=2 Then
Commentary("Section 04: Buy Trigger is true", newline, newline);
{End Debugging Code}

if BarsSinceTrigger > UESetup_DaysToPersist then
{NOT in a position and BuyTrigger is true; if the BuyTrigger has been true longer than
the bars set in UESetup_DaysToPersist, then reset the BuyTrigger}
begin
BuyTrigger = false ;
BuyTriggerBar = 0;
TriggerBarHigh = 0;
TriggerBuyPrice = 0;
{Debugging Code}
If CheckCommentary and barstatus=2 Then Begin
Commentary("Section 05: Bars since BuyTrigger > (currentbar - BuyTriggerBar),
so need to reset the trigger", newline);
Commentary("BuyTrigger: ", BuyTrigger, " ", newline);
Commentary("BuyTriggerBar: ", BuyTriggerBar:1:0, " ", newline);
Commentary("TriggerBarHigh: ", TriggerBarHigh , " ", newline);
Commentary("TriggerBuyPrice: ", TriggerBuyPrice , " " , newline);
Commentary("BarsSinceTrigger: ", BarsSinceTrigger:1:0, " " , newline, newline);
End;
{End Debugging Code}

end;
end
else
begin
{Debugging Code}
If CheckCommentary and barstatus=2 Then
Commentary( "Section 06a: BuyTrigger is false and NOT already in a position; evaluate setup conditions
and create stop entry order", newline, newline);
{End Debugging Code}

{BuyTrigger is false and NOT already in a position; evaluate setup conditions
and create stop entry order}

{Evaluate UE setup conditions for current bar}
Close_GT_Yes_High = Close > high[1];
Close_GT_Open = Close > Open;
Low_GT_Yes_Low = Low > Low[1];

{Debugging Code}
If CheckCommentary and barstatus=2 Then Begin
Commentary("Section 06b: Evalute a new buy trigger", newline);
Commentary("Close_GT_Yes_High: ", Close_GT_Yes_High, newline);
Commentary("Close_GT_Open: ", Close_GT_Open, newline);
Commentary("Low_GT_Yes_Low: ", Low_GT_Yes_Low, newline);
Commentary("BuyTrigger: ", BuyTrigger, " ", newline);
Commentary("BuyTriggerBar: ", BuyTriggerBar:1:0, " ", newline, newline);
End;
{End Debugging Code}


if Close_GT_Yes_High and
Close_GT_Open and
Low_GT_Yes_Low then
begin
BuyTrigger = true;
BuyTriggerBar = Currentbar;
BarsSinceTrigger = 0;
TriggerBarHigh = high;
TriggerBuyPrice = TriggerBarHigh + 0.05;
Buy ( "DAW_UE" ) next bar at TriggerBuyPrice stop ;
{Debugging Code}
If CheckCommentary and barstatus=2 Then Begin
Commentary ( "<font size=", doublequote, "-3", doublequote, ">") ;
Commentary("Section 07: Just triggered a new position", newline);
Commentary("Current Bar Number: ", Currentbar:4:0, newline);
Commentary("Market Position: ", MarketPosition:1:0, newline);
Commentary("Close_GT_Yes_High: ", Close_GT_Yes_High, newline);
Commentary("Close_GT_Open: ", Close_GT_Open, newline);
Commentary("Low_GT_Yes_Low: ", Low_GT_Yes_Low, newline);
Commentary("BuyTrigger: ", BuyTrigger, " ", newline);
Commentary("BuyTriggerBar: ", BuyTriggerBar:1:0, " ", newline);
Commentary("TriggerBarHigh: ", TriggerBarHigh , " ", newline);
Commentary("TriggerBuyPrice: ", TriggerBuyPrice , " " , newline);
Commentary("Bars Since Trigger: ", (currentbar - BuyTriggerBar):1:0, " " , newline);
Commentary("Section 07: End", newline, newline);
Commentary("</font>");
End;
{End Debugging Code}


end;
end;
end;


Return to “MultiCharts”