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;
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.