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

Setting up custom stop loss

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

    Setting up custom stop loss

    Hello,

    I am trying to make a custom stop loss that is different for different scenarios:

    In my current example:
    Scenario 1: a maximum stop loss of 300 ticks
    Scenario 2: a trailing stop loss of 10 ticks

    Since you can't do a TrailStop and an automatic StopLoss together... I am trying to pseudo hard-code it.

    Here is my code:



    if(currentprofit > 20)
    {

    if (currentprofit > maxprofit)
    {
    maxprofit = currentprofit;
    }


    if (currentprofit < (maxprofit - 10))
    {
    ExitShort(@"EntryShort");
    maxprofit = 0;
    }
    }

    if (currentprofit < -300)
    {
    ExitShort(@"EntryShort");​
    }


    So basically:
    If my currentprofit variable is larger than maxprofit, it will update maxprofit to the new profit value. If currentprofit dips below 10 ticks, it will exit the trade. If currentprofit never hits​ the +20 profit, it has a safety net at -300 ticks.

    The issue I am having is that max profit is always updating to current profit (i think because max profit is resetting to zero at every run of the code). I have it initialized as a "private double maxprofit = 0;" in the beginning of the strategy (before onstatechange()). Can anyone tell me how to retain this value of maxprofit through the next run of the code?

    Thank you!

    #2
    Hello Orion815,

    From the given code I see you are resetting the variable when you exit, if that condition keeps becoming true it will keep resetting the variable. The best way to go over this situation would be to add prints into your code to output the value at various points to see when it gets reset and how long it retains the value.

    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse,

      Thank you for your response.

      Yes, I want to reset the maxprofit variable at the end of every trade.

      However, it is not holding this value through each bar update. Is this possible with a variable for it to store the value at each bar update, and only update, as my code indicates, if currentprofit > maxprofit? Or should this be done with an array or data series?

      I tried doing the prints as you suggested, and it seems like it's holding the value until the end of each bar update.

      Thank you.

      Comment


        #4
        Hello Orion815,

        You mentioned in your first post that the variable is a private variable so that will retain a value over many bars so long as you are not resetting it. Because you used a private variable and the value is being reset to 0 that would indicate that your logic is not working as you expected and is resetting that variable. To know why that is happening you would need to use prints to trace how your conditions are working in relation to that variable.
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by DayTradingDEMON, Yesterday, 08:54 AM
        12 responses
        36 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by reekotubbs, Today, 01:09 PM
        0 responses
        10 views
        0 likes
        Last Post reekotubbs  
        Started by aligator, 06-05-2018, 01:25 AM
        60 responses
        5,919 views
        10 likes
        Last Post aligator  
        Started by tony_28217, 06-15-2024, 12:55 PM
        7 responses
        53 views
        1 like
        Last Post svikari
        by svikari
         
        Started by pvtrader, 07-01-2024, 09:02 AM
        4 responses
        84 views
        0 likes
        Last Post Sparkyboy  
        Working...
        X