I came across a strange behavior in a strategy we are developing that has stumped us. I am hoping that someone else may have come across this or MC staff can give some guidance.

The strategy takes a calculated position from our trade logic, and then in IOG strategy automation (SA) mode (with multiple signals in the same direction allowed), we buy, sell, short, buy to cover depending the relationship between our target position we want, and the current chart position.

The code is shown below. Usually, for most of the trading day and most symbols running in different charts, no problems occur. It all trades as expected. But then once in a while (like once a day-- randomly), no trade is executed.

I give an example of the debugging results we have when inserting print statements.

Before the if-then statements to decide what buy/sell commands to run, the variables are like this:

signal_Pos = -386 (shares of EWZ ETF) -- this is a delta of the position, or the number of shares that need to be bought or soldt_marketPos = -1

1) As we enter the loop, if signal_Pos < 0 should evaluate to true2) if t_marketPos >= 0 should evaluate to false3) Sell Short ("SE-more") should execute

However, when we come out of this if-then statement, the chart position is still = -1. And it appears that Sell Short ("SE-more") signal never gets run.

signal_Pos = -386 (shares of EWZ ETF)t_marketPos = -1

This doesn't happen for hundreds of trades on 1 minute charts for a long time. Even the same target position and current position values.

Then for some reason, a trade like this doesn't trigger in the chart. New ticks come in, but the conditional evaluation produces the same result. No change in the chart position, and seemingly no buy/sell commands executed on the chart.

It seems that the if-then conditions are not even evaluated correctly because the print statements in same begin-end segment of the buy/sell commands don't print out.

We're running MC version 8.0, the most recent release candidate.

As we go into the loop again and again, for sometimes about 1/2 hour of trades, the chart position never changes even with new signals. Then with some new trade signal (randomly), this starts working again.

I think the point is as follows, at least from my humble opinion. MC's method of handling orders prevents a study from executing the same buy long or sell short order, presumably so it can track the order internally. In order to execute a second buy long order (as a result of a different buy rule being triggered) after initiating a fresh buy long order, one has to execute another command on a separate line in the study. Although that's not logical from a pure trader's point of view, that's fine and I've learned to live with it, hence a long list of orders in my studies, each being executed once off. Now, if for some reason the study decides both buy rules are triggered at the same time then you can't execute the two buy orders. The alternative is to combine them into one but that requires extra complication, especially for exits. So, it would make programming for the trader much easier to allow those multiple buy orders on the same tick. Then the study can keep track of the different orders based on different trading rules very easily using the labels attached to each unique order. After all, the labels do serve a useful purpose, so for completeness it would be nice to allow multiple buy (or sell) orders on the same tick.

If the restriction of using the same order command was not present, we would not have brought this up in the first place. What we would then do is re-use the same entry command in the event of multiple orders. The we would also exit by re-using the same exit command. The trader can then keep track of the orders by other simple means. Alas, this is not the case with MC.

Could it be that multiple orders of the same signal name (even though it can be selected as a strategy properties) cannot run on the same bar-- even for IOG SA mode automation?

Does that affect my code when the same signal name in the code tries to go from -1 share to -387 shares?

Triage wrote:Could it be that multiple orders of the same signal name (even though it can be selected as a strategy properties) cannot run on the same bar-- even for IOG SA mode automation?

Does that affect my code when the same signal name in the code tries to go from -1 share to -387 shares?

It's indeed not possible to send multiple orders on the same tick. However, it's possible to submit multiple orders per bar, which you can specify in your settings. If I remember correctly: right-click chart -> Format Signals -> select signal -> Format -> and the last tab of that window will have an option to submit multiple trades per bar.

Edit:Tip: by default the signal is set to 'one entry and one exit per bar', so you might want to check this setting before making significant code changes.

I have already set the properties to allow unlimited entries and exits per bar.

But strangely, even if the order is on the next tick, sometimes the order does not trigger in SA mode auto trading.

I reviewed the code again, and there should only be one trade per tick. The conditionals I have in the posted code should only be valid for 1 tick if the buy/sell command gets executed on that tick.

Even if it chart position doesn't change for several ticks, once it does, the market position and my strategy trade logic position would be the same. And it would generate a position delta of 0. In that case, none of the buy/sell conditionals would be evaluated again.

