PDA

View Full Version : strategy development


SLASH
09-02-2010, 01:33 AM
Hi friends I have created this strategy after seeing big mike video.
Please help me fix its errors,Targets and stoploss not working properly
I want to enter 10 lots
target 1 : book 6 lots
target 2 : book 2 lots
target 3 ; book 2 lots

want to keep initial stoploss below low or high of entry bar
then after target 1 ,moved below low or high of target 1 bar then to target 2 bar .
cheers
nh

//#region Variables
private int smalength = 200;
private int emalength = 100;
private int hmalength = 40;

private int target1 = 100;
private int target2 = 200;
private int target3 = 300;

private int stop = 500;

private bool be2 = true;
private bool be3 = true;


//#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;
EntryHandling = EntryHandling.UniqueEntries;

Add(SMA(Close,HMAlength));
Add(EMA(Close,HMAlength));
Add(HMA(Close,HMAlength));

}

private void GoLong()
{
SetStopLoss("target1", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
SetStopLoss("target2", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
SetStopLoss("target3", CalculationMode.Price, Close[0] - (Stop*TickSize), false);

SetProfitTarget("target1", CalculationMode.Price, Close[0] + (Target1*TickSize));
SetProfitTarget("target2", CalculationMode.Price, Close[0] + ((Target1+Target2)*TickSize));
SetProfitTarget("target3", CalculationMode.Price, Close[0] + ((Target1+Target2+Target3)*TickSize));

EnterLong("target1");
EnterLong("target2");
EnterLong("target3");


}
private void GoShort()
{
SetStopLoss("target1", CalculationMode.Price, Close[0] + (Stop*TickSize), false);
SetStopLoss("target2", CalculationMode.Price, Close[0] + (Stop*TickSize), false);
SetStopLoss("target3", CalculationMode.Price, Close[0] + (Stop*TickSize), false);

SetProfitTarget("target1", CalculationMode.Price, Close[0] - (Target1*TickSize));
SetProfitTarget("target2", CalculationMode.Price, Close[0] - ((Target1+Target2)*TickSize));
SetProfitTarget("target3", CalculationMode.Price, Close[0] - ((Target1+Target2+Target3)*TickSize));

EnterShort("target1");
EnterShort("target2");
EnterShort("target3");


}

private void ManageOrders()
{
if (Position.MarketPosition == MarketPosition.Long)
{
if (BE2 && High[0] > Position.AvgPrice + (Target1*TickSize))
SetStopLoss("target2", CalculationMode.Price, Position.AvgPrice, false);

if (BE3 && High[0] > Position.AvgPrice + ((Target1+Target2)*TickSize))
SetStopLoss("target3", CalculationMode.Price, Position.AvgPrice, false);

}
if (Position.MarketPosition == MarketPosition.Short)
{
if (BE2 && Low[0] < Position.AvgPrice - (Target1*TickSize))
SetStopLoss("target2", CalculationMode.Price, Position.AvgPrice, false);

if (BE3 && Low[0] < Position.AvgPrice - ((Target1+Target2)*TickSize))
SetStopLoss("target3", CalculationMode.Price, Position.AvgPrice, false);

}

}

/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
EntryHandling = EntryHandling.UniqueEntries;

SMA smav = SMA(SMAlength);
EMA emav = EMA(EMAlength);
HMA hmav = HMA(HMAlength);

ManageOrders();

if (Position.MarketPosition != MarketPosition.Flat) return;

if (Rising(smav) && Rising(emav) && Rising(hmav))
GoLong();
else
if (Falling(smav) && Falling(emav) && Falling(hmav))
GoShort();

}

//#region Properties
[Description("")]
[Category("Parameters")]
public int SMAlength
{
get { return smalength; }
set { smalength = Math.Max(1, value); }
}
[Description("")]
[Category("Parameters")]
public int EMAlength
{
get { return emalength; }
set { emalength = Math.Max(1, value); }
}
[Description("")]
[Category("Parameters")]
public int HMAlength
{
get { return hmalength; }
set { hmalength = Math.Max(1, value); }
}
[Description("")]
[Category("Parameters")]
public int Target1
{
get { return target1; }
set { target1 = Math.Max(1, value); }
}
[Description("")]
[Category("Parameters")]
public int Target2
{
get { return target2; }
set { target2 = Math.Max(1, value); }
}
[Description("")]
[Category("Parameters")]
public int Target3
{
get { return target3; }
set { target3 = Math.Max(1, value); }
}
[Description("")]
[Category("Parameters")]
public int Stop
{
get { return stop; }
set { stop = Math.Max(1, value); }
}
[Description("")]
[Category("Parameters")]
public bool BE2
{
get { return be2; }
set { be2 = value; }
}
[Description("")]
[Category("Parameters")]
public bool BE3
{
get { return be3; }
set { be3 = value; }
}


//#endregion
}
}

