How to get a strategy to recognize strategy start bar

Questions about MultiCharts and user contributed studies.
stevenasmith1982
Posts: 5
Joined: 11 Jun 2014

How to get a strategy to recognize strategy start bar

Postby stevenasmith1982 » 11 Jun 2014

I'm working on a strategy that references the highest high and lowest low since exit of the last position while flat. However, BarsSinceExit(1) will look back forever for a previous exit. So far, I've got this:

Once Var1 = ComputerDateTime;
// Var2 = convert Var1 into a "BarsAgo" number ------ This is where I need Help

If MaxPositionsAgo = 0 then begin
// Run calculations using Var2
End;

If MaxPositionsAgo > 0 then begin
// Run calculations using BarsSinceExit(1)
End;

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

Re: How to get a strategy to recognize strategy start bar

Postby JoshM » 12 Jun 2014

However, BarsSinceExit(1) will look back forever for a previous exit.
Perhaps something like this can help?

Code: Select all

Variables:
barsSinceLastPos(0);

if (TotalTrades > 0) then
barsSinceLastPos = BarsSinceExit(1);

if (barsSinceLastPos > 0) then begin

// work with the bars since the last
// position here

end;
If not, can you expand on your code example and question? I'm note sure what `Var2` does/needs to do: your code comment suggests DateTime conversion, while your post talks about the number of bars back.

stevenasmith1982
Posts: 5
Joined: 11 Jun 2014

Re: How to get a strategy to recognize strategy start bar

Postby stevenasmith1982 » 13 Jun 2014

can you expand on your code example and question?
My strategy concept is to create a true trailing stop that doesn't require a profit target to trigger before percent profit gets locked in, then apply it to long and flat positions. It started off looking something like this:

Code: Select all

Inputs: Len (0), PctTrl (0), X (0), Y (0), Z (0) ;
Vars: AvgRng (0), BuyOrder (0), SellOrder (0);

// Set variable to hold the average Range = (High-Low)
// If preferred, substitute Range with RealBody(RB) = AbsValue(Open-Close)
AvgRng = Average ( Range , Len );

If MarketPosition > 0 then begin
Value2 = ( AbsValue(EntryPrice(0)-Highest(High,BarsSinceEntry(0))) * (PctTrl/100) ) + AvgRng ;
SellOrder = Highest(High,BarsSinceEntry(0)) - Value2 ;
If Close crosses under SellOrder then sell all shares next bar Z stop;
end ;

If MarketPosition = 0 then Begin
Value3 = ( AbsValue(ExitPrice(1)-Lowest(Low,BarsSinceExit(1))) * (PctTrl/100) ) + AvgRng ;
BuyOrder = Lowest(Low,BarsSinceExit(1)) + Value3 ;
If Close crosses over BuyOrder then buy X shares next bar Y Stop;
end ;
The problem lies in the fact that BarsSinceExit(1) looks back forever for the most recent exit, which generates an error because it will reference more than Max Bars Sudy Will Reference in the settings. So, I was trying to find a way for the strategy to recognize when it was first started. Locking in the current date and time at the start of the strategy seemed the most logical place to start. However, I think I stumbled on a way to make it work with a combination of the MaxPositionsAgo keyword - And, the CurrentBar keyword or BarNumber function interchangeably.
Like this:

Code: Select all

//Add this to the beginning of the code
Var: Start (0);

Once Value1 = CurrentBar ;
Start = CurrentBar - Value1 ;

//In the MarketPosition = 0 section use the following

If MaxPositionsAgo = 0 then begin
Value3 = ( AbsValue(Open[Start]-Lowest(Low,Start)) * (PctTrl/100) ) + AvgRng ;
BuyOrder = Lowest(Low,Start) + Value3 ;
end ;

IfMaxPositionsAgo > 0 then begin
Value3 = ( AbsValue(ExitPrice(1)-Lowest(Low,BarsSinceExit(1))) * (PctTrl/100) ) + AvgRng ;
BuyOrder = Lowest(Low,BarsSinceExit(1)) + Value3 ;
end ;

If Close crosses over BuyOrder then buy X shares next bar Y Stop;

End;
Please let me know if I'm on the right track.
Or, feel free to let me know if I'm an "Idgit". :-)

stevenasmith1982
Posts: 5
Joined: 11 Jun 2014

Re: How to get a strategy to recognize strategy start bar

Postby stevenasmith1982 » 13 Jun 2014

Yup, I'm an Idgit. :-)
CurrentBar and BarNumber both go up with every intrabar recalculation.
Which means they quickly grow larger than the actual bars back than I'm trying to reference.
I need a way to tell this machine to only look back to a bar than can be "locked in" at the
start of the strategy.

stevenasmith1982
Posts: 5
Joined: 11 Jun 2014

Re: How to get a strategy to recognize strategy start bar

Postby stevenasmith1982 » 14 Jun 2014

I've just worked out a "Start" function that could do the trick.
Again, let me know

Code: Select all

If IntervalType_ex = 4 then begin
Value1 = LastCalcJDate / BarInterval ;
Once Value2 = LastCalcJDate / BarInterval ;
Start = Value1 - Value2; end;

If IntervalType_ex = 3 then begin
Value1 = ((LastCalcJDate*24) + HoursFromDateTime(ComputerDateTime)) / BarInterval ;
Once Value2 = ((LastCalcJDate*24) + HoursFromDateTime(ComputerDateTime)) / BarInterval ;
Start = Value1 - Value2; end;

If IntervalType_ex = 2 then begin
Value1 = ((LastCalcJDate*1440) + LastCalcMMTime) / BarInterval ;
Once Value2 = ((LastCalcJDate*1440) + LastCalcMMTime) / BarInterval ;
Start = Value1 - Value2; end;

If IntervalType_ex = 9 then begin
Value1 = ((LastCalcJDate*86400) + LastCalcSSTime) / BarInterval ;
Once Value2 = ((LastCalcJDate*86400) + LastCalcSSTime) / BarInterval ;
Start = Value1 - Value2; end;

User avatar
ABC
Posts: 718
Joined: 16 Dec 2006
Location: www.abctradinggroup.com
Has thanked: 125 times
Been thanked: 408 times
Contact:

Re: How to get a strategy to recognize strategy start bar

Postby ABC » 14 Jun 2014

stevenasmith1982,
that shouldn't be the case. Both grow with every bar and are independent from the calculation cycles of your code.
CurrentBar = 1 should be the first bar the strategy is computed on after the Max bars back. So in case your max bars back is set to 50, what your strategy returns as CurrentBar = 1 is the bar 51 on your chart.

Regards,
ABC
Yup, I'm an Idgit. :-)
CurrentBar and BarNumber both go up with every intrabar recalculation.
Which means they quickly grow larger than the actual bars back than I'm trying to reference.
I need a way to tell this machine to only look back to a bar than can be "locked in" at the
start of the strategy.

stevenasmith1982
Posts: 5
Joined: 11 Jun 2014

Re: How to get a strategy to recognize strategy start bar

Postby stevenasmith1982 » 17 Jun 2014

that shouldn't be the case. Both grow with every bar and are independent from the calculation cycles of your code.
CurrentBar = 1 should be the first bar the strategy is computed on after the Max bars back. So in case your max bars back is set to 50, what your strategy returns as CurrentBar = 1 is the bar 51 on your chart.
I did a little more tinkering and you're right. I changed the data range under "Format Instrument" to "Max Bars Study Will Reference" + 1 , and it started working correctly. but how do I optimize / backtest on a greater data range without getting the same error again?


Return to “MultiCharts”