Thanks for any guidance on this.

JoshM wrote:

Triage wrote:Could it be that multiple orders of the same signal name (even though it can be selected as a strategy properties) cannot run on the same bar-- even for IOG SA mode automation?

Does that affect my code when the same signal name in the code tries to go from -1 share to -387 shares?

It's indeed not possible to send multiple orders on the same tick. However, it's possible to submit multiple orders per bar, which you can specify in your settings. If I remember correctly: right-click chart -> Format Signals -> select signal -> Format -> and the last tab of that window will have an option to submit multiple trades per bar.

Also, I wanted to mention that I tried several combinations of Position Limits properties.

What should I expect if I don't check the allow up to [x number] of entry orders in the same direction?

What should I expect if I check it, and select the radio item: when the order is generated by a different entry order?

And finally regardless of the entry that generated the order.

-----

1) By not checking "allow", I assumed that there are no position limits. All orders can be executed. There are no restrictions.

2) By checking "allow" and picking when the order is generated by DIFFERENT entry order, that only the buy/sell signal names with different names (such as "LE-new" or "LE-more") are different than the other orders.

3) By checking "allow" and picking "regardless" then it is similar to #1. The signals with the same name can send orders multiple times.

-----

However, when I select #3, regardless... orders keep sending on the chart until I get SA mode offline. Otherwise, it continues to send orders out.

If I understand this code correctly, the 'myTradeLogic' (stored in the signalPos variable) can return a negative value, but only if t_MarketPos is more then zero (since you don't use the abs_value in that segment). Are you sure that is the correct way to return the myTradeLogic? I'm asking because you also send orders for that 'signalPos' value, which according to your code might be a negative amount -- and orders can't be submitted for negative amounts.

Also note that you have conditions that can be triggered twice in your code:

I just copied the code from PowerLanguage into the code block here. It seems to have gotten rid of some extra lines.

Sorry for the confusion. What I mean by loop through the code is that it runs through one iteration per tick.

The code works fine in 99 out of 100 trades, so I don't think the code is wrong in the general sense of what it should do. Maybe I am missing something obvious.

I don't believe

if t_marketPos <= 0 then begin

and

if t_marketPos >= 0 then begin

will get evaluated twice.

That is because the conditionals: if signal_Pos > 0 then begin and if signal_Pos < 0 then begin are evaluated first. Only one of them can run each tick.

Because of the if-then conditionals, a negative number is never submitted to the buy/sell commands.

I'll add some comments to the code in another reply, but it is quite a simple code sample.

signal_Pos is the net change from the strategy's trade position and the current chart position.

signal_Pos = strategy's trade position - chart's current position.

So, if we are short already -200 shares in the chart, and we get a signal_Pos net change of 500 shares, then we want an order to buy 300 shares. The result is the chart will have a position of 300 shares.

If we are short already -200 shares in the chart, and we get a signal_Pos net change of 100 shares, then we want to buy to cover 100 shares. The result is the chart will have a position of -100 shares.

If we are short already -200 shares in the chart, and we get a signal_Pos net change of -100 shares, then we want to sell short 100 shares. The result is the chart will have a position of -300 shares.

This type of code is very rudimentary and is important as a way to handle pyramiding cases with a single signal name.

JoshM wrote:

Triage wrote:1) As we enter the loop, if signal_Pos < 0 should evaluate to true

I suspect here that you mean the branching in your code example, since I can't see a loop in it. I'm mentioning it here since it's certainly not advised to submit orders in a loop.

Don't take this personally, but your code is a mess. I tried rewriting it, but got stuck quite soon.

