PureBytes Links
Trading Reference Links
|
I have a two moving average crossover system, which should exit on a profit target. Once it exits it should not reenter on the same crossover signal.
But I find that my code enters and exits immedaitely.
The code is given below, any help is appreciated.
Vars: Buyflag(0), Sellflag(0);
{ calculate 2 simple moving averages }
Value1 = average(close,9);
Value2 = average(close,45);
{ set buy flag to 1 when there is an up crossover }
IF Value1 > Value2 AND Value1[1] < Value2[2] then begin
BuyFlag = 1;
SellFlag = 0;
end;
{ set sell flag to 1 when there is a down crossover }
IF Value1 < Value2 AND Value1[1] >Value2[2] then begin
BuyFlag = 0;
SellFlag = 1;
end;
{ Buy above previous high on any bar after crossover }
IF BuyFlag = 1 then buy at high + 1 stop;
{ Sell below previous low on any bar after crossover }
IF SellFlag = 1 then sell at low -1 stop;
{exit on a profit target }
exitlong at entryprice + 20 limit;
exitshort at entryprice - 20 limit;
{Now stop from re-entering same entry signal }
{this does not work. gives a new signal then exits immediately }
IF MarketPosition = 1 AND High > entryprice + 20 then BuyFlag = 0;
IF MarketPosition = -1 AND Low < entryprice - 20 then SellFlag = 0;
|