#5 posted on 11/19/2009 at 10:27 AM
i have trouble understanding why a certain piece of code i wrote does not do what i want. could somebody have a look at it, pls?
if you look at the output you can see that for the first couple of days
ActualMarketPosition=MarketPosition+Positionchange. Thats what its supposed to do. After a couple of days though, depending on the data u use, this stops to be true. Whats happening? In order for this code to function properly, pyramiding needs to be switched to yes, all.
code follows here:
Meta:
subchart(true);
Inputs:
Delay(0), PortfolioWGT(1), Vperiod(20), FPV(1000), Gearing(1),
F1(10), F2(10), F3(10), F4(10), F5(10), StopF(2);
Variables:
MPosition, ActualMarketPosition, PositionChange,
O1, O2, O3, O4, O5;
//the normalized osciallators
O1 = ((XAverage(c[Delay], 2) - XAverage(c[Delay], 6)) * F1/10) / AvgTrueRange(VPeriod);
O2 = ((XAverage(c[Delay], 4) - XAverage(c[Delay], 12)) * F2/10) / AvgTrueRange(Vperiod);
O3 = ((XAverage(c[Delay], 6) - XAverage(c[Delay], 24)) * F3/10) / AvgTrueRange(Vperiod);
O4 = ((XAverage(c[Delay], 12) - XAverage(c[Delay], 48)) * F4/10) / AvgTrueRange(Vperiod);
O5 = ((XAverage(c[Delay], 24) - XAverage(c[Delay], 96)) * F5/10) / AvgTrueRange(Vperiod);
MPosition = O1+O2+O3+O4+O5;
ActualMarketPosition = round(MPosition * (InitialCapital) / FPV, 0);
PositionChange = ActualMarketPosition - ActualMarketPosition[1];
StopF = AvgTrueRange;
//prints
Print(formatdate(date, 1), MarketPosition, ActualMarketPosition, CurrentContracts * MarketPosition, PositionChange);
//if flat
If MarketPosition = 0 and PositionChange >= 0 then begin
Buy PositionChange Contracts next bar at open;
Print("0Buy", PositionChange);
end;
If MarketPosition = 0 and PositionChange < 0 then begin
Short abs(PositionChange) Contracts next bar at open;
Print("0Short", PositionChange);
end;
//if long
If MarketPosition = 1 and PositionChange >= 0 then begin
Buy PositionChange Contracts next bar at open;
Print("1Buy", PositionChange);
end;
If MarketPosition = 1 and PositionChange < 0 then begin
sell abs(PositionChange) Contracts next bar at open;
Print("1Sell", PositionChange);
end;
//if short
If MarketPosition = -1 and PositionChange >= 0 then begin
cover PositionChange Contracts next bar at open;
Print("-1Cover", PositionChange);
end;
If MarketPosition = -1 and PositionChange < 0 then begin
Short abs(PositionChange) Contracts next bar at open;
Print("-1Short", PositionChange);
end;
//SetStopContract();
//SetStopLoss(StopF * AvgTrueRange);
//Print(formatdate(date, 1), MarketPosition, ActualMarketPosition, CurrentContracts * MarketPosition, PositionChange);
//Print(equity, initialcapital, contracts);
Drawline(O1);
Drawline(O2);
Drawline(O3);
Drawline(O4);
Drawline(O5);
Drawline(Actualmarketposition, "Positionsize", 1,3);
DrawForest(CurrentContracts * MarketPosition, 0);