Repeating Bar counter for trade exit  [SOLVED]

Questions about MultiCharts and user contributed studies.
GuppyDRV
Posts: 57
Joined: 20 Jan 2017
Has thanked: 1 time
Been thanked: 2 times

Repeating Bar counter for trade exit  [SOLVED]

Postby GuppyDRV » 18 Apr 2017

So....My objective is to have a peace of code that only functions on Non-live data so that it only works while back-testing. I'm trying to have it start on the 100th bar from the chart starting point and generate an order to close all open positions such that this repeats every 100 bars. This setup compiles but does not do what I'm trying to accomplish.

Has anyone tried something like this before that might be willing to point me in the right direction.

Thanks for the help.

GuppyDRV

Code: Select all



vars:

checklivedata (0),
MyCount (0),
BackTestTrade (False);

checklivedata = GetAppInfo(aiRealTimeCalc);

//Exit Trades Back Test Only

If checklivedata = 0 and Symbol_CurrentBar = 100 then begin

For MyCount = 99 downto 0 begin
BackTestTrade = True;
end;

If BackTestTrade = True then begin
Sell("ExitL") Next Bar at market;
buytocover("ExitS") Next Bar at market;
MyCount = 99;
BackTestTrade = False;
end;

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

Re: Repeating Bar counter for trade exit

Postby TJ » 18 Apr 2017

In your code, you have:

Code: Select all

If ..... Symbol_CurrentBar = 100 then begin
What happens when Symbol_CurrentBar = 101 ?

GuppyDRV
Posts: 57
Joined: 20 Jan 2017
Has thanked: 1 time
Been thanked: 2 times

Re: Repeating Bar counter for trade exit

Postby GuppyDRV » 19 Apr 2017

So...it will exit on the exact bar number specified (with the proper exit displayed "ExitL/ExitS). In this case 240 but if I change that to any other number say 500 it then exits on 500 etc..... It seems to start of properly but fails to repeat at the specified interval. My loop programing is faulty!

GuppyDRV

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

Re: Repeating Bar counter for trade exit

Postby TJ » 19 Apr 2017

So...it will exit on the exact bar number specified (with the proper exit displayed "ExitL/ExitS). In this case 240 but if I change that to any other number say 500 it then exits on 500 etc..... It seems to start of properly but fails to repeat at the specified interval. My loop programing is faulty!

GuppyDRV

You do not have a loop.

You have a repeat of some logic when Symbol_CurrentBar = 100.

Those "loop" operations NEVER went beyond bar 100.

GuppyDRV
Posts: 57
Joined: 20 Jan 2017
Has thanked: 1 time
Been thanked: 2 times

Re: Repeating Bar counter for trade exit

Postby GuppyDRV » 22 Apr 2017

I thought I would post this in case it might help some in the future. TJ thanks for the input as it got me thinking. I know the concepts are basic but sometimes the easy stuff still trips me up.

So I solved the issue by creating a simple counter. It now works on historical data only and places a repetitive exit at a specified bar interval.

Code: Select all


checklivedata (0),
MyCount (0),
BackTestTrade (False),

checklivedata = GetAppInfo(aiRealTimeCalc);

If checklivedata = 0 and Symbol_CurrentBar >= 180 then begin

MyCount = MyCount + 1;
if MyCount = 60 then
BackTestTrade = True;
end;

If BackTestTrade = True then begin
Sell("ExitL") Next Bar at market;
buytocover("ExitS") Next Bar at market;
MyCount = 0;
BackTestTrade = False;
end;

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

Re: Repeating Bar counter for trade exit

Postby TJ » 22 Apr 2017

I thought I would post this in case it might help some in the future. TJ thanks for the input as it got me thinking. I know the concepts are basic but sometimes the easy stuff still trips me up.

So I solved the issue by creating a simple counter. It now works on historical data only and places a repetitive exit at a specified bar interval.

Thank for reporting back and thanks for sharing your code with us.

Good trading to you.


Return to “MultiCharts”