Go Back   NinjaTrader Support Forum > NinjaTrader 7 > Strategy Development

Strategy Development Support for the development of custom automated trading strategies using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 12-02-2011, 05:31 PM   #1
mccallum28
Member
 
Join Date: Nov 2011
Posts: 72
Thanks: 5
Thanked 0 times in 0 posts
Thumbs up 3 Bar Long/Short Strategy - NT7

Hi

Please can someone tell me why this code will not run in NT7.

Thanks








#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Strategy;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// 3BR long short after n bars at prev bar plus/minus n ticks with ATM and cancel pending attached. Calc on bar close set to false.
/// </summary>
[Description("3BR long short after n bars at prev bar plus/minus n ticks with ATM and cancel pending attached. Calc on bar close set to false.")]
public class LS3BRATMCancel : Strategy
{
#region Variables
private string atmStrategyIdL = string.Empty;
private string atmStrategyIdS = string.Empty;

private int prevBarsPlusTicks = 1; // Default setting for PrevBarsPlusTicks
private int prevBarsMinusTicks = 1; // Default setting for PrevBarsPlusTicks

private string orderIdL = string.Empty;
private string orderIdS = string.Empty;

private int orderBarL = 0;
private int orderBarS = 0;
private int entryBarL = 0;
private int entryBarS = 0;

#endregion
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = true;
TraceOrders = true;
TimeInForce = Cbi.TimeInForce.Day;
}

/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// HELP DOCUMENTATION REFERENCE: Please see the Help Guide section "Using ATM Strategies"
// Make sure this strategy does not execute against historical data
if (Historical)
return;

// Submits an entry limit order at the current low price to initiate an ATM Strategy if both order id and strategy id are in a reset state
// **** YOU MUST HAVE AN ATM STRATEGY TEMPLATE NAMED 'TF_short_3W_3BR' CREATED IN NINJATRADER (SUPERDOM FOR EXAMPLE) FOR THIS TO WORK you can create your own****
if (orderIdL.Length == 0
&& atmStrategyIdL.Length == 0
&& Close [3] <= Open [3]
&& Close [2] <= Open [2]
&& Close [1] <= Open [1]
&& High [1] + PrevBarsPlusTicks * TickSize > GetCurrentBid())

{
atmStrategyIdL = GetAtmStrategyUniqueId();
orderIdL = GetAtmStrategyUniqueId();
orderBarL = CurrentBar;
AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Limit, High[1], 0, TimeInForce.Day, orderIdL, "TF_long_3W_3BR", atmStrategyIdL);
}
if (orderIdS.Length == 0
&& atmStrategyIdS.Length == 0
//&& FirstTickOfBar
&& Close [3] >= Open [3]
&& Close [2] >= Open [2]
&& Close [1] >= Open [1]
&& Low [1] - PrevBarsMinusTicks * TickSize < GetCurrentAsk())

{
atmStrategyIdS = GetAtmStrategyUniqueId();
orderIdS = GetAtmStrategyUniqueId();
orderBarS = CurrentBar;
AtmStrategyCreate(Cbi.OrderAction.Sell, OrderType.Limit, Low[1], 0, TimeInForce.Day, orderIdS, "TF_short_3W_3BR", atmStrategyIdS);
}

////////////////////////////////////////////////////////////////////////////////
// Check for a pending entry order
if (orderIdL.Length > 0)
{
//add in by kz
if ((CurrentBar - orderBarL) >= 3)
{
AtmStrategyCancelEntryOrder(orderIdL);

orderIdL = string.Empty;
}
string[] statusL = GetAtmStrategyEntryOrderStatus(orderIdL);

// If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements
if (statusL.GetLength(0) > 0)
{
// Print out some information about the order to the output window
Print("The entry order average fill price is:\t" + statusL[0]);
Print("The entry order filled amount is:\t" + statusL[1]);
Print("The entry order order state is:\t" + statusL[2]);
// If the order state is terminal, reset the order id value
if (statusL[2] == "Filled" || statusL[2] == "Cancelled" || statusL[2] == "Rejected")
orderIdL = string.Empty;
}
} // If the strategy has terminated reset the strategy id
else if (atmStrategyIdL.Length > 0 &&
GetAtmStrategyMarketPosition(atmStrategyIdL) == Cbi.MarketPosition.Flat)
atmStrategyIdL = string.Empty;
////////////////////////////////////////////////////////////////////////////////
// Check for a pending entry order
if (orderIdS.Length > 0)
{
//add in by kz
if ((CurrentBar - orderBarS) >= 3)
{
AtmStrategyCancelEntryOrder(orderIdS);

orderIdS = string.Empty;
}
string[] statusS = GetAtmStrategyEntryOrderStatus(orderIdS);

// If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements
if (statusS.GetLength(0) > 0)
{
// Print out some information about the order to the output window
Print("The entry order average fill price is:\t" + statusS[0]);
Print("The entry order filled amount is:\t" + statusS[1]);
Print("The entry order order state is:\t\t" + statusS[2]);
// If the order state is terminal, reset the order id value
if (statusS[2] == "Filled" || statusS[2] == "Cancelled" || statusS[2] == "Rejected")
orderIdS = string.Empty;
}
} // If the strategy has terminated reset the strategy id
else if (atmStrategyIdS.Length > 0 &&
GetAtmStrategyMarketPosition(atmStrategyIdS) == Cbi.MarketPosition.Flat)
atmStrategyIdS = string.Empty;
////////////////////////////////////////////////////////////////////////////////
if (atmStrategyIdL.Length > 0 &&
(CurrentBar - orderBarL) >= 3//added in
)
{
// You can change the stop price
// if (GetAtmStrategyMarketPosition(atmStrategyIdL) == MarketPosition.Long)//!= MarketPosition.Flat)//
// {
// AtmStrategyChangeStopTarget(0, Low[1] - 3 * TickSize, orderIdL, atmStrategyIdL);
// }
// Print some information about the strategy to the output window
Print("The current ATM Strategy market position is:\t" + GetAtmStrategyMarketPosition(atmStrategyIdL));
Print("The current ATM Strategy position quantity is:\t" + GetAtmStrategyPositionQuantity(atmStrategyIdL));
Print("The current ATM Strategy average price is:\t" + GetAtmStrategyPositionAveragePrice(atmStrategyIdL) );
Print("The current ATM Strategy Unrealized PnL is:\t$" + FormatPrice(GetAtmStrategyUnrealizedProfitLoss(atm StrategyIdL)));
}
////////////////////////////////////////////////////////////////////////////////
if (atmStrategyIdS.Length > 0 &&
(CurrentBar - orderBarS) >= 3//added in
)
{
// You can change the stop price
if (GetAtmStrategyMarketPosition(atmStrategyIdS) == MarketPosition.Short)//!= MarketPosition.Flat)
{
AtmStrategyChangeStopTarget(0, High[1] + 3 * TickSize, orderIdS, atmStrategyIdS);
}
// Print some information about the strategy to the output window
Print("The current ATM Strategy market position is:\t" + GetAtmStrategyMarketPosition(atmStrategyIdS));
Print("The current ATM Strategy position quantity is:\t" + GetAtmStrategyPositionQuantity(atmStrategyIdS));
Print("The current ATM Strategy average price is:\t" + GetAtmStrategyPositionAveragePrice(atmStrategyIdS) );
Print("The current ATM Strategy Unrealized PnL is:\t$" + FormatPrice(GetAtmStrategyUnrealizedProfitLoss(atm StrategyIdS)));
}

}//end of OnBarUpdate

