Can someone help explain what I am doing wrong. In the following code, it appears the variables ShortTarget and LongTarget change when ShortCondition or BuyCondition occur even though these conditions are not part of the definition.
Looking at a 30min chart RTH only of ESZ9-GLOBEX-FUT, I get a Sell on 9/30/2009 at 2:00PM which gives a ShortTarget of 1016.80. On 10/1/2009 at 8:30, ShortCondition = 1, but Short = 0 because of the ExRem statement, yet ShortTarget changes from 1016.80 to 1012.92 which is not intended.
Also, this "simple" excercise in coding this simple system is spiraling out of control as I keep thinking TJ's code would be like 3 lines, so I would appreciate suggested improvements for learning.
TIA,
James
SetFormulaName ("30 34 System");
/*
The 30/34 System is a swing system for trading the S&P E-mini futures (ES).
Signals are taken from a 30 minute chart of the ES with a 34 EMA (exponential moving average).
Only RTH data is used. No entries or exits are made during Globex session.
Entry rules:
Buy when price crosses 34 EMA +1 point.
Sell when price crosses below 34 EMA -1 point.
Profit target is entry price +/- 3%.
Buy stop is 34 EMA -5 points, MIT.
Sell stop is 34 EMA +5 points, MIT.
*/
BuyAvg = EMA (C,34) + 1;
SellAvg = EMA (C, 34) -1;
LongStopAvg = EMA (C,34) - 5;
ShortStopAvg = EMA (C,34) + 5;
BuyCondition = Cross (C, BuyAvg);
ShortCondition = Cross (SellAvg, C);
LongStopCondition = Cross (LongStopAvg, L);
ShortStopCondition = Cross (H, ShortStopAvg);
Buy = BuyCondition;
LongTarget = ValueWhen (Buy, C, 1) * 1.03;
LongProfitCondition = Cross (H, LongTarget);
Sell = LongStopCondition OR LongProfitCondition OR ShortCondition;
Short = ShortCondition;
ShortTarget = ValueWhen (Short, C, 1) * 0.97;
ShortProfitCondition = Cross (ShortTarget, L);
Cover = ShortStopCondition OR ShortProfitCondition OR BuyCondition;
Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell, Buy);
Short=ExRem(Short,Cover);
Cover=ExRem(Cover,Short);
Plot( BuyAvg, "Buy Average", colorGreen, ParamStyle("Style") );
Plot( SellAvg, "Sell Average", colorRed, ParamStyle("Style") );
Plot( LongStopAvg, "Long Stop", colorPink, ParamStyle("Style") );
Plot( ShortStopAvg, "Short Stop", colorPaleGreen, ParamStyle("Style") );
PlotShapes (IIf(Buy,shapeUpArrow,shapeNone), colorGreen,0,Low );
PlotShapes (IIf(Sell,shapeHollowDownArrow,shapeNone), colorRed,0,High);
PlotShapes (IIf(Short,shapeDownArrow,shapeNone), colorRed,0,High );
PlotShapes (IIf(Cover,shapeHollowUpArrow,shapeNone), colorGreen,0,Low);
PositionSize = MarginDeposit;
"BuyAvg: " + NumToStr (BuyAvg, 1.2);
"SellAvg: " + NumToStr (SellAvg, 1.2) ;
"LongStopAvg: " + NumToStr (LongStopAvg, 1.2);
"ShortStopAvg: " + NumToStr (ShortStopAvg, 1.2);
"BuyCondition: " + NumToStr (BuyCondition, 1);
"ShortCondition: " + NumToStr (ShortCondition, 1);
"Short: " + NumToStr (Short, 1);
"LongStopCondition: " + NumToStr (LongStopcondition, 1);
"ShortStopCondition: " + NumToStr (ShortStopCondition, 1);
"LongTarget: " + NumToStr (LongTarget, 1.2);
"ShortTarget: " + NumToStr (ShortTarget, 1.2);
"LongProfitCondition: " + NumToStr (LongProfitCondition, 1);
"ShortProfitCondition: " + NumToStr (ShortProfitCondition, 1);