how about using the styler?
for(int x=0; x<8; x++) // The loop that does not work { FileWrite(file_handle,"For X Loop to Write SMA Values =",x,"--",NormalizeDouble(SMA50_ARRAY[x],_Digits),"--",NormalizeDouble(SMA100_ARRAY[x],_Digits)); if(SMA50_ARRAY[x]>SMA100_ARRAY[x]) { SMASignal=0; // SMA Buy Signal SMA50Value=SMA50_ARRAY[x]; SMA100Value=SMA100_ARRAY[x]; if(EMA5_ARRAY[x]<EMA10_ARRAY[x]) { for(int y=0; y<8; y++) { if(EMA5_ARRAY[y]>EMA10_ARRAY[y]) { EMASignal=0; // EMA Buy SIgnal EMA5Value=EMA5_ARRAY[y]; EMA10Value=EMA10_ARRAY[y]; } } if(MACDMainArray[x]<MACDSignalArray[x] && MACDMainArray[x]<0 && MACDSignalArray[x]<0) { for(int y=0; y<8; y++) { if(MACDMainArray[y]>MACDSignalArray[y]) { MACDSignal=0; // MACD Buy Signal MACDMainValue=MACDMainArray[y]; MACDSignalValue=MACDSignalArray[y]; } } } if(RSI_ARRAY[x]>40) { RSISignal=0; // RSI Buy Signal RSIValue=RSI_ARRAY[x]; } } } } //+------------------------------------------------------------------+
That is something that is easy to miss, which is why it is a good idea to make sure your indents are all spaced properly, it makes it a little easier to see if you are missing a closing parenthesis or brace. Me personally, on the braces containing chunks of code, like within an if statement, I try to keep them aligned with the start of the statement they are with. Like on your code section...
if(MACDMainArray[x]<MACDSignalArray[x] && MACDMainArray[x]<0 && MACDSignalArray[x]<0) { for(int y=0; y<8; y++) { if(MACDMainArray[y]>MACDSignalArray[y]) { MACDSignal=0; // MACD Buy Signal MACDMainValue=MACDMainArray[y]; MACDSignalValue=MACDSignalArray[y]; // yellow to show spacing } } }
I would space it like so.
if(MACDMainArray[x]<MACDSignalArray[x] && MACDMainArray[x]<0 && MACDSignalArray[x]<0) { for(int y=0; y<8; y++) { if(MACDMainArray[y]>MACDSignalArray[y]) { MACDSignal=0; // MACD Buy Signal MACDMainValue=MACDMainArray[y]; MACDSignalValue=MACDSignalArray[y]; // yellow to show spacing } } }
It might make it a little easier to see if you are missing the closer. The spacing you use is a judgement call. I have seen code indented 2 spaces, all the way up to 5. Just try to keep it consistent within your code, that way if something is out of place, it is more likely to stand out.
Thanks all for the advice! I used the styler (ctrl+,) and I changed the other names of the variables in my loops but still no luck. The problem is probably staring at me but I'm too blind to see it!!
for(int z=0; z<8; z++) { FileWrite(file_handle,"For X Loop to Write SMA Values ",z,"=",NormalizeDouble(SMA50_ARRAY[z],_Digits),"--",NormalizeDouble(SMA100_ARRAY[z],_Digits)); if(SMA50_ARRAY[z]>SMA100_ARRAY[z]) { SMASignal=0; // SMA Buy Signal SMA50Value=SMA50_ARRAY[z]; SMA100Value=SMA100_ARRAY[z]; if(EMA5_ARRAY[z]<EMA10_ARRAY[z]) { for(int y=0; y<8; y++) { if(EMA5_ARRAY[y]>EMA10_ARRAY[y]) { EMASignal=0; // EMA Buy SIgnal EMA5Value=EMA5_ARRAY[y]; EMA10Value=EMA10_ARRAY[y]; } } if(MACDMainArray[z]<MACDSignalArray[z] && MACDMainArray[z]<0 && MACDSignalArray[z]<0) { for(int i=0; i<8; i++) { if(MACDMainArray[i]>MACDSignalArray[i]) { MACDSignal=0; // MACD Buy Signal MACDMainValue=MACDMainArray[i]; MACDSignalValue=MACDSignalArray[i]; } } } if(RSI_ARRAY[z]>40) { RSISignal=0; // RSI Buy Signal RSIValue=RSI_ARRAY[z]; } } } else { if(SMA50_ARRAY[z]<SMA100_ARRAY[z]) { SMASignal=1; // SMA Sell Signal SMA50Value=SMA50_ARRAY[z]; SMA100Value=SMA100_ARRAY[z]; if(EMA5_ARRAY[z]>EMA10_ARRAY[z]) { for(int y=0; y<9; y++) { if(EMA5_ARRAY[y]<EMA10_ARRAY[y]) { EMASignal=1; // EMA Sell SIgnal EMA5Value=EMA5_ARRAY[y]; EMA10Value=EMA10_ARRAY[y]; } } if(MACDMainArray[z]>MACDSignalArray[z] && MACDMainArray[z]>0 && MACDSignalArray[z]>0) { for(int y=0; y<8; y++) { if(MACDMainArray[y]<MACDSignalArray[y]) { MACDSignal=1; // MACD Sell Signal MACDMainValue=MACDMainArray[y]; MACDSignalValue=MACDSignalArray[y]; } } } if(RSI_ARRAY[z]<60) { RSISignal=1; // RSI Sell Signal RSIValue=RSI_ARRAY[z]; } } } } // Code here!! bool PosSelect; PosSelect=PositionSelect(_Symbol); if(SMASignal==0 && EMASignal==0 && MACDSignal==0 && RSISignal==0) { b++; EntrySignal=0; // All signals indicate buy //FileWrite(file_handle,"[",b,"] MACD Main Value at time of buy =",NormalizeDouble(MACDMainValue,_Digits)); //FileWrite(file_handle,"[",b,"] MACD Signal Value to Time of buy =",NormalizeDouble(MACDSignalValue,_Digits)); //FileWrite(file_handle,"[",b,"] EMA5 Value at time of buy =",NormalizeDouble(EMA5Value,_Digits)); //FileWrite(file_handle,"[",b,"] EMA10 Value at time of buy =",NormalizeDouble(EMA10Value,_Digits)); //FileWrite(file_handle,"[",b,"] SMA50 Value at time of buy =",NormalizeDouble(SMA50Value,_Digits)); //FileWrite(file_handle,"[",b,"] SMA100 Value at time buy =",NormalizeDouble(SMA100Value,_Digits)); //FileWrite(file_handle,"[",b,"] RSI Value at time of buy =",NormalizeDouble(RSIValue,_Digits)); //FileWrite(file_handle,"--"); // Have to reset all signal after a buy or sale SMASignal=-1; // SMA Signal to buy or sale EMASignal=-1; // EMA Signal to buy or sale RSISignal=-1; // RSI Signal to buy or sale MACDSignal=-1; // MACD Signal to buy or sale } if(SMASignal==1 && EMASignal==1 && MACDSignal==1 && RSISignal==1) { s++; EntrySignal=1; // All signals indicate sell FileWrite(file_handle,"[",s,"] MACD Main Value at time of buy = ",NormalizeDouble(MACDMainValue,_Digits)); FileWrite(file_handle,"[",s,"] MACD Signal Value to Time of buy =",NormalizeDouble(MACDSignalValue,_Digits)); FileWrite(file_handle,"[",s,"] EMA5 Value at time of buy =",NormalizeDouble(EMA5Value,_Digits)); FileWrite(file_handle,"[",s,"] EMA10 Value at time of buy =",NormalizeDouble(EMA10Value,_Digits)); FileWrite(file_handle,"[",s,"] SMA50 Value at time of buy =",NormalizeDouble(SMA50Value,_Digits)); FileWrite(file_handle,"[",s,"] SMA100 Value at time buy =",NormalizeDouble(SMA100Value,_Digits)); FileWrite(file_handle,"[",s,"] RSI Value at time of buy =",NormalizeDouble(RSIValue,_Digits)); FileWrite(file_handle,"--"); // Have to reset all signal after a buy or sale SMASignal=-1; // SMA Signal to buy or sale EMASignal=-1; // EMA Signal to buy or sale RSISignal=-1; // RSI Signal to buy or sale MACDSignal=-1; // MACD Signal to buy or sale } FileFlush(file_handle); FileClose(file_handle); if(EntrySignal==0 && PosSelect==false) { if(SendOrder(0)==false) { Print("Order send buy failed with error: ",GetLastError()); ResetLastError(); } } if(EntrySignal==1 && PosSelect==false) { if(SendOrder(1)==false) { Print("Order send sell failed with error: ",GetLastError()); ResetLastError(); } } PosSelect=PositionSelect(_Symbol); if(PosSelect==true) { SettingTPSL(); } }
You're still declaring int y in two of the for loops. Not necessarily causing your problems but it could be throwing unexpected results and is bad coding practice....
Simple for loop doesn't work
I want to use for loops in my EA, but I can't get them to work.
I wrote a simple program to test it. I have both a for loop and a while loop. The while loops send the alert to the log, but i get nothing from the for loop.
Do I need to #include a file?
Thanks for your help,
Jim
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I have multiple for loops in my EA program and while the first for loop works as designed the second for loop does not increment as stays stuck at zero. Both loops are in the OnTiick() function. Any thoughts as to why this is?