If I understand this code correctly, the 'myTradeLogic' (stored in the signalPos variable) can return a negative value, but only if t_MarketPos is more then zero (since you don't use the abs_value in that segment). Are you sure that is the correct way to return the myTradeLogic? I'm asking because you also send orders for that 'signalPos' value, which according to your code might be a negative amount -- and orders can't be submitted for negative amounts.

Also note that you have conditions that can be triggered twice in your code:

Please can you post compelete code with some pseudo code replacing your "MyTradeLogic"( I totally respect your view of not giving out your "Trade logic" ) so that it becomes easy for others to chip in and also it could become useful educational exercise.

JoshM is an exceptional person always eager to help out and there is a lot to learn from his coding skills.

if signal_Pos <> 0 then begin // Make sure there is a change is position

if signal_Pos > 0 then begin // If positive change in position if t_marketPos <= 0 then begin if absValue (t_marketPos) >= signal_Pos then begin // if current absolute value of chart position is greater than equal to change in position, then treat it as closing part of an existing short position Buy to Cover ("SX-cover") signal_Pos Contracts Next Bar at Market; end else begin // if current absolute value of chart position is less than the change in position, then treat it as a new buy signal Buy ("LE-new") (signal_Pos - absValue (t_marketPos)) Contracts Next Bar at Market; // end; end else begin // if current chart position is greater than 0 then buy more shares from existing long chart position Buy ("LE-more") signal_Pos Contracts Next Bar at Market; end; end;

if signal_Pos < 0 then begin // If negative change in position if t_marketPos >= 0 then begin if t_marketPos >= absValue (signal_Pos) then begin // if current chart position is greater than or equal to the absolute change in position, then treat it as closing part of an existing long position Sell ("LX-cover") absValue (signal_Pos) Contracts Next Bar at Market; end else begin // if current chart position is greater than the absolute change in position, then treat it as a new short signal Sell Short ("SE-new") (absValue(signal_Pos) - t_marketPos) Contracts Next Bar at Market; end; end else begin // if current chart position is less than 0 then sell more shares from existing short chart position Sell Short ("SE-more") absValue (signal_Pos) Contracts Next Bar at Market; end; end; end;

Please can you post compelete code with some pseudo code replacing your "MyTradeLogic"( I totally respect your view of not giving out your "Trade logic" ) so that it becomes easy for others to chip in and also it could become useful educational exercise.

JoshM is an exceptional person always eager to help out and there is a lot to learn from his coding skills.

The trade logic just delivers a net position change in the trading strategy.

For example, if the strategy should be long 500 shares and the chart position is currently short 200 shares, then the MyTradeLogic code will return 700. Buying 700 shares will get us long 500 shares.

If the strategy should be short -200 shares and the chart position is currently long 800 shares, then the MyTradeLogic code will return -1,000 shares. Selling 1000 shares will get us short -200 shares.

MyTradeLogic function receives trade signals from multiple charts and sources, and tallies them up to give me a long or short position for the strategy. Then I output the change in position that is required for the chart to buy or sell the correct amount of shares.

The most important thing here is just the delta change in position that is then evaluated in the if-then conditional code I posted. For some odd reason, one maybe out of 100 trades just does not trigger when SA mode auto trading is turned on.

It just happens when running strategy automation execution in SA mode.

Two types of wrong trades:

1) Thus far, the only constant I can tell is when there are multiple trades in the same direction it can just skip a trade and then not get back in sync until a few trades later. At least I'm guessing it is related to multiple trades in the same direction.

2) Then some other times, it creates multiple trades in rapid succession within 1 or 2 seconds. But the end result is always correct. Just that multiple orders are executed on different ticks.

For case #2, I can show a log, attached to this post (multiple trades.png).

multiple trades.png (8.56 KiB) Viewed 201 times

1) We go into this trade with a chart position of long 386 shares.

2) The target change in position is to be short -386 shares.

3) So, we should be selling 772 total shares.

