NexusFi: Find Your Edge


Home Menu

 





Help wanted! Long/short strategy with cancel order and trail stop


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one mrticks with 4 posts (0 thanks)
    2. looks_two MWinfrey with 2 posts (4 thanks)
    3. looks_3 Trader.Jon with 2 posts (1 thanks)
    4. looks_4 traderwerks with 1 posts (0 thanks)
    1. trending_up 5,932 views
    2. thumb_up 5 thanks given
    3. group 3 followers
    1. forum 9 posts
    2. attach_file 4 attachments




 
Search this Thread

Help wanted! Long/short strategy with cancel order and trail stop

  #1 (permalink)
 
mrticks's Avatar
 mrticks 
Dublin, Ireland.
 
Experience: Advanced
Platform: NinjaTrader, TOS, Multicharts, Open Source various
Trading: FDAX, cable/yen, FX, options on commodities
Posts: 67 since Jun 2009
Thanks Given: 16
Thanks Received: 10

Hey Folks am looking for some help with the attached strategy. It does everything I want it to do but only once! It will go long/short after entry signal with a stop loss and then if it is profit will trail the stop loss at the open [1] as coded. However, once this order is stopped out it does not take any other orders.

I posted on the NT forum here Long/short strategy with cancel order and trail stop - [AUTOLINK]NinjaTrader[/AUTOLINK] Support Forum and was told I need to make sure "IOrder object is reset to null" but am not sure where to do this as I copied this strat from the NT sample strategies.

Basically, I'm looking for a strat to do the following:
1. Enter long if Close[1] > Open[1]
or
Enter short if Close[1] < Open[1]

2. Set initial stop loss to n ticks.

3. Cancel pending order if not filled after n bars.

4. If n ticks in profit then set stop loss to low/high of Open[1].


If anyone could point me in the right direction I would REALLY appreciate it!


Thanks.

Attached Files
Elite Membership required to download: TrailOneBar.zip
Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
MC PL editor upgrade
MultiCharts
Trade idea based off three indicators.
Traders Hideout
Quant vue
Trading Reviews and Vendors
Better Renko Gaps
The Elite Circle
 
  #3 (permalink)
 
Trader.Jon's Avatar
 Trader.Jon 
Near the BEuTiFULL Horse Shoe
 
Experience: Beginner
Platform: NinjaTrader
Broker: MBTrading Dukascopy ZenFire
Trading: $EURUSD when it is trending
Posts: 473 since Jul 2009
Thanks Given: 401
Thanks Received: 184


I am not a programmer, and I didnt integrate into your code, so I did add some code snippets that might help you along the road (into your own shell) .. dont try to compile, I didnt use your namespaces or ordersignal names. I hope it helps.

TJ

Attached Files
Elite Membership required to download: FDAX1LBtrail.cs.txt
Reply With Quote
Thanked by:
  #4 (permalink)
 traderwerks   is a Vendor
 
Posts: 692 since Jun 2009
Thanks Given: 436
Thanks Received: 465


mrticks View Post


If anyone could point me in the right direction I would REALLY appreciate it!


Thanks.

You have a logic error so it is not going to run the way you expect it to work.

And use Round2TickSize instead of the (7 * (TickSize / 2)) thing you are doing.

Reply With Quote
  #5 (permalink)
 
mrticks's Avatar
 mrticks 
Dublin, Ireland.
 
Experience: Advanced
Platform: NinjaTrader, TOS, Multicharts, Open Source various
Trading: FDAX, cable/yen, FX, options on commodities
Posts: 67 since Jun 2009
Thanks Given: 16
Thanks Received: 10


Trader.Jon View Post
I am not a programmer, and I didnt integrate into your code, so I did add some code snippets that might help you along the road (into your own shell) .. dont try to compile, I didnt use your namespaces or ordersignal names. I hope it helps.

TJ

Hi TJ thanks for that!

I've cobbled together another strat from your sample code, will test it now and see how it behaves. I appreciate you taking a look at this.


Thanks!

Attached Files
Elite Membership required to download: Trailing_One_Bar_prev_cancel_order.txt
Started this thread Reply With Quote
  #6 (permalink)
 
mrticks's Avatar
 mrticks 
Dublin, Ireland.
 
Experience: Advanced
Platform: NinjaTrader, TOS, Multicharts, Open Source various
Trading: FDAX, cable/yen, FX, options on commodities
Posts: 67 since Jun 2009
Thanks Given: 16
Thanks Received: 10


traderwerks View Post
You have a logic error so it is not going to run the way you expect it to work.

And use Round2TickSize instead of the (7 * (TickSize / 2)) thing you are doing.

I took out that piece of code in my latest version as I only had that in there from copying a sample snippet.

I read your blog by the way and like the sample strategies you post on the website for reference!

Started this thread Reply With Quote
  #7 (permalink)
 
Trader.Jon's Avatar
 Trader.Jon 
Near the BEuTiFULL Horse Shoe
 
Experience: Beginner
Platform: NinjaTrader
Broker: MBTrading Dukascopy ZenFire
Trading: $EURUSD when it is trending
Posts: 473 since Jul 2009
Thanks Given: 401
Thanks Received: 184


mrticks View Post
Hi TJ thanks for that!

I've cobbled together another strat from your sample code, will test it now and see how it behaves. I appreciate you taking a look at this.


Thanks!

Have you tried out your new code? I will have a quick look later if you still need that comment ... unfortunately I dont use breakeven in my code ( anytime recently ) and I may have some challenge to follow correctly.

Jon

Reply With Quote
  #8 (permalink)
 
mrticks's Avatar
 mrticks 
Dublin, Ireland.
 
Experience: Advanced
Platform: NinjaTrader, TOS, Multicharts, Open Source various
Trading: FDAX, cable/yen, FX, options on commodities
Posts: 67 since Jun 2009
Thanks Given: 16
Thanks Received: 10


Trader.Jon View Post
Have you tried out your new code? I will have a quick look later if you still need that comment ... unfortunately I dont use breakeven in my code ( anytime recently ) and I may have some challenge to follow correctly.

Jon

Hi Jon,

I tested it but alas it did not perform as I had hoped. It will enter long or short and set a stop loss and then move to breakeven but does not modify the trail stop to follow at O/H/L/C of n bars ago or

SetStopLoss( "Long", CalculationMode.Price, Open[3] + 2 * TickSize, false);

as they say in C#.

I've attached the strat if you'd like to give it a whirl and see if you can do anything with it!

 
Code
/* If we have a long position and the current price is breakeven + x ticks in profit, raise the stop-loss order to Open[3] + 2 * TickSize.
            
                */
                if (Position.MarketPosition == MarketPosition.Long && Close[0] >= Position.AvgPrice + 8 * TickSize)
            {
                // Checks to see if Close[0] is b/e + x ticks
                if (GetCurrentAsk() > previousPrice + trailProfitTrigger * TickSize)
                {
                    // Modifies stop-loss to Open[3] + 2 * TickSize
                    SetStopLoss( "Long", CalculationMode.Price, Open[3] + 2 * TickSize, false);
                }
            }
            
            /* If we have a short position and the current price is x ticks in profit, raise the stop-loss order to breakeven.
            
                */
            if (Position.MarketPosition == MarketPosition.Short && Close[0] <= Position.AvgPrice - 4 * TickSize)
            {
                // Checks to see if our Stop Order has been submitted already
                if (stopOrder != null && stopOrder.StopPrice > Position.AvgPrice)
                {
                    // Modifies stop-loss to breakeven
                    stopOrder = ExitShortStop(0, true, stopOrder.Quantity, Position.AvgPrice, "ShortStop", "Short1SB");
                }
            }

Attached Files
Elite Membership required to download: sample_trail_strat_TJ.zip
Started this thread Reply With Quote
  #9 (permalink)
 
MWinfrey's Avatar
 MWinfrey 
Lubbock TX
 
Experience: Intermediate
Platform: NinjaTrader
Broker: Stage 5 Trading
Trading: CL
Posts: 1,878 since Jul 2009
Thanks Given: 1,450
Thanks Received: 3,335


mrticks View Post
Hi Jon,

I tested it but alas it did not perform as I had hoped. It will enter long or short and set a stop loss and then move to breakeven but does not modify the trail stop to follow at O/H/L/C of n bars ago or

SetStopLoss( "Long", CalculationMode.Price, Open[3] + 2 * TickSize, false);

as they say in C#.

I've attached the strat if you'd like to give it a whirl and see if you can do anything with it!

 
Code
/* If we have a long position and the current price is breakeven + x ticks in profit, raise the stop-loss order to Open[3] + 2 * TickSize.
 
                */
                if (Position.MarketPosition == MarketPosition.Long && Close[0] >= Position.AvgPrice + 8 * TickSize)
            {
                // Checks to see if Close[0] is b/e + x ticks
                if (GetCurrentAsk() > previousPrice + trailProfitTrigger * TickSize)
                {
                    // Modifies stop-loss to Open[3] + 2 * TickSize
                    SetStopLoss( "Long", CalculationMode.Price, Open[3] + 2 * TickSize, false);
                }
            }
 
            /* If we have a short position and the current price is x ticks in profit, raise the stop-loss order to breakeven.
 
                */
            if (Position.MarketPosition == MarketPosition.Short && Close[0] <= Position.AvgPrice - 4 * TickSize)
            {
                // Checks to see if our Stop Order has been submitted already
                if (stopOrder != null && stopOrder.StopPrice > Position.AvgPrice)
                {
                    // Modifies stop-loss to breakeven
                    stopOrder = ExitShortStop(0, true, stopOrder.Quantity, Position.AvgPrice, "ShortStop", "Short1SB");
                }
            }

MrTicks, it just occured to me that you aren't an elite member. The code I'm going to share will be limited to Elite members only.

Reply With Quote
  #10 (permalink)
 
MWinfrey's Avatar
 MWinfrey 
Lubbock TX
 
Experience: Intermediate
Platform: NinjaTrader
Broker: Stage 5 Trading
Trading: CL
Posts: 1,878 since Jul 2009
Thanks Given: 1,450
Thanks Received: 3,335


Must be a member of the elite circle to download and use.



Reply With Quote
Thanked by:




Last Updated on October 26, 2010


© 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