Sell / Cover existing position

Hi all,

I am trying to create a strategy that sell an existing position at a certain level. It’s not really a strategy, but I am using it as a tool for me to sell my position.

When I turn on the strategy, the Sync column would show False. If I set my StartBehavior to Sync, then it would submit an order to buy the position instead of detecting my existing position.

I have attached the strategy for your reference.

else if (State == State.Realtime)
{
    var pos = Account.Positions.FirstOrDefault(p => p.Instrument == Instrument);
    if (pos != null)
        Print("Account position found: " + pos.MarketPosition + " Qty: " + pos.Quantity);
    else
        Print("No account position found for " + Instrument.FullName);
}

protected override void OnBarUpdate()
{
    if (CurrentBar < BarsRequiredToTrade)
        return;
	
    // Get actual account position instead of strategy position
    int accountQty = 0;
    MarketPosition accountPosition = MarketPosition.Flat;
    
    var pos = Account.Positions.FirstOrDefault(p => p.Instrument == Instrument);
    if (pos != null)
    {
        accountQty = pos.Quantity;
        accountPosition = pos.MarketPosition;
    }

    // Use account position for checks
    if (accountPosition == MarketPosition.Flat)
        return;
    else if (accountPosition == MarketPosition.Long)
    {
        if (Direction != SellTradeDirection.Sell)
            return;
    }
    else if (accountPosition == MarketPosition.Short)
    {
        if (Direction != SellTradeDirection.Cover)
            return;
    }

    if (_sts == 0)
        return;

    // cancel the order after X minutes if it was not executed
    if (this._limitOrder != null &&
        Time[0] > this._limitOrder.Time.AddMinutes(CancelAfterMinutes))
    {
        CancelOrder(this._limitOrder);
        this._limitOrder = null;
        Print("Cancelled: replace order " + CancelAfterMinutes + " minutes");
        return;
    }

    if (BarsInProgress == 1) // seconds
    {
        // Don't submit if we already have a pending order
        if (_limitOrder != null)
            return;

        if (_sts > 0)
        {
            if (accountPosition == MarketPosition.Long && Direction == SellTradeDirection.Sell)
            {
                if (SellNow)
                {
                    _limitOrder = ExitLongLimit(0, true, _sts, Close[0], "ExitLong", "");
                    return;
                }
                else if (SellAt > 0 && Close[0] >= SellAt)
                {
                    _limitOrder = ExitLongLimit(0, true, _sts, SellAt, "ExitLong", "");
                    return;
                }
            }
            else if (accountPosition == MarketPosition.Short && Direction == SellTradeDirection.Cover)
            {
                if (SellNow)
                {
                    _limitOrder = ExitShortLimit(0, true, _sts, Close[0], "ExitShort", "");
                    return;
                }
                if (SellAt > 0 && Close[0] <= SellAt)
                {
                    _limitOrder = ExitShortLimit(0, true, _sts, SellAt, "ExitShort", "");
                    return;
                }
            }
        }
        return;
    }
}

Have you tried setting your StarBehavior to AdoptAccountPosition (be sure to set the IsAdoptAccountPossitionAware to true as well and code accordingly)?

StartBehaviour
Syncing Account Positions
IsAdoptAccountPositionAware

Yes, I have tried StartBehavior = StartBehavior.AdoptAccountPosition;

But when I go to the strategy analyzer GUI. It didn’t show the “AdoptAccountPosition” option still, so I didn’t think it would work.

I only have four options for StartBehavior. Do I need to upgrade my NinjaTrader platform for that option to show?

Your strategy has to set the IsAdoptAccountPositionAware to true in the Configure state. Then you should have the AdoptAccountPosition behavior available. Also, can you clarify what you mean about the strategy analyzer? afaik, the analyzer doesn’t have a start behavior property in the GUI. It doesn’t really make sense. Analyzer runs use a separate clean paper account each time, there’s nothing to sync to.

I didn’t set IsAdoptAccountPositionAware = true; That’s probably the problem. Let me give that a try and see if it works on Monday.

My mistake. It’s not strategy analyzer. I was referring to the Strategies window where I can configure the strategy.

Thank you

Hi Mark,

It’s working with the following! much appreciated.

                IsAdoptAccountPositionAware = true; 
                StartBehavior = StartBehavior.AdoptAccountPosition;
1 Like