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 CarlTrading, 03-31-2026, 09:41 PM
        1 response
        152 views
        1 like
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        89 views
        1 like
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        131 views
        2 likes
        Last Post CaptainJack  
        Started by CarlTrading, 03-30-2026, 11:51 AM
        0 responses
        127 views
        1 like
        Last Post CarlTrading  
        Started by CarlTrading, 03-30-2026, 11:48 AM
        0 responses
        107 views
        0 likes
        Last Post CarlTrading  
        Working...
        X