Thus, we should be running sell short ("SE-New) 386 contracts next bar at market;

actual system position target = -386 (what position we want to get in the chart)signal_Pos = -772 (this is the net change in the current chart position to get to our target)t_marketPos = 386 (this is currently what is shown in the chart)

And oddly, it does arrive at the correct chart market position -386. But with many more trades executed. Which this #2 type of wrong trade at least makes the trades. #1 type doesn't generate a trade at all.

The mytradelogic to generate the trade position doesn't cause the issue. That I am sure about.

So I am trying to find out is related specifically to the code I posted (where I missed something obvious), or possibly some configuration or behavior of the order management that doesn't allow be to do this.

But what really is strange is that it just doesn't occur for days, and then it will happen. I purposefully ran this on one-minute bar charts to have a large sample size of occurrences.

SUPER wrote:Triage,

Thanks for your detailed explanation, the random behaviour you have described does it happen to specific trades or part of your code.

Triage wrote:I don't believe if t_marketPos <= 0 then begin and if t_marketPos >= 0 then beginwill get evaluated twice.

That is because the conditionals: if signal_Pos > 0 then begin and if signal_Pos < 0 then begin are evaluated first. Only one of them can run each tick.

That's true, I overlooked that. Woops.

Triage wrote:MyTradeLogic function receives trade signals from multiple charts and sources, and tallies them up to give me a long or short position for the strategy. Then I output the change in position that is required for the chart to buy or sell the correct amount of shares.

The most important thing here is just the delta change in position that is then evaluated in the if-then conditional code I posted. For some odd reason, one maybe out of 100 trades just does not trigger when SA mode auto trading is turned on.

Triage wrote:So far it doesn't appear in replay mode or historically.

It just happens when running strategy automation execution in SA mode.

How have you implemented the MyTradeLogic? These quotes suggests to me, at first glance, that there might be something wrong with the trade signals from multiple charts and data sources that get 'collected' in MyTradeLogic -- would that be possible? Since this sending data from other charts can't be backtested, it might explain why your strategy performs well on historical data, but sometimes breaks down in real-time trading.

Looking at your print screen from the log, I doubt that the code example from the first post can be causing this, since in that log excerpt you have five trades that all happen on the same time. Looking at your code example, I can't see how this code is triggered five times in a row within one second.

But, apparently it's certainly not caused by MyTradeLogic, since:

Triage wrote:The mytradelogic to generate the trade position doesn't cause the issue. That I am sure about.

So I am trying to find out is related specifically to the code I posted (where I missed something obvious), or possibly some configuration or behavior of the order management that doesn't allow be to do this.

- I don't think it's related to your posted code example,- I'm not aware of some configuration that can cause this described behaviour:

Triage wrote:2) Then some other times, it creates multiple trades in rapid succession within 1 or 2 seconds. But the end result is always correct. Just that multiple orders are executed on different ticks.

- And the order management should (in theory ) could deal with this (and otherwise it would break-down more often than once per x-amount of trades).

You could try reviewing your auto-trading settings, perhaps with Optimize order flow, though I don't see how that would help in this problem since that setting is for OCO (but always worth a try off-course).

So not that much what I can to help I'm afraid.

Edit: Triage, let us know if this problem still persists in the future; there might be other things we can test.

How have you implemented the MyTradeLogic? These quotes suggests to me, at first glance, that there might be something wrong with the trade signals from multiple charts and data sources that get 'collected' in MyTradeLogic -- would that be possible? Since this sending data from other charts can't be backtested, it might explain why your strategy performs well on historical data, but sometimes breaks down in real-time trading.

Looking at your print screen from the log, I doubt that the code example from the first post can be causing this, since in that log excerpt you have five trades that all happen on the same time. Looking at your code example, I can't see how this code is triggered five times in a row within one second.

Thanks Josh.

MyTradeLogic has run fine in other configurations, such as replay mode (with historical data). No errors testing a lot of data. I put debug print statements prior and after the code posted.

MyTradeLogic returns the right value correctly in all cases, including each time this error occurs in SA mode auto trading. But coming out of the code segment, the chart position either doesn't change or just makes multiple trades. But the rare occurrence of it makes me suspect it is related to something outside the PowerLanguage code.

Like you suspected, I also did most of my debugging focus on MyTradeLogic as a first reaction. But I concluded that MyTradeLogic does return the intended values. I even swapped out the "collecting" code with something simple as close > high [1] then add shares, close < low [1] then subtract shares.

And regardless of the values generated, once passed to signal_pos variable, the posted order entry code should just carry out the buy/sell actions. Then when currentcontracts and marketposition are updated before the next tick runs the code again, it should equal the strategy position. Thus, making signal_pos = 0. And no new trade occurs.

And for sure, in the debug print-outs all throughout those few seconds, the MyTradeLogic values in signal_pos are correct.

And EWZ as a symbol is very slow to add new ticks. So, it is strange that within 2 seconds, a bunch of orders from this code can run. Five trades.

------

Then I thought this was related to a configuration setting such as position limits, which did restrict trades in the same direction. But it shouldn't cause error #2, which is multiple trades.

------

Thanks again for looking at the posted code. It is really odd behavior. I'll post a conclusion if I find out why this is happening so if someone else encounters this can save time and effort.