OnBrokerPositionChange() seems to be lagging

Questions about MultiCharts .NET and user contributed studies.
User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

OnBrokerPositionChange() seems to be lagging

Postby JoshM » 25 Sep 2014

Using MultiCharts .NET64 Version 8.8 Release (Build 8365) (but probably also for 9.0 beta, since I could not find a change log entry):

Code: Select all

protected override void OnBrokerPositionChange()
{
Output.WriteLine("MarketPos signal: {0}, MarketPos broker: {1}\r\n" +
"EntryPrice signal: {2}, EntryPrice broker: {3}",
StrategyInfo.MarketPosition,
StrategyInfo.MarketPositionAtBrokerForTheStrategy,
StrategyInfo.AvgEntryPrice,
StrategyInfo.AvgEntryPriceAtBrokerForTheStrategy);
}
Generates the following output:

Code: Select all

MarketPos signal: 0, MarketPos broker: 0
EntryPrice signal: 0, EntryPrice broker: 0
MarketPos signal: 1, MarketPos broker: 1
EntryPrice signal: 1,2722, EntryPrice broker: 1,2722
The first two lines were generated when a position was entered, the last two lines when the position was closed.

I had expected here a different output: when a position is entered, the four values are non-zero. When the position is closed, all return zero. In other words, the OnBrokerPositionChange() method is triggered correctly on the change of broker position, but the four properties referenced are not updated quickly enough.

Attached are the auto-trade settings.

How can I return current position data in the OnBrokerPositionChange() method?
Attachments
scr.25-09-2014 13.28.23.png
(21.13 KiB) Downloaded 808 times

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

Re: OnBrokerPositionChange() seems to be lagging

Postby Henry MultiСharts » 26 Sep 2014

Hello JoshM,

The events are happening asynchronously, i.e. once MultiCharts has information regarding position change at broker it calls OnBrokerPositionChange event, while internal variables are not yet updated with this info. If you add a timeout before output (for example 100 ms (System.Threading.Thread.Sleep(100);)) - variables will be already updated. You can try playing around with the timeout value - it can be less for powerful PCs.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: OnBrokerPositionChange() seems to be lagging

Postby JoshM » 06 Oct 2014

The events are happening asynchronously, i.e. once MultiCharts has information regarding position change at broker it calls OnBrokerPositionChange event, while internal variables are not yet updated with this info. If you add a timeout before output (for example 100 ms (System.Threading.Thread.Sleep(100);)) - variables will be already updated. You can try playing around with the timeout value - it can be less for powerful PCs.
Hmm, is there a workaround for this?

Even with a basic test strategy, see below, that trades one LMAX instrument (30 seconds chart) I have to set the `Thread.Sleep()` to 4000 (4 seconds!) before the output is correct. This is with one trading strategy active on a one-year old pc, so I would be surprised if the pc can't keep up.

(Edit: Even with the sleep set to 4 seconds, the OnBrokerPositionChange() info is sometimes still lagging, so it might even need to be set higher than 4).

Edit 2: This is the output with a five second Sleep:

Code: Select all

----------------------------
20:22:32.941 - Submit buy order
20:22:38.112 - MP signal: 0, MP broker: 0
Entry price signal: 0, entry price broker: 0
----------------------------
20:23:32.796 - Submit sell (exit long) order
20:23:37.964 - MP signal: 1, MP broker: 1
Entry price signal: 1,26094, entry price broker: 1,26094
----------------------------
The time stamp show that the strategy properties are still not updated after five seconds:

Five seconds after the buy order, there was still no position according to PowerLanguage .NET. But the filled time stamp in the OPT for this trade is 20:22:33 (4 seconds before the output).

Five seconds after the sell order, there was still an open position according to PowerLanguage .NET. But the time stamp (from the OPT) says this order was completed at 20:23:33 (almost four seconds before the output).

Code: Select all

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using ATCenterProxy.interop;

