I have an EA program that buys/sales based on the value of defined and calculated indicators. But when I write those values to a flat file that are they never change and the counter never increments. I think Im too close to the problem to diagnose it and would appreciate another set of eyes to point out what I am obviously missing. The output to the flat file is below and I've also attached the code. Thanks
Was looking through your code, and noticed a couple of ideas that might help you out making your code a little easier to deal with in the future. First, it is standard practice with some languages to use all caps in the names of constants. Like here, the names that are all initials are fine, but SMA50_Array would work, unless you are only planning to write to the array once. I did not look through the rest of your code to see if that is the case, as I am still learning to code in MQL.
// Indicator Arrays double MACDMainArray[]; double MACDSignalArray[]; double SMA_ARRAY[]; double SMA50_ARRAY[]; double SMA100_ARRAY[]; double EMA5_ARRAY[]; double EMA10_ARRAY[]; double RSI_ARRAY[]; double MSL,MTP;
Also, another idea is when you are creating your variables and constants, if you are doing it like the code section below...
//--- Calculate getting entry signals to buy and sale int EntrySignal; int SMASignal; int EMASignal; int RSISignal; int MACDSignal; double SMA50Value; double SMA100Value; double MACDSignalValue; double MACDMainValue; double EMA5Value; double EMA10Value; double RSIValue; int y; int x; int b; int s; b=0; // Buy Counter s=0; // Sale Counter EntrySignal=-1; // Entry Set not to 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 SMA50Value=0; SMA100Value=0; MACDSignalValue=0; MACDMainValue=0; EMA5Value=0; EMA10Value=0; RSIValue=0;
You could combine the two sections and create and initialize them at the same time, like below. If you had to define the values later on in the program instead, obviously you would give them the value then. Another thing I did is what some programmers do with the alignment to make it a little easier to read the names and values. Since the compiler basically ignores anything that isn't program code, including spaces, this works as well as the other way.
//--- Calculate getting entry signals to buy and sale int EntrySignal = -1; // Entry Set not to buy or sale int SMASignal = -1; // SMA Signal to buy or sale int EMASignal = -1; // EMA Signal to buy or sale int RSISignal = -1; // RSI Signal to buy or sale int MACDSignal = -1; // MACD Signal to buy or sale double SMA50Value = 0; double SMA100Value = 0; double MACDSignalValue = 0; double MACDMainValue = 0; double EMA5Value = 0; double EMA10Value = 0; double RSIValue = 0; int y; int x; int b = 0; // Buy Counter int s = 0; // Sale Counter
If you do this or not is totally a personal preference, but it might be something to help you in the future when looking for issues in your code. I combined the colors to show the combined version.
The question is why are you expecting the value will change ? Only the values of the current candle are changing. You are logging the signal (buy/sell) values, this candle can be from 0 to 8, the candle 8 being tested in last. Your value will change only in very specific cases.
Then my program is structurally flawed because it should A) Read indicator values into an array, B.1) check indicator arrays for certain conditions. B.2) if condition are met buy/sale and write indicator values at time of buy/sale to a flat file.
Obviously this is not happening probably because I need to move the buy/sale option within the 1st for loop. The write function is probably just writing the last value of the loop.
Was looking through your code, and noticed a couple of ideas that might help you out making your code a little easier to deal with in the future. First, it is standard practice with some languages to use all caps in the names of constants. Like here, the names that are all initials are fine, but SMA50_Array would work, unless you are only planning to write to the array once. I did not look through the rest of your code to see if that is the case, as I am still learning to code in MQL.
Also, another idea is when you are creating your variables and constants, if you are doing it like the code section below...
You could combine the two sections and create and initialize them at the same time, like below. If you had to define the values later on in the program instead, obviously you would give them the value then. Another thing I did is what some programmers do with the alignment to make it a little easier to read the names and values. Since the compiler basically ignores anything that isn't program code, including spaces, this works as well as the other way.
If you do this or not is totally a personal preference, but it might be something to help you in the future when looking for issues in your code. I combined the colors to show the combined version.
The question is why are you expecting the values will change ? Only the values of the current candle are changing. You are logging the signal (buy/sell) values, this candle can be from 0 to 8, the candle 8 being tested in last. Your value will change only in very specific cases.
Rats! Still not working right! I moved the buy/sale code into the 1st for loop thinking that was it but my flat file ForExpertsReports still keeps printing the exact same values! Any thoughts?
[1] MACD Main Value at time of buy = 0.00015 [1] MACD Signal Value to Time of buy =0.00043 [1] EMA5 Value at time of buy =1.07453 [1] EMA10 Value at time of buy =1.07474 [1] SMA50 Value at time of buy =1.07457 [1] SMA100 Value at time buy =1.07518 [1] RSI Value at time of buy =49.59316 -- [1] MACD Main Value at time of buy = 0.00015 [1] MACD Signal Value to Time of buy =0.00043 [1] EMA5 Value at time of buy =1.07453 [1] EMA10 Value at time of buy =1.07474 [1] SMA50 Value at time of buy =1.07457 [1] SMA100 Value at time buy =1.07518 [1] RSI Value at time of buy =49.59316 -- [1] MACD Main Value at time of buy = 0.00015 [1] MACD Signal Value to Time of buy =0.00043 [1] EMA5 Value at time of buy =1.07453 [1] EMA10 Value at time of buy =1.07474 [1] SMA50 Value at time of buy =1.07457 [1] SMA100 Value at time buy =1.07518 [1] RSI Value at time of buy =49.59316
Thanks for the pointers. I will definitely make those changes so I am more consistent with coding standards.
It is not so much a coding standard as one possible person's style. Some people code it just like you did all the time, and some code it how I showed you. If you were to call it a "standard," then both styles qualify for that name. My intent was to show you a way to make your code shorter and more readable for you.
As far as your loop problem, the simplest explanation is that within the loop, you are comparing two things that are basically the same, probably the same original source of the data (like actually from the same candle like was said before). Where and how to fix it isn't so clear, and I do not know MQL code well enough yet to be able to help other than with generalities. One method that has been used by me and many others is the liberal use of print or log statements. Obviously specific to the area they are placed, but output the names of the variables and the values you are expecting to see at that point. Also, not sure how exactly in MQL, but if you can use a generic sort of print that says what variable or function the data is obtained from. That might make your problem leap out at you.
Rats! Still not working right! I moved the buy/sale code into the 1st for loop thinking that was it but my flat file ForExpertsReports still keeps printing the exact same values! Any thoughts?
It's difficult to help, only you know what you are trying to achieve. Obviously it's not what you coded.
What I'm attempting to do and I admit failing miserably is capture the moment when SMA.50 crosses above SMA.100 write the values for both to a flat file and set the flag for the SMA condition to sale. The trick is the arrays should hold values for 8 candle sticks. I wonder if I should be doing a shift as opposed to a summation to increment through the values of the arrays?
One possible thing that might be happening if you are using an array to store values from your candles. I tried following your posted code, but did not see something like what I am talking about below. Unless you set the array up right to read the new values and let the old ones fall off, that could also be causing issues.
For example, let's say you have the 8 bars (indexed at 0-7) worth stored in your array. The value from a new bar comes in, and where do you put it? If you just put it in the last array position (index 7), then you have the 7 older candles, and the new information for the latest candle in the last position. Unless I am guessing wrong, this is not how you want it to work. You need a section of code that will cycle through, and effectively drop the data located at index 0. How I would do this is copy the data from index 1 to index 0, overwriting it, which you don't need any more anyway. Copy the data from index 2 to index 1, and so on up the line, then at the end of this process, index 6 and 7 both have the same data, until you write the new data into index 7. Then when the next candle information you need comes in, do this whole process all over again.
One possible thing that might be happening if you are using an array to store values from your candles. I tried following your posted code, but did not see something like what I am talking about below. Unless you set the array up right to read the new values and let the old ones fall off, that could also be causing issues.
For example, let's say you have the 8 bars (indexed at 0-7) worth stored in your array. The value from a new bar comes in, and where do you put it? If you just put it in the last array position (index 7), then you have the 7 older candles, and the new information for the latest candle in the last position. Unless I am guessing wrong, this is not how you want it to work. You need a section of code that will cycle through, and effectively drop the data located at index 0. How I would do this is copy the data from index 1 to index 0, overwriting it, which you don't need any more anyway. Copy the data from index 2 to index 1, and so on up the line, then at the end of this process, index 6 and 7 both have the same data, until you write the new data into index 7. Then when the next candle information you need comes in, do this whole process all over again.
That makes sense but let me ask you this... I'm populating my arrays in the OnInit() function but then I'm reading from and testing those arrays in the OnTick() function, so during the the OnInit() doesn't the array just get populated once? Im thinking it must because I only size it for 9 elements and anything more than 9 would error out. So when we get to the OnTick() function the array only contains eight elements correct? Or am I not understanding the flow of my program?
- 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 an EA program that buys/sales based on the value of defined and calculated indicators. But when I write those values to a flat file that are they never change and the counter never increments. I think Im too close to the problem to diagnose it and would appreciate another set of eyes to point out what I am obviously missing. The output to the flat file is below and I've also attached the code. Thanks