Hi, I tried coding it in WEalth-Lab. Im getting strange results. Its supposed to be profitable on like 9 out of 10 markets, instead it seems to lose on those. Maybe I miscoded something. The rules are: 1) Flip a coin, take a position long/short 2) Use 3*10dayATR as a trailing stop 3) When stop hit, exit and go back to 1.
Here's the code:
var Upband, Downband, bar: integer;
var stop: float;
Upband := CreateSeries();
Downband := CreateSeries();
for Bar := 1 to BarCount()-1 do
begin
SetSeriesValue( Bar, Upband, (PriceClose(Bar-1)+3*ATR(bar-1, 10)));
SetSeriesValue( Bar, Downband, (PriceClose(Bar-1)-3*ATR(bar-1, 10)));
end;
PlotSeries( Upband, 0, #Red, #Thick );
PlotSeries( Downband, 0, #Green, #Thick );
HideVolume;
Randomize;
for Bar := 1 to BarCount()-1 do
begin
if not LastPositionActive() then
begin
if Random() > 0.5 then ShortAtMarket(bar, '') else BuyAtMarket(bar, '');
if PositionLong(LastActivePosition) then stop := GetSeriesValue( bar, Downband )
else stop := GetSeriesValue( bar, Upband );
end
else begin
if PositionLong(LastActivePosition) then begin
if GetSeriesValue( bar, Downband ) > stop
then stop := GetSeriesValue( bar, Downband );
if PriceClose(bar) < stop
then SellAtClose(bar, LastActivePosition, '');
end
else
if GetSeriesValue( bar, Upband ) < stop
then stop := GetSeriesValue( bar, Upband );
if PriceClose(bar) > stop then
CoverAtClose(bar, LastActivePosition, '');
end
end;
Here's the code:
var Upband, Downband, bar: integer;
var stop: float;
Upband := CreateSeries();
Downband := CreateSeries();
for Bar := 1 to BarCount()-1 do
begin
SetSeriesValue( Bar, Upband, (PriceClose(Bar-1)+3*ATR(bar-1, 10)));
SetSeriesValue( Bar, Downband, (PriceClose(Bar-1)-3*ATR(bar-1, 10)));
end;
PlotSeries( Upband, 0, #Red, #Thick );
PlotSeries( Downband, 0, #Green, #Thick );
HideVolume;
Randomize;
for Bar := 1 to BarCount()-1 do
begin
if not LastPositionActive() then
begin
if Random() > 0.5 then ShortAtMarket(bar, '') else BuyAtMarket(bar, '');
if PositionLong(LastActivePosition) then stop := GetSeriesValue( bar, Downband )
else stop := GetSeriesValue( bar, Upband );
end
else begin
if PositionLong(LastActivePosition) then begin
if GetSeriesValue( bar, Downband ) > stop
then stop := GetSeriesValue( bar, Downband );
if PriceClose(bar) < stop
then SellAtClose(bar, LastActivePosition, '');
end
else
if GetSeriesValue( bar, Upband ) < stop
then stop := GetSeriesValue( bar, Upband );
if PriceClose(bar) > stop then
CoverAtClose(bar, LastActivePosition, '');
end
end;