#region FormatPrice
//this piece of code courtesy of eDanny?
private string FormatPrice(double iVal)
{
return Bars.Instrument.MasterInstrument.FormatPrice(iVal) ;
}
#endregion

#region Properties
[Description("")]
[GridCategory("Parameters")]
public int PrevBarsPlusTicks
{
get { return prevBarsPlusTicks; }
set { prevBarsPlusTicks = Math.Max(1, value); }
}

[Description("")]
[GridCategory("Parameters")]
public int PrevBarsMinusTicks
{
get { return prevBarsMinusTicks; }
set { prevBarsMinusTicks = Math.Max(1, value); }
}

#endregion
}
}
mccallum28 is offline  
Reply With Quote
Old 12-02-2011, 05:40 PM   #2
NinjaTrader_RyanM1
NinjaTrader Customer Service
 
NinjaTrader_RyanM1's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,127
Thanks: 249
Thanked 434 times in 427 posts
Default

Hello mccallum28,

Best is to PM the author and let them know what specific difficulty you are having with it.
NinjaTrader_RyanM1 is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Long & Short Strategy testing logic error? xbaha Platform Technical Support 3 06-27-2011 09:19 AM
Only Short or Only Long trades in NT7 kumaresh11 Platform Technical Support 1 12-29-2010 11:50 AM
Long and Short entries within the same strategy rarmington Strategy Development 2 03-31-2010 04:35 AM
exit long and entry short at same time in strategy MoreYummy Platform Technical Support 1 08-10-2008 04:07 AM
Long & short combined in 1 strategy dtf139 Platform Technical Support 5 01-08-2008 09:23 PM


All times are GMT -6. The time now is 12:05 AM.

Futures, foreign currency and options trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. View Full Risk Disclosure.

CFTC Rules 4.41 - Hypothetical or Simulated performance results have certain limitations, unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not been executed, the results may have under-or-over compensated for the impact, if any, of certain market factors, such as lack of liquidity. Simulated trading programs in general are also subject to the fact that they are designed with the benefit of hindsight. No representation is being made that any account will or is likely to achieve profit or losses similar to those shown.

This website is hosted and operated by NinjaTrader, LLC (“NT”), a software development company which owns and supports all proprietary technology relating to and including the NinjaTrader trading platform. NT is an affiliated company to NinjaTrader Brokerage (“NTB”), which is a NFA registered introducing broker (NFA #0339976) providing brokerage services to traders of futures and foreign exchange products. This website is intended for educational and informational purposes only and should not be viewed as a solicitation or recommendation of any product, service or trading strategy. No offer or solicitation to buy or sell securities, securities derivative or futures products of any kind, or any type of trading or investment advice, recommendation or strategy, is made, given, or in any manner endorsed by any NT affiliate and the information made available on this Web site is not an offer or solicitation of any kind. Specific questions related to a brokerage account should be sent to your broker directly. The content and opinions expressed on this website are those of the authors and do not necessarily reflect the official policy or position of NT or any of its affiliates. 

Vendors along with their websites, products and services, collectively referred to as (“Vendor Content”), are independent persons or companies that are in no manner affiliated with NT or any if its affiliates. NT or any of its affiliates are not responsible for, do not approve, recommend or endorse any Vendor Content referenced on this website and it’s your sole responsibility to evaluate Vendor Content. Please be aware that any performance information provided by a vendor should be considered hypothetical and must contain the disclosures required by NFA Rule 2-29(c). If you are interested in learning more about, or investigating the quality of, any such Vendor Content you must contact the vendor, provider or seller of such Vendor Content. No person employed by, or associated with, NT or any of its affiliates is authorized to provide any information about any such Vendor Content. Visit the CFTC resources for education regarding the industry and signs of fraud.