what is wrong?

Questions about MultiCharts and user contributed studies.
RedBerlin
Posts: 81
Joined: 05 Dec 2014

what is wrong?

Postby RedBerlin » 01 Mar 2015

Code: Select all

Inputs:

ATRLength(4),

ATRMult(2.1),

TicksAway(30);

Vars:

vATR(0),

vAVG(0),

vUp(0),

vDn(0),

vSuperTrend(0),

vTrend(1),

vFlag(0),

vFlagh(0),

vStopLossPrice(0),

vEndTimeS(172000);



vATR=averagetruerange(ATRLength)*ATRMult;

vAVG=(high+low)/2;

vUp=vAVG+vATR;

vDn=vAVG-vATR;

if close>vUp[1] then vTrend=1

else if close<vDn[1] then vTrend=-1;

if vTrend<0 and vTrend[1]>0 then vFlag=1 else vFlag=0;

if vTrend>0 and vTrend[1]<0 then vFlagh=1 else vFlagh=0;

if vTrend>0 and vDn<vDn[1] then vDn=vDn[1];

if vTrend<0 and vUp>vUp[1] then vUp=vUp[1];

if vFlag=1 then vUp=vAVG+vATR;

if vFlagh=1 then vDn=vAVG-vATR;

if time_s<vEndTimeS then begin

if vTrend=1 and vTrend[1]>0 then begin

buy next bar Close+4 limit;

// setta stoploss iniziale

vStopLossPrice=(close+4)-TicksAway;

end;

if vTrend=-1 and vTrend[1]<0 then begin

sellshort next bar Close-4 limit;

// setta stoploss iniziale

vStopLossPrice=(close-4)+TicksAway;

end;

end;

// Manage open long position

if (MarketPosition(0) > 0) then begin

// Alla chiusura della barra aggiorna il trailingstop se long

if (BarStatus(1) = 2) then begin



// se togli // avrai un ricalcolo del trailing stop

// se lasci // avrai invece uno stoploss fisso

// vStopLossPrice = MaxList(vStopLossPrice , Close-TicksAway);



end;

Sell all contracts next bar at vStopLossPrice stop;

end;

// Manage open short position

if (MarketPosition(0) < 0) then begin

// Alla chiusura della barra aggiorna il trailing stop se short

if (BarStatus(1) = 2) then begin

// se togli // avrai un ricalcolo del trailing stop

// se lasci // avrai invece uno stoploss fisso

// vStopLossPrice = MinList(vStopLossPrice , Close+TicksAway);



end;

buytocover all contracts next bar at vStopLossPrice stop;

end;

if time_s>vEndTimeS then begin

Sell all contracts next bar at market;

buytocover all contracts next bar at market;

end;
hi
someone could please see how it doesn t work?
grazie

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

Re: what is wrong?

Postby JoshM » 02 Mar 2015

someone could please see how it doesn t work?
What does "it doesn't work" mean in this context?

And this time, if I may suggest, please give a thorough explanation of what you mean (so we don't need to guess or ask 10 follow-up questions), keep it factual & on-topic, and don't use many exclamation marks when making your point. That would make providing help much more enjoyable.

RedBerlin
Posts: 81
Joined: 05 Dec 2014

Re: what is wrong?

Postby RedBerlin » 02 Mar 2015

someone could please see how it doesn t work?
What does "it doesn't work" mean in this context?

And this time, if I may suggest, please give a thorough explanation of what you mean (so we don't need to guess or ask 10 follow-up questions), keep it factual & on-topic, and don't use many exclamation marks when making your point. That would make providing help much more enjoyable.
my apologize Josh
sorry for my post but I m new multicharts's user and Im very unexpert about easy language.
I ve asked support to set stop loss and take profit payng as well.
the code above doesnt enter stop loss for example.
I have a lot question but for begin I d like at least set stoploss and take profit with your help .
1) my broker doesnt accept=setstoploss(100);=I need strategy for it,
same thing for take profit.
I try to do it but there is not stop in backtest.
in real time I didnt try it.
2)is possible buy 2 tiks up or down?
example=with "buy next bar at open "buy 2 tiks up.
grazie

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

