Short Selling and Covering happening at the same minute

Hi Guys,

So since 1 month been messing around with various aspects of amibroker and AFL languages. Getting a good drip on it now! However, I do have a question about shorting. I wrote this piece of code which basically shorts the stock (I selected a single one) at 9:45AM and covers it at end of day (3:59 PM). However the problem I am having is that its opening it at 9:45, covering it at 9:46,.. then shorting it at 9:47 and covering it at 9:48 and so on until end of day! Below is my code and screenshots. Any help appreciated! Thanks!!!

EndDay = (Day()!= Ref(Day(), 1));

tn = TimeNum();
startTime = 94500; // start in HHMMSS format
endTime = 153000;

Short = tn>startTime AND tn<endTime;
Cover = tn>EndDay;
Short = ExRem( Short, Cover );
Cover = ExRem( Cover, Short );

SetBacktestMode( backtestRegularRaw);

SetOption("NoDefaultColumns", False);
SetOption("InitialEquity", 100000);
SetOption("AllowSameBarExit", True);
SetOption("ActivateStopsImmediately", True);
SetOption("AllowPositionShrinking", True);
SetOption("FuturesMode", False);
SetOption("InterestRate", 0);
SetOption("MaxOpenPositions", 1);
SetOption("MinShares", 1);
SetOption("PriceBoundChecking", True);
SetOption("CommissionMode", 2); //  $ per trade
SetOption("CommissionAmount", 1);
SetOption("AccountMargin", 50);
SetOption("ReverseSignalForcesExit", False);
SetOption("UsePrevBarEquityForPosSizing", False);
SetOption("PortfolioReportMode", 0);
SetOption("DisableRuinStop", False);
SetOption("GenerateReport", 1);
SetOption("RefreshWhenCompleted", True);
SetOption("SettlementDelay", 0);


You've got mistake in your code. You've got this:

while you should have this:

Cover = tn>= endTime;

To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?

Hi, I tried changing ">" to ">=" but its giving same results. I dont see how that would solve my problem, but I tried it anyways.

Just curious, when you run my code, are you getting the same results as me? or the good results?


It is NOT about > vs >=.

Really, you apparently DO NOT see that you are using wrong variable identifier. In your code you were using EndDAY variable (wrong) instead of endTime (correct). tn is TimeNum() and should only be compared with endTime.

You really need to be careful and read what is actually written not what you think

1 Like

Thanks @Tomasz .. I feel dumb that I didnt see the variable. You are right!

Anyways, I had tn>endTime in the begginning, and I want to close it at EndDay. So I changed the variabl, but I forgot to remove the "tn>" part.. Anyways its working now!

Thanks for your help!

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.