NexusFi: Find Your Edge


Home Menu

 





Swing in a Strategy


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one MXASJ with 6 posts (0 thanks)
    2. looks_two Big Mike with 1 posts (0 thanks)
    3. looks_3 RJay with 1 posts (0 thanks)
    4. looks_4 hondo69 with 1 posts (0 thanks)
    1. trending_up 8,735 views
    2. thumb_up 0 thanks given
    3. group 4 followers
    1. forum 10 posts
    2. attach_file 4 attachments




 
Search this Thread

Swing in a Strategy

  #1 (permalink)
 MXASJ 
Asia
 
Experience: Beginner
Platform: NinjaTrader, TOS
Posts: 796 since Jun 2009
Thanks Given: 109
Thanks Received: 800

Hi All,

I've been looking at incorporating the Swing High/Low indicator in a strategy and have hit a wall. Basically what I'm trying to code is;

If Close[0] > most recent swing high

Go long. And its counterpart;

If Close[0] < most recent swing low

Go short.

I might even want to try something like;

If (most recent swing high) - (most recent swing low) < 12 ticks
and close[0] is between them

Don't trade.

Can anyone point me in the right direction? Thanks!

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Futures True Range Report
The Elite Circle
Ninja Mobile Trader VPS (ninjamobiletrader.com)
Trading Reviews and Vendors
Build trailing stop for micro index(s)
Psychology and Money Management
Deepmoney LLM
Elite Quantitative GenAI/LLM
ZombieSqueeze
Platforms and Indicators
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
60 thanks
Funded Trader platforms
43 thanks
NexusFi site changelog and issues/problem reporting
24 thanks
GFIs1 1 DAX trade per day journal
22 thanks
The Program
19 thanks
  #2 (permalink)
 
max-td's Avatar
 max-td 
Frankfurt
 
Experience: Intermediate
Platform: NinjaTrader
Trading: FGBL 6E B4
Posts: 1,752 since Jun 2009
Thanks Given: 2,309
Thanks Received: 927

hi mx,

why you have hit a wall ?
what did you try yet with the swing high/low indie ?
how you define : most recent swing high ?

max-td
Reply With Quote
  #3 (permalink)
 
RJay's Avatar
 RJay 
Hartford, CT. USA
 
Experience: Intermediate
Platform: NinjaTrader
Broker: AMP/CQG, Kinetick
Trading: RTY
Posts: 682 since Jun 2009
Thanks Given: 757
Thanks Received: 787



MXASJ View Post
Hi All,

I've been looking at incorporating the Swing High/Low indicator in a strategy and have hit a wall. Basically what I'm trying to code is;

If Close[0] > most recent swing high

Go long. And its counterpart;

If Close[0] < most recent swing low

Go short.

I might even want to try something like;

If (most recent swing high) - (most recent swing low) < 12 ticks
and close[0] is between them

Don't trade.

Can anyone point me in the right direction? Thanks!

XMASJ,

You could try some sort of compare, different but more complex.

--------------------------------------------------------------