Re: what is wrong?

Postby JoshM » 02 Mar 2015

the code above doesnt enter stop loss for example.
That's because the `vStopLossPrice` variable is calculated each time when the if statement above it is true. For example:

Code: Select all

if (vTrend = 1) and (vTrend[1] > 0) then begin

buy ("EL") next bar Close + 4 limit;

vStopLossPrice = (Close + 4) - TicksAway;

end;
So if you're long and this `vTrend` condition happens, the `vStopLossPrice` is calculated again. That way it will keep moving along with the price. One approach to this is to use MarketPosition to only calculate the stop loss price when there is no open position (see example below).
I try to do it but there is not stop in backtest.
in real time I didnt try it.
See the point above for why this happened. Now, with the updated code, it works for me:

Image
2)is possible buy 2 tiks up or down?
example=with "buy next bar at open "buy 2 tiks up.
Aren't you already doing this in the code, with the limit order 4 ticks above the close of the current bar?

Code: Select all

buy ("EL") next bar Close + 4 limit;

*****************************

Code: Select all

Inputs:
ATRLength(4),
ATRMult(2.1),
TicksAway(30);

Variables:
vATR(0),
vAVG(0),
vUp(0),
vDn(0),
vSuperTrend(0),
vTrend(1),
vFlag(0),
vFlagh(0),
vStopLossPrice(0),
vTakeProfitPrice(0),
vEndTimeS(172000);


vATR = averagetruerange(ATRLength) * ATRMult;
vAVG = (high + low) / 2;
vUp = vAVG + vATR;
vDn = vAVG - vATR;

if (Close > vUp[1]) then
vTrend = 1
else if (Close < vDn[1]) then
vTrend = -1;

if (vTrend < 0) and (vTrend[1] > 0) then
vFlag = 1
else
vFlag = 0;

if (vTrend > 0) and (vTrend[1] < 0) then
vFlagh = 1
else
vFlagh = 0;

if (vTrend > 0) and (vDn < vDn[1]) then
vDn = vDn[1];

if (vTrend < 0) and (vUp > vUp[1]) then
vUp = vUp[1];

if (vFlag = 1) then
vUp = vAVG + vATR;

if (vFlagh = 1) then
vDn = vAVG - vATR;

// Only trade during certain times
if (Time_s < vEndTimeS) and (MarketPosition(0) = 0) then begin

// Enter long
if (vTrend = 1) and (vTrend[1] > 0) then begin

buy ("EL") next bar Close + 4 limit;

vStopLossPrice = (Close + 4) - TicksAway;
vTakeProfitPrice = Close + TicksAway;

end;

// Enter short
if (vTrend = -1) and (vTrend[1] < 0) then begin

sellshort ("SE") next bar Close - 4 limit;

vStopLossPrice = (Close - 4) + TicksAway;
vTakeProfitPrice = Close - TicksAway;

end;

end;

// Manage open long position
if (MarketPosition(0) > 0) then begin

Sell ("XL-STP") all contracts next bar at vStopLossPrice stop;
Sell ("XL-TP") all contracts next bar at vTakeProfitPrice limit;

end;

// Manage open short position
if (MarketPosition(0) < 0) then begin

BuyToCover ("XS-STP") all contracts next bar at vStopLossPrice stop;
BuyToCover ("XS-TP") all contracts next bar at vTakeProfitPrice limit;

end;

// Exit on close
if (Time_s > vEndTimeS) then begin

Sell ("XL-TIME") all contracts next bar at market;

BuyToCover ("XS-TIME") all contracts next bar at market;

end;
Attachments
scr.02-03-2015 13.01.41.png
(11.5 KiB) Downloaded 704 times

RedBerlin
Posts: 81
Joined: 05 Dec 2014

Re: what is wrong?

Postby RedBerlin » 02 Mar 2015

the code above doesnt enter stop loss for example.
That's because the `vStopLossPrice` variable is calculated each time when the if statement above it is true. For example:

Code: Select all

if (vTrend = 1) and (vTrend[1] > 0) then begin

buy ("EL") next bar Close + 4 limit;

