Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

{NinjaScript} - Code sample of create Long (market) and set take profit and stop loss

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

    {NinjaScript} - Code sample of create Long (market) and set take profit and stop loss

    Dear all, hope you are fine.
    I need to dinamically create an market order and set TakeProfit and StopLoss by ninjascript.
    I noted that creating by Strategy Builder StopLoss and TakeProfit are defined in State == State.Configure, but all trades gives the same configuration.
    I need to create market orders when some condition happend and set TP and SL dinamically depending on the condition.
    Could you give me some code sample?
    Thank you so much!

    PD: I need market orders, not stop or limit orders.

    I have this code:


    double tp=a.max
    double sl=a.min;

    if(condition)
    {
    SetProfitTarget("PT", CalculationMode.Price,tp);
    SetStopLoss("SL", CalculationMode.Price, sl, true);
    EnterLong(Size,"Buy");
    }

    The long is created with the correct size, but TP and SL doesnt get configured.
    tp and sl variables are ok (min and max prices of a session), I used Print() to confirm that.

    For example> Sell - Price: 1,17776 SL : 1,17984 TP : 1,17574
    Last edited by fscabrera03; 03-30-2021, 08:40 PM.

    #2
    In my opinion, I suggest you used SubmitOrderUnmanaged().

    You can see the link below :



    For your request, use this one
    SubmitOrderUnmanaged(int selectedBarsInProgress, OrderAction orderAction, OrderType orderType, int quantity, double limitPrice, double stopPrice, string oco, string signalName)

    selectedBarsInProgress can be 0
    orderAction can be sellshort
    orderType can be Market
    quantity can be the number you want
    limitPrice can be 0
    stopPrice can be stoploss price
    oco can be ""
    signalName can be sell

    Together with this, you need another SubmitOrderUnmanaged which is for you profit target.

    Comment


      #3
      Hello fscabrera03,

      Thanks for the post.

      If you unlocked the code you could use the set methods like you had shown in your example, you would need to call the set methods and give the values you wanted before calling the entry. Upon fill the targets are submitted.

      From the code it looks like you are not correctly using the SignalName.

      Code:
      SetProfitTarget("PT", CalculationMode.Price,tp);
      SetStopLoss("SL", CalculationMode.Price, sl, true);
      should be

      Code:
      SetProfitTarget("Buy", CalculationMode.Price,tp);
      SetStopLoss("Buy", CalculationMode.Price, sl, true);
      The FromEntrySignal name would be the name of the entry these target.

      I look forward to being of further assistance.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      80 views
      1 like
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      40 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      63 views
      2 likes
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      63 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      54 views
      0 likes
      Last Post CarlTrading  
      Working...
      X