Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Create Order problem

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

    Create Order problem

    Hello, I would like to create orders from NinjaScript in my indicator in following way:

    - Stop market
    - buy - press ctrl, place cursor any place above the price and left click
    - sell - press ctrl, place cursor any place bellow the price and left click

    - Stop limit
    - buy - press alt, place cursor any place bellow the price and left click
    - sell - press alt, place cursor any place above the price and left click​

    I can get the price on mouse click, check if keys are pressed and have code for creating orders however sometimes I get an error that order could not be placed.

    I have attached screenshots from my code.

    I think I am setting incorrect values for the limitPrice and stopPrice in account.CreateOrder function??

    Could you please help?

    Thanks in advance
    Attached Files

    #2
    Hello 09try,

    Thank you for your post.

    What is the specific error you are getting? Can you post a screenshot?

    I look forward to assisting further.

    Comment


      #3
      Hello Gaby, sorry I forgot to paste error

      Click image for larger version

Name:	image.png
Views:	56
Size:	4.7 KB
ID:	1315752

      Comment


        #4
        Thank you for the screenshot.

        This error may appear when submitting a stop order if the stop price is ahead of the market.

        For a buy stop order, such as when exiting a short position or entering a long position, the stop price must be above the current ask price.
        For a sell stop order, such as when exiting a long position or entering a short position, the stop price must be below the current bid price.

        You can use GetCurrentBid() and GetCurrentAsk() to make sure that the price you are submitting or changing the order to is a valid price and on the correct side of the market.

        Help Guide: NinjaScript > Language Reference > Common > Analytical > GetCurrentBid()
        Help Guide: NinjaScript > Language Reference > Common > Analytical > GetCurrentAsk()

        For the example below, consider a variable holding your calculated stop price. This could be a price from a custom calculation, or one obtained from an indicator. In this example 100 will be used as a placeholder.

        Code:
        stopPrice = 100; // ← price you have calculated to use as the stop price
        Using Math.Max() / Math.Min() to ensure the calculated stopPrice is above/below the ask/bid

        Code:
        // ensure sell stop order is below the bid
        stopPrice = Math.Min(stopPrice, GetCurrentBid() - 1 * TickSize);
        ExitLongStopMarket(stopPrice);
        
        // ensure buy stop order is above the ask
        stopPrice = Math.Max(stopPrice, GetCurrentAsk() + 1 * TickSize);
        ExitShortStopMarket(stopPrice);
        Please note that if the error occurred due to market volatility, then there is no way to 100% avoid this occurring, as in volatile markets the market could move so far and fast that this would occur.

        Comment


          #5
          Hello Gaby, thanks for detailed answer. Could you please clarify? stopPrice in your response is the priceClicked in my question? You can see how I use priceClicked in my screenshot.

          Comment


            #6
            Hello,

            That is just example code. The stopPrice is a variable which holds the stopPrice you are submitting your order to.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            648 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            369 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            108 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            572 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            574 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X