vStopLossPrice = (Close + 4) - TicksAway;

end;
So if you're long and this `vTrend` condition happens, the `vStopLossPrice` is calculated again. That way it will keep moving along with the price. One approach to this is to use MarketPosition to only calculate the stop loss price when there is no open position (see example below).
I try to do it but there is not stop in backtest.
in real time I didnt try it.
See the point above for why this happened. Now, with the updated code, it works for me:

Image
2)is possible buy 2 tiks up or down?
example=with "buy next bar at open "buy 2 tiks up.
Aren't you already doing this in the code, with the limit order 4 ticks above the close of the current bar?

Code: Select all

buy ("EL") next bar Close + 4 limit;

grazie mille Josh but with your code it continue to enter in market every bar.
about 2 tiks up ,I use +4 limit because my broker doesnt accept sellshort at market!!
however ,wth your code my ts contine to ebter market every bar!!

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

Re: what is wrong?

Postby JoshM » 03 Mar 2015

with your code it continue to enter in market every bar.
I didn't change the enter long or enter short conditions, there are still:

Code: Select all

// Enter long
if (vTrend = 1) and (vTrend[1] > 0) then begin

(...)

// Enter short
if (vTrend = -1) and (vTrend[1] < 0) then begin
These come from your code. If you want to have less trades, you probably need to thinker with these two conditions a bit.
about 2 tiks up ,I use +4 limit because my broker doesnt accept sellshort at market!!
Not sure if I follow you here if my previous comment about this didn't help.
however ,wth your code my ts contine to ebter market every bar!!
It's not my code. I didn't change the entry conditions as far as I'm aware of.

RedBerlin
Posts: 81
Joined: 05 Dec 2014

Re: what is wrong?

Postby RedBerlin » 03 Mar 2015

with your code it continue to enter in market every bar.
I didn't change the enter long or enter short conditions, there are still:

Code: Select all

// Enter long
if (vTrend = 1) and (vTrend[1] > 0) then begin

(...)

// Enter short
if (vTrend = -1) and (vTrend[1] < 0) then begin
These come from your code. If you want to have less trades, you probably need to thinker with these two conditions a bit.
thank a lot Josh for help, but I tried to solve this problem but I couldnt.
have you any ideas to what I have to change?
in this way is impossible trading.
grazie

RedBerlin
Posts: 81
Joined: 05 Dec 2014

Re: what is wrong?

Postby RedBerlin » 03 Mar 2015

Josh sorry
I ve seen that in real time it doesnt enter each bar but desnt enter at color change as well.
maybe it enter in market each 30 ticks?
i dont know but I see it doesnt follow indicator.
stop and profit is ok but entry at market are irregular.
if you try in real time you can see if you have time.
ciao

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

Re: what is wrong?

Postby JoshM » 04 Mar 2015

have you any ideas to what I have to change?
in this way is impossible trading.
I suppose you can make the entry conditions more restrictive like this:

Code: Select all

// Enter long
if (vTrend = 1) and (vTrend[1] > 0) and (vTrend[2] > 0) then begin

(...)

// Enter short
if (vTrend = -1) and (vTrend[1] < 0) and (vTrend[2] < 0) then begin
But then you'd also enter trades later, reducing the profit on those trades that would have been profitable. You can also add another indicator as a filter.
i dont know but I see it doesnt follow indicator.
See How to make indicator and signal calculation results the same. If after following that wiki page there are still irregularities, there has to be a difference in the code of the indicator and strategy.

RedBerlin
Posts: 81
Joined: 05 Dec 2014

Re: what is wrong?

Postby RedBerlin » 04 Mar 2015

have you any ideas to what I have to change?
in this way is impossible trading.
I suppose you can make the entry conditions more restrictive like this:

Code: Select all

// Enter long
if (vTrend = 1) and (vTrend[1] > 0) and (vTrend[2] > 0) then begin

(...)

// Enter short
if (vTrend = -1) and (vTrend[1] < 0) and (vTrend[2] < 0) then begin
.
nothin g to do Josh...it doesnt work unfortunately.
thanks a lot however.


Return to “MultiCharts”