MQL5 Invalid array access error when trying to access first element of an array - moving-average

I have a condition which checks if the latest fast moving average value is above or below the slow moving average and if the bid price is above or below the fast ma.
But I am getting this compile error:
The iMA function is returning data.
Here is my code:
int fast_ma_handle;
int slow_ma_handle;
int OnInit()
{
fast_ma_handle = iMA(_Symbol,_Period,8,0,MODE_EMA,PRICE_CLOSE);
slow_ma_handle = iMA(_Symbol,_Period,21,0,MODE_EMA,PRICE_CLOSE);
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
}
void OnTick()
{
double fast_ma_array[], slow_ma_array[];
int copied1 = CopyBuffer(fast_ma_handle,0,0,1,fast_ma_array);
int copied2 = CopyBuffer(slow_ma_handle,0,0,1,slow_ma_array);
double bid_price = SymbolInfoDouble(_Symbol,SYMBOL_BID);
int trend_direction = 0;
if(fast_ma_array[0] > slow_ma_array[0] && bid_price > fast_ma_array)
trend_direction = 1;
else if(fast_ma_array[0] < slow_ma_array[0] && bid_price < fast_ma_array)
trend_direction = -1;
}
Thanks

You have a couple of mistakes in your code. I've corrected it for you.
int fast_ma_handle;
int slow_ma_handle;
int OnInit()
{
fast_ma_handle = iMA(_Symbol,_Period,8,0,MODE_EMA,PRICE_CLOSE);
slow_ma_handle = iMA(_Symbol,_Period,21,0,MODE_EMA,PRICE_CLOSE);
return(INIT_SUCCEEDED);
}
void OnTick()
{
double fast_ma_array[], slow_ma_array[];
ArraySetAsSeries(fast_ma_array, true);
ArraySetAsSeries(slow_ma_array, true);
int copied1 = CopyBuffer(fast_ma_handle,0,0,100,fast_ma_array);
int copied2 = CopyBuffer(slow_ma_handle,0,0,100,slow_ma_array);
double bid_price = SymbolInfoDouble(_Symbol,SYMBOL_BID);
int trend_direction = 0;
if(fast_ma_array[0] > slow_ma_array[0] && bid_price > fast_ma_array[0])
trend_direction = 1;
else if(fast_ma_array[0] < slow_ma_array[0] && bid_price < fast_ma_array[0])
trend_direction = -1;
}

Related

Uncaught TypeError: Cannot read property 'substring' of undefined - Processing.js