NinjaTrader_Bertrand
09-02-2010, 06:03 AM
ninja hattori, can you please clarify what you see not working? For the specific qty's please use an overload for the EnterLong / EnterShort that would allow to enter them - http://www.ninjatrader-support.com/HelpGuideV6/EnterLong.html

To debug strategy's, the TraceOrders feature is very helpful - http://www.ninjatrader.com/support/forum/showthread.php?t=3627

SLASH
09-05-2010, 10:47 AM
Hi Bert my trailing stop not working I want to place it below my entry bar and then below profit target 1 bar and then below profit target 2 bar when long same for short above the entry ,profit target 1,profit target 2,bars.


//#region Variables
private int target1 = 70;
private int target2 = 110;
private int target3 = 120;
private bool be2 = true;
private bool be3 = true;
private int trailstop = 50;
private double highestHigh = 0;
private double lowestLow = 0;
//#endregion
/// This method is used to configure the strategy and is called once before any strategy method is called.
protected override void Initialize()
{
CalculateOnBarClose = true;
ExitOnClose = true;
ExitOnCloseSeconds = 30;
EntriesPerDirection = 3;
SetTrailStop(CalculationMode.Price, 50);
EntryHandling = EntryHandling.AllEntries;


}
private void GoLong()
{
SetTrailStop("target1", CalculationMode.Price, Position.AvgPrice - (TrailStop *TickSize), false);
SetTrailStop("target2", CalculationMode.Price, Position.AvgPrice - (TrailStop *TickSize), false);
SetTrailStop("target3", CalculationMode.Price, Position.AvgPrice - (TrailStop *TickSize), false);

SetProfitTarget("target1", CalculationMode.Price, Close[0] + (Target1*TickSize));
SetProfitTarget("target2", CalculationMode.Price, Close[0] + ((Target1+Target2)*TickSize));
SetProfitTarget("target3", CalculationMode.Price, Close[0] + ((Target1+Target2+Target3)*TickSize));

EnterLong("target1");
EnterLong("target2");
EnterLong("target3");
}
private void GoShort()
{
SetTrailStop("target1", CalculationMode.Price, Position.AvgPrice + (TrailStop*TickSize), false);
SetTrailStop("target2", CalculationMode.Price, Position.AvgPrice + (TrailStop*TickSize), false);
SetTrailStop("target3", CalculationMode.Price, Position.AvgPrice + (TrailStop*TickSize), false);

SetProfitTarget("target1", CalculationMode.Price, Close[0] - (Target1*TickSize));
SetProfitTarget("target2", CalculationMode.Price, Close[0] - ((Target1+Target2)*TickSize));
SetProfitTarget("target3", CalculationMode.Price, Close[0] - ((Target1+Target2+Target3)*TickSize));

EnterShort("target1");
EnterShort("target2");
EnterShort("target3");
}

private void ManageOrders()
{
if (Position.MarketPosition == MarketPosition.Long)
{
if (BE2 && High[0] > Position.AvgPrice + (Target1*TickSize))
SetTrailStop("target2", CalculationMode.Price, (Position.AvgPrice-TrailStop), false);

if (BE3 && High[0] > Position.AvgPrice + ((Target1+Target2)*TickSize))
SetTrailStop("target3", CalculationMode.Price, (Position.AvgPrice-TrailStop), false);

}
if (Position.MarketPosition == MarketPosition.Short)
{
if (BE2 && Low[0] < Position.AvgPrice - (Target1*TickSize))
SetTrailStop("target2",CalculationMode.Price,(Position.AvgPrice+TrailSto p), false);

if (BE3 && Low[0] < Position.AvgPrice - ((Target1+Target2)*TickSize))
SetTrailStop("target3", CalculationMode.Price, (Position.AvgPrice+TrailStop), false);
}
}


