Please help with the script Power Language.

Questions about MultiCharts and user contributed studies.
335
Posts: 1
Joined: 06 Apr 2021

Please help with the script Power Language.

Postby 335 » 12 Jul 2022

Hello,
I want to have a moving stop profit .The idea is ,
1. After long position, if it rises 20-39 points, it will set stop profit at the entryprice + 10 points.
2.If it rises 40-59 points , it will set stop profit at the entryprice +30 points.

My script is as below:

vars:
profit200(False),
profit400(False),

[IntrabarOrderGeneration = True];

if marketposition =1 and close - postradeentryprice(0,currententries-1) >= 20 and close - postradeentryprice(0,currententries-1) < 40 then profit200=true;
if profit200 = true then begin
if close <= postradeentryprice(0,currententries-1) + 10 then sell("100LE") next bar at market;
end;
if marketposition =1 and close - postradeentryprice(0,currententries-1) >= 40 and close - postradeentryprice(0,currententries-1) < 60 then profit400=true;
if profit400 = true then begin
if close <= postradeentryprice(0,currententries-1) + 30 then sell("300LE") next bar at market;
end;

The problem now is that , sometimes it is work and sometimes don't.
Please teach me how to improve it , thanks .

User avatar
rrams
Posts: 128
Joined: 10 Feb 2011
Location: USA
Has thanked: 7 times
Been thanked: 70 times
Contact:

Re: Please help with the script Power Language.

Postby rrams » 13 Jul 2022

335, what you describe is called a ratchet stop. There are many ways to program it depending on if you are working with positions or entries, IOG or not, etc. George Pruitt has some good free lessons and books.

I would do something like (untested):

Code: Select all

vars: PointAmount(-30); if MaxContractProfit>=40 then PointAmount=30 else if MaxContractProfit>=20 then PointAmount=10 else PointAmount=-30; if MarketPosition>0 then sell("LX") next bar EntryPrice+PointAmount point stop; if MarketPosition<0 then buytocover("SX") next bar EntryPrice-PointAmount point stop;
Remember that strategy code might work perfectly in backtesting but fail in real trading due to the broker adapter not updating position information. You need to code prints of what is happening and how it is failing.


Return to “MultiCharts”