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;
How to get a strategy to recognize strategy start bar
-
- Posts: 5
- Joined: 11 Jun 2014
- 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
Perhaps something like this can help?However, BarsSinceExit(1) will look back forever for a previous exit.
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;
-
- Posts: 5
- Joined: 11 Jun 2014
Re: How to get a strategy to recognize strategy start bar
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:can you expand on your code example and question?
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 ;
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;
Or, feel free to let me know if I'm an "Idgit".
-
- Posts: 5
- Joined: 11 Jun 2014
Re: How to get a strategy to recognize strategy start bar
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.
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.
-
- Posts: 5
- Joined: 11 Jun 2014
Re: How to get a strategy to recognize strategy start bar
I've just worked out a "Start" function that could do the trick.
Again, let me know
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;
- ABC
- Posts: 720
- 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
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
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.
-
- Posts: 5
- Joined: 11 Jun 2014
Re: How to get a strategy to recognize strategy start bar
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?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.