Trade sequence reset

I am coding a intraday system where I enter on signal and exit all position at the end of the day.
The trading system has a trade sequence like

  1. Buy
  2. Sell
  3. short
  4. cover

The system is missing few trades because of the trade sequence. say for example On day 1 I enter on buy signal and sell/exit at end of day, on day 2 I get a buy signal again and the sequence is not recognizing this buy signal and waits for short signal.

Is there any way to make the afl to read and take trades fresh on everyday and ignore previous day sequence.

The code is pasted below

_SECTION_BEGIN("Chart Options");
GfxSetBkMode(0);
SetChartOptions(0,chartShowArrows|chartShowDates);
BkColor = ParamColor("Chart Background Color",colorBlack);
SetChartBkColor( BkColor);
SetChartBkGradientFill( BkColor, BkColor);
UpColor = ParamColor("Up Bar Color",colorLime);
DnColor = ParamColor("Down Bar Color",colorRed);
Plot( C, "Close",IIf(C>O,UpColor,DnColor),ParamStyle("Chart Style",styleCandle,maskPrice)|styleNoTitle);
_SECTION_END();

#include "C:\Program Files\AmiBroker\Formulas\Custom\Common\LookUpFunctionNew.afl"

_SECTION_BEGIN("Default Variables");
Buy=Sell=Short=Cover=0;
BuyPrice=SellPrice=ShortPrice=CoverPrice=COBuySL=COSellSL=0;
BuyTgtPrice = BuySLPrice = ShortTgtPrice = ShortSLPrice = 0;
Ticker = Name();
_SECTION_END();

_SECTION_BEGIN("User Inputs");
TargetPct = Param("Target Percentage",25,1,300,1);
StoplossPct = Param("Stoploss Percentage",20,1,300,1);
OrdType = ParamList("Order Type","LIMIT",0);

_SECTION_END();

_SECTION_BEGIN("Trading System");
TrdBeginTime = ParamTime("Trade Begin Time","09:20:00",0);
TrdEndTime = ParamTime("Trade End Time","14:50:00",0);
SqOffTime = ParamTime("Square Off Time","15:15:00",0);
IsTrdTime = TimeNum()>=TrdBeginTime AND TimeNum()<=TrdEndTime;
IsSqOffTime = TimeNum()>=SqOffTime;

MA1 = MA(C, Param("MA Period1",15,1,500,1));
MA2 = MA(C, Param("MA Period2",25,1,500,1));
Plot(MA1, "MA1", colorLime, styleLine);
Plot(MA2, "MA2", colorRed, styleLine);

Buy = (Cross(MA1, MA2) AND IsTrdTime);
Short = (Cross(MA2, MA1) AND IsTrdTime);
Buy = ExRem(Buy,Short);
Short = ExRem(Short,Buy);
Buy = Ref(Buy,-1);
Short = Ref(Short,-1);
BuyPrice = ValueWhen(Buy,O);
ShortPrice = ValueWhen(Short,O);

BuyTgtPrice = BuyPrice + TargetPct;
BuySLPrice = BuyPrice - StoplossPct;
ShortTgtPrice = ShortPrice - TargetPct;
ShortSLPrice = ShortPrice + StoplossPct;
COBuySL= BuyPrice - ((BuyPrice * 1)/100);
COShortSL = ShortPrice + ((ShortPrice * 1)/100);

Sell = ((H >= BuyTgtPrice OR L <= BuySLPrice OR IsSqOffTime OR Short) AND Buy ==0);
Cover = ((H >= ShortSLPrice OR L <= ShortTgtPrice OR IsSqOffTime OR Buy) AND Short ==0);

Sell = IIf(Cum(Buy)==0,0,ExRem(Sell,Buy));
Cover = IIf(Cum(Short)==0,0,ExRem(Cover,Short));

SellPrice = ValueWhen(Sell,IIf(H >= BuyTgtPrice,BuyTgtPrice,IIf(L <= BuySLPrice,BuySLPrice,O)));
CoverPrice = ValueWhen(Cover,IIf(L <= ShortTgtPrice,ShortTgtPrice,IIf(H >= ShortSLPrice,ShortSLPrice,O)));

PlotShapes(IIf(Buy, shapeHollowUpArrow, shapeNone),colorGreen, 0,L, Offset=-30);
PlotShapes(IIf(Cover, shapeHollowStar, shapeNone),colorDarkGreen, 0,L, Offset=-50);
PlotShapes(IIf(Short, shapeHollowDownArrow, shapeNone),colorRed, 0,H, Offset=-30);
PlotShapes(IIf(Sell, shapeHollowStar, shapeNone),colorDarkRed, 0,H, Offset=50);
_SECTION_END();

You are explicitly removing the Buy signal on Day 2 of your example by doing this:

Buy = ExRem(Buy,Short);

Perhaps you really wanted to do this?

Buy = ExRem(Buy,Sell);

Of course, you'd want to move that logic down below your Sell logic...

Also, instead of doing this:

Buy = Ref(Buy,-1);

Why not just set your entry delay to 1 and get rid of the statement above. That approach would also allow you to set your BuyPrice to Open rather than using ValueWhen(). Whichever way you do it, you probably ought to be doing it for the Short entries as well as the Long entries.

3 Likes

@girinib1, you have been forum member for two years now and have still not read here? Please use code tags. It is one mandatory rule to use them. Please watch video animation of given link.


You are apparently trying to create fix SL/TP stops. The code is incorrect for that goal.
It has been mentioned multiple times already that for creating proper stops you either have to use Applystop or looping.
http://www.amibroker.com/kb/category/analysis/backtest-analysis/stops-backtest/
http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/
etc.

If you use ValueWhen then what will happen is this one.

So in same way if applying your(?) code then this will happen:
2


Just another thing having been mentioned/discussed a few times already and KB (AmiBroker knowledge base) is one of 1st addresses to look at...
Take a look at second example here:
http://www.amibroker.com/kb/2014/11/28/how-to-restrict-trading-to-certain-hours-of-the-day/


What he does there in that line is valid and is not incorrect. It is1 bar trade delay. Also he using PlotShapes so SetTradeDelays (if that's what you refer too) won't apply in chart pane if not using Equity function since SetTradeDelays is backtester function.

So this is valid code:

Buy = Cross(MA1, MA2);
Buy = Ref(Buy, -1);
BuyPrice = Open;

and is doing same as this

Buy = Ref(Cross(MA1, MA2), -1);
BuyPrice = Open;

Or if using analysis..

SetTradeDelays(1, 1, 1, 1);

Buy = Cross(MA1, MA2);
BuyPrice = Open;

But again one actual main issue are your exits (@girinib1). See above picture and explanation...

1 Like

@mradtke thanks for the reply.. I changed the Buy = ExRem(Buy,Short); to Buy = ExRem(Buy,Sell); and it worked.

@fxshrat thanks for your advise on how to use this site, I am newbie to programming and I dont post questions very frequently. Going forward I will follow the rules.

Being new to programming does not mean being new to reading, does it? The threads are clearly tagged and visible. Also there are info messages for newbies popping up. Before posting and if being new to something one should explore the site. The thing is many people do the same thing (incorrectly). And it always looks like not bothering. And since it is (too) many... It becomes frustrating.

:+1:


BTW your code will not work properly just by changing to different Exrem content. It is one of several other issues as explained.

1 Like