@leob the problem, as I see it, is that you should learn the arrays logic to go ahead!
In any case, here is a very basic example to use an indicator level as a signal generator for an elementary trading system.
The code provides also an exploration to help you to figure out the ARRAY values for each bar in your exploration range.
Apply this code to any single ticker (like SPY, QQQ, etc) for a recent range (like 100/200 days) and see the exploration results (this is only for LONG positions). Then try to do a backtest.
As you can see in the code, there is NO "if" since in both in backtest and in the exploration every single bar in the selected range is evaluated to see (according to your code) if there is a valid signal to buy or sell.
// System and exploration example - not to be traded
_d_period = 9;
_d = StochD( _d_period );
// Mean reverting....
levelToBuyD = 20;
levelToSellD = 80;
signalToBuyD = ( _d < levelToBuyD );
signalToSellD = ( _d > levelToSellD );
// Add your other conditions.....
// These will be different from signalToBuyD/signalToSellD
// when you'll add some other conditions...
buySignal = signalToBuyD; // AND/OR any other condition
sellSignal = signalToSellD; // AND/OR any other condition
// Buy/Sell on the next bar at the open
BuyPrice = Open;
SellPrice = Open;
Buy = Ref( buySignal, -1 );
Sell = Ref( sellSignal, -1 );
// Remove any excessive signals
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
// Basic Indicator
Plot( C, "Close", colorDefault, styleCandle );
PlotShapes( shapeUpArrow * Buy, colorWhite, 0, O, 0 );
PlotShapes( shapedownArrow * Sell, colorYellow, 0, O, 0 );
/////// Exploration
// A small utility function to improve readibility of exploration
// results for boolean arrays - only ones (1) will appear
function nil( array )
{
return ( IIf( array, 1, Null ) );
}
// Filter = buySignal or SellSignal;
Filter = 1; // See all bars/days signals
AddColumn( C, "Close" );
AddColumn( _d, "StochD(" + _d_period + ")" );
AddColumn( nil( signalToBuyD ), "_d > " + levelToBuyD, 1.0 );
AddColumn( nil( signalToSellD ), "_d < " + levelToSellD, 1.0 );
AddColumn( nil( buySignal ), "Buy Signal", 1.0 );
AddColumn( nil( sellSignal ), "Sell Signal", 1.0 );
AddColumn( nil( Buy ), "Buy", 1.0 ); // at next open
AddColumn( nil( Sell ), "Sell", 1.0 ); // at next open
SetSortColumns(-2);
The "1" (ones) indicate when a particular condition is TRUE. You can combine multiple rules (and add further columns to check their values) to create your final Buy/Sell signals.
But maybe some more experienced users than me will be able to better explain it and provide you more relevant examples.
In any case, re, my previous answer, it was timely brought to my attention that the looping PDF I linked (hosted - apparently uncredited - on the linked site) is the original work of another author.
That document is due to a user that was very active in the AussieStockForums with the nickname of "GreatPig" (see this other thread for details and to get a link to the original document).