I use the code hereafter, that opens a long position then closes this latter after the profit and loss reaches 100€.
When the PnL of 100€ is reached, the (account) position is well closed but I have an error that I
try to intercept with try/catch blocks in order for the strategy not to be disabled but it is still disabled after the error...
The error is :
Exception while trying to access Strategy Position : La référence d'objet n'est pas définie à une instance d'un objet.
**NT** Error on calling 'OnBarUpdate' method for strategy 'IchimokuExpertStrategy/682e1335e2244371ba1fae15d0be724c': La référence d'objet n'est pas définie à une instance d'un objet.
Thanks !
double ask = 0;
double bid = 0;
double PnL = 0;
DateTime dt = new DateTime();
bool done = false;
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
dt = DateTime.Now;
Print("**** Account Positions ****");
if (Account.Positions.Count>0)
{
for (int i=0;i<Account.Positions.Count;i++)
{
try
{
Print("Account Position " + i);
Print(">> " + dt + " : Instrument Name = " + Account.Positions[i].Instrument.FullName);
Print(">> " + dt + " : PnL = " + Account.Positions[i].GetProfitLoss(GetCurrentBid(), PerformanceUnit.Currency));
Print(">> " + dt + " : Qty = " + Account.Positions[i].Quantity);
double profit = Account.Positions[i].GetProfitLoss(GetCurrentBid(), PerformanceUnit.Currency);
if (profit>=100)
{
Account.Positions[i].Close();
}
}
catch(Exception ex)
{
Print("Exception while trying to access Account Position " + i + " : " + ex.Message);
}
}
}
try
{
Print("**** Strategy Position ****");
PnL = Position.GetProfitLoss(GetCurrentBid(), PerformanceUnit.Currency);
Print(">> " + dt + " : PnL =" + PnL);
Print(">> " + dt + " : Qty =" + Position.Quantity);
}
catch(Exception ex)
{
[B][COLOR="lime"]Print("Exception while trying to access Strategy Position : " + ex.Message);[/COLOR][/B]
}
if (this.Historical == true) return;
double ask = this.GetCurrentAsk();
double bid = this.GetCurrentBid();
if (done == false)
{
Print("Will enter long position");
IOrder iorder = EnterLong(1000000);
done = true;
}
Print("");
}

Comment