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
}
}

Comment