I think the code of this signal script does what you want:
Code: Select all
using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using ATCenterProxy.interop;
namespace PowerLanguage.Strategy
{
public class AlertProfitTarget : SignalObject
{
public AlertProfitTarget(object _ctx) : base(_ctx) { }
private int positionCount = 0;
protected override void CalcBar()
{
// Only process when there's a new position traded
if (TotalTrades < 1 ||
Positions.Count == positionCount)
{
return;
}
// Get the last exit trade
int numExits = Positions[1].ClosedTrades.Count - 1;
ITradeOrder lastExit = Positions[1].ClosedTrades[numExits].ExitOrder;
// See if that trade was a limit order with a particular name
if (lastExit.Category == OrderCategory.Limit && lastExit.Name == "PT")
{
Alerts.Alert("Profit target hit! Exit price: {0} for {1} contracts.",
lastExit.Price, lastExit.Contracts);
}
positionCount = Positions.Count;
}
}
}
Note that this code looks if the exit trade was a limit order named PT. You'll have to replace that bit with the name of your take profit orders. Else the code cannot distinguish between a regular limit order exit and a take profit limit order.