Hi, let's say you want to set your open position to be a particular number such as long 3 contracts, regardless of what your current position is. To do that I can inspect the current PositionSide and calculate what order to send to close any opposite positions if needed or reduce/increase positions on my target direction and send an order to adjust the open position to what I want. But I was thinking maybe there is an easier way to do this without me having to manually take care of the order optimization. Is there any way to do this to just tell MultiCharts to send whatever order needed to set my current open position to for example +3?
Thanks!
Modifying the Open Position
- JoshM
- Posts: 2195
- Joined: 20 May 2011
- Location: The Netherlands
- Has thanked: 1544 times
- Been thanked: 1565 times
- Contact:
Re: Modifying the Open Position
You can just submit a long order for 3 contracts and MultiCharts .NET will then close any short position and go long.let's say you want to set your open position to be a particular number such as long 3 contracts, regardless of what your current position is.
From the wiki:
EOrderAction:
Buy – long position entry (buying). If the short position is currently opened, then it will be closed and the long one will be opened.
Sell – long position exit (selling). This order cannot be sent, if the long position is not opened. Also, this position cannot be reversed into short.
SellShort – short position entry (selling). If the long position is currently opened, then it will be closed, and the short one will be opened.
BuyToCover – short position exit (buying). This order cannot be sent, if the short position is not opened.
Re: Modifying the Open Position
That works only when the order you're submitting is in the opposite direction of the current position. For example, if you want to set current position to +3, and you just send long entry 3, then if your current position is 1, it will become 4. Not what you want. If the current position is 5, it will become 8, again not what you want. In the first case you want to send 2, in the second case you want to exit 2.
Below is the logic and test I came up with to go from current position to a long/short target position (you can run them in LINQPad 5):
From current position to a Long target position:
From current position to a Short target position:
Below is the logic and test I came up with to go from current position to a long/short target position (you can run them in LINQPad 5):
From current position to a Long target position:
Code: Select all
var targetPosition = 3; // long
for (var i = -5; i <= 5; i++)
{
var currentPosition = i;
var adjust = targetPosition - currentPosition;
if (adjust == 0)
{
$"{currentPosition}\t {targetPosition}\t HOLD".Dump();
}
else if (adjust > 0)
{
if (currentPosition < 0) adjust = targetPosition;
$"{currentPosition}\t {targetPosition}\t LE {adjust}".Dump();
}
else
{
adjust = -adjust;
$"{currentPosition}\t {targetPosition}\t LX {adjust}".Dump();
}
}
// Output:
// -5 3 LE 3
// -4 3 LE 3
// -3 3 LE 3
// -2 3 LE 3
// -1 3 LE 3
// 0 3 LE 3
// 1 3 LE 2
// 2 3 LE 1
// 3 3 HOLD
// 4 3 LX 1
// 5 3 LX 2
Code: Select all
var targetPosition = 3; // short
for (var i = -5; i <= 5; i++)
{
var currentPosition = i;
var adjust = targetPosition + currentPosition;
if (adjust == 0)
{
$"{currentPosition}\t {-targetPosition}\t HOLD".Dump();
}
else if (adjust > 0)
{
if (currentPosition > 0) adjust = targetPosition;
$"{currentPosition}\t {-targetPosition}\t SE {adjust}".Dump();
}
else
{
adjust = -adjust;
$"{currentPosition}\t {-targetPosition}\t SX {adjust}".Dump();
}
}
// Output:
// -5 -3 SX 2
// -4 -3 SX 1
// -3 -3 HOLD
// -2 -3 SE 1
// -1 -3 SE 2
// 0 -3 SE 3
// 1 -3 SE 3
// 2 -3 SE 3
// 3 -3 SE 3
// 4 -3 SE 3
// 5 -3 SE 3