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

{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.

      JesseNinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by bortz, 11-06-2023, 08:04 AM
      47 responses
      1,607 views
      0 likes
      Last Post aligator  
      Started by jaybedreamin, Today, 05:56 PM
      0 responses
      9 views
      0 likes
      Last Post jaybedreamin  
      Started by DJ888, 04-16-2024, 06:09 PM
      6 responses
      19 views
      0 likes
      Last Post DJ888
      by DJ888
       
      Started by Jon17, Today, 04:33 PM
      0 responses
      6 views
      0 likes
      Last Post Jon17
      by Jon17
       
      Started by Javierw.ok, Today, 04:12 PM
      0 responses
      15 views
      0 likes
      Last Post Javierw.ok  
      Working...
      X