Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

scaling out

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

    scaling out

    I have a simple strategy:

    Code:
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion
    
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    /// bandom padaryt kazka prasmingo su %
    [Description("bandom padaryt kazka prasmingo su % ")]
    public class darkarta : Strategy
    {
        
    #region Variables
    // Wizard generated variables
    private double profit_Target = 0.1; // Default setting for Profit_Target
    private double stop_Loss = 0.1; // Default setting for Stop_Loss
    // User defined variables (add any user defined variables below)
    #endregion
        
    
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    
    protected override void Initialize()
    {
    
    // The following settings configure your strategy to execute only one entry for each uniquely named entry signal.
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.UniqueEntries;
        
    /* These Set methods will place Profit Target and Trail Stop orders for our entry orders.
    Notice how the Profit Target order is only tied to our order named 'Long 1a'. This is the crucial step in achieving the following behavior.
    If the price never reaches our Profit Target, both long positions will be closed via our Trail Stop.
    If the price does hit our Profit Target, half of our position will be closed leaving the remainder to be closed by our Trail Stop. */
    SetProfitTarget("Long 1a", CalculationMode.Percent, Profit_Target);
    SetTrailStop(CalculationMode.Percent, Stop_Loss);
    
    
        //show indicators on every chart in the strategy analyzer
    
    
    
    
    
        CalculateOnBarClose = true;
    }
    
    /// Called on each bar update event (incoming tick)
    
    protected override void OnBarUpdate()
    {
    
        // Condition set 1
    
        if (EMA(5)[0] > SMA(30)[0]
            && Aroon(14).Up[0] > 80
            && Aroon(14).Down[0] < 25)
            
                    
            {
            /* Enters two long positions.
            We submit two orders to allow us to be able to scale out half of the position at a time in the future.
            With individual entry names we can differentiate between the first half and the second half of our long position.
            This lets us place a Profit Target order only for the first half and Trail Stops for both. */
            EnterLong("Long 1a");
            EnterLong("Long 1b");
            }
            
            
            
        // Condition set 2
            if (EMA(5)[0] > SMA(30)[0]
            && Aroon(14).Down[0] > 80
            && Aroon(14).Up[0] < 25)
                
            {
            EnterShort(DefaultQuantity, "");
            }
    }
    #region Properties
    [Description("")]
    [GridCategory("Parameters")]
    public double Profit_Target
    {
    get { return profit_Target; }
    set { profit_Target = Math.Max(0.00, value); }
    }
    [Description("")]
    [GridCategory("Parameters")]
    public double Stop_Loss
    {
    get { return stop_Loss; }
    set { stop_Loss = Math.Max(0.00, value); }
    }
    #endregion
    }
    }
    Question 1:
    When I go to strategy analyzer/charts, why do I sometimes see the occurence of 1 long(Long 1a), instead of the expected 2 longs as usual (Long 1a + Long 1b). I noticed it happens when system exits half the position. So looks like the system trigers 1 new long (Long 1a) after profit taking and returns to the allowed 2 longs (Long 1a + Long 1b).

    Question 2:
    In the code above, where do I add this code below to ensure that once the price is greater than entry price by 12%, I want to take profit by closing the Long 1a, and for the Long 1b set the stop loss to breakeven (level where I entered with both Long 1a and Long 1b)
    Code:
    protected override void OnBarUpdate()
    {
                // Resets the stop loss to the original value when all positions are closed
                if (Position.MarketPosition == MarketPosition.Flat)
                {
                    SetStopLoss("", CalculationMode.Percent, Stop_Loss, false);
                }
                
                // If a long position is open, allow for stop loss modification to breakeven
                else if (Position.MarketPosition == MarketPosition.Long)
                {
                    // Once the price is greater than entry price by 12%, set stop loss to breakeven
                    if (Close[0] > Position.AvgPrice * 1.12)
                    {
                        SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                    }
                }
        
        // Condition set 1
    Question 3:
    Do I need all the conditions in this second code or just the one with if (Close[0] > Position.AvgPrice * 1.12)?

    Question 4:
    How do I set a different profit target (say 4%) ONLY for Long 1b?

    Question5:
    Finally, is the second code correct?, I mean would it do what I want it to do in question 2? I amended the code slightly, then went to the chart to check the result and could not see if it was actually working.


    MANY THANKS
    Last edited by ionaz; 01-29-2013, 04:39 PM.

    #2
    Hi ionaz, thanks for the post.

    1. This likely happens as you're not entering only when you're in flat state for the strategy, so currently if your first target has filled and the condition occurs again, you would see a partial entry again.

    2. The code for the stop adjustment would be in your OnBarUpdate() method as well. This is called on every bar, historical as well as realtime then.

    3. You would need the reset condition as well, as noted in our example -



    4. You would add another call in Initialize() to a SetProfitTarget tied to signal Long1b that would be targetting your 4% exit then.

    5. The code would only adjust the stop loss once your profit trigger is reached, it would not take any partial exit.

    To know and understand what your code would be exatly doing, we would suggest working with Print statements, here's a tip that would explain this helpful deubbing technique deeper - http://www.ninjatrader.com/support/f...ead.php?t=3418

    Comment


      #3
      Thank you very much for your input Bertrand

      Let me follow-up
      1. So how do I code that I'm entering only when I'm in a flat state for the strategy? What's the code for that?

      2. thx
      3. thx
      4. thx

      5. So how to code to integrate all goals successfully:
      i) take partial exit once the profit (12%) is reached,
      ii) set profit target to 4% for Long 1B,
      iii) exit Long 1B once 4% target is reached (after 12% was reached by Long 1a beforehand).

      Thank you

      Comment


        #4
        ionaz, you can check the strategy position with the MarketPosition property -



        You would now need to combine the elements we discussed into your final script and let it then run with the EntryHandling set to .UniqueEntries :

        Comment


          #5
          Thanks Bertrand

          Did everything as per your suggestion. I believe I did, compiled OK.

          However, where it doesn't behave as expected is:
          Long 1b closes before Long 1a (I can understand why - because its profit target comes first - 4% vs 12% for Long 1a).

          As I said, I want it occur in exactly this sequence:
          1) take partial exit once the target_1a (12%) is reached = exit Long_1a,
          2) set stoploss to breakeven
          3) exit Long 1B once 4% target is reached (after 12% was reached by Long 1a beforehand).

          Please help!
          Last edited by ionaz; 02-01-2013, 05:14 PM.

          Comment


            #6
            ionaz, your NinjaScript / C# Code will always be logically processed and evaluate according to your set logic – this can of course lead to unexpected results at times, thus we would suggest to simplify and debug your code to better understand the event sequence it would go through - unfortunately we cannot offer such debug or code modification services here, but please see the provided resources below to help you proceed productively :

            First of all you would want to use Print() statements to verify values are what you expect -


            For strategies add TraceOrders = true to your Initialize() method and you can then view valuable output related to strategy submitted orders through Tools > Output window - http://www.ninjatrader.com/support/f...ead.php?t=3627

            It may also help to add drawing objects to your chart for signal and condition confirmation.

            Also to consider: would the EntryHandling set allow multiple entries for your script?



            If you would prefer the debug assist of a professional NinjaScript consultant, please check into the following listings of certified partners in this area - http://www.ninjatrader.com/partners#...pt-Consultants

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            648 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            369 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            108 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            572 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            573 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X