For loop, or array processing?

Hi guys,

I tried to transfer one strategy from C#, but there is a learning curve ahead of me. In the below code, I am sure the mistake is mixing the for loop bar by bar coding and array processing. I have thought a while, still get stuck in this trouble. Could you please help? Thanks so much.

OptimizerSetEngine( "cmae" );
SetTradeDelays( 1, 1, 1, 1 );

BuyPrice = Open;
SellPrice = Open;

PosQty = Optimize( "PosQty", 20, 5, 25, 5 );
SetOption ( "MaxOpenPositions", PosQty );
SetPositionSize( 100 / PosQty, spsPercentOfEquity );

Overbought=Optimize( "Overbought", 75, 65, 90, 5 );
Oversold=Optimize( "Oversold", 35, 15, 35, 5 );
HoldDays = Optimize( "HoldDays", 6, 3, 15, 1 );

myrsi=RSI(2);
isAbove=LLV(myrsi-Overbought, HoldDays);

setup=False;
//setup=IIf(isAbove>0, True, False);
//setup=IIf(setup==True AND Cross(myrsi, Overbought)==1, False, setup);
//Buy=Cross(Oversold, myrsi) AND Ref(setup,-1)==True;
for( i = 0; i < BarCount; i++ )
{
	if(setup==False)
		{
			if(isAbove[i]==True)
				setup=True;
		}
	if(setup==True)
		{
			if(Cross(myrsi, Overbought)==True)
				setup=False;
			if(Cross(Oversold, myrsi)==True)
				setup=False;
		}
}

Buy=Cross(Oversold, myrsi) AND setup==True;
PositionScore=-Close;
Sell=Cross(myrsi, Overbought);

stp=1.5*Ref(ATR(14),0)/BuyPrice*100;
ApplyStop( stopTypeLoss, stopModePercent, stp, True );
ApplyStop( stopTypeNBar, stopModeBars, 15);

Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy ); 

Above for loop part, there is a comments part which can not fully implement the for loop part. I don't know how to change it to be array processing way. However with for loop part, this code does not generate any trade.

Any help solving this problem is greatly and deeply appreciated.

Overbought=Optimize( "Overbought", 75, 65, 90, 5 );
Oversold=Optimize( "Oversold", 35, 15, 35, 5 );
HoldDays = Optimize( "HoldDays", 6, 3, 15, 1 );


myrsi=RSI(2);
isAbove=LLV(myrsi-Overbought, HoldDays) > 0;
setup=isAbove;

Buy= (Oversold > myrsi) AND Ref(setup,-1);
Sell= (myrsi > Overbought) AND setup;

BTW,

AFL Function Reference - ENABLEROTATIONALTRADING

The score (PositionScore) for all securities is calculated first. Then all scores are sorted according to absolute value of PositionScore. Then top N are choosen to be traded. N depends on available funds and "max. open positions" setting. Backtester successively enters the trades starting from highest ranked security until the number of positions open reaches "max. open positions" or there are no more funds available. The score has the following meaning:

  • higher positive score means better candidate for entering long trade
  • lower negative score means better candidate for entering short trade
  • the score of zero means no trade (exit the trade if there is already open position on given symbol)
  • the score equal to scoreNoRotate constant means that already open trades should be kept and no new trades entered
  • the score equal to scoreExitAll constant causes rotational mode backtester to exit all positions regardless of HoldMinBars. Note that this is global flag and it is enough to set it for just any single symbol to exit all currently open positions, no matter on which symbol you use scoreExitAll (it may be even on symbol that is not currently held). By setting PositionScore to scoreExitAll you exit all positions immediatelly regardless of HoldMinBars setting

Hi fxshrat,

thanks for your quick answer.

Your code is very clear however it is almost the same as my array processing way, both generates the same trades.

The right logic is for loop part. If you go deeply you can see there is a slight difference. However this slight difference generates double number of the trades which both above codes generate. To make it clear, the origional C# code is followed. Please focus on the variable setupLong.

			bool setupLong = false; 

			for (int bar = rsi.FirstValidValue * 3; bar < Bars.Count; bar++)
			{
				if (IsLastPositionActive)
				{
					Position p = LastPosition;
					if (p.EntrySignal.Contains("Group2|"))
					{
						double Stop = p.EntryPrice - ATR.Value(p.EntryBar, Bars, 14) * 1.0d;
						SellAtStop(bar + 1, p, Stop, "STP");
					}
					if (p.EntrySignal.Contains("Group1|"))
					{
						if (CrossOver(bar, rsi, overbot))
							ExitAtMarket(bar + 1, p, "Exit");

					}
				}
				else
				{
					if (!setupLong)
					{
						if (isAbove[bar] > 0)
						{
							setupLong = true;
						}
					}

					if (setupLong)
					{
						if (CrossOver(bar, rsi, overbot))
							setupLong = false;

						if (CrossUnder(bar, rsi, oversold)
							)
						{
							if (BuyAtMarket(bar + 1, "Entry|Group1|Group2|Group3|Group4|" + lrs[bar].ToString("n") + "|" + r2[bar].ToString("n")) != null)
							{
								setupLong = false;
								continue;
								LastPosition.Priority = -Close[bar];
							}
						}
					}
				}

In addition, thanks for your explanation of PositionScore. But for trading priority based on previous close, what is the best way to select the right stock to trade?

Thanks again for your help.

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