I keep getting this error, and I have no clue what is wrong. I checked all variables, but all of them are defined at least somewhere
int creatures = 5;
int deathChance = 5;
int repChance = 10;
int timer = 0;
int rand = 0;
int CurrentCreatures = 0;
int b = 0;
void draw(); {
if(timer < 100)
{
b = 0;
CurrentCreatures = creatures;
while(b < CurrentCreatures)
{
rand = random(0,100);
if(rand <= repChance)
{
creatures += 1;
}
rand = random(0,100);
if(rand <= deathChance)
{
creatures -= 1;
}
b += 1;
}
println(creatures);
timer += 1;
}
}
what the program should be doing is printing the value of creatures over 100 generations (yes this is my attempt at some "life" simulator.
I think the problem lies here: void Draw(); { the ; is an end of instruction character
So it is trying to execute a function called draw, the it tries to do everything inside the {'s
possibly something like:
function draw() {
if(timer < 100) {
b = 0;
CurrentCreatures = creatures;
while(b < CurrentCreatures) {
rand = random(0,100);
if(rand <= repChance) {
creatures += 1;
}
rand = random(0,100);
if(rand <= deathChance) {
creatures -= 1;
}
b += 1;
}
println(creatures);
timer +=1;
}
}

Can't stop while loop which has scanf inside

#include <stdio.h>
int main() {
int test,i = 0,a = NULL;
int max2 = 0;
int n;
int max = -1000000, min = 1000000;
while (scanf("%d", &n) == 1 && max2 < 50)
{
if(n < min) { min = n; }
if(n > max) { max = n; }
max2++;
}
printf("%d",min+max);
return 0;
}
Input should be like this "1 5 8 9 10", I don't know how many numbers would be entered so I have to use the while loop.
Try using do while to put the scanf inside the loop.
#include <stdio.h>
int main() {
int test,i = 0,a = NULL;
int max2 = 0;
int n;
int max = -1000000, min = 1000000;
do{
scanf("%d", &n);
if(n < min) { min = n; }
if(n > max) { max = n; }
max2++;
}while ( n == 1 && max2 < 50)
printf("%d",min+max);
return 0;
}

Flat buffers : Object serialization must not be nested

FlatBufferBuilder fbb = new FlatBufferBuilder(1024);
String directory = "/Users/samarnath/RmsOne/CreateFlatBuffer/src/com/rms/objects/resources";
File [] policyfiles = ReturnFilesWithPattern(directory, "singlecoverriskpolicy");
for (File file: policyfiles)
{
Long StructureId = 0L;
int insurer = 0;
int insured = 0;
int UnderWriter = 0;
int inception = 0;
int Expiration = 0;
int ExternalID = 0;
Long SubjectId =0L;
int SubjectName = 0;
int SubjectStructureName = 0;
int Share = 0;
Double blanketLimit = 0.0;
Double attachment = 0.0;
int causeofLoss = 0;
int maxDeductible = 0;
int attachmentCurrency = 0;
int offset= 0;
int deductibleCurrencyOffset = 0;
int createOffset =0;
int blanketLimitCurrency = 0;
String folderName = "nfs://dev-spark-share.lab.rmsonecloud.net/mnt/data/UserData/import/outputfiles/Job_5/SmokeTest_2M/eufl_only_client4_2_edm__20151203-134544__24/contract/";
List<String> lines = Files.readAllLines(file.toPath());
List<String> actualLines = lines.subList(1, lines.size());
for (String line:actualLines)
{
String [] riskitems = line.split("~");
SingleCoverRiskPolicy.startSingleCoverRiskPolicy(fbb);
Long Id = Long.parseLong(riskitems[0]);
int policyName = fbb.createString(riskitems[1]);
After the above line i get an error saying Exception in thread "main" java.lang.AssertionError: FlatBuffers: object serialization must not be nested.I get an error in fbb.createString.
The code is simple and i cant figure out whats wrong here
From the documentation: "Everything else (other tables, strings, vectors) MUST be created before the start of the table they are referenced in."
So move int policyName = fbb.createString(riskitems[1]) and any other strings/vectors/tables you reference in SingleCoverRiskPolicy to before startSingleCoverRiskPolicy.

Non recursive BST (binary search tree)

Hello I don't know what is wrong with my BST insert method.
Any suggestions it has too be non recursive it adds to the right always I want to know why it adds to the ends to the BST when I print it shows that the nodes were added at the right only.
void InsertBST(LZWCmp cmp, TreeNode **root, int code)
{
TreeNode tmp = *root;
TreeNode current = NULL;
Code temp;
Code temp1;
int size;
int comp;
int direction = -1;
if(*root == NULL)
root = CreateNode(code)
while(tmp != NULL) {
temp = GetCode(cmp->cst, tmp->cNum);
temp1 = GetCode(cmp->cst, code);
size = temp.size;
if(temp1.size < temp.size)
size = temp1.size;
comp = memcmp(temp1.data, temp.data, size);
if(temp1.size < temp.size && comp == 0)
comp = -1;
else if(temp1.size < temp.size && comp == 0)
comp = 1;
if(comp < 0) {
current = tmp;
direction = FALSE;
tmp = tmp->left;
} else (
current = tmp;
direction = TRUE;
tmp = tmp->right;
}
}
if(direction == FALSE)
current->left = CreateNode(code);
else
current->right = CreateNode(code);
}
There you go:
void InsertBST(LZWCmp cmp, TreeNode **root, int code)
{
Code temp;
Code temp1;
int size;
int comp;
while ( *root ) {
temp = GetCode(cmp->cst, (*root)->cNum);
temp1 = GetCode(cmp->cst, code);
size = (temp1.size < temp.size) ? temp1.size : temp.size;
comp = memcmp(temp1.data, temp.data, size);
if (comp ==0 ) comp = (temp1.size < temp.size ) ? -1 : 1;
root = (comp < 0) ? &(*root)->left
: &(*root)->right;
}
*root = CreateNode(code);
}
NOTE: this code does not check for duplicates. It always inserts a node.
NOTE2: I am not sure about the sign in: if (comp ==0 ) comp = (temp1.size < temp.size ) ? -1 : 1; (in the original the sign was the same in both cases)

Arduino Automatic Light Switch 2

I'm currently working on an automatic light switch. Here's my code:
#include <Servo.h>
boolean time = false;
const int timeLim = 10000;
const int delLen = 5000;
int pirVal = 0;
const int pirPin = 2;
const int sensePin = 5;
boolean timeRet = false;
int lightVal;
Servo myServo;
unsigned long limit;
void setup() {
Serial.begin(9600);
pinMode(pirPin, INPUT);
myServo.attach(11);
myServo.write(40);
}
void loop() {
unsigned long Timer = millis();
pirVal = digitalRead(pirPin);
int lightVal = analogRead(sensePin);
Serial.print(pirVal);
Serial.print(' ');
Serial.print(lightVal);
Serial.print(' ');
Serial.print(Timer);
Serial.print(' ');
Serial.print(time);
Serial.print(' ');
Serial.print(limit);
Serial.print(' ');
Serial.println(timeRet);
if ( lightVal < 400 ) {
time = false;
limit = 0;
timeRet = false;
} if ( lightVal < 400 && pirVal == 1 ) {
unsigned long time = false;
pirVal = 0;
myServo.write(160);
} if ( lightVal > 400 && pirVal == 0 && timeRet == false){
limit = getTimeLim( timeLim, Timer );
pirVal = 0;
timeRet = true;
} if ( lightVal > 400 && pirVal == 0 && timeRet == true ) {
time = timeStat ( limit, Timer );
} if ( lightVal > 400 && time == true ) {
myServo.write(40);
}
}
int getTimeLim( const int timeLim, unsigned long Timer ) {
unsigned long limit = Timer + timeLim;
return limit;
}
boolean timeStat( unsigned long limit, unsigned long Timer ) {
if ( Timer < limit ) {
time = false;
} else if ( Timer > limit ) {
time = true;
}
return time;
}
The problem is that when you look at the serial the first time the getTimeLim function it works, but the second time is always some outrageous number (e.g. 4294937965). I don't know why it would give me this huge number. Help would be much appreciated.
since your code works now and you want to optimize it, i would suggest this:
// ( pseudo code since i'm not familiar with arduino )
void loop( ){
if( ( analogRead( sensePin ) < 400 )
&& ( digitalRead( pirPin ) ) ){
myServo.write( 160 ); // turn on light
int time_end = millis( ) + 60,000; // initiate timer value
while( millis( ) < time_end ); // poll time until at 60s
myServo.write( 40 ); // turn off light
}
}