Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

how to place OCO orders with Exit()??

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

    how to place OCO orders with Exit()??

    I am trying to place stoploss and profitlimit orders in ExitLongStopMarket() and ExitLongLimit(). in the link , https://ninjatrader.com/support/help...stopmarket.htm, they are formed as ExitLongStopMarket(int quantity, double stopPrice, string signalName, string fromEntrySignal). but when i place orders , no active stoploss and profitlimit orders. they did not appear.
    the following is very basic code, if open is greater tahn close, it would be long with oco orders of stoploss and profitlimit
    region Using declarations
    using System;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using NinjaTrader.Cbi;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.NinjaScript.Strategies;
    #endregion

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class OpenCloseLongOnlyStrategy : Strategy
    {
    private Order entryOrder = null;
    private Order stopOrder = null;
    private Order targetOrder = null;
    private double stopPrice;
    private double targetPrice;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = "Enters a long position based on Open vs Close price comparison.";
    Name = "OpenCloseLongOnlyStrategy";
    Calculate = Calculate.OnBarClose;
    IsOverlay = true;
    BarsRequiredToTrade = 20;
    PositionSize = 1; // Default position size
    }
    }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name = "Position Size", Description = "Number of contracts/shares per trade", Order = 1, GroupName = "Parameters")]
    public int PositionSize { get; set; }

    protected override void OnBarUpdate()
    {
    if (CurrentBar < BarsRequiredToTrade) return; // Wait for enough bars

    if (Position.MarketPosition == MarketPosition.Flat)
    {
    // Check the condition to enter a long position
    if (Open[0] < Close[0]) // Current bar where open is less than close
    {
    // Long setup
    Print($"Long Setup - Open: {Open[0]}, Close: {Close[0]}");
    stopPrice = Low[1] - 3 * TickSize; // Stop is 3 ticks below previous bar's low
    targetPrice = Close[0] + 20 * TickSize; // Target is 20 ticks above the entry price

    // Enter long position with limit order at close price of the current bar
    entryOrder = EnterLongLimit(PositionSize, Close[0], "Entry");

    // Set OCO group for stop and target
    stopOrder = ExitLongStopMarket(PositionSize, stopPrice, "StopLoss", "Entry");
    targetOrder = ExitLongLimit(PositionSize, targetPrice, "Target", "Entry");

    Print($"Long Entry Price: {Close[0]}, Stop: {stopPrice}, Target: {targetPrice}");
    }
    }
    }

    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice,
    int quantity, int filled, double averageFillPrice,
    OrderState orderState, DateTime time, ErrorCode error, string nativeError)
    {
    if (orderState == OrderState.Filled)
    {
    Print($"Order {order.Name} Filled: {order.OrderAction} {quantity} at {averageFillPrice}");

    // Reset entry order reference on fill to allow new order entry
    if (order == entryOrder)
    {
    entryOrder = null;
    }
    }
    }

    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity,
    MarketPosition marketPosition, string orderId, DateTime time)
    {
    if (marketPosition == MarketPosition.Flat)
    {
    // When position is closed, clear the order references
    entryOrder = null;
    stopOrder = null;
    targetOrder = null;
    Print($"Position closed at {price} on {time}");
    }
    }
    }
    }

    #2
    Hello steinberg123,

    When using the managed approach OCO is handled internally, you would use signal names to link orders. For example if you make an entry named "MyEntry" in your exits you will specify "MyEntry" as the from entry signal name to link them to that position. Any exits using that name will be OCO tied.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    52 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    130 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    70 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    43 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    48 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X