/// Called on each bar update event (incoming tick)

protected override void OnBarUpdate()
{
// Resets the highest high and lowest low at the start of every new session
if (Bars.FirstBarOfSession)
{
highestHigh = High[0];
lowestLow = Low[0];
}

// Stores the highest high and lowest low from the first 1 bars
if (Bars.BarsSinceSession < 1)
{
// If current high is greater than current highest high, set highest high to current high
if (High[0] > highestHigh)
highestHigh = High[0];

// If current low is lower than current lowest low, set lowest low to current low
if (Low[0] < lowestLow)
lowestLow = Low[0];
}

/* Entry Condition: After the first 1 bars, submit an entry order when price crosses either the highest high or the lowest low
if you have no market positions open */
if (Bars.BarsSinceSession > 1 && Position.MarketPosition == MarketPosition.Flat)
{
// If price crosses above the highest high, enter long
if (CrossAbove(Close, highestHigh, 1))
GoLong();

// If price crosses below the lowest low, enter short
else if (CrossBelow(Close, lowestLow, 1))
GoShort();
}
}

//#region Properties
[Description("")]
[Category("Parameters")]
public int Target1
{
get { return target1; }
set { target1 = Math.Max(1, value); }
}
[Description("")]
[Category("Parameters")]
public int Target2
{
get { return target2; }
set { target2 = Math.Max(1, value); }
}
[Description("")]
[Category("Parameters")]
public int Target3
{
get { return target3; }
set { target3 = Math.Max(1, value); }
}
[Description("")]
[Category("Parameters")]
public int TrailStop
{
get { return trailstop; }
set { trailstop = Math.Max(1, value); }
}
[Description("")]
[Category("Parameters")]
public bool BE2
{
get { return be2; }
set { be2 = value; }
}
[Description("")]
[Category("Parameters")]
public bool BE3
{
get { return be3; }
set { be3 = value; }
}
//#endregion
}
}

SLASH
09-05-2010, 10:59 AM
also how can I enter as below
entry 1 : 6 lots
entry 2 : 2 lots
entry 3 : 2 lots

NinjaTrader_Bertrand
09-06-2010, 06:56 AM
ninja hattori, you would need to custom code this trailstop behavior, you can for example save the low of your entry bar and then start trailing from this point on - a reference sample to get started can be found here -

http://www.ninjatrader.com/support/forum/showthread.php?t=3222

You can directly enter the qty you wish to trade in the EnterLong / EnterShort overloads -

http://www.ninjatrader-support.com/HelpGuideV6/EnterLong.html

SLASH
09-06-2010, 01:25 PM
ninja hattori, you would need to custom code this trailstop behavior, you can for example save the low of your entry bar and then start trailing from this point on - a reference sample to get started can be found here -

http://www.ninjatrader.com/support/forum/showthread.php?t=3222

You can directly enter the qty you wish to trade in the EnterLong / EnterShort overloads -

http://www.ninjatrader-support.com/HelpGuideV6/EnterLong.html
Thanks Bert
qty problem solved but I am not able to solve trailstop behavior.when profit target given in private void is working why manage orders not working?
how can I custom code it ?

NinjaTrader_Bertrand
09-07-2010, 05:41 AM
Great you got the qty's entered successfully - if you want to move your stops, you would just call the SetStopLoss again as needed, we have dedicated sample posted here - http://www.ninjatrader.com/support/forum/showthread.php?t=3222

SLASH
09-07-2010, 10:28 AM
Great you got the qty's entered successfully - if you want to move your stops, you would just call the SetStopLoss again as needed, we have dedicated sample posted here - http://www.ninjatrader.com/support/forum/showthread.php?t=3222
Bert tried everything according to link but no success.:(

NinjaTrader_Bertrand
09-07-2010, 11:01 AM
ninja hattori, unfortunately we would not offer scripting / debugging services, however a NinjaScript could code this professionally for you as alternative - http://www.ninjatrader.com/webnew/partners_onlinetrading_NinjaScript.htm