Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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
        BertrandNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by GussJ, 03-04-2020, 03:11 PM
        15 responses
        3,271 views
        0 likes
        Last Post xiinteractive  
        Started by Tim-c, Today, 02:10 PM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by Taddypole, Today, 02:47 PM
        0 responses
        2 views
        0 likes
        Last Post Taddypole  
        Started by chbruno, 04-24-2024, 04:10 PM
        4 responses
        51 views
        0 likes
        Last Post chbruno
        by chbruno
         
        Started by TraderG23, 12-08-2023, 07:56 AM
        10 responses
        403 views
        1 like
        Last Post beobast
        by beobast
         
        Working...
        X