Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Reverse Position

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

    Reverse Position

    I am trying to develop a strategy where I can automatically open a position in the opposite direction of the existing trade, and close that position when there is no trade. Using MNQ DEC23 as an example, here is what I am trying to do:

    1. Check if there is an open position (It can be of any instrument on that particular account).
    2. If there is an open position, it should immediately open 1 MNQ DEC23 contract in the opposite direction to that of the open position. So, if the open position is LONG, the MNQ DEC23 contract should be SHORT, and vice versa.
    3. If there is no position, the strategy should close the existing MNQ DEC23 position

    Here is the code that I am using, but it doesn't so anything. What could be the problem.

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class MultiInstrumentTest : Strategy
    {
    private Order entryOrder;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = "A simple strategy that opens a MNQ DEC23 contract in the opposite direction of the open position.";
    Name = "MultiInstrumentTest";
    }
    else if (State == State.Configure)
    {
    AddDataSeries("MNQ DEC23", Data.BarsPeriodType.Minute, 5, Data.MarketDataType.Last);
    }
    }

    protected override void OnBarUpdate()
    {
    if (Position.MarketPosition != MarketPosition.Flat)
    {
    if (Position.MarketPosition == MarketPosition.Long)
    {
    entryOrder = EnterShort(1, 1, "MNQENTRY");
    }
    else if (Position.MarketPosition == MarketPosition.Short)
    {
    entryOrder = EnterLong(1, 1, "MNQENTRY");
    }
    }
    else
    {
    if (entryOrder != null && entryOrder.OrderState == OrderState.Filled)
    {
    ExitLong(1, 1, "MNQEXIT", "MNQENTRY");
    ExitShort(1, 1, "MNQEXIT", "MNQENTRY");
    entryOrder = null;
    }
    }
    }
    }
    }




    #2
    Hello cranky,

    Thank you for your post.

    Since you added an additional data series, you'll need to add a check for if BarsInProgress == 1 to trigger the orders on the secondary series.

    AddDataSeries -https://ninjatrader.com/support/help...dataseries.htm

    BarsInProgress - https://ninjatrader.com/support/help...inprogress.htm

    Please let me know if you have any other questions.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    24 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    120 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    63 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    41 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    45 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X