if (Math.Max(close[0], swing high) <= close[0] {Go long}

if (Math.Min(close[0], swing low) >=close[0] {Go short}

--------------------------------------------------------------

RJay

Reply With Quote
  #4 (permalink)
 MXASJ 
Asia
 
Experience: Beginner
Platform: NinjaTrader, TOS
Posts: 796 since Jun 2009
Thanks Given: 109
Thanks Received: 800

I'm on the road now so I'll respond more clearly when I'm back.

Getting the value of most recent swing hi/lo has confused me as close[0] doesn't have a value, and there are times when no hi|lo is drawn or has a null(zero?) value for some time. I'm sure its a "duh" thing that I am missing. In RJay's example its the "swing high" and "swing low" that has me stumped.

Totally OT to my own thread () I'm trying GoToMyPC for the first time and am chuffed that I can access my three-screen trading rig back home while sitting in a hotel in a different country with my laptop.

But I digress, and the day job becons...

Started this thread Reply With Quote
  #5 (permalink)
piersh
California
 
Posts: 87 since Jun 2009
Thanks Given: 5
Thanks Received: 120


MXASJ View Post
Hi All,

I've been looking at incorporating the Swing High/Low indicator in a strategy and have hit a wall. Basically what I'm trying to code is;

If Close[0] > most recent swing high

Go long. And its counterpart;

If Close[0] < most recent swing low

Go short.

I might even want to try something like;

If (most recent swing high) - (most recent swing low) < 12 ticks
and close[0] is between them

Don't trade.

Can anyone point me in the right direction? Thanks!

Something like this?

 
Code
                            
using System;

using System.ComponentModel;
using NinjaTrader.Indicator;

namespace 
NinjaTrader.Strategy
{
    [
Description ("Swing strategy.")]
    public class 
SwingStrategy Strategy
    
{
        
Swing _swing;

        protected 
override void Initialize ()
        {
            
Add (_swing Swing (_strength));
            
CalculateOnBarClose true;
        }

        protected 
override void OnBarUpdate ()
        {
            if (
CurrentBar 1)
                return;

            
double high _swing.SwingHigh [0];    // most recent swing high
            
double low _swing.SwingLow [0];    // most recent swing low
            
double close Close [0];    // latest price

            
if (high low >= 12 TickSize)    // only trade if swing is big enough
            
{
                if (
close high)
                    
EnterLong ();
                else if (
close low)
                    
EnterShort ();
            }
        }

        [
Category ("Parameters")]
        public 
int Strength
        
{
            
get { return _strength; }
            
set _strength Math.Max (1value); }
        }
        
int _strength;
    }


Attached Thumbnails
Click image for larger version

Name:	Untitled.png
Views:	461
Size:	101.0 KB
ID:	2975  
Reply With Quote
  #6 (permalink)
 MXASJ 
Asia
 
Experience: Beginner
Platform: NinjaTrader, TOS
Posts: 796 since Jun 2009
Thanks Given: 109
Thanks Received: 800

Piresh that is very close to what I am trying to do. Before I saw your code I wrote this:

 
Code
                            
protected override void OnBarUpdate()
{
// Condition set 1 SHORT
if ((Swing(7).SwingLow[1] == 0)||((Swing(7).SwingHigh[1] - Swing(7).SwingLow[1]) >= 2.00))
{
DrawArrowDown("My down arrow" CurrentBarfalse02*TickSizeColor.Red);
}
// Condition set 2 LONG
if ((Swing(7).SwingHigh[1] == 0)||((Swing(7).SwingHigh[1] - Swing(7).SwingLow[1]) >= 2.00))
{
DrawArrowUp("My up arrow" CurrentBarfalse02*TickSizeColor.Lime);
}

Which doesn't work. You will see I'm trying an OR condition as well. The logic is;

The swing range needs to be wide enough OR the current SwingLow is 0 for shorts

The swing range needs to be wide enough OR the current SwingHigh is 0 for longs

I'll play some more and post it back!

EDIT TO ADD: For newcomers to Ninja Script [I include myslef in that comment!] you will see I'm making to simple mistakes in the code that I'm fixing as I go along. Look at the difference in the draw arrow code from this to the next.

Started this thread Reply With Quote
  #7 (permalink)
 MXASJ 
Asia
 
Experience: Beginner
Platform: NinjaTrader, TOS
Posts: 796 since Jun 2009
Thanks Given: 109
Thanks Received: 800

Some progress... but I'm getting up and down arrows on every bar. My broker would love me but it's not quite right;
 
Code
                            
protected override void Initialize()
{
Add (_swing Swing (_strength));
CalculateOnBarClose true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
 
if (
CurrentBar 1)
return;
 
double high _swing.SwingHigh[0]; // Most recent swing high (?)
double low _swing.SwingLow[0]; // Most recent swing low (?)
double close Close[0]; // latest price
 
// Condition set 1 SHORT
if ((high low >= 20TickSize));
{
DrawArrowDown("My down arrow" CurrentBarfalse0High[0]+(2*TickSize), Color.Red);
}
// Condition set 2 LONG
if ((high low >= 20TickSize));
{
DrawArrowUp("My up arrow" CurrentBarfalse0Low[0]+(-2*TickSize), Color.Lime);
}
}
#region Properties
[Description("")]
[
Category("Parameters")]
public 
int Strength
{
get { return _strength; }
set _strength Math.Max (7value); }
}
int _strength
I upped the value to 20*TickSize just to try it, but still not there...

EDIT: Long arrows are now being drawn 2 ticks below the low of the bar, and short arrows two tick above the highs of the bar. Doh!

Started this thread Reply With Quote
  #8 (permalink)
 MXASJ 
Asia
 
Experience: Beginner
Platform: NinjaTrader, TOS
Posts: 796 since Jun 2009
Thanks Given: 109
Thanks Received: 800

Added a condition to see where it was doing what;

 
Code
                            
// Condition set 1 SHORT
if (high low >= 12TickSize
&& close low
)
{
DrawArrowDown("My down arrow" CurrentBarfalse0High[0]+(2*TickSize), Color.Red);
}
// Condition set 2 LONG
if (high low >= 12TickSize
&& close high
)
{
DrawArrowUp("My up arrow" CurrentBarfalse0Low[0]+(-2*TickSize), Color.Lime);

If memory serves this is where I gave up when a first looked at Swing a few months back. I think the value of swing hight or low is zero some times and that throws the logic out of whack.

Started this thread Reply With Quote
  #9 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,399 since Jun 2009
Thanks Given: 33,175
Thanks Received: 101,541

Always use Print statements to see what the data looks like if you are having trouble. Open the output window.

You might want to only know when a new Swing high or low has been formed, which you need to write some code to detect.

Mike

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #10 (permalink)
 MXASJ 
Asia
 
Experience: Beginner
Platform: NinjaTrader, TOS
Posts: 796 since Jun 2009
Thanks Given: 109
Thanks Received: 800


Printin' as we speak

 
Code
                            
// Condition set 1 SHORT
if (high low >= 12TickSize
&& close low
)
{
DrawArrowDown("My down arrow" CurrentBarfalse0High[0]+(2*TickSize), Color.Red);
Print (
Time[0] + " The Swing High value is: " high);
Print (
Time[0] + " The Swing Low value is: " low);
}
// Condition set 2 LONG
if (high low >= 12TickSize 
&& close high
)
{
DrawArrowUp("My up arrow" CurrentBarfalse0Low[0]+(-2*TickSize), Color.Lime);
Print (
Time[0] + " The Swing High value is: " high);
Print (
Time[0] + " The Swing Low value is: " low);

What appears to be happening is it only trades "breakouts" from the swing zone (fair enough, based on the entry trigger), but the 12 tick filter stays in place even if no new swing line is drawn on the chart by the indicator. What that means is if we are in a 14 tick swing range and it breaks out, a trade is put on. If we are in a 12 tick or less range and the price breaks out, it will NOT trade until a new swing range greater than 12 is drawn. So some of those lovely swing breakouts that keep climbing up or down are NOT traded if the previous swing range was <12... until they top or bottom out and a new swing range >12 is drawn by the indicator. Hmmm ...

Attached Thumbnails
Click image for larger version

Name:	Wide Range.jpg
Views:	316
Size:	102.8 KB
ID:	3026   Click image for larger version

Name:	Narrow Range.jpg
Views:	241
Size:	97.5 KB
ID:	3027  
Started this thread Reply With Quote




Last Updated on October 3, 2009


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts