NexusFi: Find Your Edge


Home Menu

 





Strategy + indicator coding - samples / examples


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one mrticks with 7 posts (0 thanks)
    2. looks_two max-td with 7 posts (6 thanks)
    3. looks_3 MXASJ with 3 posts (3 thanks)
    4. looks_4 cclsys with 2 posts (0 thanks)
    1. trending_up 19,772 views
    2. thumb_up 9 thanks given
    3. group 2 followers
    1. forum 19 posts
    2. attach_file 1 attachments




 
Search this Thread

Strategy + indicator coding - samples / examples

  #11 (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 mrticks,
see MXASJs post.

max-td
Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
REcommedations for programming help
Sierra Chart
ZombieSqueeze
Platforms and Indicators
How to apply profiles
Traders Hideout
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
MC PL editor upgrade
MultiCharts
 
  #12 (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


max-td View Post
hi mrticks,
it should work like this with all three together.

I have a break-even component in there also. Maybe that is my problem. I'll go over a few strategies from other people and see how they have coded it out.


Thanks for the response.

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


The SetTrailStop() method can NOT be used concurrently with the [COLOR=#0000ff]SetStopLoss()[/COLOR] method for the same position, if both methods are called for the same position (fromEntrySignal) the SetStopLoss() will always take precedence. You can however, use both methods in the same strategy if they reference different signal names.

-NT 6 Help

Reply With Quote
Thanked by:
  #14 (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


MXASJ View Post
The SetTrailStop() method can NOT be used concurrently with the [COLOR=#0000ff]SetStopLoss()[/COLOR] method for the same position, if both methods are called for the same position (fromEntrySignal) the SetStopLoss() will always take precedence. You can however, use both methods in the same strategy if they reference different signal names.

-NT 6 Help

Thanks MXAJ, good to know, appreciate it.

Reply With Quote
  #15 (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


max-td View Post
here some snippets for

StopLoss
ProfitTarget
TrailStop

in strategys

 
Code
                            
    protected override void Initialize()

        {
            
// Submit stop-loss and profit target orders


            
SetStopLoss(CalculationMode.Ticks5);

            
SetProfitTarget(CalculationMode.Ticks10);

           
SetTrailStop(CalculationMode.Ticks40);
            
            
CalculateOnBarClose true;
            

        } 
https://www.ninjatrader-support.com/HelpGuideV6/helpguide.html?SetProfitTarget

https://www.ninjatrader-support.com/HelpGuideV6/helpguide.html?SetTrailStop

https://www.ninjatrader-support.com/HelpGuideV6/helpguide.html?SetStopLoss


Attention :
The SetTrailStop() method can NOT be used concurrently with the SetStopLoss() method for the same position, if both methods are called for the same position (fromEntrySignal) the SetStopLoss() will always take precedence. You can however, use both methods in the same strategy if they reference different signal names.


Here is the code that I had:

 
Code
                            
#region Breakeven and Trailing Stops
            // Resets the stop loss to the original value when all positions are closed
               
switch (Position.MarketPosition)
            {
                case 
MarketPosition.Flat:
                    
SetStopLoss(CalculationMode.TicksstopLoss);
                    
previousPrice 0;
                    break;
                case 
MarketPosition.Long:
                    
// Once the price is greater than entry price+ breakEvenTicks ticks, set stop loss to breakeven
                    
if (Close[0] > Position.AvgPrice breakEvenTicks TickSize
                        
&& previousPrice == 0)
                    {
                        
initialBreakEven Position.AvgPrice plusBreakEven TickSize;
                        
SetStopLoss(CalculationMode.PriceinitialBreakEven);
                        
previousPrice Position.AvgPrice;
                        
PrintWithTimeStamp("previousPrice = "+previousPrice);
                    }
                    
// Once at breakeven wait till trailProfitTrigger is reached before advancing stoploss by trailStop size step
                    
else if (previousPrice    != ////StopLoss is at breakeven
                             
&& GetCurrentAsk() > previousPrice trailProfitTrigger TickSize
                        
)
                    {
                        
newPrice previousPrice trailStop TickSize;
                        
SetStopLoss(CalculationMode.PricenewPrice);
                        
previousPrice newPrice;
                        
PrintWithTimeStamp("previousPrice = "+previousPrice);
                    }
                    break; 
My entries are beautiful on the strategy!!!! Just can't get the trailing stop working. It takes the stop loss and initial breakeven but doesn't trail. When I get the code for trailing stop then I'll post it.

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

You need to get rid of the SetStopLoss code and use SetTrailStop I believe. I'll post an example later.

EDIT: I found this in my 6.5 folder and if memory serves the SetTrailStop function worked in this strategy both short and long. It's sloppy but might give you some ideas.

Attached Files
Elite Membership required to download: ZN_v1_Backtest.cs
Reply With Quote
Thanked by:
  #17 (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


MXASJ View Post
You need to get rid of the SetStopLoss code and use SetTrailStop I believe. I'll post an example later.

EDIT: I found this in my 6.5 folder and if memory serves the SetTrailStop function worked in this strategy both short and long. It's sloppy but might give you some ideas.

Thanks MXASJ, I'll take a look at that template. Appreciate the response.

Reply With Quote
  #18 (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


MXASJ View Post
You need to get rid of the SetStopLoss code and use SetTrailStop I believe. I'll post an example later.

EDIT: I found this in my 6.5 folder and if memory serves the SetTrailStop function worked in this strategy both short and long. It's sloppy but might give you some ideas.

I got exactly what I wanted from that, perfect entries and spot on trail stops, so am going to modify a breakeven to it and see how it runs.

 
Code
                            
SetTrailStop(CalculationMode.TicksStop);
EnterLongStop(ContractAmountHigh[0] + PrevBarPlusTicks TickSize"Long Entry"); 

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

Glad it helped! In terms of breakeven you can do that with your target/trail in some ways. A two contract ZN example with a target on the first contract @ 5 ticks and an 8 tick trail on both contracts becomes a "breakeven" trade once that target is hit (ignoring slippage).

Reply With Quote
  #20 (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



MXASJ View Post
Glad it helped! In terms of breakeven you can do that with your target/trail in some ways. A two contract ZN example with a target on the first contract @ 5 ticks and an 8 tick trail on both contracts becomes a "breakeven" trade once that target is hit (ignoring slippage).

Thanks. Will hopefully get some code up here later with a breakeven type method on it. Depending on how the day goes!

Reply With Quote




Last Updated on December 2, 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