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

OCO and setting up profit targets and trailing stops

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

    OCO and setting up profit targets and trailing stops

    I finally figured out how to place my OCO order sucessfully but having problems once the order is filled to set up my profit target and a trailing stop. I got close in creating a buy to cover but the issue is because i am closing my first position it wants to close all. here is my code so far. I would rather just manage the order entry.



    private string orderId = string.Empty;
    private IOrder entryOrder = null;
    private IOrder stopOrder = null;

    // User defined variables (add any user defined variables below)
    #endregion

    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {
    CalculateOnBarClose = false;
    Unmanaged = true;
    TraceOrders = true;

    }


    protected override void OnBarUpdate()
    {

    if ( Position.MarketPosition == MarketPosition.Flat )
    {

    entryOrder = SubmitOrder(0, OrderAction.Buy, OrderType.Stop, 2,0, longPrice, "Enter", "EnterLong");
    entryOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.Stop, 2,0, shortPrice, "Enter", "EnterShort");


    }

    }

    //not being used at this moment
    protected override void OnOrderUpdate(IOrder order)
    {
    if (entryOrder != null && entryOrder == order)
    {

    if (order.OrderState == OrderState.Filled)
    // entryOrder = null;
    Print(order.ToString());

    }
    }
    // this test is to create a profit target for the short order
    protected override void OnExecution(IExecution execution)
    {
    if(Position.MarketPosition == MarketPosition.Short)
    {


    Print("we are short");
    stopOrder = SubmitOrder(0, OrderAction.BuyToCover, OrderType.Limit,1,Position.AvgPrice -3*TickSize,0, "Enter", "EnterLong");



    }


    }

    #2
    getting there but not sure if i am doing it correct

    Everything is working according to the methodology I wanted but why doesn't change order not work All I want to do is when my OCO is filled is to make a profit target and a trailing stop. I think the code is wrong but it does work except making the algo a trailing stop.






    if ( Position.MarketPosition == MarketPosition.Flat )
    {

    entryOrder = SubmitOrder(0, OrderAction.Buy, OrderType.Stop, 2,0, longPrice, "Enter", "EnterLong");
    entryOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.Stop, 2,0, shortPrice, "Enter", "EnterShort");


    }

    }

    }

    }
    protected override void OnOrderUpdate(IOrder order)
    {
    if (entryOrder != null && entryOrder == order)
    {

    if (order.OrderState == OrderState.Filled)
    // entryOrder = null;
    Print(order.ToString());

    }
    }

    protected override void OnExecution(IExecution execution)
    {
    if(Position.MarketPosition == MarketPosition.Short)
    {


    Print("we are short");
    if(Position.Quantity> 1)
    {
    profitOrder = SubmitOrder(0, OrderAction.BuyToCover, OrderType.Limit,1,Position.AvgPrice -3*TickSize,0, "Enter", "EnterLong");
    stopOrder = SubmitOrder(0, OrderAction.BuyToCover, OrderType.Stop,2,0,Position.AvgPrice +7*TickSize, "Enter", "EnterLong");
    }
    else if(Position.Quantity == 1)
    {

    //CHANGE ORDER DOES NOT WORK I HAVE TO SUBMIT ANOTHER ORDER
    //IF I USE THE CHANGEORDER METHOD MY OLD STOPS DISAPPEAR
    ChangeOrder(stopOrder,1,0,Position.AvgPrice);
    stopOrder = SubmitOrder(0, OrderAction.BuyToCover, OrderType.Stop,1,0,Position.AvgPrice, "Enter", "EnterLong");
    }



    }

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Jonafare, 12-06-2012, 03:48 PM
    5 responses
    3,986 views
    0 likes
    Last Post rene69851  
    Started by Fitspressorest, Today, 01:38 PM
    0 responses
    2 views
    0 likes
    Last Post Fitspressorest  
    Started by Jonker, Today, 01:19 PM
    0 responses
    2 views
    0 likes
    Last Post Jonker
    by Jonker
     
    Started by futtrader, Today, 01:16 PM
    0 responses
    8 views
    0 likes
    Last Post futtrader  
    Started by Segwin, 05-07-2018, 02:15 PM
    14 responses
    1,792 views
    0 likes
    Last Post aligator  
    Working...
    X