AUTO AVERY MIDDLE TRADE STRATEGY

trading strategies and money management discussion, code, results

Moderator: moderators

User avatar
TheRumpledOne
rank: 10000+ posts
rank: 10000+ posts
Posts: 11440
Joined: Sun May 14, 2006 9:31 pm
Reputation: 45
Location: Oregon
Real name: Avery T. Horton, Jr.
Gender: None specified
Contact:

AUTO AVERY MIDDLE TRADE STRATEGY

Postby TheRumpledOne » Mon Jun 19, 2006 4:29 am

Perhaps someone could make this trigger intrabar... I have NOT learned how to code that yet.


{MiddleTrade001 - AUTO AVERY MidPoint Crossover Strategy }


{*************************************************}
{ I N P U T S }
{*************************************************}

inputs:

iPortfolioMgr(FALSE), {IF TRUE THEN IF iGreed_Amt reached, do not enter any more trades}


iQuantity(10),

iGreed_Amt( 1000 ); {how much you want to make in a day}



inputs:

iBackTest(false), {IF TRUE THEN BACKTEST MODE, USE CLOSE/OPEN INSTEAD OF InsideBid/InsideAsk}
iDUMP( false ), {IF TRUE THEN debug dumps data to file}
iFolder ( "c:\DATA\F\" ) , // folder for output


iStartDate(currentdate),

iExit( false ), // if true then use exits

iDebug( false ),
iDebugDate( currentdate ) ,

idummy("");


{*************************************************}
{ THESE VARIABLES ARE REALLY INPUTS }
{*************************************************}

inputs:

iOpenBuyZone(.10),


iMaxTradesLong(1),
iMaxTradesShort(1),

iCandleCountLongMin(0),
iCandleCountShortMin(0),

iBuyZoneHI(.20), {inside ask must be <= (OPEN + iBuyZoneHI) to place buy order}
iBuyZoneLO(.20), {inside bid must be >= (OPEN - iBuyZoneLO) to place buy order}


iGreed_PCT( 0 ), {how much you want to make in a day - percentage of stock price}


ShareOrPosition( 1 ), { pass in 1 for per share basis, 2 for position basis }

ProfitTargetAmt( 0 ), { pass in 0 if you don't want a profit target }

StopLossAmt( 0 ), { pass in 0 if you don't want a stop loss }

BreakevenFloorAmt( 0 ), { pass in 0 if you don't want a breakeven stop }
DollarTrailingAmt( 0 ), { pass in 0 if you don't want a dollar trailing stop }
PctTrailingFloorAmt( 0 ), { pass in 0 here and/or in next input if you don't want
a percent trailing stop }
PctTrailingPct( 0 ), { pass in 0 here and/or in previous input if you don't want
a percent trailing stop; else pass in XX for XX percent }
ExitOnClose( false ) ; { pass in true if you want to exit the position at the
close of the day, else pass in false. CAUTION: We recommend that you set this to
TRUE only for back-testing, if at all; in automated execution, the exit order
will NOT be filled at the close of the day; instead, the order can be sent into
the extended session as a limit order. }


{*************************************************}
{ V A R I A B L E S }
{*************************************************}

variables:

xQuickProfit(0),
xprice( 0 ),
xavgprice( 0 ),
Current_Profit(0),
Max_Profit(0),
Cur_Max_Profit(0),
xRTUnrealizedPL(0),
xRTRealizedPL(0),
xCurrentPL(0),
maxCurrentPL(0),
xBDAccountNetWorth(0),
xRTAccountNetWorth(0);

vars:
DayHigh( 0 ),
Middle( 0 ),
PrevMid( 0 ),
DayLow( 0 );




variables:

float xVolatility( 0 ) ;


{*************************************************}
{ V A R I A B L E S }
{*************************************************}
variables:

ShortBot( 0 ),
ShortTop( 0 ),
LongTop( 0 ),
LongBot( 0 ),

xGreed_Amt( 100000 ), {how much you want to make in a day, = stock price * iGreed_PCT * iQuantity}


xCurrentShares(0),


OldTotalTrades(0),
xNumTradesLong( 0 ),
xNumTradesShort( 0 ),

Last_Trade_Date ( 000000 ),

LastTradeType(""),

xEntryPrice(0),


xGetSymbolName(GetSymbolName),
xELDateToString(" "),
xELDateCurrent(" "),



dBackTest("t"), {switch}

tPass(0),

sLONG(FALSE), {switch}
sSHORT(FALSE), {switch}
sTREND(""), {switch}

sFirstPass(True), {switch - first time through the code}
StopTrading(TRUE); {switch}


{*************************************************}
{ V A R I A B L E S }
{*************************************************}

variables:
float ErrorCode( -1 ) ,


xInsideBid(0),
xInsideAsk(0),

xOldInsideBid(0),
xOldInsideAsk(0),


xCurrent_Profit(0),

xLast_Trade_Date ( 000000 ),
xSellPrice1(0),
xSellPrice2(0),
xTrailStop(0),
xProfitToday(0),
xCurrentNWPL(0),
xBuyingPower(0), {real time buying power}
xBuyZone(0), {distance from open}

xBuyZoneHI(0), {inside ask must be <= (xBuyZoneHI) to place buy order}
xBuyZoneLO(0), {inside bid must be >= (xBuyZoneLO) to place buy order}
xGap(0),
xGapABS(0),
xStop1(0),
xspread(0),


xDollarTrailingAmt( 0 ),
xProfitTargetAmt(0),
xStopLossAmt(0),
xBreakevenFloorAmt(0),
xPctTrailingFloorAmt(0),
xPBMultiplier(100), {position basis multiplier}

xMinutesPassed(0),
xAskBelowOpen(0),

xPremiumPaid(0), {Average share price - Open price}
sCase(0), {switch}

xDailyOpen(0),
xPrevClose(0),

xStrDailyOpen(0),
xStrPrevClose(0),
xRadDailyOpen(0),
xRadPrevClose(0),
xDifDailyOpen(0),
xDifPrevClose(0),

sTrailStop(0),
sBuyOpen(0),
sOKtoBuyGap(1), {switch}
sOKtoBuyZone(0), {switch}

sMAIN_BREAKER(1), {switch}

sAskBelowOpen(1), {switch}




tBuy(0), {trace variable}
tExit(0), {trace variable}

tTakeProfit(0), {trace variable}
tTrailingStop(0), {trace variable}
tBail(0), {trace variable}
tStopLoss(0), {trace variable}
tProtectDime(0), {trace variable}
tTimeExit(0), {trace variable}


tSetProfitTarget(0), {trace variable}
tSetStopLoss(0), {trace variable}
tSetBreakeven(0), {trace variable}
tSetDollarTrailing(0), {trace variable}
tSetPercentTrailing (0), {trace variable}

sTradeFlag(0), {switch used to keep buy condition true until filled because of TS auto cancel}

sWinner(FALSE), {switch}
sOKtoTrade(FALSE), {switch}
sOKtoPrint(FALSE), {switch}
sBuyingPower(FALSE), {switch, IF TRUE THEN enough real time buying power to place trade}
sStopTrading(FALSE), {switch}

sOKtoEXIT(FALSE), {switch}
sBuying(FALSE), {switch}

sDiscrepancy(FALSE), {switch}

{ use the "d" variables for dump report... until I can figure out how to dump logicals}

dStopTrading(" "),
dOKtoPrint(" "),

Variablesend(FALSE); {dummy - so I don't forget the ";" }


{*************************************************}
{ I N I T I A L I Z E F I E L D S }
{*************************************************}


xELDateToString = ELDateToString(Date);


{*************************************************}
{ F I R S T P A S S O N L Y }
{*************************************************}

IF sFirstPass = TRUE
THEN BEGIN

// xGetSymbolName = GetSymbolName;

IF iBackTest
THEN dBackTest = "T"
ELSE dBackTest = "F";

{ xGreed_Amt = iGreed_PCT * OpenD(0) * iQuantity; }

IF iDUMP = TRUE
THEN BEGIN

FileAppend( iFolder + xGetSymbolName + "_AAFG002_DUMP.txt",
"BAR DATE, " +
"TIME, " +
"STOCK, " +

"OPEN, " +
"CLOSE, "+
"HIGH, " +
"LOW, " +
"OpenD(0), " +
"xEntry, " +

"AVG PRICE, "+
"SHARES, " +

"TRADES L, " +
"TRADES S, " +

"DATE TODAY, "+
"CLOCK TIME, "+
"_AAFG002_," +
NewLine);

END; { IF iDUMP = TRUE }

END; {IF sFirstPass = TRUE}


{*************************************************}
{ C A L C U L A T I O N S }
{*************************************************}

IF ExitsToday(date this bar) > 0
THEN xProfitToday = PositionProfit(1) ;

tBuy = 0; {trace variable}
tExit = 0; {trace variable}
tTakeProfit = 0; {trace variable}
tTrailingStop = 0; {trace variable}
tBail = 0; {trace variable}
tStopLoss = 0; {trace variable}
tProtectDime = 0; {trace variable}
tTimeExit = 0; {trace variable}




{*************************************************}
{ FOR MULTI DAY TESTING }
{*************************************************}

if Date this bar > Last_Trade_Date
then begin

StopTrading = TRUE;

sLONG = false;
sSHORT = false;

sTREND = "";

Last_Trade_Date = Date this bar;

xNumTradesLong = 0;
xNumTradesShort = 0;

end;



{*************************************************}
{ keep track of number of trades }
{*************************************************}

IF TOTALTRADES > OldTotalTrades
THEN BEGIN

If LastTradeType = "LONG"
then xNumTradesLong = xNumTradesLong + 1;

If LastTradeType = "SHORT"
then xNumTradesShort = xNumTradesShort + 1;

OldTotalTrades = TotalTrades;
sTradeFlag = 0 ;

end; {IF TOTALTRADES > OldTotalTrades }


{*************************************************}
{ reset tradeflag if in a trade }
{*************************************************}

if MarketPosition <> 0
then sTradeFlag = 0 ;

{*************************************************}
{ compute previous session midpoint }
{*************************************************}


Middle = ( High + Low ) * .50 ;

PrevMid = ( High[1] + Low[1] ) * .50 ;


{*************************************************}
{ determine trade direction }
{*************************************************}

sLONG = false;
sSHORT = false;


If Close > PrevMid
then begin
sLONG = TRUE;
end
else
If Close < PrevMid
then begin
sSHORT = TRUE;
end; // If Close > PrevMid



{*************************************************}
{ BEGIN TRADING }
{*************************************************}



if xNumTradesLong < iMaxTradesLong
and close > PrevMid
and close > Middle
and sLONG = TRUE
then sTradeFlag = 1 ;

if sTradeFlag = 1
then begin
// buy ("MiddleTrade001 Buy") iQuantity shares next bar at LongTop stop;

// buy ("MiddleTrade001 Buy") iQuantity shares this bar on close;

buy ("Middle001 Buy") iQuantity shares NEXT BAR at market;

LastTradeType = "LONG";
end;


if xNumTradesShort < iMaxTradesShort
and close < PrevMid
and close < Middle
and sSHORT = TRUE
then sTradeFlag = -1 ;

if sTradeFlag = -1
then begin


// Sell Short ("MiddleTrade001 Short") iQuantity shares next bar at ShortBot stop;

// Sell Short ("MiddleTrade001 Short") iQuantity shares this bar on close;


Sell Short ("Middle001 Short") iQuantity shares NEXT BAR at market;


LastTradeType = "SHORT";
end;


{*************************************************}
{ Load trade variables }
{*************************************************}

xAvgPrice = avgentryprice;
xCurrentShares = currentshares;

{*************************************************}
{ T A K E P R O F I T }
{*************************************************}

if ShareOrPosition = 1
then SetStopShare
else SetStopPosition ;

if xGreed_Amt > 0 then
SetProfitTarget( xGreed_Amt ) ;

if ProfitTargetAmt > 0 then
SetProfitTarget( ProfitTargetAmt ) ;

if StopLossAmt > 0 then
SetStopLoss( StopLossAmt ) ;

if BreakevenFloorAmt > 0 then
SetBreakeven( BreakevenFloorAmt ) ;

if DollarTrailingAmt > 0 then
SetDollarTrailing( DollarTrailingAmt ) ;

if PctTrailingFloorAmt > 0 and PctTrailingPct > 0 then
SetPercentTrailing( PctTrailingFloorAmt, PctTrailingPct ) ;

if ExitOnClose = true then
SetExitOnClose ;

{*************************************************}
{ Check for exit }
{*************************************************}

If iExit
and marketposition <> 0
then begin


if close > PrevMid
and marketposition = -1
then begin
buy to cover ("Middle S-StopLoss") iQuantity shares NEXT BAR at market;
end;

if close < PrevMid
and marketposition = 1
then begin
Sell ("Middle L-StopLoss") iQuantity shares NEXT BAR at market;
end;

end; {If StopTrading = FALSE }

{*************************************************}
{ Check for exit }
{*************************************************}

if marketposition <> 0 then begin

{*************************************************}
{ C O M P U T E P R O F I T }
{*************************************************}

xavgprice = GetPositionAveragePrice (GetSymbolName, GetAccountID);

Cur_Max_Profit = HighD(0) - xavgprice;

If Cur_Max_Profit > Max_Profit then
Max_Profit = Cur_Max_Profit;

Current_Profit = close - xavgprice;

xQuickProfit = insidebid - xAvgPrice;
{*************************************************}
{ C O M P U T E P / L }
{*************************************************}

xRTUnrealizedPL = GetRTUnrealizedPL (GetAccountID);

xRTRealizedPL = GetRTRealizedPL (GetAccountID);

xCurrentPL = xRTUnrealizedPL + xRTRealizedPL;


{************************************************************}
{Don't risk your daily portfolio gross profit once made }
{************************************************************}

IF iPortfolioMgr = TRUE
AND marketposition <> 0
AND xRTRealizedPL + xRTUnrealizedPL > iGreed_Amt
AND insidebid < xAvgPrice
THEN begin
If marketposition = 1
then SELL ("ProtectGP SELL") next bar at market; {don't lose daily portfolio GP goal}
If marketposition = -1
then BUY TO COVER ("ProtectGP COVER") next bar at market; {don't lose daily portfolio GP goal}
END; {iPortfolioMgr = TRUE....}

END; {if marketposition <> 0 }



{*************************************************}
{ L A S T L I N E S }
{*************************************************}

sFirstPass = FALSE;



{*************************************************}
{ D U M P R E P O R T }
{*************************************************}

tPass = tPass + 1;

IF iDUMP = TRUE
THEN BEGIN

xELDateToString = ELDateToString(Date);


FileAppend( iFolder + xGetSymbolName + "_AAFG002_DUMP.txt",
XELDateToString + ", " +
NumToStr(Time,0)+ ", " +
{FormatTime("hh:mm:ss tt",Time) + ", " +}
GetSymbolName + ", " +


NumToStr(OPEN,2)+", "+
NumToStr(CLOSE,2)+", "+
NumToStr(HIGH,2)+", "+
NumToStr(LOW,2)+", "+
NumToStr(xDailyOpen ,2)+", "+
NumToStr(xEntryPrice,2)+", "+

NumToStr(xAvgPrice,2)+", "+
NumToStr(xCurrentShares,0)+", "+
NumToStr(xNumTradesLong ,0)+", "+
NumToStr(xNumTradesShort ,0)+", "+

xELDateCURRENT + ", " +
FormatTime("hh:mm:ss tt",ComputerDateTime()) + ", " +
NumToStr(tPass,0)+ ", " +

NewLine);

END;

{*************************************************}
{ E N D D U M P R E P O R T }
{*************************************************}




{*************************************************}
{ D E B U G }
{*************************************************}

If iDebug
and Date >= iDebugDate
then begin

print( GETSYMBOLNAME ,date," ",time,"=============== DEBUG ===================" );


// function call value1 = _fTrueFalseToStr(sLONG , "TRUE", "FALSE", tTrueFalse ) ;

var: tTrueFalse("");
VALUE1 = _fTrueFalseToStr(sLONG , "TRUE", "FALSE", tTrueFalse ) ;

print( date," ",time," sLONG ", tTrueFalse );

var: tTrueFalse2("");
VALUE1 = _fTrueFalseToStr(sShort , "TRUE", "FALSE", tTrueFalse2 ) ;

print( date," ",time," sShort ", tTrueFalse2 );

print( date," ",time," LastTradeType ", LastTradeType );

print( date," ",time," PrevMid ", numtostr(PrevMid , 4 ) );

print( date," ",time," Middle ", numtostr(Middle, 4 ) );


print( date," ",time," Close ", numtostr(Close, 4 ) );


end; // If iDebug




{*************************************************}
{ E N D D E B U G }
{*************************************************}

Please add www.kreslik.com to your ad blocker white list.
Thank you for your support.

Return to “strategy trading & money management”