Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Creating Target and StopLoss based on ATR

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

    Creating Target and StopLoss based on ATR

    ExitLongMIT(Convert.ToInt32(Position.Quantity), (Position.AveragePrice + atrtarget) , @"ATR Target");
    ExitLongMIT(Convert.ToInt32(Position.Quantity), (Position.AveragePrice - atrstop) , @"ATR Stop", "");

    I am trying to create the above to send when I have a Long Position just opened

    I tried this approach but will not compile

    private ATR atr;

    atr = ATR(7);

    then create values for atrtarget and atrstop is my issue

    this is what I tried

    double atrnow = atr[0];
    double atrtarget = atrnow * 2;
    double atrstop = atrnow * 1;


    Thanks​​

    #2
    Hello DTSSTS,

    Thank you for your post.

    You mentioned the script will not compile; what errors are you getting?
    • To send a screenshot with Windows 10 or newer I would recommend using the Windows Snipping Tool.
    • Alternatively to send a screenshot press Alt + PRINT SCREEN to take a screenshot of the selected window. Then go to Start--> Accessories--> Paint, and press CTRL + V to paste the image. Lastly, save it as a jpeg file and send the file as an attachment.
    ​I look forward to your reply.

    Comment


      #3
      ExitLongMIT(Convert.ToInt32(Position.Quantity), (Position.AveragePrice + atrtarget) , @"ATR Target"); the error is no having "" after the "ATR Target"

      ExitLongMIT(Convert.ToInt32(Position.Quantity), (Position.AveragePrice + atrtarget) , @"ATR Target", ""); << this works

      Sorry - I will repost if I have issue with this working

      Comment


        #4
        It worked find, but I do not understand how a stop would make a Profit but in backtest I am seeing that many times

        Comment


          #5
          Hello DTSSTS,

          Thank you for your reply.

          There are known discrepancies between backtests and real-time results. One of those discrepancies is the fill price of orders; the fill price is based on OHLC prices of a bar. If you would like to use more granular data for order fill prices, you can set the Order Fill Resolution to High in order to add a more granular (smallest being 1-tick) series for the price data. Otherwise, if it is a multi-time frame or multi-instrument strategy, you may program the strategy to submit orders to a single tick data series as shown in the following reference sample:The following resources go into more detail about real-time vs. backtest and the order fill algorithm and resolution options:Please let us know if we may be of further assistance.

          Comment


            #6
            Ok after watching live charts this is not following the stop price

            ExitLongMIT(Convert.ToInt32(Position.Quantity), (Position.AveragePrice - atrstop) , @"ATR Stop", "");

            I attach an image of the order log Click image for larger version

Name:	orderlog0713640pm.png
Views:	402
Size:	134.7 KB
ID:	1260311

            The line highlighted is the StopLoss that was filled MIT / 2 Lines below we did the Buy at 15715.00
            then 2 sell orders were send the Target and the Stop - the line below the highlighted one is the target 15720.00 which cancelled after the stop fill

            the Highlighted is the MIT Stop order which immediately filled once position was taken the stop was 15705.75 and the fill was 15714.25

            the price was never near the stop, SO WHAT GIVES

            this is an OnPriceChange Strategy

            thanks

            Last edited by DTSSTS; 07-13-2023, 04:45 PM. Reason: added order log image

            Comment


              #7
              Hello DTSSTS,

              Thank you for your reply.

              When it comes to MIT orders, a buy MIT order should be placed below the market and a sell MIT should be placed above the market. It appears that the price of the highlighted order was below the market price at the time of submission (15705.75) so it was immediately submitted and executed. You will notice that when manually trading on a chart, if you right-click to place an order from Chart Trader you will only have the option to buy MIT if you clicked above the market and sell MIT if you clicked below the market. In the SuperDOM, if you place an MIT order on the opposite side of the market on a simulation account, it will fill immediately.

              To resolve this, you will need to consider using a different order type for your Stop Loss. We have a blog post with the 3 main order types used for stop loss orders that might help to understand which order type is right for you and your strategy logic:
              Stop loss orders can help to protect trading capital by closing a position when the market turns unfavorable.


              Please let us know if we may be of further assistance.

              Comment


                #8
                Yes I did make those changes overnight - I will share what I came up with as to show for others reading this post later

                I added a Trail ATR as well to tighten my stops as the trade progress and not have just the fixed stop below the entry point

                // double atrtarget = atrnow * TargetATR;
                // double atrstop = atrnow * StopATR;
                // double atrtrail = atrnow * TrailATR;


                I hard coded my inputs for testing here but will add variables as shown above

                This is for a Strategy OnPriceChange - if you used OnBarClose you would want to change the atr[1] to atr[0] and also in the TrailStop orders Change Close[1] to Close[0]

                You do not want to use the Close[0] in the OnPriceChange as the TrailStop Order

                double atrnow = atr[1];
                double atrtarget = atrnow * 2;
                double atrstop = atrnow * 1;
                double atrtrail = atrnow * .75;

                these are the order types I used


                /// Long Target and StopLoss

                ExitLongMIT(Convert.ToInt32(Position.Quantity), (Position.AveragePrice + (atrtarget)) , @"ATR LTarget", "");
                ExitLongStopMarket(Convert.ToInt32(Position.Quanti ty), (Position.AveragePrice + (-atrstop)) , @"ATR LStop", "");

                /// TrailStop

                ExitLongStopMarket(Convert.ToInt32(Position.Quanti ty), (Close[1] + (-atrtrail)) , @"ATR LTrail", "");​

                ​/// Short Target and StopLoss

                ExitShortMIT(Convert.ToInt32(Position.Quantity), (Position.AveragePrice + (-atrtarget)) , @"ATR STarget", "");
                ExitShortStopMarket(Convert.ToInt32(Position.Quant ity), (Position.AveragePrice + (atrstop)) , @"ATR SStop", "");

                /// TrailStop

                ExitShortStopMarket(Convert.ToInt32(Position.Quant ity), (Close[1] + (atrtrail)) , @"ATR STrail", "");

                Thanks for the help and Good Trading to all​

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Yesterday, 05:17 AM
                0 responses
                64 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                139 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                75 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                45 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                50 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X