how to set protfit target and stop lose based on percentage

Questions about MultiCharts and user contributed studies.
davidlaw_mc
Posts: 5
Joined: Jul 08 2024
Has thanked: 3 times

Jul 10 2024

thanks for letting me know the format in posting! for easy reading here is my question again -

im trying to write a script with follow logic

when MA1 cross over MA2 - put a buy order
when MA1 cross under MA2 - put a sell order (at moment no sellshort)

once there is an open position (ie buy order executed), i want to set a profit target and stop lose based on percentage as well

for example - when it makes a 20% profit of the value in the buy order, then sell
when it makes a 10% lose of the value in the buy order, then also sell

i had written the first part of the MA1 cross MA2 but no clue how to write the second part (set profit target n stop lose). will much appreciate if you guys can advice!

fyi here is the script i wrote for the first part

Code: Select all

inputs: Price( Close ), FastLength( 9 ), SlowLength( 18 ); variables: var0( 0 ), var1( 0 ); var0 = AverageFC( Price, FastLength ) ; var1 = AverageFC( Price, SlowLength ) ; condition11 = CurrentBar > 1 and var0 crosses over var1 ; if condition11 then Buy ( "MA2CrossLE" ) next bar at market ; condition12 = CurrentBar > 1 and var0 crosses below var1 ; if condition12 then sell ( "MA2CrossSE" ) next bar at market ;

HellGhostEvocatorX
Posts: 143
Joined: Feb 10 2022
Has thanked: 68 times
Been thanked: 23 times

Jul 11 2024

They probably wanted to post this in multicharts and not in the multicharts.net forum ;)

User avatar
BB123
Posts: 78
Joined: Nov 24 2023
Has thanked: 29 times
Been thanked: 15 times

Jul 15 2024

I just coded this up and not tested-- but this is how id go about it. With that said, someone else might have a MUCH better/cleaner solution.


Code: Select all

Inputs: TpPctToUse(20),SlPctToUse(10); Vars: TpPct(0.0),SlPct(0.0); TpPct= (close * (TpPctToUse* .01)); SlPct= (close * (SlPctToUse* .01)); condition11 = CurrentBar > 1 and var0 crosses over var1 ; if condition11 then begin Buy ( "MA2CrossLE" ) next bar at market ; SetProfitTarget(TpPct); SetStopLoss(SlPct); end;
The example used is using reserved words.. Another way you could do it is assigning a tp value using pct to a var and then check each tick if price is above the tp value

Code: Select all

[IntrabarOrderGeneration = true]; Inputs: TpPctToUse(20),SlPctToUse(10); Vars: TpPct(0.0),SlPct(0.0),lastbarnumber (0); if (barnumber <> lastbarnumber ) then begin lastbarnumber = barnumber; TpPct= (close +(close * (TpPctToUse* .01))); SlPct= (close-(close * (SlPctToUse* .01))); condition11 = CurrentBar > 1 and var0 crosses over var1 ; if condition11 then begin Buy ( "MA2CrossLE" ) 1 contract next bar at market ; SetProfitTarget(TpPct); SetStopLoss(SlPct); end;end; END; //this will check if price is greater if close > tpPct then Sell 1 contract next bar at market;

davidlaw_mc
Posts: 5
Joined: Jul 08 2024
Has thanked: 3 times

Jul 18 2024

i think i hv found the way to do it -

Code: Select all

inputs: Price( Close ), FastLength( 9 ), SlowLength( 18 ), SL(0.1), SPT(0.2); variables: var0( 0 ), var1( 0 ), START_PRICE(0); var0 = AverageFC( Price, FastLength ) ; var1 = AverageFC( Price, SlowLength ) ; condition11 = CurrentBar > 1 and var0 crosses over var1 ; if condition11 then Buy ( "MA2CrossLE" ) next bar at market ; START_PRICE = CLOSE; end; setprofittarget(START_PRICE*SPT); SETSTOPLOSS(START_PRICE*SL); condition12 = CurrentBar > 1 and var0 crosses below var1 ; if condition12 then sell ( "MA2CrossSE" ) next bar at market ;

User avatar
boup09
Posts: 12
Joined: May 01 2023
Has thanked: 6 times
Been thanked: 3 times

Jul 28 2024

You can use Avgentryprice, to get the correct price of your trade.

Example for a 5% take profit from opening price=>

TP = Avgentryprice*Bigpointvalue*0.05;
SetStopShare;
Setprofittarget(TP);