Random entry with N bar(s) random exit


I am total beginner as you will learn.

I would like to code a random long and short entry with n random bars to exit, I am struggling with the random short and the N bar random close, have tried the following


SetTradeDelays( 1, 1, 1, 1 ); 
maxpos = 10; // maximum number of open positions
SetOption("InitialEquity", 100000 ); // set initial equity = 100K
SetOption( "MaxOpenPositions", maxpos );
SetPositionSize( 10000, spsValue ); // 10K in each trade 
SetOption( "CommissionMode", 2 ); // set commissions AND costs as $ per trade //
SetOption( "CommissionAmount", 0 ); // commissions AND cost //
SetOption( "AllowSameBarExit", True );
PositionScore = 100/C; /*Set the order for which stock trades when getting mulitple signals in one bar in backtesting //

RandomNumber = Random();
Buy = RandomNumber > 0.5;
Sell = RandomBar

//Short = RandomNumber > 0.5; I dont understand how to setup long and short positions, could someone give me a simple example?
//Cover = RandomNumber < 0.5; ??

RandomNBars = Random () * 500; //random bars upto 500??
RandomBar = ApplyStop( stopTypeNBar, stopModeBars, RandomNBars, True ); //have mercy on me I'm still learning

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
BuyPrice = Close;
SellPrice = Close;


Also new, so can not give you a clear solution, but maybe some help.

Buy and Sell have the same condition. That will not work I would think. It would go long and short at the same time, it will need different conditions.

What you could do is reversie the short to < 0.5

Then make Cover > 0.5 and Sell <0.5

It would give you a random system that would always be in the market.

No idea if that is what you want though....


1 Like
Sell = RandomBar

You should set Sell to zero if exit is solely based on stop.


Sell = 0;

That one is not correct as ApplyStop is function that does not return anything (void). It is a procedure.

You should not use ExRem in Analysis.
And in Chart you rather should call Equity function to evaluate stops (Equity() not required in Analysis too!!).

1 Like

I have found a good way to solve AFL coding questions is to do tiny tests with the barest minimum of code and output the result as an Exploration.

So I would suggest losing all the top part of the code (down to PositionScore) and just test say the Random function first and see what that produces. The less lines of code in these tests the better.

Then add a Buy condition and fiddle around with it and see what it produces with each variation. Then add a Sell condition, fiddle around some more and check what appears in the Exploration results.

In these tests, forget about trying to create profitable trades, just experiment with the functions.

From these basic steps, you can build up an understanding of how each function works and be confident that your results are correct because you have learnt first hand, exactly how each function works.

An added benefit of this approach, is that it teaches you all about the formatting options for Exploration results. You can colour cells, alter column widths, filter out rows you don't need to see, add totals etc. It's a fantastic learning and testing tool. Then in the future, any time you want to test out a new idea, you can quickly and easily produce clear and easy-to-understand columns displaying the results of your tests. Highly recommended.


Exploration is only one of many tools in AmiBroker arsenal.
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?

1 Like

Thanks Dave, I can see where you are coming from, I'll take your advice on this and check out the Exploration

Thankyou Henri, I will try that logic :slight_smile:

Regular backtest mode automatically removes excess signals.


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