Code to pyramid up to N contracts on a day trade basis.
input: maxSize(5),startTime(1000),endTime(1555);
var: stb(0),sts(0),tpAmt(0),lprft(0),sprft(0);
stb = High + minMove/priceScale;
sts = Low - minMove/priceScale;
print(date," ",time," ",stb," ",sts," ",currentShares);
if (time > startTime and time < endTime ) then
begin
tpAmt = average(range,10);
if(high>high[1]) then lprft = highD(0)+1*tpAmt;
if(low < low[1]) then sprft = lowD(0) -1*tpAmt;
if(currentShares < maxSize and c < average(c,9) and low < low[1] and close < close[1]) then buy("pyrabuy")next bar at sts limit;
if(currentShares < maxSize and c < average(c,9) and high >high[1] and close > close[1]) then sellShort("pyrasell") next bar at stb limit;
end;
//if(currentShares >= maxSize and marketPosition = 1) then sell("longmaxliq") next bar sts stop;
//if(currentShares >= maxSize and marketPosition =-1) then buyToCover("shortmaxliq") next bar stb stop;
if(marketPosition = 1) then sell("longProf") next bar lprft limit;
if(marketPosition =-1) then buytoCover("shortProf") next bar at sprft limit;
setexitonclose;
Here is a nice little function that helps determine market choppiness. This function measures the actual distance the market moves against the total distance traveled. If the market starts in the lower left of your chart and moves, without much gyrations, to the upper right then the CMI will reflect a higher value. However if the market moves frantically around the chart and doesn’t achieve a great distance from the initial to the end points then the CMI will reflect a lower number.
Inputs: periodLength(NumericSimple);
Vars: num(0),denom(1);
if(periodLength<>0)then
begin
denom = Highest(High,periodLength) - Lowest(Low,periodLength);
num =Close[periodLength - 1]- Close;
num = AbsValue(num);
if (denom <> 0) then ChoppyMarketIndex = num/denom * 100;
end;
You can use the CMI function to help determine when you should follow a Turtle style break out. If the market has been trading in a range and you get a break out it might just carry a little more validity. Buying/Selling at really high/low prices may lead to false break outs.
Backtesting with [Trade Station,Python,AmiBroker, Excel]. Intended for informational and educational purposes only!