Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Implement Basic Entry - ATM Strategy in Strategy Builder

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

    Implement Basic Entry - ATM Strategy in Strategy Builder


    Hi, I take orders via Basic entry panel with Quantity 3 and use ATM Strategy like below:


    Order Quantity 3, and Parameter type Ticks

    Target1 : Quantity 1: Stop loss 10, Profit 10
    Target2 : Quantity 1: Stop loss 20, Profit 20
    Target3 : Quantity 1: Stop loss 30, Profit 30

    Can yoy help me to implement this logic in OnBarUpdate() ?

    maybe something like this

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0 && CurrentBar < BarsRequiredToTrade)
    return;

    // Some bool variable enterBullish to take long position
    if (enterBullish && Position.Quantity <= 0) {
    EnterLong("First");
    EnterLong("Second");
    EnterLong("Third");

    ExitLongStopMarket(1, Position.AveragePrice - 2.5);
    ExitLongStopMarket(1, Position.AveragePrice - 5);
    ExitLongStopMarket(1, Position.AveragePrice - 7.5);
    }

    if (GetCurrentAsk() >= Position.AveragePrice + 2.5)
    ExitLong(1);

    if (GetCurrentAsk() >= Position.AveragePrice + 5)
    ExitLong(1);

    if (GetCurrentAsk() >= Position.AveragePrice + 7.5)
    ExitLong(1);

    }​



    ​​

    #2
    Hello csrkkalyan,

    Thanks for your post.

    Variables could be created in the Inputs and Variables screen of the Strategy Builder window.

    You could then use the variable you create for conditions in the Conditions and Actions screen of the Strategy Builder.

    Position.Quantity could be accessed from the Strategy folder > 'Current position size' when creating a condition in the Conditions and Actions screen. Position.Quantity could be compared to a Numeric value (Misc folder > Numeric value) to check if the Position.Quantity is less than or equal to 0.

    GetCurrentAsk() could be found in the Price folder.

    Position.AveragePrice could be found in the Strategy folder > Average position price. This could be offset by a value, such as 2.5.

    Attached is a simple example script demonstrating submitting Exit orders for a profit target and stop loss.

    See the help guide documentation below for more information.

    Builder Screens: https://ninjatrader.com/support/help...er_screens.htm
    Conditions: https://ninjatrader.com/support/help...on_builder.htm
    Actions: https://ninjatrader.com/support/help...t8/actions.htm
    Attached Files
    <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


      #3
      I did similar way, but orders are not getting closed, below code is in OnBarUpdate

      if (enterBullish)
      {
      EnterLong(1, "First Long");
      EnterLong(1, "Second Long");
      EnterLong(1, "Third Long");
      EnterLong(1, "Fourth Long");
      }

      if (GetCurrentAsk() >= Position.AveragePrice + 0.25 && Position.MarketPosition == MarketPosition.Long && Position.Quantity == 4)
      ExitLong(1, "First Long", "First Long");

      if (GetCurrentAsk() >= Position.AveragePrice + 0.5 && Position.MarketPosition == MarketPosition.Long && Position.Quantity == 3)
      ExitLong(1, "Second Long", "Second Long");

      if (GetCurrentAsk() >= Position.AveragePrice + 0.75 && Position.MarketPosition == MarketPosition.Long && Position.Quantity == 2)
      ExitLong(1, "Third Long", "Third Long");

      if (GetCurrentAsk() >= Position.AveragePrice + 1 && Position.MarketPosition == MarketPosition.Long && Position.Quantity == 1)
      ExitLong(1, "Fourth Long", "Fourth Long");

      if (GetCurrentAsk() <= Position.AveragePrice - 1.5 && Position.MarketPosition == MarketPosition.Long && Position.Quantity <= 4) {
      ExitLong();
      }​

      Comment


        #4
        Hello csrkkalyan,

        Thanks for your notes.

        To understand why the script is behaving as it is, such as placing orders or not placing orders when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

        In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar.

        Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

        Below is a link to a forum post that demonstrates how to use prints to understand behavior.
        https://ninjatrader.com/support/foru...121#post791121​​
        <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


          #5
          if (enterBullish)
          {
          EnterLong(1, "First Long");
          EnterLong(1, "Second Long");
          EnterLong(1, "Third Long");
          EnterLong(1, "Fourth Long");
          execPrice = Position.AveragePrice;
          }
          Print(Time[0] + " --> ask: " + GetCurrentAsk() + " Avg price: "+ execPrice
          + " --> position: " + Position.MarketPosition + " Qty: "+ Position.Quantity);

          if (GetCurrentAsk() >= execPrice + 0.25 && Position.MarketPosition == MarketPosition.Long && Position.Quantity == 4)
          ExitLong(1, "First Long", "First Long");

          if (GetCurrentAsk() >= execPrice + 0.5 && Position.MarketPosition == MarketPosition.Long && Position.Quantity == 3)
          ExitLong(1, "Second Long", "Second Long");

          if (GetCurrentAsk() >= execPrice + 0.75 && Position.MarketPosition == MarketPosition.Long && Position.Quantity == 2)
          ExitLong(1, "Third Long", "Third Long");

          if (GetCurrentAsk() >= execPrice + 1 && Position.MarketPosition == MarketPosition.Long && Position.Quantity == 1)
          ExitLong(1, "Fourth Long", "Fourth Long");

          if (GetCurrentAsk() <= exitBullishPrice && Position.MarketPosition == MarketPosition.Long) {
          ExitLong();
          }

          For above simple logic, here is the log:

          11/28/2023 5:05:00 PM --> ask: 4562.75 Avg price: 0 --> position: Flat Qty: 0
          11/28/2023 5:10:00 PM --> ask: 4563 Avg price: 0 --> position: Flat Qty: 0
          11/28/2023 5:15:00 PM --> ask: 4563.75 Avg price: 4563 --> position: Long Qty: 4
          11/28/2023 5:20:00 PM --> ask: 4563.75 Avg price: 4563 --> position: Long Qty: 3
          11/28/2023 5:25:00 PM --> ask: 4564 Avg price: 4563 --> position: Long Qty: 3
          11/28/2023 5:30:00 PM --> ask: 4564 Avg price: 4563 --> position: Long Qty: 3
          11/28/2023 5:35:00 PM --> ask: 4564.5 Avg price: 4563 --> position: Long Qty: 3
          11/28/2023 5:40:00 PM --> ask: 4565 Avg price: 4563 --> position: Long Qty: 3
          11/28/2023 5:45:00 PM --> ask: 4564.25 Avg price: 4563 --> position: Long Qty: 3
          11/28/2023 5:50:00 PM --> ask: 4564.5 Avg price: 4563 --> position: Long Qty: 3
          11/28/2023 5:55:00 PM --> ask: 4564.5 Avg price: 4563 --> position: Long Qty: 3
          11/28/2023 6:00:00 PM --> ask: 4564.25 Avg price: 4563 --> position: Long Qty: 3
          11/28/2023 6:05:00 PM --> ask: 4564.25 Avg price: 4563 --> position: Long Qty: 3
          11/28/2023 6:10:00 PM --> ask: 4564.25 Avg price: 4563 --> position: Long Qty: 3

          if you see the above log, that second long should have been triggered around "11/28/2023 5:25:00 PM" and it got many chances after that as well.

          so in theory, once the exitlong being executed, will this never be executed again forever?​

          Comment


            #6
            Hello csrkkalyan,

            The order can be submitted again as long as the EntriesPerDirection / EntryHandling has not been met.

            What do you have EntriesPerDirection and EntryHandling set to?

            Have you enabled TraceOrders to see if orders are being ignored?

            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi ChelseaB,

              I have tried both EntryHandling as EntryHandling.AllEntries and EntryHandling.UniqueEntries, also TraceOrders = true in State == State.Configure

              My requirement is simple, when bullish condition is met, just take two separate orders, like

              if (bullish) {
              EnterLong(1, "First Long");
              EnterLong(1, "Second Long");
              }

              This logic ALWAYS takes the first one but never second order. I feel EnterLong always execute once and ignore all rest.

              Comment


                #8
                Hello csrkkalyan,

                Thanks for your notes.

                What do you have EntriesPerDirection and EntryHandling set to?

                To accomplish this you could set EntryHandling to EntryHandling.AllEntries and set the EntriesPerDirection to 2.

                If you set Entry Handling to EntryHandling.AllEntries, NinjaScript will process all entry order methods up to the maximum allowable entries set in the EntriesPerDirection property. For example, if you set EntriesPerDirection to 2 and use EntryHandling.AllEntries, up to a total of 2 entry orders will process.

                See the help guide documentation below for more information.

                EntriesPerDirection: https://ninjatrader.com/support/help...rdirection.htm
                EntryHandling: https://ninjatrader.com/support/help...ryhandling.htm

                In the TraceOrder prints do you see if any orders are being ignored?

                ​Share your TraceOrder Output with us if you would like us to take a look and see if orders are being ignored.
                <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


                  #9
                  Thanks Brandon, after changing the EntriesPerDirection to 2, it started taking multiple orders.

                  Really appreciate your help and patience on this. Thanks again.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by NullPointStrategies, Today, 05:17 AM
                  0 responses
                  38 views
                  0 likes
                  Last Post NullPointStrategies  
                  Started by argusthome, 03-08-2026, 10:06 AM
                  0 responses
                  124 views
                  0 likes
                  Last Post argusthome  
                  Started by NabilKhattabi, 03-06-2026, 11:18 AM
                  0 responses
                  64 views
                  0 likes
                  Last Post NabilKhattabi  
                  Started by Deep42, 03-06-2026, 12:28 AM
                  0 responses
                  41 views
                  0 likes
                  Last Post Deep42
                  by Deep42
                   
                  Started by TheRealMorford, 03-05-2026, 06:15 PM
                  0 responses
                  46 views
                  0 likes
                  Last Post TheRealMorford  
                  Working...
                  X