Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to scale out of a position

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

    How to scale out of a position

    After lurking around a bit there seems to be a lot of questions regarding how to scale out of a position and apparently you have to scale in to scale out etc but here's another way to do it. Sorry if a re-post of something from before.

    here's some code to get you thinking that does work. i can give you part but not all of the strategy obviously. My target exits are predicted automatically for me and are dynamic based on realtime calculations. Each target is then stored in its own variable. it'll be up to you to calculate targets on your own however you want.

    1 big position is entered when conditions are met. It'll stop out if the function that monitors risk indicate it should exit. thats a separate function all together and never places hard stops or trails etc (why show your hand to the MMs and level 3 only to have them sniff it out and trigegr it?) as a result i use mkt orders for everything and to get fills right way. Just keep in mind this is a completely separate function of the strategy

    I'm not a coder so this may be the most inefficient thing in the world, but it works.

    public class Scaleout: Strategy
    {
    #region Variables
    private double target1 = 0;
    private double target2 = 0;
    private double target3 = 0;

    private double basket = 0;
    private double exitsize = 0;
    private int counter = 0;
    #endregion


    protected override void Initialize()
    {

    counter = 0;

    }

    protected override void OnBarUpdate()
    {
    target1 = /* your mathmatics here to calculate in realtime, or enter a number in the strat properties if you want. its up to you*/;
    target2 = /* your mathmatics here to calculate in realtime, or enter a number in the strat properties if you want. its up to you*/;
    target3 = /* your mathmatics here to calculate in realtime, or enter a number in the strat properties if you want. its up to you*/;

    //See what the current position size is
    if (Position.MarketPosition == MarketPosition.Long)
    {
    basket = Position.Quantity;
    }

    //scale out 1/3 on target 1 hit
    if (High[0] >= target1 && counter == 0 && Position.MarketPosition == MarketPosition.Long)
    {
    exitsize = basket * 0.33;
    exitsize = Math.Round(exitsize);
    ExitLong((int)exitsize);
    counter = 1;
    }

    //scale out next 2/3 on target 2 hit
    if (High[0] >= target2 && counter == 1 && Position.MarketPosition == MarketPosition.Long)
    {
    exitsize = Position.Quantity / 2; /* "basket / 2" didnt work for some reason */
    exitsize = Math.Round(exitsize);
    ExitLong((int)exitsize);
    counter = 2;
    }

    //Exit remainder and quit on target high hit
    if (High[0] >= target3 && counter == 2 && Position.MarketPosition == MarketPosition.Long)
    {
    counter = 3;
    StopStrategy();
    return;
    }

    }

    private void StopStrategy()
    {
    if (Position.MarketPosition == MarketPosition.Long)
    ExitLong();
    else if (Position.MarketPosition == MarketPosition.Short)
    ExitShort();
    }



    "stopping out" is handled completely separately and you can build that however you want. If it does get stopped out after it has already started scaling out, and winds up entering again later, the counter is reset to zero and the scaling out begins again with target 1, 2 etc. Hopefully you found this to be worthwhile t least for idea generation. If you improve upon the general concept, feel free to post your revisions.

    Last edited by tortexal; 04-03-2009, 04:54 PM.

    #2
    Originally posted by tortexal View Post
    I'm not a coder so this may be the most inefficient thing in the world, but it works.
    I am personally like it tortexal, because it is a clear concept. The reason why "basket / 2" doesn't work is because you didn't adopt the value of basket after first scale-out. But for the second scale-out you don't use this variable anyway, so you don't need it for the first scale-out either. You could code something like this:

    ExitLong((int)Math.Round(Position.Quantity * 0.33));
    ...
    ExitLong((int)Math.Round(Position.Quantity / 2));
    ...
    StopStrategy();

    Regards
    Ralph

    Comment


      #3
      Thats awesome! the 2nd part
      ExitLong((int)Math.Round(Position.Quantity / 2));
      errors when compiling, something about converting decimal to double.

      However:
      ExitLong((int)Math.Round(Position.Quantity * 0.5));

      compiles w/o a problem

      Comment


        #4
        Yes sounds funny. Math.Round allows decimal and double as parameters only, but Position.Quantity is an integer. Therefore you made the right choice: *0.5 or /2.0

        Regards
        Ralph

        Comment


          #5
          Scale out code

          I am trying to use your scale out method that you poased on 4/9/2009 and I get the scale out code to compile but it will not work. Can you help ?I am not a programmer so please explain in English.
          Thanks for your help

          Billo

          Comment


            #6
            add :

            TraceOrders = true;

            to the initialize() section and look in the output window for errors. post them back here if there are. if so, we need to scale in and out the original way

            Comment


              #7
              scale out

              4/25/2009 10:42:31 AM Entered InternalSetStop()Target method: Type=Stop FromEntrySignal=Mode=Ticks Value=50 Currency=0 Simulated=False

              This Statement is repeated 3 times. I am sorry i have no idea what this means or how to correct it.

              Thanks
              Billo

              Comment


                #8
                i should have clarified, run the strat on the market replay and note any errors.

                Comment


                  #9
                  Here's the general idea of the old way. how you handle being stopped out is up to you. Another very important thing to note.

                  DO NOT run 1 strat on multiple instruments at the same time when scaling out. The reason is, you need separate unique names for each entry for each instrument. If you use the same strat w/ the same "a, b, c" name for each instrument, NT will crash and or not properly exit at times bc it has trouble differentiating between position "a" on instrument X vs Y vs Z". I found this out from back testing other parts of my strat in mkt replay over and over and over again until i noticed it at times trip up on exits. So have a separate strat for each instrument with its own unique identifiers for that instrument :

                  #region Variables

                  private double target1 = 0;
                  private double target2 = 0;
                  private double target3 = 0;
                  private int counter = 0;
                  private bool entrySubmit = false;
                  #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()
                  {

                  EntriesPerDirection = 4;
                  EntryHandling = EntryHandling.UniqueEntries;
                  CalculateOnBarClose = false;
                  counter = 0;
                  TraceOrders = true;

                  }

                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {

                  target1 = /* your mathmatics here to calculate in realtime, or enter a number in the strat properties if you want. its up to you*/;
                  target2 = /* your mathmatics here to calculate in realtime, or enter a number in the strat properties if you want. its up to you*/;
                  target3 = /* your mathmatics here to calculate in realtime, or enter a number in the strat properties if you want. its up to you*/;



                  //scale out 1/3 on target1 hit
                  if (Position.MarketPosition == MarketPosition.Long && High[0] >= target1 && counter == 0)
                  {
                  ExitLong("a");
                  counter = 1;
                  }
                  //scale out 2/3 on target2 hit
                  if (Position.MarketPosition == MarketPosition.Long && High[0] >= target2 && counter == 1)
                  {
                  ExitLong("b");
                  counter = 2;
                  }

                  //Exit remainder and quit on target3 hit
                  if (Position.MarketPosition == MarketPosition.Long && High[0] >= target3)
                  {
                  entrySubmit = true;
                  StopStrategy();
                  return;
                  }

                  //enter the position:
                  if (zyx = yourcritearia && entrySubmit == false)
                  {
                  EnterLong((int)YourSizeHere, "a");
                  EnterLong((int)YourSizeHere, "b");
                  EnterLong((int)YourSizeHere, "c");
                  entrySubmit = true;
                  counter = 0;
                  }


                  }

                  private void StopStrategy()
                  {
                  if (Position.MarketPosition == MarketPosition.Long)
                  ExitLong();
                  }
                  Last edited by tortexal; 04-25-2009, 11:36 AM.

                  Comment


                    #10
                    Scale Out

                    Hello Again

                    Many thanks for all your help today.is there any reason this strategy will only run on range charts ?I can not get the strategy to run on any of the Minute charts.Will I have to duplicate the buy conditions for the sell side if I want this to execute on the sell side ? Is there no way to buy a 3 lot and scale out 1 at a time from this 3 lot buy ? This puts a lot of unnecessary buys on the charts.How do I copy a blank Strategy window to create different strategies for each instrument such as the E-Mini
                    Nasdaq etc. or do i just have to create a basic strategy wizard and copy from the other strategy.Again many thanks for all of your help today.I would have been a long time resolving this issue.Thanks again

                    Billo

                    Comment


                      #11
                      Originally posted by billo View Post
                      Hello Again

                      Many thanks for all your help today.is there any reason this strategy will only run on range charts ?I can not get the strategy to run on any of the Minute charts.
                      i dont know what a range chart is, i run the above code on 4 1 min charts at the same time

                      Will I have to duplicate the buy conditions for the sell side if I want this to execute on the sell side ?
                      if you are shorting, yes everything will be reversed. you will make 3 sell orders and then one by one buy to cover

                      Is there no way to buy a 3 lot and scale out 1 at a time from this 3 lot buy ?
                      i may not understand what you mean. the above code makes 3 orders (3 separate lots) and sells them off one by one

                      This puts a lot of unnecessary buys on the charts.
                      if the original code at the beginning of the thread doesn't work with making one big order and scaling out, the only alternative is making 3 smaller separate orders. and selling one by one to scale out

                      How do I copy a blank Strategy window to create different strategies for each instrument such as the E-Mini
                      Nasdaq etc. or do i just have to create a basic strategy wizard and copy from the other strategy.
                      tools->new ninja script-> new strategy

                      Again many thanks for all of your help today.I would have been a long time resolving this issue.Thanks again

                      Billo
                      no pob happy to help, hope that stuff can help

                      Comment


                        #12
                        scale out

                        Hello Again

                        I have trried touse the scale out coding we worked on Saturdaybut I am not able touse the scale out feature.
                        I am getting totally crazy results on 2 different computers.The first computer appeared to be working correctly when i talked with you on saturday but i have only signal on friday and none since.The second computer is generating multiple buy signals.Can you point me to something to try and resole this problem.
                        As always thanks for your help.

                        Billo

                        Comment


                          #13


                          is the original code the second sample in this post is from. that may help

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                          0 responses
                          558 views
                          0 likes
                          Last Post Geovanny Suaza  
                          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                          0 responses
                          324 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by Mindset, 02-09-2026, 11:44 AM
                          0 responses
                          101 views
                          0 likes
                          Last Post Mindset
                          by Mindset
                           
                          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                          0 responses
                          545 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by RFrosty, 01-28-2026, 06:49 PM
                          0 responses
                          547 views
                          1 like
                          Last Post RFrosty
                          by RFrosty
                           
                          Working...
                          X