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

Stop Loss and Profit Target on indicator

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

    Stop Loss and Profit Target on indicator

    Hello,
    I am trying to add a calculation of High EMA -Low EMA as the take profit and stop loss for my strategy. So when an order is executed the strategy figures out the difference and uses this calculation to set the SL/TP position. Can you assist?

    #2
    Hello FXIndustries,

    Thank you for your note.

    You would need to unlock the code from the Strategy Builder window and program the MAX and MIN values to use EMA as the input. You would then calculate the difference and use that as the price level (+/- the entry price).

    The following code can be used to find the difference between your high and low EMA (maxEMA and minEMA):

    if (CurrentBar < 10) return; //This will ensure that there are more than 10 bars before proceeding
    double maxEMA = MAX(EMA(20), 10)[0];
    double minEMA = MIN(EMA(10), 10)[0];
    double diff = Math.Abs(maxEMA - minEMA);

    You can set your Profit Target/Stop Loss using CalculationMode.Price and add or subtract diff from Close[0]:

    if (Position.MarketPosition == MarketPosition.Short)
    {
    SetProfitTarget(CalculationMode.Price, Close[0] - diff)
    SetStopLoss(CalculationMode.Price, Close[0] + diff)
    }

    if (Position.MarketPosition == MarketPosition.Long)
    {
    SetProfitTarget(CalculationMode.Price, Close[0] + diff)
    SetStopLoss(CalculationMode.Price, Close[0] - diff)
    }

    Please let us know if we may provide further assistance.

    Comment


      #3
      Profit Target

      Chris,

      Thanks for your help. I am trying to work out another scenario... I have been able to get my strategy to work and the Stop Loss is either a low or high EMA Wave Line based on an Indicator that I created. I can simply put the following for the Stop Losses...

      SetStopLoss("short",CalculationMode.Price, emawave(1).Plot1[0],false);
      SetStopLoss("long",CalculationMode.Price, emawave(1).Plot2[0],false);

      These put the Stop Losses at the indicator Plot points. The problem I am having is getting the Take Profit to work. I would like the Profit target to be the same distance that the Stop loss is for the open, only going the other direction. Since the Stoploss is a variable what is the easiest way to accomplish this? Thanks

      Comment


        #4
        Hello FXIndustries,

        Thank you for your reply.

        You would first need to find the difference between the Close and the EMA:

        double diff = Close[0] - emawave(1).Plot1[0];

        You would then need to add the difference to the Close as the target price:

        double target = Close[0] + diff;

        The Close minus the indicator should be a total price between the two and adding that value to the current price should be the price of the target. You would also need to continue using CalculationMode.Price like you have been.

        Please let us know if we may provide further assistance.

        Comment


          #5
          Chris,

          Just curious if this should be in the Initialize() Section or OnBarUpdate() Section? It does not seem to be working properly?

          Comment


            #6
            Hello FXIndustries,

            Thank you for the reply.

            Initialize() is only called once when the script starts before it knows any price data. You will want to include in the OnBarUpdate() Section of your code to create a dynamic price as opposed to Initialize() which is a static value.

            Please let us know if you have any additional questions.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by jclose, Today, 09:37 PM
            0 responses
            5 views
            0 likes
            Last Post jclose
            by jclose
             
            Started by WeyldFalcon, 08-07-2020, 06:13 AM
            10 responses
            1,414 views
            0 likes
            Last Post Traderontheroad  
            Started by firefoxforum12, Today, 08:53 PM
            0 responses
            11 views
            0 likes
            Last Post firefoxforum12  
            Started by stafe, Today, 08:34 PM
            0 responses
            11 views
            0 likes
            Last Post stafe
            by stafe
             
            Started by sastrades, 01-31-2024, 10:19 PM
            11 responses
            169 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Working...
            X