Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trying to Switch Qty based on Previous Direction of Previous Most Recent Closed Trade

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Trying to Switch Qty based on Previous Direction of Previous Most Recent Closed Trade

    To clarify, I want the strategy to:
    1. Increase the position size to 2 when switching from short to long.
    Long Entry Conditions:
    • If the previous position was short, the position size is increased to 2.
    • Otherwise, it enters a long position with the default size.

    Code:
                 // Longs
                if ((Close[0] > SMA1[0])
                     && (Close[0] <= Bollinger1.Lower[0]))
                {
                    if (lastPosition == MarketPosition.Short)
                    {
                        EnterLong(2, "GoLong2");
                    }
            //        lastPosition = MarketPosition.Long;
                    else
                    {
                        EnterLong(Convert.ToInt32(DefaultQuantity), "GoLong");
                    }
                }
                else if ((Close[0] > SMA1[0]) && (Close[0] <= Bollinger2.Lower[0]))
                {
                    if (lastPosition == MarketPosition.Short)
                    {
                        EnterLong(2, "AgroLong2");
                    }
                    else
                    {
                        EnterLong(Convert.ToInt32(DefaultQuantity), "AgroLong");
                    }
                }
                
                // Shorts Exit.........​
                       ..........................
    When I backtest and view the trades, it is switching the QTY upon a switch from shorts to longs, however, it gets stuck on the QTY increase and now any long after that as well is continuing to enter with a QTY of 2. What am I doing wrong in my logic?
    Attached Files
    Last edited by agclub; 06-11-2024, 01:15 PM.

    #2
    Hello agclub,

    I think you are wanting something like:

    Code:
    private MarketPosition lastPosition;
    private int quantity;
    In State.DataLoaded:
    Code:
    lastPosition = MarketPosition.Flat;
    quantity = 1;
    InOnBarUpdate():
    Code:
    if (/* conditions to enter long here */)
    {
        if (lastPosition == MarketPosition.Short)
        {
           quantity += 1;
        }​
        EnterLong(quantity, "myLongEntrySignalName");
        lastPosition = MarketPosition.Long;
    }
    
    else if (/* conditions to enter short here */)
    {
       EnterShort(quantity, "myShortEntrySignalName");
       lastPosition = MarketPosition.Short;
    }
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    79 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    40 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    63 views
    2 likes
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    63 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    54 views
    0 likes
    Last Post CarlTrading  
    Working...
    X