When the profit target is reached, I would like auto-trading automatically stop  [SOLVED]

Questions about MultiCharts and user contributed studies.
User avatar
Gian Maria Rossi
Posts: 3
Joined: 11 Oct 2023
Has thanked: 3 times
Been thanked: 1 time

When the profit target is reached, I would like auto-trading automatically stop

Postby Gian Maria Rossi » 11 Oct 2023

A warm greeting to the entire Multicharts forum community.

I've owned a lifetime license for Multicharts for many years, but I've never ask access to the forum until now, so this is my first post. I hope you can kindly assist me.

I've been using Adaptrade Builder for several years solely for study purposes (I've conducted more than 50,000 tests), and now it's time to transition to real trading (with Multicharts plus Interactive Brokers).

My plan is to manage the trading systems generated by Adaptrade Builder with an overall profit target for each system. Essentially, I want to calculate an expected profit for each system I choose to trade in real markets, and when this profit target is achieved (either with a single trade or with the cumulative result of multiple trades potentially spanning in multiple trading days), I want the system to automatically stop trading.

It's important to note that I don't want this global profit target to be calculated in real-time while a trade is in progress. Instead, I'd like it to be determined at the conclusion of the last trade that results in a profit (i.e., accounted trade). This means that the actual profit earned might exceed the expected target. Therefore, I'd like this overall profit target to be calculated based on the series of recorded trades (accounted trades): depending on the market scenario, this could involve a single trade reaching the profit target or multiple trades occurring over several trading days.

For example, if I expect a profit of "n" for System X, and the last completed/accounted trade for this trading system results in a total profit greater than or equal to "n" (generated either by a single trade or by the cumulative result of multiple trades potentially spanning in multiple trading days), I want the system to automatically stop trading.

The same principle applies to the stop-loss strategy.

Unfortunately, I'm not familiar with Easy Language, so I kindly request if you could provide me with the necessary lines of code to achieve what I want and specify at which point in the code generated by Adaptrade Builder I should add these code lines.

So my request is

1) The lines of code necessary to achieve what I have requested
2) And so, taking this randomly selected Trading System code generated by Adaptrade Builder as an example like that:

Code: Select all

{------------------------------------------------------------------------------- Trading Strategy Code Population member: 2237 Max bars back: 81 Created by: Adaptrade Builder version 4.4.0.0 Created: 20/06/2023 07:39:21 Scripting language: TS 6 or newer Symbol: Fake micro-s&p500-60min-10yr (Intraday, 60 min bars), Primary (Data1) (C:\Program Files\Adaptrade Software\Data\Builder-Stock-60min-10yr.csv) Build dates: 02/01/2001 to 11/07/2006 Project file: C:\Users\Utente\Desktop\TRADING LAVORO\TESTs Adaptrade Builder\TEST ADAPTRADE BUILDER 4\ACTUAL 2 FAKE sp500\BASIC+3sub_3sub_scelta_alto potenziale_ma da verifcarne consistenza+Crossover 50_Mutation 100.gpstrat -------------------------------------------------------------------------------} { Strategy inputs } Inputs: NBarEntL1 (81), { Indicator look-back length (bars), long trades } NStdEntL1 (-1.319498), { Number of standard deviations, long trades } EntPtsL (0.250000), { Number of points for stop price, long trades } EntrySzS (0), { Value of fixed size entry (stop/limit) per share/contract, short trades } NBarExS1 (81), { Number of bars from entry for market exit, short trades } TargPctS (9.197), { Value of percentage exit target, short trades } PSParam (1.00), { Position sizing parameter value } RoundPS (true), { Round-to-nearest (true/false) } RoundTo (1), { Round-to position size value } MinSize (1), { Minimum allowable position size } SizeLimit (100); { Maximum allowable position size } { Variables for entry and exit prices } Var: EntPrL (0), EntPrS (0), TargPrS (0); { Variables for entry and exit conditions } Var: VarL1 (0), VarL2 (0), EntCondL (false), EntCondS (false), ExCondL (false); { Variables for position sizing } Var: NShares (0); { Entry prices } EntPrL = BollingerBand(C, NBarEntL1, NStdEntL1) + EntPtsL; EntPrS = C + EntrySzS/BigPointValue; { Entry and exit conditions } VarL1 = OpenD(0); VarL2 = LowD(0); EntCondL = VarL1 > H; EntCondS = true; ExCondL = VarL2 = H; { Position sizing calculations } NShares = PSParam; If RoundPS and RoundTo > 0 then NShares = IntPortion(NShares/RoundTo) * RoundTo; NShares = MaxList(NShares, MinSize); NShares = MinList(NShares, SizeLimit); { Entry orders } If MarketPosition = 0 and EntCondL and C < EntPrL then begin Buy("EnStop-L") NShares shares next bar at EntPrL stop; end; If MarketPosition = 0 and EntCondS and C < EntPrS then begin Sell short("EnLimit-S") NShares shares next bar at EntPrS limit; end; { Exit orders, long trades } If MarketPosition = 1 then begin If ExCondL then Sell("ExMark-L") next bar at market; end; { Exit orders, short trades } If MarketPosition = -1 then begin TargPrS = (1 - TargPctS/100.0) * EntryPrice; Buy to cover("ExTarg-S") next bar at TargPrS limit; If BarsSinceEntry >= NBarExS1 then Buy to cover("ExMark-S") next bar at market; end;
I would like to know where I should insert the lines of code in such a way that the 'Compile' process in Multicharts is successful, and therefore everything functions correctly

