Alert when position hits target

Questions about MultiCharts .NET and user contributed studies.
lordongi
Posts: 29
Joined: 18 Dec 2017
Has thanked: 10 times
Been thanked: 3 times

Alert when position hits target

Postby lordongi » 09 Apr 2020

Hi,

I use standard signal Profit_Target. I'd like to receive an email when my positions strikes target. If I enable Alerts in the signal properties, nothing happens. Method used there is

Code: Select all

SignalObject.GenerateProfitTarget
.

Kind Regards
Ingo Martienssen

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Alert when position hits target

Postby JoshM » 16 Apr 2020

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.

lordongi
Posts: 29
Joined: 18 Dec 2017
Has thanked: 10 times
Been thanked: 3 times

Re: Alert when position hits target

Postby lordongi » 16 Apr 2020

Thank you, JoshM. This is what I was looking for.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Alert when position hits target

Postby JoshM » 17 Apr 2020

Awesome, glad I could help! :)


Return to “MultiCharts .NET”