![]() |
This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com
|
|||||||
| General Programming General NinjaScript programming questions. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Member
Join Date: Apr 2008
Location: Fenton, MI
Posts: 33
Thanks: 0
Thanked 0 times in 0 posts
|
Im tyring to program a Strategy that enters a long position in two separate pairs and exits both pairs when a certian profit target (dollar amount of unrealized P&L) is achieved.
I think I may be close but on back tests is only entering the primary pair and not the secondary (it should enter both at the same time and exit both at the same time). It does seem to be exiting when the profit target is achieved but im not sure if it will work when both pairs are entered properly. If anyone can look at the following and give some tips I would really apprieciat it. Please excuse my ignorance, this is my first try at this. Thank You, #region Variables // Wizard generated variables // User defined variables (add any user defined variables below) #endregion ///<summary> /// This method is used to configure the strategy and is called once before any strategy method is called. ///</summary> protectedoverridevoid Initialize() { // Add an $USDCHF 2 minute Bars object to the strategy Add("$USDCHF", PeriodType.Minute, 2); // Note: Bars are added to the BarsArray and can be accessed via an index value // E.G. BarsArray[1] ---> Accesses the 1 minute Bars added above // Add ZeroLagStochs indicator to the chart for display // This only displays the indicator for the pimary Bars object (main instrument) on the chart Add(ZeroLagStochs()); CalculateOnBarClose = true; } ///<summary> /// Called on each bar update event (incoming tick) ///</summary> protectedoverridevoid OnBarUpdate() { // OnBarUpdate() will be called on incoming tick events on all Bars objects added to the strategy // We only want to process events on our primary Bars object (main instrument) (index = 0) which // is set when adding the strategy to a chart if (BarsInProgress != 0) return; // Condition set 1 if (CrossAbove(ZeroLagStochs().Mov, 75, 1)) { //Submits buy market order for EURUSD EnterLong(100000, "long"); //Submits buy market order for USDCHF EnterLong(120000, "BUY $USDCHF"); } // Condition set 2 if (Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) >= 200) { //Exits EURUSD long position when total unrealized P&L reaches $200.00 ExitLong("", "long"); //Exit CHF ExitLong("", "Buy $USDCHF"); } } #region Properties #endregion } } |
|
|
|
|
|
#2 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
You would need to do something like:
//Submits buy market order for EURUSD EnterLong(100000, "long"); //Submits buy market order for USDCHF EnterLong(1, 120000, "BUY $USDCHF"); Notice the "1" I have added which represents the secondary series. Check out the following Help Guide section and scroll down to the "Working with a Multi-Instrument Strategy" section. http://www.ninjatrader-support.com/H...rHandling.html
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Member
Join Date: Apr 2008
Location: Fenton, MI
Posts: 33
Thanks: 0
Thanked 0 times in 0 posts
|
Thanks Ray, I tried making the change you pointed out in you last post but it just made it only enter into the secondary pair. I read the section you asked me to. I have made a few changes but I still cant seem to get the Stategy to enter into both instruments. Can you please review what I have changed? Maybe you can steer me in the right direction.
Thank you, protectedoverridevoid Initialize() { // Add an $USDCHF 2 minute Bars object to the strategy Add("$USDCHF", PeriodType.Minute, 2); // Note: Bars are added to the BarsArray and can be accessed via an index value // E.G. BarsArray[1] ---> Accesses the 2 minute Bars added above // Add ZeroLagStochs indicator to the chart for display // This only displays the indicator for the pimary Bars object (main instrument) on the chart Add(ZeroLagStochs()); CalculateOnBarClose = true; } ///<summary> /// Called on each bar update event (incoming tick) ///</summary> private IOrder entryOrder = null; protectedoverridevoid OnBarUpdate() { // OnBarUpdate() will be called on incoming tick events on all Bars objects added to the strategy // We only want to process events on our primary Bars object (main instrument) (index = 0) which // is set when adding the strategy to a chart if (BarsInProgress != 0) return; // Condition set 1 if (CrossAbove(ZeroLagStochs().Mov, 75, 1)) { // Submit an order for EURUSD in the context of EURUSD bar update event if (entryOrder == null) entryOrder = EnterLongLimit(0, true, 100000,Close[1], "long"); // Submit an order for USDCHF in the context of EURUSD bar update event if (entryOrder == null) entryOrder = EnterLongLimit(1, true, 120000,Close[1], "BUY $USDCHF"); } // Condition set 3 if (Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) >= 200) { //Exits EURUSD long position when total unrealized P&L reaches $200.00 ExitLong("", "long"); //Exit CHF ExitLong(1,"", "Buy $USDCHF"); } } #region Properties #endregion } } |
|
|
|
|
|
#4 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
Don't see anything popping out at me.
You will need to debug your strategy. Here is a tip we put out on that in case you have never seen it. http://www.ninjatrader-support.com/v...ead.php?t=3418
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#5 |
|
Member
Join Date: Apr 2008
Location: Fenton, MI
Posts: 33
Thanks: 0
Thanked 0 times in 0 posts
|
Thanks again Ray, You have been a big help. I would like to use the "Print()" command to try to find some of the bugs but I am having trouble getting it to work. Would you mind showing me an example of how I would attach it on the codes below?
Thank you, // Condition set 2 if ( Position.GetProfitLoss( Close [0],PerformanceUnit.Currency) >= 200) { { ExitLong("", "long"); ExitLong(1,"", "Buy $USDCHF"); } TraceOrders = false; } |
|
|
|
|
|
#6 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
First, put TraceOrders in Initialize() method:
Then something like: if (yourCondition == true) { Print("Whatever information you want to see here"); }
Ray
NinjaTrader Customer Service |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Multi-instrument Strategy | dgregor5 | Strategy Development | 6 | 06-29-2010 04:47 AM |
| Multi instrument order glitch/bug/question? | adrian | Strategy Development | 5 | 05-01-2008 07:10 AM |
| Multi-Instrument help | Json | General Programming | 11 | 04-22-2008 10:42 AM |
| Adding TICK as a multi instrument | altrader | Connecting | 2 | 03-27-2008 08:25 AM |
| Multi Time Frame/Multi Instrument? | GreenTrade | Strategy Development | 3 | 01-14-2008 02:24 PM |