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 judysamnt7, 03-13-2023, 09:11 AM
        4 responses
        59 views
        0 likes
        Last Post DynamicTest  
        Started by ScottWalsh, Today, 06:52 PM
        4 responses
        36 views
        0 likes
        Last Post ScottWalsh  
        Started by olisav57, Today, 07:39 PM
        0 responses
        7 views
        0 likes
        Last Post olisav57  
        Started by trilliantrader, Today, 03:01 PM
        2 responses
        21 views
        0 likes
        Last Post helpwanted  
        Started by cre8able, Today, 07:24 PM
        0 responses
        10 views
        0 likes
        Last Post cre8able  
        Working...
        X