How I close stop pending order by program code?

Questions about MultiCharts .NET and user contributed studies.
User avatar
svopex
Posts: 74
Joined: 16 Oct 2013
Has thanked: 7 times
Been thanked: 5 times
Contact:

How I close stop pending order by program code?

Postby svopex » 05 Dec 2013

I have this code (simplified):

protected override void Create()
{

order = OrderCreator.Stop(new SOrderParameters(Contracts.Default, EOrderAction.Buy));

}

if (StrategyInfo.MarketPosition == 0)
{
order.Send(LongBreakout);
}

How I by program code close pending Stop order?

Thank Petr

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

Re: How I close stop pending order by program code?

Postby JoshM » 05 Dec 2013

How I by program code close pending Stop order?
Stop orders are cancelled when you don't resubmit them. Also see this thread.

User avatar
svopex
Posts: 74
Joined: 16 Oct 2013
Has thanked: 7 times
Been thanked: 5 times
Contact:

Re: How I close stop pending order by program code?

Postby svopex » 06 Dec 2013

Ok.

But use this session (9:45-16:15):
session.png
My session settings.
(5.2 KiB) Downloaded 900 times
And this code (simplification):

protected override void Create()
{

order = OrderCreator.Stop(new SOrderParameters(Contracts.Default, EOrderAction.Buy));

}

if (StrategyInfo.MarketPosition == 0)
{
order.Send(LongBreakout);
}


i have next problem (market position is 0).

Stop order is active after 16:15 still, and I don’t know why.

By my opinion should be stop order active from xxx to 16:14:59.999, here should be closed and reopen at 9:45 of the next day
(next candle after 16:00 by session is on the next day in 9:45).

But stop order is not canceled in 16:15.
Stop order cancel when exchange closed (at 18:00).
And in 9:45 order will be go to pending state, it's ok.

Petr

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

Re: How I close stop pending order by program code?

Postby JoshM » 06 Dec 2013

(...)
By my opinion should be stop order active from xxx to 16:14:59.999, here should be closed and reopen at 9:45 of the next day
(next candle after 16:00 by session is on the next day in 9:45).

But stop order is not canceled in 16:15.
Stop order cancel when exchange closed (at 18:00).
And in 9:45 order will be go to pending state, it's ok.
Then you need to stop resubmitting the stop order prior to 16:14:59.999, with, for example, not resubmitting the stop order in the 10 seconds prior to the session close.

Otherwise, if you send a stop order on the last tick of your session, there is no possibility to cancel the order after that last tick for the session, since the market would have closed by then (according to your session settings) and the CalcBar() method is not called any more.

You can try to use ExecControl.RecalcLastBarAfter to force a recalculation of your script after the 16:15 session close and to cancel the stop loss order prior to the actual close at 18:00, but keep in mind that the price bars won't be updated with fresh prices any more if the session close has been reached, so your strategy might make decisions based on old data in this case.

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

Re: How I close stop pending order by program code?

Postby Henry MultiСharts » 09 Dec 2013

svopex, check this code to learn how to close the position at specified time. You can also adjust this code to cancel your orders at specified time.

User avatar
svopex
Posts: 74
Joined: 16 Oct 2013
Has thanked: 7 times
Been thanked: 5 times
Contact:

Re: How I close stop pending order by program code?

Postby svopex » 11 Dec 2013

JoshM write:

Then you need to stop resubmitting the stop order prior to 16:14:59.999, with, for example, not resubmitting the stop order in the 10 seconds prior to the session close.


I use this code, but don't work...
Does anyone know why?

  • protected override void Create()
    {

    order = OrderCreator.Stop(new SOrderParameters(Contracts.Default, EOrderAction.Buy));

    }

    protected override void CalcBar()
    {
    ...
    const int closeExchangeInterval = 3;
    TimeSpan ts = Bars.Sessions[0].EndTime - Bars.StatusLine.Time.TimeOfDay;
    ...
    if (
    (ts.TotalMinutes > closeExchangeInterval || ts.TotalMinutes < -closeExchangeInterval)
    && StrategyInfo.MarketPosition == 0)
    {
    order.Send(LongBreakout);
    }

    }

User avatar
svopex
Posts: 74
Joined: 16 Oct 2013
Has thanked: 7 times
Been thanked: 5 times
Contact:

Re: How I close stop pending order by program code?

Postby svopex » 24 Jan 2014

This is still a mystery to me.

In the Multicharts.NET - ProgrammingGuide - v1.0 is written:
Calculation upon Close of each bar and filling of the generated orders collection on the next bar.

If I have close session on 16:15 and open in the next day 9:30,
stop order should be filled on the open 16:00 and after it on the open 9:30.
On the 16:15 candle does not render, therefore should be not open candle, but is.

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

Re: How I close stop pending order by program code?

Postby Henry MultiСharts » 27 Jan 2014

Hello svopex,

Have you ensured this condition is vald?
(ts.TotalMinutes > closeExchangeInterval || ts.TotalMinutes < -closeExchangeInterval)&& StrategyInfo.MarketPosition == 0)
Have you tried using a simple condition - if current time is inside the specified "begin trading time" and "end trading time" (like if (time>=930 and time<1600) then begin) interval then execute trading logic?

hairyMug
Posts: 57
Joined: 03 Feb 2014
Has thanked: 5 times
Been thanked: 6 times

Re: How I close stop pending order by program code?

Postby hairyMug » 27 Sep 2014

take a look at the attached code;
It can be used to cancel open orders after a specific time.
It may work for what you need... (v9 RC)

Note: not tested live yet and I'm not sure if it works on "managed" trades...
Attachments
killOrders.pln
(2.03 KiB) Downloaded 398 times


Return to “MultiCharts .NET”