On the left side you see a tiny EU chart. Each candle or bar represent 1 hour of price movement. If you right click on the chart (or press CTRL-E) you can export the data behind this chart. On the right you see the exported data for this tiny chart.
The data in each column is a SERIE of data. For example we have a SERIE of opening prices.
1,40756 - 1,40832 - 1,40838 - 1,40748 - 1,40720 – 1,41014
position 1 – position 2 – position3 – position 4 - position 5 - position 6
The FRONT of a serie is the place of the first data in that serie. In the above serie the FRONT is position 1 with holds the data 1,40756.
The BACK of a serie is the place of the last data in that serie. In the above serie the back is the last position, this is position 6 with holds the data 1,41014.
The first column in our tiny chart indicates when the candle was starting to take shape. The open price was fixed at 24.03.2011 @16h00. The value of the HIGH, LOW, CLOSE were only available when the next hour candle started.
A strategy runs from candle to candle. Take the following strategy.
strategy bar;
input lots = 1,maxloss=40,offset=0.0001;
begin
integer currentbar;
if back(close) < front(close) then return;
currentbar := back(close);
if close[currentbar]>open[currentbar] then
begin
if short() then exitshort();
if not long() then buy(lots);
end;
if close[currentbar]<open[currentbar] then
begin
if long() then exitlong();
if not short() then sell(lots);
end;
end.
The strategy strategy runs at the end of each candle. In the following table you see the used values at each candle.
currentbar | back(close) | front(close) | close[currentbar] | open[currentbar] |
1 | 1 | 1 | 1,40831 | 1,40756 |
2 | 2 | 1 | 1,40838 | 1,40832 |
3 | 3 | 1 | 1,40749 | 1,40838 |
4 | 4 | 1 | 1,40720 | 1,40748 |
5 | 5 | 1 | 1,41013 | 1,40720 |
6 | 6 | 1 | 1,41242 | 1,41014 |
Take a piece of paper. Put it on the chart so you can only see the first candle. Take another piece of paper and cover the lines below so you can only see the first line.
1 | 1 | 1 | 1,40831 | 1,40756 |
Now move the paper so you can see 2 candles. On the above table cover all lines except the second one.
Now move the paper so you can see 3 candle. On the above table cover all lines except the third line.
..........
This is how the strategy runs from candle to candle.
Really appreciate your posts! That solves many of my confusions!
ReplyDeleteJust one quick question: what is the purpose of the below line since it seems back(close) is always equal to or greater than front(close)
if back(close) < front(close) then return;
Very good remark. Actually in this small strategy you don't need it. I placed it out of habit. Because in most strategies you do need it. If the strategy would use a moving average with period 20 for example the line would be:
ReplyDeleteif back(close) < front(close) +20 then return;
This would make sure the lines lower aren't executed when we don't have enough candles to calculate the moving average. You need 20 candles to calculate a ma with period 20.
In most strategies you will find
if back(close) < front(close) + max( period1,period2) then return;
This will make sure the lines below aren't executed when we don't have enough candles to calculate the technical indicators used. In this case 2 technical indicators.
You can also take it further and write
max(period1 , max(period2,period3));
…....
Hi,
ReplyDeleteThanks for writing this front back coding post, it help me understand a great deal about these two codings as I had been trying to figure out how it works.
Very well elaborated, thanks a lot for the help :)
Best Regards,
Emmanuel
Hi there...nice try...but my brain capacities prevent me from sorting out a strategy error ;-)
ReplyDeletePart of the code:
begin
if back(close) <= front(close) +300 then return;
currentbar := back(close);
I took this from the TrendSurfer Strategy.
And adapt it to run with home made indicator.
The issue is that it only displays Buy/Sell/Exit signals back in 2007 and only during 4 months...
I cant have running on all candles from 2006 till today.
I tried to change it to
if back(close) <= Front(close) then return;
no luck
I also changed the +300 from +1 to +100000...the strategy will display signals for few months randomly but will never displays them for all candles on the chart...
Any ideas as I running out of clues
Many thanks
Henri
Would be alot easier if you post the complete code.......
ReplyDeleteSure... below is Strategy code
ReplyDeletestrategy RF_StratSignalsv2;
input lots=1, StopLoss=-50, TakeProfit=100;
vars currentbar(number),
PositiveTrend(series), NegativeTrend(series),
EnterShort(series),EnterLong(series),
KeepLong(series), KeepShort(series);
begin
if back(close) <= front(close)+1 then return;
currentbar := back(close);
RF_Signals_SimpleEMAv5(3,6,28,14,7,14,70,30,50,60,80,90,40,30,20,10);
NegativeTrend := RF_Signals_SimpleEMAv5.Trenddown;
EnterShort := RF_Signals_SimpleEMAv5.DTrend;
EnterLong := RF_Signals_SimpleEMAv5.UTrend;
KeepLong := RF_Signals_SimpleEMAv5.KeepCall;
KeepShort := RF_Signals_SimpleEMAv5.KeepShort;
PositiveTrend := RF_Signals_SimpleEMAv5.Trendup;
//Enter Long Position//
if (PositiveTrend[currentbar] = 1) then
begin
if short() then exitshort();
if ((EnterLong[currentbar] = -1) and (EnterLong[currentbar-1] = 0))then
begin
if not long() then buy(lots);
if long() then
begin
if ((KeepLong[currentbar] = 0) and ((KeepLong[currentbar-1] = 3) or (fpl()=StopLoss))) then
begin
if long() then exitlong();
end;
end;
end;
end;
//Enter Short Position//
if (NegativeTrend[currentbar] = -1) then
begin
if long() then exitlong();
if ((EnterShort[currentbar] = 1) and (EnterShort[currentbar-1] =0))then
begin
if not short() then sell(lots);
if short() then
begin
if ((KeepShort[currentbar] = 0) and ((KeepShort[currentbar-1] = -3) or (fpl()=StopLoss)))then
begin
if short() then exitshort();
end;
end;
end;
end;
end.
The strategy is to be run on daily chart.
ReplyDeleteAs the home made indicator does.
If you give me a lead, you would definitively help around.
Henri
Well as most programmers will say:"avoid nested IF's at all time”. Meaning never exaggerate with IF's in other IF's. You need to rewrite this code. A IF four levels deeps is to much. :-)
ReplyDelete//when to open long//
//when to open short//
//when to close long//
//when to close short//
//when to close short and open long//
//when to close long and open short//
if back(close) <= front(close)+X then return;
currentbar := back(close);
Well the value of X depends on the number of candles the indicator RF_Signals_SimpleEMAv5 needs. Need to see code of indicator to tell.
You spoted right! Thanks for the light CTLpro
ReplyDeletebelow is the new code...works great...X is left at 100
strategy RF_StratSignalsv3;
input lots=1, StopLoss=-50, TakeProfit=100;
vars currentbar(number),
PositiveTrend(series), NegativeTrend(series),
EnterShort(series),EnterLong(series),
KeepLong(series), KeepShort(series);
begin
if back(close) <= front(close)+ 100 then return;
currentbar := back(close);
RF_Signals_SimpleEMAv5(3,6,28,14,7,14,70,30,50,60,80,90,40,30,20,10);
NegativeTrend := RF_Signals_SimpleEMAv5.Trenddown;
EnterShort := RF_Signals_SimpleEMAv5.DTrend;
EnterLong := RF_Signals_SimpleEMAv5.UTrend;
KeepLong := RF_Signals_SimpleEMAv5.KeepCall;
KeepShort := RF_Signals_SimpleEMAv5.KeepShort;
PositiveTrend := RF_Signals_SimpleEMAv5.Trendup;
//Enter Long Position//
if (PositiveTrend[currentbar] = 1) and
(EnterLong[currentbar] = 0) and
(EnterLong[currentbar-1] = -1) and
not short() then
buy(lots);
//Exit long
if long() and
KeepLong[currentbar]=0 and
KeepLong[currentbar-1]=3 and
fpl()TakeProfit then
exitlong();
//Enter Short Position//
if (NegativeTrend[currentbar] = -1) and
(EnterShort[currentbar] = 0) and
(EnterShort[currentbar-1] = 1) and
not long() then
sell(lots);
//Exit short
if short() and
KeepShort[currentbar] = 0 and
KeepShort[currentbar-1] = -3 and
fpl()TakeProfit then
exitshort();
end.