Gordon
Attached is a basic one I found, using moving average cross overs and % trailing stop loss. The results are shown as markers, but can be displayed as text by adding code to change the line type to text.
Is this the kind of thing you were looking for?
CR
{Input criteria}
startDate := inputdate("Start date");
endDate := inputdate("End date");
entryMA1 := input("entry fast MA", 10, 1);
entryMA2 := input("entry slow MA", 20, 1);
trailingPerc := input("Trailing Stop %", 5, 0);
{*********************************************************** **************************************************}
{fixed data}
tradeSize := 10000;
commission := 0.002*tradesize;
{*********************************************************** **************************************************}
{calculate moving averages}
ma1 := ma(C,entryMA1,E);
ma2 := ma(C,entryMA2,E);
{*********************************************************** **************************************************}
{Detect entry trigger}
validDate := now>=startDate and now<=endDate; {check that time is within the nominated trade dates}
maEntry := hist(cross(ma1, ma2) ,1);
enterWhen := maEntry and validDate;
{*********************************************************** **************************************************}
{Detect exit triggers}
{Detect moving average exit}
maExit := hist(cross(ma2, ma1) ,1);
{Detect date exit}
dateExit := if(now>=endDate,1,0);
{Detect SL exit}
anotherExit := maExit;
SLposition := if(enterWhen,1,if(anotherExit,0,prev));
buyToday := SLposition=1 and hist(SLposition,1)=0;
dayAfterBuy := hist(buyToday,1)=1;
{stop loss or profit protection, based on closing prices}
trailingSL := highestsince(1, dayAfterBuy, hist(C,1)*(1-trailingPerc/100));
SLexit := C<=trailingSL and hist(C,1)>trailingSL;
{Set exit trigger}
exitWhen := hist(maExit,1) or hist(SLexit,1) or dateExit;
{*********************************************************** **************************************************}
{Calculate buy & sell prices and determine position status}
buyPrice := (H+L)/2;
sellPrice := (H+L)/2;
position := if(enterWhen,1,if(exitWhen,0,prev)); {Determine position status based on ma entry and exit}
enterReal := position=1 and hist(position,1)=0;
lastBuy := if(enterReal,buyPrice,prev);
exitReal := position=0 and hist(position,1)=1;
{*********************************************************** **************************************************}
{Calculate win size and accumulated profit}
shareQty := tradeSize/lastBuy;
tradeWin := (if(exitReal, sellPrice-lastBuy, 0)*shareQty - commission)*100/tradeSize;
profit := if(exitReal, (tradeWin+prev), prev);
{Display results}
Win := if(tradeWin>0, tradeWin, 0);
Loss := if(tradeWin<0, tradeWin, 0);
[color=black]; {Display entry mark}
if(enterReal,5,0);
[color=limegreen];
Win;
[color=red];
Loss;
[color=blue];
profit;
|