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

Re-Entry After Stop Loss Prevention and Stop Loss Names

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

    Re-Entry After Stop Loss Prevention and Stop Loss Names

    I am trying to code in some logic to prevent my strategy from re-entering a position after my stop loss is triggered while the entry conditions still hold true. One of the problems I have run into is the fact that you cannot change the name of exits generated by SetStopLoss() as this forum post highlights:



    The post suggests using stop orders instead so you can set your own signal names, I unfortunately have not been able to figure out how to implement that suggestion. I will paste my code governing order handling below so you can see what I am trying to do. I can set specific stop losses to specific entries, but I cannot name those stop losses as mentioned above.

    In order for this to work I would need to find some kind of way to name my stops. That way, the program knows that the previous order status was a filled stop out from a long position and won't be allowed to go long again until the OnOrderUpdate() designation changes.

    If you could show me how to make and implement the stop orders with custom names into this code I would greatly appreciate it. My hope is that they would replace
    SetStopLoss(@"Buy", CalculationMode.Currency, 250, false); and SetStopLoss(@"Short", CalculationMode.Currency, 250, false); while maintaining their functionality. Thank you very much.



    public class SmoothZLEMAandZLTEMAcrossover : Strategy
    {

    private bool DoNotReEnterLong = false;
    private bool DoNotReEnterShort = false;
    .
    .
    .

    else if (State == State.Configure)
    {
    //Stop Loss (These are what I am trying to rename so they match the designation in OnOrderUpdate)
    SetStopLoss(@"Buy", CalculationMode.Currency, 250, false);

    SetStopLoss(@"Short", CalculationMode.Currency, 250, false);
    }
    .
    .
    .
    protected override void OnBarUpdate()
    {
    .
    .
    .
    //Order Logic

    if (CurrentBars[0] < BarsRequiredToTrade) return;

    //Long Entry
    if ((SmoothZLTEMA[0] > SmoothZLEMA[0]) && (SmoothZLTEMA[0] > SmoothZLTEMA[1]) && (DoNotReEnterLong == false))
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), @"Buy");
    }

    //Long Exit

    if ((SmoothZLTEMA[0] < SmoothZLEMA[0]))

    {
    ExitLong(Convert.ToInt32(DefaultQuantity), @"Sell", @"Buy");
    }

    //Short Entry
    if ((SmoothZLTEMA[0] < SmoothZLEMA[0]) && (SmoothZLTEMA[0] < SmoothZLTEMA[1]) && (DoNotReEnterShort == false))
    {
    EnterShort(Convert.ToInt32(DefaultQuantity), @"Short");
    }

    //Short Exit
    if ((SmoothZLTEMA[0] > SmoothZLEMA[0]))

    {
    ExitShort(Convert.ToInt32(DefaultQuantity), @"Cover", @"Short");
    }
    }

    protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,
    int quantity, int filled, double averageFillPrice,
    Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
    {
    if(order.OrderState == OrderState.Filled && order.Name == "Stop loss long")
    DoNotReEnterLong = true;

    if(order.OrderState == OrderState.Filled && order.Name == "Stop loss short")
    DoNotReEnterShort = true;
    }

    #2
    Hello StoneMan78,

    Thank you for your post.

    That is correct. A custom signal name cannot be set for the SetStopLoss() method. What the forum post you linked is referring to is using Exit methods to place stop orders. This way you would be able to specify a signal name for the stop orders.

    See the attached example script that demonstrates using exit methods, such as ExitLongStopLimit(), to place a stop and target order with custom signal names.

    Also, see the help guide documentation below for more information.
    SetStopLoss - https://ninjatrader.com/support/help...etstoploss.htm
    Managed Approach Methods - https://ninjatrader.com/support/help...d_approach.htm

    Let us know if we may assist further.
    Attached Files
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hello Brandon H.

      Thank you for the example code, it is very helpful. As I continued to work through this problem I believe I may have found a solution but will need a bit more help to implement. I think this order management stuff is a bit more on the advanced end of things for ninja script. This potential solution will be using the OnExecutionUpdate() method to try to best leverage ninja scripts event driven frame work to create a robust solution.

      When my long order fills I am going to want to submit my ExitLongStop order. That way a stop will only be submitted if I get a fill. In order to set my stop I am going to need to know the execution price of my long fill. I do not know how to retrieve this value so if you could show me that would be greatly appreciated. This stop will be canceled when the chart based exit signal gets filled (or when the short order fills because long exit and short entry can happen at the same time, I'm not sure if these two events happening at the same time matters, could you let me know?)

      By applying this logic to the framework established above I hope to be able to replace the SetStopLoss functions with these exit orders to give me the functionality I want. These exit signals would be named "Stop loss long" and "Stop loss short" as is shown in the code from my previous comment in the OnOrderUpdate() method.

      Your assistance and feedback is greatly appreciated,
      StoneMan78

      Comment


        #4
        Hello StoneMan78,

        Thank you for your note.

        You could use 'execution.Order.AverageFillPrice' in the OnExecutionUpdate() method to get the AverageFillPrice of an execution.

        See the SampleOnOrderUpdate example script on the help guide page linked below for a demonstration of using execution.Order.AverageFillPrice to set up stops and targets.
        SampleOnOrderUpdate - https://ninjatrader.com/support/help...and_onexec.htm

        Also, please see the help guide documentation below for more information.
        Order.AverageFillPrice - https://ninjatrader.com/support/help.../nt8/order.htm
        OnExecutionUpdate() - https://ninjatrader.com/support/help...tionupdate.htm
        Execution - https://ninjatrader.com/support/help.../execution.htm

        Let us know if we may assist further.
        Brandon H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by DJ888, 04-16-2024, 06:09 PM
        6 responses
        18 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by Jon17, Today, 04:33 PM
        0 responses
        1 view
        0 likes
        Last Post Jon17
        by Jon17
         
        Started by Javierw.ok, Today, 04:12 PM
        0 responses
        6 views
        0 likes
        Last Post Javierw.ok  
        Started by timmbbo, Today, 08:59 AM
        2 responses
        10 views
        0 likes
        Last Post bltdavid  
        Started by alifarahani, Today, 09:40 AM
        6 responses
        41 views
        0 likes
        Last Post alifarahani  
        Working...
        X