namespace PowerLanguage.Strategy
{
public class Practice_AlertBrokerPositionChange : SignalObject
{
private IOrderMarket buyOrder, sellOrder;

public Practice_AlertBrokerPositionChange(object _ctx) : base(_ctx) { }

private double avgEntryPriceBroker, marketPosBroker;

protected override void Create()
{
buyOrder = OrderCreator.MarketNextBar(new SOrderParameters(
Contracts.Default, EOrderAction.Buy));

sellOrder = OrderCreator.MarketNextBar(new SOrderParameters(
Contracts.Default, EOrderAction.Sell));
}

protected override void CalcBar()
{
if (!Environment.IsAutoTradingMode || !Bars.LastBarOnChart)
{
Output.Clear();
return;
}

if ((StrategyInfo.MarketPosition > 0) && (Bars.CurrentBar % 4 == 0))
{
sellOrder.Send();
Output.WriteLine("{0} - Submit sell (exit long) order",
DateTime.Now.ToString("HH:mm:ss.fff"));

}
else if ((StrategyInfo.MarketPosition == 0) && (Bars.CurrentBar % 2 == 0))
{
buyOrder.Send();
Output.WriteLine("{0} - Submit buy order",
DateTime.Now.ToString("HH:mm:ss.fff"));
}
}

protected override void OnBrokerPositionChange()
{
System.Threading.Thread.Sleep(4000); // 3000 does not work

Output.WriteLine("{0} - MP signal: {1}, MP broker: {2}\r\n" +
"Entry price signal: {3}, entry price broker: {4}",
DateTime.Now.ToString("HH:mm:ss.fff"),
StrategyInfo.MarketPosition,
StrategyInfo.MarketPositionAtBrokerForTheStrategy,
StrategyInfo.AvgEntryPrice,
StrategyInfo.AvgEntryPriceAtBrokerForTheStrategy);

Output.WriteLine("----------------------------");
}
}
}

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

Re: OnBrokerPositionChange() seems to be lagging

Postby Henry MultiСharts » 07 Oct 2014

JoshM, as we have already mentioned - we do not have such delay on our end. OnBrokerPositionChange is updated almost instantaneously (max 100 ms). Please check the latency and processing speed on your end.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: OnBrokerPositionChange() seems to be lagging

Postby JoshM » 07 Oct 2014

JoshM, as we have already mentioned - we do not have such delay on our end. OnBrokerPositionChange is updated almost instantaneously (max 100 ms).
No need to sound annoyed Henry, especially since you did not say that:
If you add a timeout before output (for example 100 ms (System.Threading.Thread.Sleep(100);)) - variables will be already updated.
"For example" has a very different meaning than "we verified it on our end and it's absolutely not more than 100ms".
Please check the latency and processing speed on your end.
At which metrics do I need to look? The order generation and filled time are the same (i.e., less than 1 second), and the CPU usage of MultiCharts64.exe is around 2-3% with the CPU usage of the whole pc less than 10 percent. As I mentioned, there are no other trading strategies running so there is no "queue" of orders/positions to update.

Perhaps you can look at my posted code to see if there's an error there? In the meantime I'll update to the newest version released yesterday to see if that helps.

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

Re: OnBrokerPositionChange() seems to be lagging

Postby Henry MultiСharts » 08 Oct 2014

JoshM, sorry, I did not mean to sound annoyed or offensive :-)

Please go to Control Panel\System and Security\System or right click on the Computer icon and select properties. Create a screenshot of this window and send it to me.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: OnBrokerPositionChange() seems to be lagging

Postby JoshM » 09 Oct 2014

JoshM, sorry, I did not mean to sound annoyed or offensive :-)
No problem, I shouldn't always take things so seriously. :)
Please go to Control Panel\System and Security\System or right click on the Computer icon and select properties. Create a screenshot of this window and send it to me.
Send you an e-mail because it also happens with MultiCharts .NET64 Version 9.0 Release (Build 10016).

User avatar
PatrickSocal
Posts: 58
Joined: 27 Apr 2013
Location: San Diego, CA
Has thanked: 23 times
Been thanked: 30 times

Re: OnBrokerPositionChange() seems to be lagging

Postby PatrickSocal » 19 Oct 2014

Hi Josh,

Did you make any progress with this issue?

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: OnBrokerPositionChange() seems to be lagging

Postby JoshM » 21 Oct 2014

Not yet. It's not pc hardware related according to MC Support, but we haven't found yet what causes it.

If I disable the antivirus, the behaviour seems to improve slightly, but with a three second pause still less than half of the OnBrokerPositionChange() updates are off (which is much too many for such a long delay, in my opinion).

I'll update the thread if we learn more.


Return to “MultiCharts .NET”