Alerts sending 1 bar late

Questions about MultiCharts and user contributed studies.
DaveAronow
Posts: 72
Joined: 20 Apr 2014
Has thanked: 9 times
Been thanked: 8 times

Alerts sending 1 bar late

Postby DaveAronow » 07 Mar 2016

I'm using the following alert signal which works but I noticed tonight is sending the alert one bar after the signal (after the signal changes the MarketPosition of the script).

The alert condition check in the settings is set to Every Tick.

I noticed on some charts it's fine, while on others it fires one bar late. I can't see anything different in the settings, so I'm wondering if it's related to the PrevMP and the IntraBarPersist setting? How do I clear that fully?

Alert Signal:

Code: Select all

Inputs: WorkspaceName("TestWorkspace");
Variables:
IntraBarPersist PrevMP(0),
MP(0), msgText(""), netdiff(0);

MP = MarketPosition(0) * CurrentContracts;
msgText = "MC - Strategy Filled Order: ";
if(MP <> PrevMP) then
begin
netdiff = MP - PrevMP;
if netdiff <> 0 then
Alert(msgText + NumToStr(netdiff,0) + NEWLINE + "Workspace: \" + WorkspaceName);
end;
PrevMP = MP;

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

Re: Alerts sending 1 bar late

Postby TJ » 07 Mar 2016

MultiCharts version
Chart resolution
Screen shot
IOG?

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

Re: Alerts sending 1 bar late

Postby TJ » 07 Mar 2016

::


I'm wondering if it's related to the PrevMP and the IntraBarPersist setting? How do I clear that fully?
Do you understand the purpose of "IntraBarPersist"?

DaveAronow
Posts: 72
Joined: 20 Apr 2014
Has thanked: 9 times
Been thanked: 8 times

Re: Alerts sending 1 bar late

Postby DaveAronow » 08 Mar 2016

::


I'm wondering if it's related to the PrevMP and the IntraBarPersist setting? How do I clear that fully?
Do you understand the purpose of "IntraBarPersist"?
Apparently I thought I did but I didn't. That said now that I've RTFM I'm still not clear why it's not always sending the alert on the bar the entry/exit occurs in all cases.

The MC version is 9.1 r2 build 12010, I see it with 5 minute bars and also 15 minute bars (the two timeframes I generally use). I don't have any screenshots of the issue, but on one 5 minute chart for NYBOT Sugar (SB) there's an exit at 12:55 PM NY time (I have this set to the last bar of the session so I can exit 5 minutes prior to close) and that alert gets sent at 3:35 AM (the first bar of the next session). However last night on a 15 minute chart in the middle of the evening (i.e. not near the close) I saw the same thing with the alert occurring 15 minutes after the strategy position changed.

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

Re: Alerts sending 1 bar late

Postby TJ » 08 Mar 2016

MarketPosition is calculated at the end of the bar (EOB).

DaveAronow
Posts: 72
Joined: 20 Apr 2014
Has thanked: 9 times
Been thanked: 8 times

Re: Alerts sending 1 bar late

Postby DaveAronow » 08 Mar 2016

MarketPosition is calculated at the end of the bar (EOB).
OK so you're saying after the bar ends, it recalcs MarketPosition and then won't fire the alert until the end of the following bar? I would have thought setting the alert conditions check to every tick would take care of this, but apparently I'm wrong (again!!!). How would I get the alerts to go out on bar end?

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

Re: Alerts sending 1 bar late

Postby TJ » 08 Mar 2016

MarketPosition is calculated at the end of the bar (EOB).
OK so you're saying after the bar ends, it recalcs MarketPosition

and then won't fire the alert until the end of the following bar?
::
NO. That's not what I meant.

Your code determines how and when the alert is fired.

Your code asked MultiCharts to fire the alert when these conditions are met:

if(MP <> PrevMP) then

if netdiff <> 0 then

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

Re: Alerts sending 1 bar late

Postby TJ » 08 Mar 2016

::

I would have thought setting the alert conditions check to every tick would take care of this, but apparently I'm wrong (again!!!). How would I get the alerts to go out on bar end?

EasyLanguage is a Procedural Language.

ie. MultiCharts executes your instructions in a step-by-step "procedure", beginning at the top of the script, one line at a time, until it reaches the end of the script. At which time, MultiCharts will go on standby (ie do nothing). When a new tick comes in, MultiCharts will spring back into action and start the process all over again.

DaveAronow
Posts: 72
Joined: 20 Apr 2014
Has thanked: 9 times
Been thanked: 8 times

Re: Alerts sending 1 bar late

Postby DaveAronow » 08 Mar 2016

I added a RecalcLastBarAfter with a last bar on chart check and that definitely fixes my issue. I guess it is not determining the MarketPosition change until after the bar closes, so when that code executes it has not determined the position has changed, so nothing happens until the next bar ends and it now has a change.

I don't really understand why the MarketPosition isn't detected on bar end, but as long as it works this won't keep me up at night...

Thanks for your help.

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: Alerts sending 1 bar late

Postby tony » 09 Mar 2016

Couple reasons why you may have the delay.

1: You should declare all variables, not just the initial as intrabarpersist so these variables can be updated on every tick.

2: At the close of the bar, if a signal is generated it is "filled" on the open of the next bar. But at that time, MP is still 0, all that has happened is you sent a buy or sellshort signal for example and it won't be until that bar closes, that MP is updated thus the delay.

Always best to understand why a fix "fixed" something versus having some issue in your script that could resurface and cause you money in a live trade.

DaveAronow
Posts: 72
Joined: 20 Apr 2014
Has thanked: 9 times
Been thanked: 8 times

Re: Alerts sending 1 bar late

Postby DaveAronow » 10 Mar 2016

tony,

Still having random weirdness. Relying on email is a bit of a weak link anyway so I'm re-architecting this to use one of my accounts as the "base" account which will be traded via MC auto trading and then through the API will capture the fill and replicate it to my other accounts.

I'll revisit this at some point and will post a script if I get it working.

Dave

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

Re: Alerts sending 1 bar late

Postby TJ » 10 Mar 2016

tony,

Still having random weirdness.
::
Dave
You have to prepare a table and manually work out the variable to find the bug.

I know where the problem is, but I will have to write a long essay to explain it; it would be easier if you'd just fill out the table with all the pertinent variables, from Bar #1 onward...
Usually you should be able to detect the bugs by bar #3.

Image
Attachments
PMP.jpg
(18.52 KiB) Downloaded 1055 times

DaveAronow
Posts: 72
Joined: 20 Apr 2014
Has thanked: 9 times
Been thanked: 8 times

Re: Alerts sending 1 bar late

Postby DaveAronow » 10 Mar 2016

Thanks will try later. Not sure if it's the same issue but I found when I re-open the charts it fires the alerts again. Luckily my email client was not running or it would have been bad.


Return to “MultiCharts”