for some time now I've been trying to solve the following problem:
I want to enter the market by limit order at a fixed price, whether long or short is not relevant at first.
The important thing is that I can manage the position differently after entering the market (at least 2 contracts). To realize this, I enter the market with two EnterShort/LongLimitMarket orders, and give the respective order a string tag, so that I can later use the stoploss or takeprofit method to scale out.
This works in realtime trading, but not in the backtest function, where both trades are not identical, even though entered at the same price. Most of the time one of them runs into profit and the other one realizes a loss. How is this possible? How can I fix this?
I have attached two sample codes:
First of all, it doesn't matter if the strategy makes sense or is profitable (They're just code snippets), I also know that the backtest is not reliable when I open and close intrabar, the only important point I want to solve is that both trades are executed like in example 2, but the possibility exists to manage them differently.
Candlestickchart, Calculate : OnBarClose, OnBarUpdate
Code 1 (with exisiting problem):
protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
Cbi.MarketPosition marketPosition, string orderId, DateTime time)
{
SetStopLoss("S1",CalculationMode.Ticks, 10,false);
SetProfitTarget("S1",CalculationMode.Ticks,10);
SetStopLoss("S2",CalculationMode.Ticks, 10,false);
SetProfitTarget("S2",CalculationMode.Ticks,10);
}
protected override void OnBarUpdate()
{
if((Position.MarketPosition==MarketPosition.Flat) && (Low[0]>Low[1]))
{
EnterShortStopMarket(0, false, 1, Low[1], "S1") ;
EnterShortStopMarket(0, false, 1, Low[1], "S2") ;
}
}
I want that the code in example 1 gives the same results in backtest as the code below :
protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
Cbi.MarketPosition marketPosition, string orderId, DateTime time)
{
SetStopLoss("S1",CalculationMode.Ticks, 10,false);
SetProfitTarget("S1",CalculationMode.Ticks,10);
}
protected override void OnBarUpdate()
{
if((Position.MarketPosition==MarketPosition.Flat) && (Low[0]>Low[1]))
{
EnterShortStopMarket(0, false, 2, Low[1], "S1") ;
}
Thank you very much for your help!
Kevin


Comment