Stop Orders not executing

Questions about MultiCharts .NET and user contributed studies.
dnickless
Posts: 21
Joined: 02 May 2016
Has thanked: 2 times
Been thanked: 2 times

Stop Orders not executing

Postby dnickless » 08 Oct 2018

I am incurring losses over and over again these days because my stop losses simply do not trigger/get sent (anymore?).

I am using the latest version of MC.Net Portfolio Trader (v12.0 R3) to trade across a range of equity symbols. And my observation is that sometimes, stop losses *do* get sent, sometimes they *don't* despite the fact that we are calling

Code: Select all

GenerateStopLoss()
.

So in my trading signal code, there is this:

Code: Select all

double stopLossLevel = StrategyInfo.AvgEntryPrice * Math.Abs(StrategyInfo.MarketPosition) * 0.01;

Output.WriteLine("stop loss @ {0}", stopLossLevel); // output: “stop loss @ 333.5919”
GenerateStopLoss(stopLossLevel);

Output.WriteLine("trailing from {0} profit", stopLossLevel); // output “trailing from 333.5919”
GeneratePercentTrailing(stopLossLevel, 50);
So I am thinking that this should result in my position getting closed once I have a loss of more than 333.5919$ in total dollars. And also, I would expect that once I have 1% profit in the books (equating to the same amount again of 333.5919$) and the price goes down my remaining profit will be cashed in once it drops below 50% of the highest profit.

BUT: I have a position in my books that is -943.81$ so well beyond the point when the stop loss should have flattened it. However, I do not see a stop loss order on the “Orders” tab. Also, when I restart the system, the position stays untouched as well while I would expect it to get closed immediately. If have tried with "Optimize order flow" and without.

I have no clue if this behavior is caused by something similar to viewtopic.php?t=51602 or if I just don't understand how that stop loss should function in theory but it's certainly starting to drive me nuts. And I'm starting to just randomly guess what this could be caused by:
  • I do have a MMS in place - could that thing somehow tamper with the stop loss orders that the signal generates?
  • I always open my positions at the end of a trading day (about a minute before the exchange closes) so I'm wondering if it could be related to that.
  • Or perhaps it's caused by the broker connectivity drops that happen once a night with Interactive Brokers and sometimes even during the day?
  • Or is it perhaps because of an overnight price change where the next trading already starts at a price that is low enough to cause a loss of more than the stop loss amount and then there is no "transition" which could cause the stop loss or something?
  • Or could it perhaps be because I have Max Potential Loss set to "0" (for whatever reason)? This appears to also explain (viewtopic.php?f=1&t=10522&p=51440&hilit ... tal#p51440) why I am getting a value of 0 when I use Portfolio.InvestedCapital btw. which is another strange and surprising thing that keeps bugging me.
  • Or is it all because I do not have a base currency set?
Any help is highly appreciated. It will save my sanity...

User avatar
Svetlana MultiCharts
Posts: 645
Joined: 19 Oct 2017
Has thanked: 3 times
Been thanked: 163 times

Re: Stop Orders not executing

Postby Svetlana MultiCharts » 12 Oct 2018

Hello, Dnickless,

MMS can influence only if you have order sending prohibition in it. If there are no such prohibitions, it shouldn’t affect.

Please note that IOG is not supported in PortfolioTrader, and the orders can be generated on the bar close only.

If you leave the orders for the next day please make sure their TIF = GTC.

Max Potential Loss set to "0" doesn’t influence on GenerateStopLoss.

GenerateStopLoss should work event if Base Currency = none

Please add additional logging right before the GenerateStopLoss command in your script to be able to trace this behaviour:

Code: Select all

if (Environment.IsAutoTradingMode && Bars.LastBarOnChart)
{
Output.WriteLine("sysDT= {0}, Name= {1}, barDT= {2}, BStatus= {3}, MP= {4}, MPBS= {5}, EP= {6}, EPBS= {7}, SL @ {8}",
DateTime.Now.ToString("MM/dd/yy hh:mm:ss.fff"),
Bars.Info.Name,
Bars.BarUpdateTime.ToString("MM/dd/yy hh:mm:ss.fff"),
Bars.Status,
StrategyInfo.MarketPosition,
StrategyInfo.MarketPositionAtBrokerForTheStrategy,
StrategyInfo.AvgEntryPrice,
StrategyInfo.AvgEntryPriceAtBrokerForTheStrategy,
stopLossLevel);
}
GenerateStopLoss(stopLossLevel);
Stop Loss can be generated only when you see BStatus= Close in the Output of PLEditor.

After the trace is added to your script, and when the issue is reproduced, please send us the following files to support@multicharts.com so that we would be able to investigate the issue:

1. The Portfolio workspace where the issue is reproduced.
2. Export of used symbols from QuoteManager in .qmd archive:
http://www.multicharts.com/trading-soft ... rting_Data
3. MultiCharts Logs, collected via Feedback application, when the issue is reproduced: http://www.multicharts.com/trading-soft ... harts_Logs
4. Outputs of PowerLanguage Editor, where we could see, that the script has generated the order.

As soon as we receive the requested files, the engineers will study the case and we will get in touch with you when we have any feedback from them.

dnickless
Posts: 21
Joined: 02 May 2016
Has thanked: 2 times
Been thanked: 2 times

Re: Stop Orders not executing

Postby dnickless » 12 Oct 2018

Thanks for getting back to me. What do you mean by this:
MMS can influence only if you have order sending prohibition in it. If there are no such prohibitions, it shouldn’t affect.
If you're referring to

Code: Select all

IPortfolioStrategy.AllowEntries
then, no, I am not using it. Or are you referring to something else?

Anyway, I will do as you said and you shall receive my logs soon. Thank you in advance!

User avatar
Svetlana MultiCharts
Posts: 645
Joined: 19 Oct 2017
Has thanked: 3 times
Been thanked: 163 times

Re: Stop Orders not executing

Postby Svetlana MultiCharts » 15 Oct 2018

dnickless,

We meant the following:

Code: Select all

IPortfolioStrategy.AllowExits
IPortfolioStrategy.AllowExitsLong
IPortfolioStrategy.AllowExitsShort

dnickless
Posts: 21
Joined: 02 May 2016
Has thanked: 2 times
Been thanked: 2 times

Re: Stop Orders not executing

Postby dnickless » 15 Oct 2018

Ok, obviously. ;) We're not using any of those, either, so that should be ok. Another thing that I didn't fully understand, is where you wrote:
Please note that IOG is not supported in PortfolioTrader, and the orders can be generated on the bar close only.
I am aware of this behaviour when it comes to "normal" trades that we place through e.g.

