for
onbarupdate below is the code.:
if(BarsInProgress != 0)
{
// order active for 1 minute. cancel it now
var msg = string.Format("Current Bar is = {0}, Limit bar is = {1}", CurrentBars[1] , limit_bar_number);
Print(msg);
if(((CurrentBars[1] - limit_bar_number) >0) && (limit_bar_number != 0))
{
if((Limit_order.OrderState != OrderState.Filled) && (Limit_order.OrderState != OrderState.Cancelled) )
{
CancelOrder(Limit_order);
msg = string.Format("Cancelling Limit Order, Position Flat and signal is Long Dumpling with entry of = {0} and current bar at {1}", CurrentBars[1] , limit_bar_number);
Print(msg);
}
}
trailing_Stop();
ManagePosition();
return;
}
the secondary dataframe is 1 minute which is added via the code
else if (State == State.Configure)
{
AddDataSeries(Data.BarsPeriodType.Minute, 1);
}
coming back to original code, my orders are managed in ManagePosition described below. Problem is the limit orders and stoploss orders appers on alternate bars. So after end of 1st bar of entry all orders are cancelled. Then on second bar they come back, thrid bar they disappear so on and so forth. It works fine on single frame however when i decided to move the position management to smaller timeframe by adding additional TF(1 minute) they appear and disappear every minute.
private void ManagePosition()
{
if(Position.MarketPosition == MarketPosition.Long)
{
if(Stop_Loss_L >= Close[0])
{
ExitLong(Position.Quantity);
}
else
{
if((Stop_Loss_L < (Low[0] - (2 * TickSize))) && (Close[0] > Open[0]) && (BarsInProgress == 0))
{
double stoploss_prev = Stop_Loss_L;
Stop_Loss_L = Low[0] - (2 * TickSize);
var msg = string.Format("StopLoss Moved from {0} to {1} up {2} ticks", stoploss_prev, Stop_Loss_L, (stoploss_prev - Stop_Loss_L)/TickSize);
Print(msg);
}
ExitLongStopMarket(Position.Quantity, Stop_Loss_L, "StopLoss", "LongDumpling");
if(Position.Quantity == 3)
{
ExitLongLimit(1, First_Target_L , "Emo Target", "LongDumpling");
ExitLongLimit(1, Second_Target_L , "SPH Target", "LongDumpling");
ExitLongLimit(1, Final_Target_L , "Final Target", "LongDumpling");
}
if(Position.Quantity == 2)
{
ExitLongLimit(1, Second_Target_L , "SPH Target", "LongDumpling");
ExitLongLimit(1, Final_Target_L , "Final Target", "LongDumpling");
}
if(Position.Quantity == 1)
{
ExitLongLimit(1, Final_Target_L , "Final Target", "LongDumpling");
}
}
}
if(Position.MarketPosition == MarketPosition.Short)
{
Print(Position.Quantity);
if (position_flag)
{
position_flag = false;
}
if(Stop_Loss_S <= Close[0])
{
ExitShort(Position.Quantity);
}
else
{
if((Stop_Loss_S > (High[0] - (2 * TickSize))) && (Close[0] < Open[0]) && (BarsInProgress == 0))
{
double stoploss_prev = Stop_Loss_S;
Stop_Loss_S = High[0] + (2 * TickSize);
var msg = string.Format("StopLoss Moved from {0} to {1} up {2} ticks", stoploss_prev, Stop_Loss_S, (stoploss_prev - Stop_Loss_S)/TickSize);
Print(msg);
}
ExitShortStopMarket(Position.Quantity, Stop_Loss_S, "StopLoss", "ShortDumpling");
if(Position.Quantity == 3)
{
ExitShortLimit(1, First_Target_S , "Emo Target", "ShortDumpling");
ExitShortLimit(1, Second_Target_S , "SPL Target", "ShortDumpling");
ExitShortLimit(1, Final_Target_S , "Final Target", "ShortDumpling");
}
if(Position.Quantity == 2)
{
ExitShortLimit(1, Second_Target_S , "SPL Target", "ShortDumpling");
ExitShortLimit(1, Final_Target_S , "Final Target", "ShortDumpling");
}
if(Position.Quantity == 1)
{
ExitShortLimit(1, Final_Target_S , "Final Target", "ShortDumpling");
}
}
}
}
debug output looks somehting like this
Current Bar is = 5846, Limit bar is = 0
Executing Trailing stop Routine
2
2
Order status = CancelPending, Market Postiion = Short
Order status = CancelSubmitted, Market Postiion = Short
Order status = Cancelled, Market Postiion = Short
Order status = CancelPending, Market Postiion = Short
Order status = CancelSubmitted, Market Postiion = Short
Order status = Cancelled, Market Postiion = Short
Order status = CancelPending, Market Postiion = Short
Order status = CancelSubmitted, Market Postiion = Short
Order status = Cancelled, Market Postiion = Short
Current Bar is = 5847, Limit bar is = 0
Executing Trailing stop Routine
2
Order status = Submitted, Market Postiion = Short
Order status = Accepted, Market Postiion = Short
Order status = Submitted, Market Postiion = Short
Order status = Accepted, Market Postiion = Short
Order status = Working, Market Postiion = Short
Order status = Submitted, Market Postiion = Short
Order status = Accepted, Market Postiion = Short
Order status = Working, Market Postiion = Short
2
So looks like the cancellationa nd submission of orders in happening alternatively.
Comment