Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Count Existing position!!

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

    Count Existing position!!

    Hello

    I have a following code in MQL Language. This code (function) checks if there is a existing order

    This code will return with true if there is existing order and false if there is no existing orders.

    // ================================================== ===
    bool ExistPosition() {
    bool ExistingOrder = FALSE;
    for (int i = 0; i < OrdersTotal(); i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
    if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
    if (OrderType() == OP_BUY || OrderType() == OP_SELL) {
    ExistingOrder = TRUE;
    break;
    }
    }
    }
    }
    return (ExistingOrder);
    }

    // ================================================== ===



    What would be best way to check existing orders in NT?

    This is how I got so far in NT script


    public ExistPosition(){
    int TotalTrades = Performance.AllTrades.Count;
    bool ExistingOrder = false;
    for ( int i =0 ; i <= TotalTrades; i++){
    if(Performance.AllTrades("MSFT", EntryReference, i)) {
    ExistingOrder = true;
    }else{ExistingOrder = false;}

    }


    return(ExistingOrder);

    }

    All I wanted to do is just checking existing order so My Automated strategy does not open two orders at the same time. That is all.

    Is there any way of doing this nicely in NT?

    Thansk all

    #2
    One way to do it is to you surround your order entry code with the following you will not have more than one order

    Code:
    if(Position.MarketPosition== MarketPosition.Flat)
    {
    
    }
    I believe you can also limit the number of entries per direction when you insert the strategy to keep more than one executing.

    hope this helps.
    mrlogik
    NinjaTrader Ecosystem Vendor - Purelogik Trading

    Comment


      #3
      Originally posted by mrlogik View Post
      One way to do it is to you surround your order entry code with the following you will not have more than one order

      Code:
      if(Position.MarketPosition== MarketPosition.Flat)
      {
       
      }
      I believe you can also limit the number of entries per direction when you insert the strategy to keep more than one executing.

      hope this helps.
      That is certainly great help. Thanks a lot!!!

      I got this and if anyone want to use. please use it

      Code:
       
      [FONT=Courier New][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] bool ExistPosition(){[/SIZE][/FONT]
       
      [FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2](Position.MarketPosition == MarketPosition.Flat){[/SIZE][/FONT]
      [FONT=Courier New][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]([/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]);[/SIZE][/FONT]
      [SIZE=2][FONT=Courier New]}[/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]{[/SIZE][/FONT]
      [FONT=Courier New][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]([/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]);[/SIZE][/FONT]
      [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
      [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]

      Comment


        #4
        Thanks for posting your solution - here's what strategy defaults one could set from the Initialize() - http://www.ninjatrader-support2.com/...06&postcount=5

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        569 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        330 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        101 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        548 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        548 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X