I am working on a custom indicator using the sendmail functionality and I am trying to have ninjatrader automatically e-mail out the details of a trade on order execution. So for example when I enter a trade it will e-mail out a message saying " long 100 shares xyz at $50 ". Now i have this part working fine with no problems.
But what I want to have happen is once I have a position, say a long position, and I go to sell that position, instead of the e-mail sending out "short 100 shares of xyz at 50" i want it to say "closing position: sold long at 50". So in other words I need to implement logic for it to check whether or not I have a current position. I have tried a handful of different things, currently the code is as follows, but doesn't assign correctly. Any thoughts on what variables (account/position/etc) I should use to implement this logic?
Thanks!
foreach (Account acct in Cbi.Globals.Accounts)
{
if (acct.Positions[0].MarketPosition != MarketPosition.Flat)
{
PositionCollection positions = acct.Positions;
foreach (Position pos in positions)
{
if (e.Execution.MarketPosition == MarketPosition.Long)
action = "NEW POSITION: Long";
else action = "NEW POSITION: Sold Short";
}
}
else
{
if (e.Execution.MarketPosition == MarketPosition.Long)
action = "POSITION CLOSED: Covered Short";
else action = "POSITION CLOSED: Sold Long";
}
}
Comment