Help with a study - StopLoss

Questions about MultiCharts and user contributed studies.
omer95
Posts: 5
Joined: 09 May 2023

Help with a study - StopLoss

Postby omer95 » 10 May 2023

hello everyone,
I am having difficulties to set stoploss as i want in my script, for example in this script:

Code: Select all

input: AVGFAST(50), AVGMID(100), AVGSLOW(200); vars: maFast(0), maMid(0), maSlow(0), isLongCondtion(false), entryBarLow(0), stopLossPrice(0), takeProfitPrice(0); maFast = XAverage(close, AVGFAST); maMid = XAverage(close, AVGMID); maSlow = XAverage(close, AVGSLOW); isLongCondtion = (maFast > maMid and maMid > maSlow); if marketposition = 0 then begin if isLongCondtion and close cross above maFast then begin entryBarLow = low[0] - 1; buy next bar at market; end; end; if marketposition = 1 then begin if close cross below entryBarLow then sell next bar at market; setprofittarget(50); end;
i would like to set a stoploss at the low of the entry bar -1. the problem is that its seems that the value of the entry bar that im trying to save and use as a stoploss does not work. see attached a picture with stoploss required.
Many thanks.
Attachments
example.png
(32.49 KiB) Not downloaded yet

User avatar
Vlada MultiCharts
Posts: 293
Joined: 22 Apr 2020
Has thanked: 8 times
Been thanked: 76 times

Re: Help with a study - StopLoss

Postby Vlada MultiCharts » 11 May 2023

Hello omer95,

Unfortunately, you cannot address the values of the future bars. It is relevant for backtesting as well. MultiCharts treats the subsequent bars as though they have not been plotted yet.

buy next bar at market; will be executed when Open = Hight = Low = Close on the bar that follows the bar on which the script is being calculated.

omer95
Posts: 5
Joined: 09 May 2023

Re: Help with a study - StopLoss

Postby omer95 » 18 May 2023

Hello omer95,

Unfortunately, you cannot address the values of the future bars. It is relevant for backtesting as well. MultiCharts treats the subsequent bars as though they have not been plotted yet.

buy next bar at market; will be executed when Open = Hight = Low = Close on the bar that follows the bar on which the script is being calculated.
Hi Vlada, thanks for your reply,

I was able to figure out the problem in the script I sent as you mentioned.
Unfortunately I still don't know how I can place a stoploss according to the entrance bar.
I would appreciate it if you could continue the example I wrote here so that I can understand the idea of ​​how to do this.

Code: Select all

AVGFAST(50), AVGMID(100), AVGSLOW(200); vars: maFast(0), maMid(0), maSlow(0), isLongCondtion(false), entryBarLow(0), stopLossPrice(0), takeProfitPrice(0); maFast = XAverage(close, AVGFAST); maMid = XAverage(close, AVGMID); maSlow = XAverage(close, AVGSLOW); isLongCondtion = (maFast > maMid and maMid > maSlow);
Many thanks Omer

User avatar
ZaphodB
Posts: 64
Joined: 12 Feb 2019
Has thanked: 20 times
Been thanked: 2 times

Re: Help with a study - StopLoss

Postby ZaphodB » 21 May 2023

There is a function that tells you the number bars back you made the entry. You could use this to set the stoploss but it's not going to be accurate/set until the first bar AFTER the entry is made. Something like this. Maybe it will work for you

Code: Select all

MyEntryBarLow = Low [ BarsSinceEntry(0) ]

omer95
Posts: 5
Joined: 09 May 2023

Re: Help with a study - StopLoss

Postby omer95 » 23 May 2023

i would like to thank you for your replays as Vlada said the problem was "buy next bar at market", i change it to "buy this bar at close" and its works fine.

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

Re: Help with a study - StopLoss

Postby TJ » 24 May 2023

See Post #7
7) This Bar on Close
viewtopic.php?t=10811


Return to “MultiCharts”