Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need help with code to move targets

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

    Need help with code to move targets

    I have an button indicator that moves my targets when I click the button - and it works great. However, I am looking to use this on the NQ and the movement can be too quick and I do not get the button clicked in time. I would like to use it within 'protected override void OnBarUpdate() ' are of the indicator but I have not been successful in modifying the code to make it work with that area of the indicator. Can you help me out and give me some direction. I trade multiple accounts and want it to modify all the orders across all the accounts. I have variables that define all the accounts I want it to loop through to modify their orders. I use 'lock (Account.All)' to define those accounts. Thanks in Advance.

    Here is the button code:

    protected void myButtonClick(object sender, RoutedEventArgs e)
    {
    Account account = Account.All.FirstOrDefault(x => x.Name == xAlselector.SelectedAccount.Name);
    Position thisPosition=account.Positions.FirstOrDefault(x => x.Instrument.FullName==Instrument.FullName) ;
    if (account==null)
    return;

    var buyOrders = account.Orders.Where(o=>o.Instrument==Instrument && o.OrderType.ToString().Contains("Limit") && o.OrderAction.ToString().Contains("BuyToCover"));
    var sellOrders = account.Orders.Where(o=>o.Instrument==Instrument && o.OrderType.ToString().Contains("Limit") && o.OrderAction.ToString().Contains("Sell"));
    List<Order> ordersToSubmit = new List<Order>();
    foreach (var order in buyOrders)
    {
    order.LimitPriceChanged = thisPosition.AveragePrice - (Mytarget * TickSize); // adjust to new target price
    ordersToSubmit.Add(order);
    }

    foreach (var order in sellOrders)
    {
    order.LimitPriceChanged = thisPosition.AveragePrice + (Mytarget * TickSize); // adjust to new target price
    ordersToSubmit.Add(order);
    }
    account.Change(ordersToSubmit.ToArray());
    }​
    Last edited by thepcmd; 01-29-2023, 04:13 PM.

    #2
    Hello thepcmd,

    Thanks for your post.

    In the Support department at NinjaTrader using custom C# logic such as using custom buttons to change order prices would go beyond the support we would be able to provide. Using Lists (List<T>) also requires custom C# logic and goes beyond the support we can provide. For information about these topics, you could do a Google search to research the C# topics further.

    That said, to change orders on an account in an indicator within OnBarUpdate() you could use the Account class Account.Change() method.

    See the help guide page linked below for more information and sample code.

    <Account>.Change: https://ninjatrader.com/support/help...nt8/change.htm
    Account class: https://ninjatrader.com/support/help...ount_class.htm
    Order object: https://ninjatrader.com/support/help.../nt8/order.htm

    This forum thread will also be open for community members to share their insights on the topic.

    Please let me know if I may assist further.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    561 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    325 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
    547 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    547 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X