I am trying to update the stop loss when it hits profit target one, two and three but the stop loss does not move up to the stop points. I am getting a message saying
#region Variables
// Wizard generated variables
private double pivotrange = .002; // Default setting for MyInput0
private int x=0;
private double adxtrend=48;
private double adxsell=20;
private double mfishort=80;
private int targetMkt1 = 16;
private int targetMkt2 = 32; // Number of ticks to Profit Target 2
private int targetMkt3 = 64; // Number of ticks to Profit Target 3
private int cntsMkt1 = 1; // Number of contracts for Profit Target 1
private int cntsMkt2 = 1; // Number of contracts for Profit Target 2
private int cntsMkt3 = 1; // Number of contracts for Profit Target 3
private int cntsMkt4 = 1; // Number of contracts for Profit Target 3
private IOrder eMkt1Order = null; // Order data for "Buy/SellMkt2", used to get fill price
private IOrder eMkt2Order = null; // Order data for "Buy/SellMkt2", used to get fill price
private IOrder eMkt3Order = null; // Order data for "Buy/SellMkt3", used to get fill price
private double bestMktFill = 0;
private bool controlOne = true;
private bool controlTwo = true;
protected override void Initialize()
{
Add(PeriodType.Minute, 1); // index 1 on barsinprogress
Add(PeriodType.Minute, 15); // index 2 on barinsprogress
Add(PeriodType.Minute, 60); // index 3 on barinsprogress
Add(PeriodType.Day, 1); // index 4 on barinsprogress
SetProfitTarget("BuyMkt1", CalculationMode.Ticks, targetMkt1);
SetProfitTarget("BuyMkt2", CalculationMode.Ticks, targetMkt2);
SetProfitTarget("BuyMkt3", CalculationMode.Ticks, targetMkt3);
protected override void OnBarUpdate()
{
if (BarsInProgress == 0) // 5min bars used to show chart
{
//long entry
if (ADX(BarsArray[4], 10)[0]>= adxsell && Rising(ADX(BarsArray[2],10)) )
{
eMkt2Order = null; eMkt3Order = null;
if (cntsMkt1 > 0) eMkt1Order = EnterLong(1, cntsMkt1, "BuyMkt1");
if (cntsMkt2 > 0) eMkt2Order = EnterLong(1, cntsMkt2, "BuyMkt2");
if (cntsMkt3 > 0) eMkt3Order = EnterLong(1, cntsMkt3, "BuyMkt3");
}
////////////////////////////////////////////////////////////////// stop loss code
if( eMkt2Order != null && eMkt3Order != null )
{
Print(eMkt2Order.AvgFillPrice + targetMkt1*TickSize);
if (High[0] >= (eMkt2Order.AvgFillPrice + targetMkt1*TickSize) && controlOne )
{
SetStopLoss("", CalculationMode.Price, eMkt2Order.AvgFillPrice + targetMkt1* TickSize, false);
controlOne = false;
}
if (High [0] >= (eMkt3Order.AvgFillPrice + targetMkt2* TickSize) && !controlOne)
{
SetStopLoss("", CalculationMode.Price, eMkt3Order.AvgFillPrice + targetMkt2* TickSize, false);
controlTwo = false;
}
if (High [0] >= (eMkt3Order.AvgFillPrice + targetMkt3* TickSize) && !controlTwo)
{
SetStopLoss("", CalculationMode.Price, eMkt3Order.AvgFillPrice + targetMkt3* TickSize,false);
}
}
////////////////////////////////////////////////////////reset
if( eMkt2Order != null)
{
if (Position.MarketPosition == MarketPosition.Flat)
{
SetStopLoss("", CalculationMode.Ticks, stoplosstk, false);
controlOne = true;
controlTwo=true;
eMkt2Order = null; eMkt3Order = null;
eMkt1Order = null;
bestMktFill = 0;
I also tried a different route from which I found searching the forum. Using IOrder's but the script kept running until I got this error
The code is shown below
protected override void OnOrderUpdate(IOrder order)
// {
// if( eMkt3Order.AvgFillPrice != null && eMkt1Order.AvgFillPrice != null && eMkt2Order.AvgFillPrice != null)
// {
// double bestMktFill = (eMkt1Order.AvgFillPrice + eMkt2Order.AvgFillPrice + eMkt3Order.AvgFillPrice)/3;
//
//
// if (bestMktFill <= (High[0]+targetMkt1*TickSize) )
//{
// SetStopLoss("", CalculationMode.Ticks, 0-targetMkt1, false);
//}
//
//if (bestMktFill <= (High[0]+targetMkt2*TickSize) )
//{
// SetStopLoss("", CalculationMode.Ticks, 0-targetMkt2, false);
//}
//
//if (bestMktFill <= (High[0]+targetMkt3*TickSize) )
//{
// SetStopLoss("", CalculationMode.Ticks, 0-targetMkt3, false);
//}
//}
//}
Any help would be much appreciated!
Thanks
Oliver

Comment