I get a problem when running a simple backtest.
This stratégie buy after 11h30, and exits after 2 times the initial risk (30 points), on FCE-CAC40 future contract.
With CalculateOnBarClose = true
I need to test when the strategie is flat.
I use Print fonction to get this information, in OnBarUpdate() and in OnExecution() method.
Prints are different, as OnExecution() returns flat information 1 bar before price is reached and trade is closed.
Could anyone explain me this strange action ??
This stratégie has been tested on Ninja 6.5 and Ninja 7, same result.
Here is the graph:
Here is the code:
public class Turtle1_Long_DEBUG : Strategy
{
#region Variables
double NewStop=1;
double FixedStop;
double Risk;
int OneTrade;
#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;
TraceOrders = true;
EntriesPerDirection=1;
EntryHandling=EntryHandling.UniqueEntries;
ExitOnClose=true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if (ToDay(Time[0]) != ToDay(Time[1]))
{
OneTrade = 0;
}
if (Position.MarketPosition==MarketPosition.Flat
&& BarsInProgress==1
&& ToTime(Time[0]) >= 113000
&& OneTrade == 0
)
{
EnterLong(3,"Turtle Long");
NewStop=1;
OneTrade = 1;
}
if (Position.MarketPosition==MarketPosition.Long)
{
Risk=Position.AvgPrice-NewStop;
FixedStop=Position.AvgPrice-30;
if (FixedStop>NewStop)
{NewStop=FixedStop;}
ExitLongLimit(3,Position.AvgPrice+2*Risk,"Exit at X times Risk","");
}
Print("OnBarUpdate");
Print("Date : " +ToDay(Time[0])+" Time : " +ToTime(Time[0]));
Print("Risk= "+Risk);
Print("Situation= "+Position.MarketPosition);
Print ("Position.Quantity :" +Position.Quantity);
Print("");
}
protected override void OnExecution(IExecution execution)
{
Print("OnExecution");
Print("Date : " +ToDay(Time[0])+" Time : " +ToTime(Time[0]));
Print("Risk= "+Risk);
Print("Situation= "+Position.MarketPosition);
Print ("Position.Quantity :" +Position.Quantity);
Print("");
}
#region Properties
#endregion
}
Here is the TraceOrders:
21/01/2009 15:50:00 Entered internal PlaceOrder() method at 21/01/2009 15:50:00: Action=Sell OrderType=Limit Quantity=3 LimitPrice=2935,0 StopPrice=0 SignalName='Exit at X times Risk' FromEntrySignal=''
21/01/2009 15:50:00 Ignore order amendment: Action=Sell OrderType=Limit Quantity=3 LimitPrice=2935,0 StopPrice=0 SignalName=Exit at X times Risk' FromEntrySignal='' Reason='Order already has this stop price/limit price/quantity'
OnBarUpdate
Date : 20090121 Time : 155000
Risk= 30
Situation= Long
Position.Quantity :3
OnExecution
Date : 20090121 Time : 155000
Risk= 30
Situation= Flat
Position.Quantity :0
Thanks a lot, have a nice day.
Arnaud
Comment