Scaling out of a position  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
MidKnight
Posts: 343
Joined: 12 Aug 2012
Has thanked: 123 times
Been thanked: 56 times

Scaling out of a position

Postby MidKnight » 21 Oct 2013

Hi there,

Have been exploring some planed scale in/out techniques and this scale out is NOT working as I am trying to code it do to. Please see the attached screenshot.

What should be happening is that the new open positions should be treated like a stack and subsequent exits are matched with the most recent entries. In the attached example, it should be exiting the 400k position FIRST - instead though MC is exiting the oldest positions first. Can I force it to exit the oldest position first?

Here is the code:

Code: Select all

private IOrderPriced buyOrder;
private IOrderPriced buyTarget;


protected override void Create()
{
buyOrder = OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "GStat_LE", EOrderAction.Buy));
buyTarget = OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "GStat_LX", EOrderAction.Sell, OrderExit.Total));
}

protected override void CalcBar()
{
if (StrategyInfo.MarketPosition > 0)
{
ManageLongs();
}
}

private void ManageLongs()
{
ITradeOrder lastTradeOrder = CurrentPosition.OpenTrades[CurrentPosition.OpenTrades.Count - 1].EntryOrder;

buyOrder.Send(lastTradeOrder.Price - 0.002, CurrentPosition.OpenLots);

if (CurrentPosition.OpenTrades.Count > 1)
{
buyTarget.Send(lastTradeOrder.Price + 0.002, lastTradeOrder.Contracts);
}
else
{
buyTarget.Send(StrategyInfo.AvgEntryPrice + 0.005, lastTradeOrder.Contracts);
}
}
Attachments
MK_2013-10-21_204719.png
(29.42 KiB) Downloaded 1895 times

User avatar
Dave Masalov
Posts: 1712
Joined: 16 Apr 2010
Has thanked: 51 times
Been thanked: 487 times

Re: Scaling out of a position

Postby Dave Masalov » 22 Oct 2013

Hello MidKnight,

Hedging is not supported in MultiCharts. There is only one position opened by several entries and it will be closed in the same sequence in wich it was opened.

MidKnight
Posts: 343
Joined: 12 Aug 2012
Has thanked: 123 times
Been thanked: 56 times

Re: Scaling out of a position

Postby MidKnight » 22 Oct 2013

G'day Dave,

I wouldn't call what I'm asking about to be "hedging". It is just controlling which exit is matched to the CurrentPosition.OpenTrades entries. You are saying this is beyond my (the programmers) control?

With kind regards,
MK

User avatar
JoshM
Posts: 2099
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1526 times
Been thanked: 1503 times
Contact:

Re: Scaling out of a position

Postby JoshM » 22 Oct 2013

It is just controlling which exit is matched to the CurrentPosition.OpenTrades entries. You are saying this is beyond my (the programmers) control?
It seems so. I don't see an overloaded method for Send() which would allow specifying which entry order should be closed (though of course you can specify the amount of contracts, but then you're back at the behaviour described in your start post).

User avatar
Dave Masalov
Posts: 1712
Joined: 16 Apr 2010
Has thanked: 51 times
Been thanked: 487 times

Re: Scaling out of a position

Postby Dave Masalov » 22 Oct 2013

G'day Dave,

I wouldn't call what I'm asking about to be "hedging". It is just controlling which exit is matched to the CurrentPosition.OpenTrades entries. You are saying this is beyond my (the programmers) control?

With kind regards,
MK
MidKnight,

You can use the ExitFromEntry mechanism. Different order groups should be created.

MidKnight
Posts: 343
Joined: 12 Aug 2012
Has thanked: 123 times
Been thanked: 56 times

Re: Scaling out of a position

Postby MidKnight » 22 Oct 2013

Is there an example of this Dave? I just searched the help and there is no mention of an ExitFromEntry. The closest match would be ExitFromOneEntryOnce enumeration or ExitFromOneEntryOnceAttribute

User avatar
JoshM
Posts: 2099
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1526 times
Been thanked: 1503 times
Contact:

Re: Scaling out of a position  [SOLVED]

Postby JoshM » 22 Oct 2013

You can use the ExitFromEntry mechanism. Different order groups should be created.
That's great, didn't knew that yet.
Is there an example of this Dave? I just searched the help and there is no mention of an ExitFromEntry. The closest match would be ExitFromOneEntryOnce enumeration or ExitFromOneEntryOnceAttribute
I also couldn't find an example, so I tried to make one. Is this what you meant? :

Image

Code: Select all

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

namespace PowerLanguage.Strategy
{
public class Example_ScalingOut : SignalObject
{
private IOrderPriced buyOrder1, buyOrder2, buyOrder3, sellOrder1, sellOrder2, sellOrder3;

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

protected override void Create()
{
buyOrder1 = OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "LE_LMT1", EOrderAction.Buy));
buyOrder2 = OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "LE_LMT2", EOrderAction.Buy));
buyOrder3 = OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "LE_LMT3", EOrderAction.Buy));

sellOrder1 = OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "XL_LMT1", EOrderAction.Sell, OrderExit.FromEntry(buyOrder3)));
sellOrder2 = OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "XL_LMT2", EOrderAction.Sell, OrderExit.FromEntry(buyOrder2)));
sellOrder3 = OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "XL_LMT3", EOrderAction.Sell, OrderExit.FromEntry(buyOrder1)));
}

protected override void CalcBar()
{
if (Bars.CurrentBar == 100)
{
buyOrder1.Send(Bars.Close[0], 1);
}

if (Bars.CurrentBar == 110)
{
buyOrder2.Send(Bars.Close[0], 2);
}

if (Bars.CurrentBar == 120)
{
buyOrder3.Send(Bars.Close[0], 3);
}

if (StrategyInfo.MarketPosition > 0)
{
if (Bars.CurrentBar == 140)
{
sellOrder1.Send(Bars.Close[0], 3);
}

if (Bars.CurrentBar == 150)
{
sellOrder2.Send(Bars.Close[0], 2);
}

if (Bars.CurrentBar == 160)
{
sellOrder3.Send(Bars.Close[0], 1);
}
}
}
}
}
Attachments
scr.23-10-2013 06.45.58.png
(5.34 KiB) Downloaded 2305 times

MidKnight
Posts: 343
Joined: 12 Aug 2012
Has thanked: 123 times
Been thanked: 56 times

Re: Scaling out of a position

Postby MidKnight » 23 Oct 2013

That is exactly what I'm after JoshM - thank you! Really good example.

With thanks again,
MK


Return to “MultiCharts .NET”