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 NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    54 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    130 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    71 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    44 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    49 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X