Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help with Strategy Creation

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

    Help with Strategy Creation

    I know zip about script/code. I've been playing around with the NT7 strategy generator, but I'm obviously doing something terribly wrong. So if anyone can help I would appreciate it. Here's what I'm after on an ES long for example (short would be exactly opposite):

    Using a range chart set to range value of 4, I have 2 ema's - 50 ema (in yellow) & 150 ema (in red). I want to use the red line as my entry for long (and short), and I want to use the yellow line as my exit once the trade has been triggered. My entries are the same long or short: 2 complete bars (not touching the red line) +1 tick above (or below for short) to trigger long (short). Once triggered I want to have an OCO entry with profit target as 2 complete bars below the yellow line for longs & stop loss as 3 complete bars +1 tick below red for longs. I've attached a picture that shows both long & short entries.
    Click image for larger version

Name:	ES RANGE LONG ENTER EXIT EXAMPLE.jpg
Views:	1
Size:	155.2 KB
ID:	900057
    Click image for larger version

Name:	ES RANGE SHORT ENTER EXIT EXAMPLE.jpg
Views:	1
Size:	139.9 KB
ID:	900058

    I've also included the current code, which I know is messed up big time.
    REDLINE CODE.txt


    Any help would be appreciated. Thanks.

    #2
    Hi MFinMO,

    Thanks for posting,

    I took a brief look at your code and saw that you declare ExitLong and ExitShort with integer variables. NinjaScript already uses these two as exit orders for the programming.

    Changing them to a different name such as ExitLongOrder and ExitShortOrder will clean up the code.

    Additionally, you have a 'LongSignal' that is being checked for a crossabove against an EMA. Your 'LongSignal' has a value of 3, which in this statement checks if 3 crosses above the EMA within the last bar. This could be problematic in your checking when to enter a long trade.

    Your enter short entry is also different than the Long. What I mean is that you don't check if it crosses but rather if its less than.

    The last thing is you want to set an Entry Signal name for the SetStopLoss and SetProfitTarget for your long and short orders if you want them to be different.

    I have linked a page on the 'CrossAbove' method from our online help guide.
    http://www.ninjatrader.com/support/h...crossabove.htm

    I'm also linking how to enter and exit orders using a managed approach from the help guide.
    http://www.ninjatrader.com/support/h...d_approach.htm

    Please let me know if I can be of further assistance.
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Lol, I'm truly an idiot when it comes to this stuff. I still can't get it to work right. I know this has to be a simple entry, but I'll be damned if I can figure it out.

      I'm not sure if maybe it's the Range chart that screwing this up or my incompetence (I'll go with the latter). Forget the short part of it for now. I just want to try a long strategy off of a Range chart with a value set to 4 that enters long when 2 full bars +1 tick (or 9 ticks on the ES) are above a 150 period ema; then I want to exit the long when 2 full bars +1 tick (9 ticks on ES) are below a 50 period ema; and I want a stop loss of 5 bars, or 20 ticks. Simple. But I'm too stupid to figure it out. Can anyone help? Thanks.

      Comment


        #4
        Originally posted by MFinMO View Post
        Lol, I'm truly an idiot when it comes to this stuff. I still can't get it to work right. I know this has to be a simple entry, but I'll be damned if I can figure it out.

        I'm not sure if maybe it's the Range chart that screwing this up or my incompetence (I'll go with the latter). Forget the short part of it for now. I just want to try a long strategy off of a Range chart with a value set to 4 that enters long when 2 full bars +1 tick (or 9 ticks on the ES) are above a 150 period ema; then I want to exit the long when 2 full bars +1 tick (9 ticks on ES) are below a 50 period ema; and I want a stop loss of 5 bars, or 20 ticks. Simple. But I'm too stupid to figure it out. Can anyone help? Thanks.
        You realize of course that your description means that almost always an exit will be followed by an immediate reentry?

        Comment


          #5
          Then I'm describing it incorrectly? After looking back at the description I caught what you were talking about. I need to somehow add that after the exit signal is triggered, a long signal can only be triggered again AFTER the price falls below or touches (equal to) the 150 ema. Thanks for catching that. Any idea how to make it happen on the strategy template?

          Comment


            #6
            Originally posted by MFinMO View Post
            Then I'm describing it incorrectly? After looking back at the description I caught what you were talking about. I need to somehow add that after the exit signal is triggered, a long signal can only be triggered again AFTER the price falls below or touches (equal to) the 150 ema. Thanks for catching that. Any idea how to make it happen on the strategy template?
            Here is some quick-and-dirty code that does what you describe. It will need quite a bit of wwork to be ready for prime time, as it has practically no error checking, and some things are defined in OBU that are better defined as class variables and assigned values in OnStartUp().
            Code:
                    #region Variables
                    private int referenceRange = 4; // Default setting for ReferenceRange
                    private int fastPeriod = 50; // Default setting for FastPeriod
                    private int slowPeriod = 150; // Default setting for SlowPeriod
                    private int stopLossMultiplier = 5; // Default setting for StopLossMultiplier
                    private int entriesMultiplier = 2; // Default setting for EntriesMultiplier
                    private int exitsMultiplier = 2; // Default setting for ExitsMultiplier
              
                    private int TradeQuantity = 1;
                    private bool TradeEnabledLong = false;
                    private bool TradeEnabledShort = false;
                    #endregion
            Code:
                    protected override void Initialize()
                    {
                        Add(EMA(FastPeriod));
                        Add(EMA(SlowPeriod));
                        SetStopLoss("LongEntry", CalculationMode.Ticks, 20, false);
                        TradeQuantity = 1;
                        CalculateOnBarClose = true;
                        EntriesPerDirection = 1;
               
                        EMA(SlowPeriod).Plots[0].Pen.Color = Color.Blue;
                        EMA(slowPeriod).Plots[0].Pen.Width = 3;
                    }
            Code:
                    protected override void OnBarUpdate()
                    {
            	if (CrossAbove(Close, EMA(this.SlowPeriod), 1))
            	        {
            		this.TradeEnabledLong = true; //we can trade long only after we cross above the SlowEMA.
            	        }
            			
                        // Entry Handling
            	double entryPriceLong = Instrument.MasterInstrument.Round2TickSize(EMA(SlowPeriod)[0]) + (this.ReferenceRange * this.EntriesMultiplier + 1) * TickSize;
            
            	if (Position.MarketPosition != MarketPosition.Long
            	        && this.TradeEnabledLong
                        	        && Close[0] >= entryPriceLong)
                                        {
            	                int stopLossTicks = this.ReferenceRange * this.ExitsMultiplier + 1;
                        	                SetStopLoss("LongEntry", CalculationMode.Ticks, stopLossTicks, false);
                                         EnterLongLimit(TradeQuantity, entryPriceLong, "LongEntry");
            	                DrawArrowUp(CurrentBar.ToString(), false, 0, Low[0] - 2 * TickSize, Color.Lime);
                                        }
            			
            	// Disable trades while we are in a long position. Will only be reenabled per
            	// above CrossAbove statement
            	if (Position.MarketPosition == MarketPosition.Long) this.TradeEnabledLong = false;
            
                        // Exit Handling
                        double exitPriceLong = Instrument.MasterInstrument.Round2TickSize(EMA(FastPeriod)[0]) - (this.ReferenceRange * this.EntriesMultiplier + 1) * TickSize;
                        if (Position.MarketPosition == MarketPosition.Long
                        	  && Close[0] <= exitPriceLong)
                                  {
                                        ExitLong("LongExit", "LongEntry");
                                  }
            			
                        if (Position.MarketPosition == MarketPosition.Flat) //reset when flat
            	  {
                        	        SetStopLoss("LongEntry", CalculationMode.Ticks, 20, false);
            	  }
                    }
            Code:
                    #region Properties
                    [Description("Reference range used to calculate entry and exit prices.")]
                    [GridCategory("Parameters")]
                    public int ReferenceRange
                    {
                        get { return referenceRange; }
                        set { referenceRange = Math.Max(1, value); }
                    }
            
                    [Description("Period of moving average used for exits.")]
                    [GridCategory("Parameters")]
                    [Gui.Design.DisplayName("EMA-ExitFilter")]
                    public int FastPeriod
                    {
                        get { return fastPeriod; }
                        set { fastPeriod = Math.Max(1, value); }
                    }
            
                    [Description("Period of moving average used for entries.")]
                    [GridCategory("Parameters")]
                    [Gui.Design.DisplayName("EMA-EntryFilter")]
                    public int SlowPeriod
                    {
                        get { return slowPeriod; }
                        set { slowPeriod = Math.Max(1, value); }
                    }
            
                    [Description("Multiplier applied to reference range to determine StopLoss.")]
                    [GridCategory("Parameters")]
                    [Gui.Design.DisplayName("Multiplier-StopLoss")]
                    public int StopLossMultiplier
                    {
                        get { return stopLossMultiplier; }
                        set { stopLossMultiplier = Math.Max(1, value); }
                    }
            
                    [Description("Multiplier applied to reference range to determine entries. Entry is one tick beyond the calculated value.")]
                    [GridCategory("Parameters")]
                    [Gui.Design.DisplayName("Multiplier-Entries")]
                    public int EntriesMultiplier
                    {
                        get { return entriesMultiplier; }
                        set { entriesMultiplier = Math.Max(1, value); }
                    }
            
                    [Description("Multiplier applied to reference range to determine exits. Exit is one tick beyond the calculated value.")]
                    [GridCategory("Parameters")]
                    [Gui.Design.DisplayName("Multiplier-Exits")]
                    public int ExitsMultiplier
                    {
                        get { return exitsMultiplier; }
                        set { exitsMultiplier = Math.Max(1, value); }
                    }
                    #endregion

            Comment


              #7
              Wow, thanks so much! I'll plug & play with this tonight!

              Comment


                #8
                Well, it's still not quite right. I ran the backtest and it only had 2 trades from 1-1-13 to 4-12-13. The 1st entry long was at 05:10 on 1/2/13 and the exit was 15 minutes later at 05:25 1/2/13; followed by another long entry 1 minute later at 05:26 on 1/2/13....this trade exit happened on this Friday 4/12/13 at the close.

                I love the winning %, , but how can I tweak this to more closely match the picture I posted above in my initial posting?
                Attached Files

                Comment


                  #9
                  Originally posted by MFinMO View Post
                  Well, it's still not quite right. I ran the backtest and it only had 2 trades from 1-1-13 to 4-12-13. The 1st entry long was at 05:10 on 1/2/13 and the exit was 15 minutes later at 05:25 1/2/13; followed by another long entry 1 minute later at 05:26 on 1/2/13....this trade exit happened on this Friday 4/12/13 at the close.

                  I love the winning %, , but how can I tweak this to more closely match the picture I posted above in my initial posting?
                  Show the chart, marked to where there was supposed to be a trade and there was not.

                  Comment


                    #10
                    Ok, here's the chart with my ema's on it & I've marked where the trades should've occurred and I've got the corresponding chart from the strategy backtest run (it was actually on the 2nd not the 3rd). I've also included 2 charts from a week later that give an example of where my ema's would've shown to take the trade and exit vs. the strategy backtest run that shows no trades were put on.
                    Attached Files

                    Comment


                      #11
                      Actually, the first example is my chart showing that no trade would've have been put on vs. the strategy showing where the backtest put a trade on. The last 2 charts are an example of where trades should've been put vs. the strategy.

                      Comment


                        #12
                        Originally posted by MFinMO View Post
                        Ok, here's the chart with my ema's on it & I've marked where the trades should've occurred and I've got the corresponding chart from the strategy backtest run (it was actually on the 2nd not the 3rd). I've also included 2 charts from a week later that give an example of where my ema's would've shown to take the trade and exit vs. the strategy backtest run that shows no trades were put on.
                        I am totally at a loss to undertstand what you are showing here. You show me 3 charts, all of which do not have any strategy loaded. How can you expect trades to be taken, backtest or otherwise, if the strategy has not been applied to the chart? The one picture that has a strategy showing on it, does have a strategy applied that is very obviously not what I wrote, as there is not even a semblance of similarity to the Properties that I exposed in my code. What comment are you seeking on that? I cannot possibly know what is in that code, as I have never seen it.

                        Comment


                          #13
                          There are 4 charts that I posted. Charts 2 & 4 are with your strategy, charts 1 & 3 are from my charts? I copied your code & pasted it into my original code? Maybe I made a mistake copying? I've included the code for the strategy that was run (which I think is what you wrote).
                          Attached Files

                          Comment


                            #14
                            To clarify just a little: charts 1 & 3 are from my charts, but they are the mirror images & time periods for charts 2 & 4 (which are the strategy generated charts with the above code run on them).

                            Comment


                              #15
                              Originally posted by MFinMO View Post
                              There are 4 charts that I posted. Charts 2 & 4 are with your strategy, charts 1 & 3 are from my charts? I copied your code & pasted it into my original code? Maybe I made a mistake copying? I've included the code for the strategy that was run (which I think is what you wrote).
                              In that case you have to crosscheck what you are doing. If a strategy is loaded, it will show as a Printout in the area at the top of the chart window. If there is nothing there, no strategy has been loaded. There is nothing showing that a strategy has been loaded on chart 4 that you posted.

                              As far as chart 2, the text file that you posted clearly shows the names of the Properties. The names of the properties in your posted chart do not match the names of the properties in the text file that you posted. That means that you have not loaded the correct strategy. You can try to reload it, or you can restart NT and see if you can load it properly. The evidence is clear that you have not loaded the strategy that is in the text that you posted: it is the only explanation of the lack of identity in Property descriptions.
                              Last edited by koganam; 04-13-2013, 08:22 PM. Reason: Corrected spelling and grammar.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Tradereh2020, Today, 06:29 AM
                              1 response
                              9 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Started by ryan_21, Yesterday, 08:46 PM
                              2 responses
                              6 views
                              0 likes
                              Last Post ryan_21
                              by ryan_21
                               
                              Started by Salahinho99, 05-05-2024, 04:13 AM
                              8 responses
                              65 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by Seneca, 08-25-2020, 08:31 AM
                              3 responses
                              5,946 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Started by slightly, Today, 12:49 AM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Working...
                              X