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

Entry Orders on indicator??

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

    Entry Orders on indicator??

    Hello Guys,

    I am trying to develop an indicator that does the following:

    1 - If Price is > EMA20 && Price is > SMA5 --> DO NOTHING

    2 - When I make an entry or I set an Order Entry and the Nr 1 conditions are FALSE, then Flatten my accounts (please be aware that the calculation has to be made untill an order is filled or submitted) and set a LogLevel.Alert (ONLY ONCE, I GET CURRENTLY A LOOP).

    3 - If an order is filled and the Nr 1 conditions are true but the price goes below the EMA20 and SMA5 --> let the trade continue untill the stop loss is filled, do not flatten everything.

    Regarding this, I have the following questions:

    1 - Is there a way to see if an order has been filled/submitted and if so flatten everything when the conditions Nr 1 are met?

    2 - How can I Flatten all accounts? Can I make an account selector in the indicator, so that the indicator only works in the selected account?

    3 - How can I make the Alert only to show once? Now I am getting an annoying loop, I think that is because I did not set up the order filled/submitted detection...



    Here the code:


    Code:
        public class TEACHER : Indicator
        {
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Indicator here.";
                    Name                                        = "TEACHER";
                    Calculate                                    = Calculate.OnEachTick;
                    IsOverlay                                    = false;
                    DisplayInDataBox                            = true;
                    DrawOnPricePanel                            = true;
                    DrawHorizontalGridLines                        = true;
                    DrawVerticalGridLines                        = true;
                    PaintPriceMarkers                            = true;
                    ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                    //See Help Guide for additional information.
                    IsSuspendedWhileInactive                    = true;
                    ArePlotsConfigurable = false;
                }
                else if (State == State.Configure)
                {
    
                }
            }
    
            protected override void OnBarUpdate()
            {
    
                if (State == State.Historical)
    
                return;
    
                if ((Close[0] > EMA(Close, 20)[0])
                    && (Close[0] > SMA(Close, 5)[0]))
    
                {
                return;
                }
    
                else
                {
                Account myAccount = Account.All.FirstOrDefault(a => a.Name == "Playback101");
                myAccount.Flatten(new [] { Instrument.GetInstrument(Instrument.ToString()) });
                Log("YOU CAN'T TRADE HERE, PLEASE WAIT!!!", LogLevel.Alert);
                }
    
            }
        }​[B][U][/U][/B]

    Thanks a lot for your help!!!!

    #2
    Hello tradingnasdaqprueba,

    Thanks for your post.

    You would need to use the Account class to submit orders to an account using an indicator.

    Account.CreateOrder() could be used to create an order to submit to an account.

    Account.Submit() would be used to submit the created order to the account.

    Account.Flatten() would be used to flatten the account on an instrument. You would have to define each account in your script and then call <Account>.Flatten() for each of the accounts to flatten all those accounts on an instrument.

    See this forum thread for information about creating an account selector as a user-defined paramter: https://forum.ninjatrader.com/forum/...98#post1192898

    OrderUpdate could be used to subscribe to order update events to see what the order state of an order is.

    If by "Alert" you are referring to the Log() method, the Log() method would be called anytime that condition is true. You would need to modify your logic to call the method less frequently. Prints should be added to the script one line above your conditions that print out all the values being used for the conditions to see how they are evaluating.

    See the help guide documentation below for more information.

    Account: https://ninjatrader.com/support/help...ount_class.htm
    Account.CreateOrder(): https://ninjatrader.com/support/help...reateorder.htm
    Account.Submit(): https://ninjatrader.com/support/help...nt8/submit.htm
    Account.Flatten(): https://ninjatrader.com/support/help...t8/flatten.htm
    OrderUpdate: https://ninjatrader.com/support/help...rderupdate.htm
    Orders: https://ninjatrader.com/support/help...rs_account.htm

    Brandon H.NinjaTrader Customer Service

    Comment


      #3

      Great stuff. Brandon, if you wanted to submit an OCO order using an indicator and the Account class is there an example? I have a working indicator using Account.Submit() which works fine. But I want to augment it to include a stop order with the initial order using an OCO. I cant find any examples, but it appears I have to wait for the submitted order to be filled before adding the stop order using Account.Submit() ?

      Comment


        #4
        Hello MisterTee,

        Thanks for your notes.

        No, we do not have a reference sample demonstrating this specifically.

        Yes, you must wait for the submitted entry order to be filled before submitting the stop loss order.

        The Account class methods Account.CreateOrder() and Account.Submit() would need to be used to submit orders to an account from a custom NinjaScript indicator.

        You would need to submit the entry order. Once the entry order is filled then you would submit the stop loss and profit target orders for that entry order.

        You could subscribe to OrderUpdate events in the script and use OnOrderUpdate to check if e.OrderState == OrderState.Filled and check if e.Order.IsLong == true followed by creating and submitting the stop loss and profit target order. For short orders, you could check if e.OrderState == OrderState.Filled and check if e.Order.IsShort == true

        See the help guide documentation below for more information.

        Account: https://ninjatrader.com/support/help...ount_class.htm
        Account.CreateOrder(): https://ninjatrader.com/support/help...reateorder.htm
        Account.Submit(): https://ninjatrader.com/support/help...nt8/submit.htm
        OrderUpdate: https://ninjatrader.com/support/help...rderupdate.htm
        Orders: https://ninjatrader.com/support/help...rs_account.htm
        Brandon H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by fx.practic, 10-15-2013, 12:53 AM
        5 responses
        5,406 views
        0 likes
        Last Post Bidder
        by Bidder
         
        Started by Shai Samuel, 07-02-2022, 02:46 PM
        4 responses
        98 views
        0 likes
        Last Post Bidder
        by Bidder
         
        Started by DJ888, Yesterday, 10:57 PM
        0 responses
        8 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by MacDad, 02-25-2024, 11:48 PM
        7 responses
        160 views
        0 likes
        Last Post loganjarosz123  
        Started by Belfortbucks, Yesterday, 09:29 PM
        0 responses
        9 views
        0 likes
        Last Post Belfortbucks  
        Working...
        X