determine if the last closed trade was short or long  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
hairyMug
Posts: 57
Joined: 03 Feb 2014
Has thanked: 5 times
Been thanked: 6 times

determine if the last closed trade was short or long

Postby hairyMug » 29 Sep 2014

in V9, I'm trying to determine if the last closed trade was short or long. To get the last position, would I reference the "0" index or the "Count-1" index. Same question for the "ClosedTrades". (BTW: debugging never shows any information in the Positon returned from the collection) Here is my code:

Code: Select all

bool lastLong = false;
bool lastShort = false;
try
{
int lastPos=0;
int lastTrade=0;
lastLong=lastPos>=0
&& Positions[lastPos].ClosedTrades[lastTrade].IsLong
&& !Positions[lastPos].ClosedTrades[lastTrade].IsOpen;

lastShort=lastPos>=0
&& !Positions[lastPos].ClosedTrades[lastTrade].IsLong
&& !Positions[lastPos].ClosedTrades[lastTrade].IsOpen;
}
catch{}

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

Re: determine if the last closed trade was short or long  [SOLVED]

Postby Henry MultiСharts » 01 Oct 2014

Hello hairyMug,

0 - open position;
1 - one position back (the last position closed);
2 - two positions back, etc.

Here is a sample code:

Code: Select all

int _DirectionClosedTrades = -1;
int _idx = 1;

if (Positions.Count > _idx && Positions[_idx].ClosedTrades.Count > 0)
{
IMarketPosition _lastPos = Positions[_idx];

if (_lastPos.ClosedTrades[0].IsLong)
_DirectionClosedTrades = 1;
else
_DirectionClosedTrades = 2;
Output.WriteLine("_DirectionClosedTrades = {0}, dateTime: {1}", _DirectionClosedTrades, _lastPos.ClosedTrades[0].ExitOrder.Time);
}


Return to “MultiCharts .NET”