Code review appreciated. Thanks in advance!

hi, I am new to Amibroker. I just created my first afl which is a rotational system using the following rules

  • RSI should be above its MA
  • RSI should be above a threshold value
  • Price should be above supertrend
    If the above conditions are met then I am assigning Positionscore which is the current weekly RSI
    I could not get the logic around TimeframeSet and TimeframeCompress functions. Still working on understanding the same. However I just changed the settings to weekly to check this code.
    The problem I am facing is that it seems that the Conditions specified above are not being checked by the code and seems it is just using the positionscore to give me the trades.
    I am sure i am making some silly error here but new to both programming and AFL. So any help will be greatly appreciated. Thanks in advance!!

Vivek

// Rotational System with RSI Score+Supertrne on weekly charts
Plot(C,"Price",IIf(C>O,ParamColor("Wick UP Color", colorDarkGreen),IIf(C<=O,ParamColor("Wick Down Color", colorDarkRed),colorLightGrey)),32,0,0,0,0);
sigwd=ParamToggle("sigchange","weekly|daily",1);
SetChartOptions(0, chartShowArrows | chartShowDates);
SetChartBkColor(ParamColor("Background Color", colorDefault));


SetOption( "InitialEquity", 1000000);
SetBacktestMode( backtestRotational );
Totalpositions = 20;
SetOption("WorstRankHeld", 50);
SetOption("MaxOpenPositions", Totalpositions );
PositionSize = -5;

// Set weekly candle chart
//TimeFrameSet(inWeekly);
WeeklyRSI_Period = Optimize("Weekly RSI", 21, 7,100,1);
WeeklyRSIMA_Period = Param("Weekly RSI MA", 55, 1, 100,1); 
Pd=Param("ATR Periods",10,1,100,1);
Factor=Param("Factor",3,1,10,1);

// Weekly RSI and RSI MA Calculation
WeeklyRSI = RSI(WeeklyRSI_Period);
WeeklyRSIMA = MA(WeeklyRSI, WeeklyRSIMA_Period);

// Weekly SuperTrend Calculation
Up=(H+L)/2+(Factor*ATR(Pd));
Dn=(H+L)/2-(Factor*ATR(Pd));
iATR=ATR(Pd);
TrendUp=TrendDown=Null;
trend[0]=1;
changeOfTrend=0;
flag=flagh=0;
 
for (i = 1; i <BarCount-1; i++) {
      TrendUp[i] = Null;
      TrendDown[i] = Null;
     
      trend[i]=1;
   
       
      if (Close[i]>Up[i-1]) {
        trend[i]=1;
        if (trend[i-1] == -1) changeOfTrend = 1;
         
      }
      else if (Close[i]<Dn[i-1]) {
        trend[i]=-1;
        if (trend[i-1] == 1) changeOfTrend = 1;
      }
      else if (trend[i-1]==1) {
        trend[i]=1;
        changeOfTrend = 0;      
      }
      else if (trend[i-1]==-1) {
        trend[i]=-1;
        changeOfTrend = 0;
      }
 
      if (trend[i]<0 && trend[i-1]>0) {
        flag=1;
      }
      else {
        flag=0;
      }
       
      if (trend[i]>0 && trend[i-1]<0) {
        flagh=1;
      }
      else {
        flagh=0;
      }
       
      if (trend[i]>0 && Dn[i]<Dn[i-1]){
        Dn[i]=Dn[i-1];
      }
       
      if (trend[i]<0 && Up[i]>Up[i-1])
        { Up[i]=Up[i-1];
      }
       
      if (flag==1)
      {  Up[i]=(H[i]+L[i])/2+(Factor*iATR[i]);;
        } 
      if (flagh==1)
        { Dn[i]=(H[i]+L[i])/2-(Factor*iATR[i]);;
        }
      if (trend[i]==1) {
        TrendUp[i]=Dn[i];
        if (changeOfTrend == 1) {
            TrendUp[i-1] = TrendDown[i-1];
            changeOfTrend = 0;
        }
      }
      else if (trend[i]==-1) {
        TrendDown[i]=Up[i];
        if (changeOfTrend == 1) {
            TrendDown[i-1] = TrendUp[i-1];
            changeOfTrend = 0;
        }
      }
  } 

// Restore to original timeframe
//TimeFrameRestore();

// Rotational System
Condition1 = IIf((WeeklyRSI > WeeklyRSIMA) AND (Trend==1) AND (WeeklyRSI > 55), True, False);
Condition2 = IIf((WeeklyRSIMA < WeeklyRSI) AND Close < Trend, True, False);

Tradeday = DayofWeek() == 5; 

Score = WeeklyRSI; 
PositionScore = IIf(Condition1, Score, 0); 
//PositionScore = IIf(Condition2, 0);
PositionScore = IIf(Tradeday, Score, scoreNoRotate);

// Exploration
Filter = 1;
AddColumn(Close, "Close");
AddColumn(WeeklyRSI, "WeeklyRSI");
AddColumn(WeeklyRSIMA, "WeeklyRSIMA");
AddColumn(Trend, "SuperTrend");
AddColumn(PositionScore, "Position Score");
AddColumn(PositionSize, "Position Size");

Plot(Close, "Close", colorDefault, styleCandle);
Plot(WeeklyRSI, "WeeklyRSI", colorRed, styleLine);
Plot(WeeklyRSIMA, "WeeklyRSIMA", colorBlue, styleLine);
Plot(TrendUp,"Trend",colorGreen);
Plot(TrendDown,"Down",colorRed);