Thank you for any guidance or assistance you can provide on achieving this setup.

Best regards, Gian Maria Rossi

User avatar
TJ
Posts: 7743
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: When the profit target is reached, I would like auto-trading automatically stop

Postby TJ » 11 Oct 2023

You can add a CONDITION called "Profit.Reached".

eg. Here's the concept:

Code: Select all

Input: Profit.Target( 1000 ); Var: Profit.Reached( False ); If Netprofit > Profit.Target then Profit.Reached = true. If Profit.Reached = false then BEGIN // your trading code here END;

User avatar
Gian Maria Rossi
Posts: 3
Joined: 11 Oct 2023
Has thanked: 3 times
Been thanked: 1 time

Re: When the profit target is reached, I would like auto-trading automatically stop

Postby Gian Maria Rossi » 11 Oct 2023

You can add a CONDITION called "Profit.Reached".

eg. Here's the concept:

Code: Select all

Input: Profit.Target( 1000 ); Var: Profit.Reached( False ); If Netprofit > Profit.Target then Profit.Reached = true. If Profit.Reached = false then BEGIN // your trading code here END;
Hi TJ, thank you immensely. I'm not familiar with EasyLanguage, but reading the code, I seem to understand it - more or less - (at least in general terms). You've been very kind and quick in your response; I thank you again.

I wouldn't want to bore you or take up too much of your time, but unfortunately, I have two more questions: when you write

Code: Select all

If Netprofit > Profit.Target then Profit.Reached = true.
for what I need, do I need to insert also an "equal" sign? In other words, is

Code: Select all

If Netprofit >= Profit.Target then Profit.Reached = true.
the correct modification?

And finally, is there a corresponding function, and thus code, to stop the entire system if expected losses are equal to or exceeded instead? For example, a condition called -I am inventing - 'Loss.Reached'? If so, could I simply replicate the same code, modifying it where necessary, and add it below the code you provided, then follow it with the code of the TS, and finally END.

Is that correct? Or do I need to find another method of writing the code for the overall stop due to potential losses (accounted of course, just like for gains)?

Thanks again; you've already been very, very, very helpful and kind. I apologize for adding more questions/requests, sorry. :cry:

User avatar
TJ
Posts: 7743
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: When the profit target is reached, I would like auto-trading automatically stop

Postby TJ » 11 Oct 2023

the correct modification?
Sharp eyes and sharp mind ! Yes that is the correct way to specify "Larger or Equal" !

User avatar
TJ
Posts: 7743
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: When the profit target is reached, I would like auto-trading automatically stop  [SOLVED]

Postby TJ » 11 Oct 2023


And finally, is there a corresponding function, and thus code, to stop the entire system if expected losses are equal to or exceeded instead? For example, a condition called -I am inventing - 'Loss.Reached'? If so, could I simply replicate the same code, modifying it where necessary, and add it below the code you provided, then follow it with the code of the TS, and finally END.

Is that correct? Or do I need to find another method of writing the code for the overall stop due to potential losses (accounted of course, just like for gains)?

Thanks again; you've already been very, very, very helpful and kind. I apologize for adding more questions/requests, sorry. :cry:
You can use a similar conditional logic:

Code: Select all

Input: Profit.Target( 1000 ), Loss.Limit( -500 ); Var: OK.To.Trade( False ); If Netprofit < Profit.Target AND Netprofit > Loss.Limit then Ok.To.Trade = TRUE else Ok.To.Trade = FALSE; If Ok.To.Trade then BEGIN // your trading code here END;

User avatar
Gian Maria Rossi
Posts: 3
Joined: 11 Oct 2023
Has thanked: 3 times
Been thanked: 1 time

Re: When the profit target is reached, I would like auto-trading automatically stop

Postby Gian Maria Rossi » 13 Oct 2023

Hi TJ, sorry for the delay in my response, but I had some things to take care of.

This morning, I took a moment to understand the codes you wrote for me (just to learn a bit more - for example, I didn't even know that TradeStaton’s built-in reserved word 'NetProfit' already referred to accounted trades, it gives the value for all closed trades and it does not also refer to real-time trades, and various other "discoveries" - they are discoveries for me at the moment :mrgreen: :mrgreen: :mrgreen: ). And, okay, the last code does exactly what I want, so thank you so much again.


Return to “MultiCharts”