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 NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      63 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      139 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      75 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      45 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      50 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X