Code: Select all

buyOrder = OrderCreator.MarketThisBar(new SOrderParameters(Contracts.UserSpecified, EOrderAction.Buy));
// and then in CalcBar()
buyOrder.Send(numberOfShares);
But is that statement also true for GenerateStopLoss() and GeneratePercentTrailing()?

User avatar
Svetlana MultiCharts
Posts: 645
Joined: 19 Oct 2017
Has thanked: 3 times
Been thanked: 163 times

Re: Stop Orders not executing

Postby Svetlana MultiCharts » 17 Oct 2018

dnickless,

If these orders are not under an IF statement – they are placed at the same moment when a position is opened (even if IOG is off).

dnickless
Posts: 21
Joined: 02 May 2016
Has thanked: 2 times
Been thanked: 2 times

Re: Stop Orders not executing

Postby dnickless » 17 Oct 2018

Aha!!! I see... That could potentially be where my confusion is coming from... I am using daily bars but using ReCalcAfter to recalculate the strategy during the day, too, but, in general, my code currently looks like the below:

Code: Select all

if (StrategyInfo.MarketPosition == 0)
{
[...if bla bla...]
buyOrder.send();
}
else if (StrategyInfo.MarketPosition > 0)
{
GenerateStopLoss();
GeneratePercentTrailing();
}
So given the fact that I am using daily bars that would effectively mean that only upon the end of the first day *after* the position was openend the stop losses would get created. If that is it then I should be able to fix this simply by moving the stop loss generation immediately below the buyOrder.send() calls and things should magically start working. That could also explain why I actually did see some stop losses trigger sometimes (I presume that was when the value would go down on the second holding day) and sometimes I didn't (probably when they positions would go down during the first day).

dnickless
Posts: 21
Joined: 02 May 2016
Has thanked: 2 times
Been thanked: 2 times

Re: Stop Orders not executing

Postby dnickless » 17 Oct 2018

...but that would also mean that it wouldn't make sense to calculate the stop loss levels based on the StrategyInfo.AvgEntryPrice and the StrategyInfo.MarketPosition (both which would return zero without an open position). So what's the best approach here? Use the Close price and the assumed number of shares that might get bought? As I stated above, I have been using this *with an open position in place* to set the stop losses:

Code: Select all

double stopLossLevel = StrategyInfo.AvgEntryPrice * Math.Abs(StrategyInfo.MarketPosition) * 0.01;

User avatar
Svetlana MultiCharts
Posts: 645
Joined: 19 Oct 2017
Has thanked: 3 times
Been thanked: 163 times

Re: Stop Orders not executing

Postby Svetlana MultiCharts » 18 Oct 2018

dnickless,

You are right. However, in your case, you can use GenerateStopLossPt method instead of GenerateStopLoss in order to be independent from the market position.

e.g. instead of this:

Code: Select all

if (StrategyInfo.MarketPosition > 0)
{
double stopLossLevel = StrategyInfo.AvgEntryPrice * Math.Abs(StrategyInfo.MarketPosition) * 0.01;
GenerateStopLoss(stopLossLevel);
}
use this:

Code: Select all

double amount = 0.01 /(Bars.Point * Bars.Info.MinMove);
amount *= (StrategyInfo.MarketPosition > 0) ? StrategyInfo.AvgEntryPrice : Bars.Close[0];
GenerateStopLossPt(amount);
It is better to place a slightly approximate stop than not to place it at all.
On the next bar the price shall be corrected.


Return to “MultiCharts .NET”