close position

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

close position

Postby RedBerlin » 26 Sep 2017

please someone could take a look on this strategy and say why it non close position when time is reached.

Code: Select all

[LegacyColorValue = TRUE];

inputs:
ATRLength(2), ATRMult(2.4), UpColor(green), DnColor(red),TICKSAWAY(1),
starttrade (0800),
endtrade(1600),
dailyprofit(20),
dailyloss(50),
vEndTimeS(162000);

vars:
ATR(0),
avg(0),
dn(0),
up(0),
trend(1),
flag(0),
flagh(0),
SuperTrend(0),
todaynet(0),
yesterdaynet(0);
if date <> date[1] then begin
yesterdaynet = NetProfit;
end;
todaynet = NetProfit + openpositionprofit - yesterdaynet;






ATR = AvgTrueRange(ATRLength) * ATRMult;
avg = (high + low)/2;
up = avg + ATR;
dn = avg - ATR;

if close > up[1] then
trend = 1
else if close < dn[1] then
trend = -1;

if trend < 0 and trend[1] > 0 then flag=1 else flag=0;
if trend > 0 and trend[1] < 0 then flagh = 1 else flagh = 0;

if trend > 0 and dn < dn[1] then dn=dn[1];
if trend < 0 and up > up[1] then up=up[1];

if flag = 1 then up = avg + ATR;
if flagh = 1 then dn = avg - ATR;
condition98 = (-dailyloss < todaynet and todaynet < dailyprofit) and (Time > starttrade and time < endtrade);


if condition98 then begin

if trend = 1 and trend [1] < 0 then BUY next bar
MARKET;

if trend = -1 and trend [1] > 0 THEN SELLSHORT next bar market;



end;


it works but as I said if target daily is not reached and there is open position it continuos the nex day with this open position.
thanks

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

Re: close position

Postby TJ » 26 Sep 2017

please someone could take a look on this strategy and say why it non close position when time is reached.

Code: Select all

[LegacyColorValue = TRUE];

inputs:
ATRLength(2), ATRMult(2.4), UpColor(green), DnColor(red),TICKSAWAY(1),
starttrade (0800),
endtrade(1600),
dailyprofit(20),
dailyloss(50),
vEndTimeS(162000);

vars:
ATR(0),
avg(0),
dn(0),
up(0),
trend(1),
flag(0),
flagh(0),
SuperTrend(0),
todaynet(0),
yesterdaynet(0);
if date <> date[1] then begin
yesterdaynet = NetProfit;
end;
todaynet = NetProfit + openpositionprofit - yesterdaynet;

ATR = AvgTrueRange(ATRLength) * ATRMult;
avg = (high + low)/2;
up = avg + ATR;
dn = avg - ATR;

if close > up[1] then
trend = 1
else if close < dn[1] then
trend = -1;

if trend < 0 and trend[1] > 0 then flag=1 else flag=0;
if trend > 0 and trend[1] < 0 then flagh = 1 else flagh = 0;

if trend > 0 and dn < dn[1] then dn=dn[1];
if trend < 0 and up > up[1] then up=up[1];

if flag = 1 then up = avg + ATR;
if flagh = 1 then dn = avg - ATR;
condition98 = (-dailyloss < todaynet and todaynet < dailyprofit) and (Time > starttrade and time < endtrade);


if condition98 then begin

if trend = 1 and trend [1] < 0 then BUY next bar
MARKET;

if trend = -1 and trend [1] > 0 THEN SELLSHORT next bar market;



end;


it works but as I said if target daily is not reached and there is open position it continuos the nex day with this open position.
thanks
Which part of this code that says to close any open position by the end of the day?

RedBerlin
Posts: 81
Joined: 05 Dec 2014

Re: close position

Postby RedBerlin » 26 Sep 2017

HALLLO TJ
your help is always welcome.....
to be honest I did copy and paste from another forum and apply it .
so ,programming is not my daily food and if someone could help to correct this strategy it would be nice.
danke schon

TraderWalrus
Posts: 63
Joined: 13 Sep 2016
Has thanked: 30 times
Been thanked: 8 times

Re: close position

Postby TraderWalrus » 05 Oct 2017

Add something like:

Code: Select all

If time = endtrade then
begin
if marketposition(0) = 1 then
sell this bar on close
else if marketposition(0) = -1 then
buytocover this bar on close;
end;
- Exiting a position that way will not work in live trading, but it might be good enough for backtesting
- I didn't test it for complication errors, perhaps there are small corrections necessary.
- If you run scripts you will receive more value from the process if you learn to understand them.


Return to “MultiCharts”