Why a MQL4 bot does not detect a 5-EMA & 20-EMA cross-over in a StrategyTester run? - moving-average

I am a newbie in coding bot for MetaTrader Terminal 4 platform, but I have a decent experience in coding JavaScript for the web.
I just build a simple MQL4 bot, using EMA(5) and EMA(20) to place an order, but when I try to test it via a StrategyTester run, it keeps showing both condition not met, please what am I doing wrong?
Below is my code ( https://codeshare.io/5NzvYl )
input int fastEma_Period = 5;
input int fastEma_ma_Shift = 0;
input int fastEma_ma_Method = 1; // EXPONENTIAL
input int fastEma_applied_Price = 0; // CLOSED PRICE
input int fastEma_shift_CandleIndex = 0; // CURRENT FORMING CANDLESTICK
input int slowEma_Period = 20;
input int slowEma_ma_Shift = 0;
input int slowEma_ma_Method = 1; // EXPONENTIAL
input int slowEma_applied_Price = 0; // CLOSED PRICE
input int slowEma_shift_CandleIndex = 0; // CURRENT FORMING CANDLESTICK
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double fastEmaBar0 = iMA(NULL,0,fastEma_Period,fastEma_ma_Shift,fastEma_ma_Method,fastEma_applied_Price,fastEma_shift_CandleIndex);
double slowEmaBar0 = iMA(NULL,0,slowEma_Period,slowEma_ma_Shift,slowEma_ma_Method,slowEma_applied_Price,slowEma_shift_CandleIndex);
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double fastEmaBar1 = iMA(NULL,0,fastEma_Period,fastEma_ma_Shift,fastEma_ma_Method,fastEma_applied_Price,fastEma_shift_CandleIndex + 1);
double slowEmaBar1 = iMA(NULL,0,slowEma_Period,slowEma_ma_Shift,slowEma_ma_Method,slowEma_applied_Price,slowEma_shift_CandleIndex + 1);
double lotSize = AccountBalance() / 10000;
int maBt;
int maSt;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
enterMarket();
// ExpertRemove();
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string maCrossOverSignal(double fastEmaBar1, double slowEmaBar1, double fastEmaBar0, double slowEmaBar0)
{
string signal;
if((fastEmaBar1 <= slowEmaBar1) && (fastEmaBar0 > slowEmaBar0))
{
signal = "Buy";
}
else
if((fastEmaBar1 >= slowEmaBar1) && (fastEmaBar0 < slowEmaBar0))
{
signal = "Sell";
}
return signal;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void enterMarket()
{
if(/*OrdersTotal() == 0 &&*/ maCrossOverSignal(fastEmaBar1,slowEmaBar1,fastEmaBar0,slowEmaBar0)== "Buy")
{
//continue inside this condition, bcos its not getting met
Print("time to Buy ");
maBt = OrderSend(NULL,OP_BUY,lotSize,Ask,100,0,0,"maBt");
if(maBt > 0)
{
if(OrderSelect(maBt,SELECT_BY_TICKET))
{
modify(maBt,OrderOpenPrice(),OrderOpenPrice() - 50 * Point,OrderOpenPrice() + 200 * Point,0);
}
}
}
else
if(/*OrdersTotal() == 0 && */maCrossOverSignal(fastEmaBar1,slowEmaBar1,fastEmaBar0,slowEmaBar0)== "Sell")
{
Print("time to Sell ");
maSt = OrderSend(NULL,OP_SELL,lotSize,Bid,100,0,0,"maSt");
if(maSt > 0)
{
if(OrderSelect(maSt,SELECT_BY_TICKET))
{
modify(maSt,OrderOpenPrice(),OrderOpenPrice() + 50 * Point,OrderOpenPrice() - 200 * Point,0);
}
}
}
else
{
Print("both condition not met");
}
}

Q : "... what am I doing wrong?"
Almost there.
You've missed a tiny, yet cardinal detail. The both assignments of iMA()-values are fine, but in a wrong place.
You need to let re-calculate & re-assign iMA()-values after each QUOTE-message arrival ( per each tick, coming from your FX-Market access mediator ( emulated in StrategyTester runs from past-data, sure ) ).
void OnTick() { /*\/\/\/\/\/\/\/\/\ QUOTE-has arrived, i.e.
new prices are known here:
//------------------------------------------- UPDATE each control-DATA now */
fastEmaBar0 = iMA( NULL, 0, fastEma_Period,
fastEma_ma_Shift,
fastEma_ma_Method,
fastEma_applied_Price,
fastEma_shift_CandleIndex
);
slowEmaBar0 = iMA( NULL, 0, slowEma_Period,
slowEma_ma_Shift,
slowEma_ma_Method,
slowEma_applied_Price,
slowEma_shift_CandleIndex
);
fastEmaBar1 = iMA( NULL, 0, fastEma_Period,
fastEma_ma_Shift,
fastEma_ma_Method,
fastEma_applied_Price,
fastEma_shift_CandleIndex + 1
);
slowEmaBar1 = iMA( NULL, 0, slowEma_Period,
slowEma_ma_Shift,
slowEma_ma_Method,
slowEma_applied_Price,
slowEma_shift_CandleIndex + 1
);
//------------------------------------------- UPDATE
//------------------------------------------- DONE, may start making decisions
enterMarket();
// ExpertRemove();
}

Related

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

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;
}

Breaking a while loop in Arduino

I have a program that moves a stepper motor to the right, left and have a stop button that stops the motor. In one part of my program, a motor gradually lowers a speed and stops after a certain period of time iv.
The problem is that in this part of a program (when a motor gradually lowers a speed and then stops) I can’t stop the motor upon pressing a stop button. I understand that I need to break a while loop somehow, but using a break statement doesn't wort for me.
Do you have some ideas?
Here is my function:
/* --- STEPPER MOTOR ---*/
const int motor_step = 3;
const int motor_dir = 4;
int stepSpeed = 0;
int stepMaxSpeed = 1000;
int fadeAmount = 100;
int fadeDelay = 10;
/* ---- STOP BUTTON ---- */
int buttonStop = 5;
int stateStop=0;
void setup() {
.
.
.
stateStop = digitalRead(buttonStop);
}
void loop () {
.
.
.
myfunc();
}
void myfunc() {
if(stateStop == HIGH) {noTone(motor_step); stepSpeed = 0;}
elapsedMillis te;
unsigned int iv = 1500;
while (te < iv) {
if(stepSpeed == stepMaxSpeed) {
stepSpeed = stepSpeed+0;
tone(motor_step,stepSpeed);
digitalWrite(motor_dir,HIGH);
}
else {
stepSpeed = stepSpeed + fadeAmount;
tone(motor_step,stepSpeed);
digitalWrite(motor_dir,HIGH);
delay(fadeDelay);
}
if(stateStop == HIGH) { stepSpeed = 0; break;}
}
if(stepSpeed == stepMaxSpeed) {
while(stepSpeed>0){
stepSpeed = stepSpeed-fadeAmount;
tone(motor_step,stepSpeed);
digitalWrite(motor_dir,HIGH);
delay(fadeDelay);
if(stateStop == HIGH) { stepSpeed = 0; break;}
}
}
stepSpeed = 0;
noTone(motor_step);
digitalWrite(enable,LOW); // enable changed from HIGH
}
Your break condition does never trigger as stateStop is never being updated inside your while loop. How is your program supposed to know? It's busy running the loop and does not care about anything outside it's scope.
Check the button state inside the while loops or use interrupts

STM32F4 - Button GPIO input doesn't work

I'm using a STM32F469 Discovery board, and I'm trying to get an external momentary push button to work.
It is currently connected to PG11, and it is set up so than when pressed it connects the pin to +5V supplied by the MCU. Before I continue, I just want to state that I use the following code in stmf4xx_it.c to perform debounce:
#define REFdebounce 200
int In1 = 3;
int In1_0 = 0;
int In1_1 = 0;
int StatoIn1 = 3;
void SysTick_Handler(void)
{
In1 = HAL_GPIO_ReadPin( GPIOG, GPIO_PIN_11 );
if ( In1 == 0 )
{
In1_0++;
In1_1 = 0;
if ( In1_0 >= REFdebounce )
{
In1_0 = REFdebounce + 1;
StatoIn1 = 0;
}
}
else
{
In1_0 = 0;
In1_1++;
if ( In1_1 >= REFdebounce )
{
In1_1 = REFdebounce + 1;
StatoIn1 = 1;
}
}
}
I have the following code in a header file inout.h:
typedef void ( * TSelectCallback ) ( int aSelectSignal );
void ConfigSelectPin
(
TSelectCallback aSelectCallback
);
And then in inout.c I have the following code for setup of the button GPIO pin:
#define SELECT_SIGNAL_PIN GPIO_PIN_11
#define SELECT_SIGNAL_GPIO_PORT GPIOG
#define SELECT_SIGNAL_GPIO_CLK_ENABLE() __HAL_RCC_GPIOG_CLK_ENABLE()
#define SELECT_SIGNAL_GPIO_CLK_DISABLE() __HAL_RCC_GPIOG_CLK_DISABLE()
#define SELECT_SIGNAL_EXTI_IRQn EXTI15_10_IRQn
void ConfigSelectPin( TSelectCallback aSelectCallback )
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOC clock */
SELECT_SIGNAL_GPIO_CLK_ENABLE();
/* Configure washer signal pin as input floating */
GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStructure.Pull = GPIO_PULLDOWN;
GPIO_InitStructure.Pin = SELECT_SIGNAL_PIN;
HAL_GPIO_Init( SELECT_SIGNAL_GPIO_PORT, &GPIO_InitStructure );
/* Enable and set EXTI lines 0 Interrupt to the lowest priority */
HAL_NVIC_SetPriority( SELECT_SIGNAL_EXTI_IRQn, 8, 0 );
HAL_NVIC_EnableIRQ( SELECT_SIGNAL_EXTI_IRQn );
SelectCallback = aSelectCallback;
}
void EXTI15_10_IRQHandler( void )
{
if (__HAL_GPIO_EXTI_GET_IT( SELECT_SIGNAL_PIN ) != RESET)
{
RedLedOn();
__HAL_GPIO_EXTI_CLEAR_IT( SELECT_SIGNAL_PIN );
HAL_GPIO_EXTI_IRQHandler( SELECT_SIGNAL_PIN );
}
}
void HAL_GPIO_EXTI_Callback( uint16_t GPIO_Pin )
{
if ( GPIO_Pin == SELECT_SIGNAL_PIN )
{
YellowLedOn();
GPIO_PinState pinState;
//pinState = HAL_GPIO_ReadPin( SELECT_SIGNAL_GPIO_PORT, GPIO_Pin );
pinState = 1;
if ( SelectCallback )
SelectCallback ( pinState );
}
}
Then in my main.c file I have the following:
/* variable to detect that hardware button is pressed */
static int Select = 0;
extern int StatoIn1;
void SelectIsrCallback( int aSelectSignal )
{
if ( StatoIn1 == 1 )
{
OrangeLedOn();
Select = 1;
}
}
I then use the following code to detect if the button has been pressed and perform my required action
if ( Select )
{
BlueLedOn();
Select = 0;
}
Now EVERY time I press the button, the EXTI15_10_IRQHandler is called as acknowledged by the red led turning on.
If I keep pressing the button, many many many times, the HAL_GPIO_EXTI_Callback will eventually be called as acknowledged by the yellow led turning on.
If I then keep pressing the button even more times, then eventually the SelectIsrCallback is called and my desired action is perfomed, as acknowledged by the orange and blue led turning on.
Why do the HAL_GPIO_EXTI_Callback and SelectIsrCallback not get called on the first button press? And why does the SelectIsrCallback not get called once the HAL_GPIO_EXTI_Callback is called?
NOTE: I've just moved the YellowLedOn() call, to before the if statement in HAL_GPIO_EXTI_Callback to see if it was this function of the if statement that takes loads of button presses before it is called. It made no difference, so the issue is with the calling of the HAL_GPIO_EXTI_Callback function.
Okay, so despite spending days to figure this out, it turns out the answer is a simple one. The order of the functions called in EXTI15_10_IRQHandler need to be switched. I.e. the HAL_GPIO_EXTI_IRQHandler needs to b called first and then the flag needs to be cleared. So this:
void EXTI15_10_IRQHandler( void )
{
if (__HAL_GPIO_EXTI_GET_IT( SELECT_SIGNAL_PIN ) != RESET)
{
RedLedOn();
**__HAL_GPIO_EXTI_CLEAR_IT( SELECT_SIGNAL_PIN );
HAL_GPIO_EXTI_IRQHandler( SELECT_SIGNAL_PIN );**
}
}
Needs to be switched to this:
void EXTI15_10_IRQHandler( void )
{
if (__HAL_GPIO_EXTI_GET_IT( SELECT_SIGNAL_PIN ) != RESET)
{
RedLedOn();
HAL_GPIO_EXTI_IRQHandler( SELECT_SIGNAL_PIN );
__HAL_GPIO_EXTI_CLEAR_IT( SELECT_SIGNAL_PIN );
}
}
This seems obvious now, because you can't clear the interrupt flag before calling the interrupt function otherwise it will not execute. However, I'm sure I saw a lot of examples where it was in the order I had the functions originally.

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
}
}

UART0 to UART2 gateway (sort of) for AtMega2560

I connected a device to the UART0 of the AtMega2560. I want to transfer the UART0 data to the UART2 to view it on the Terminal(PC).
When I connect the device directly to the PC using an UART to serial device (FTDI) It sends the data nicely.
When I put the UART2 in the middle for said purpose, then It only sends the first line, specifically:
Ver V2DAPV142 On-Line: And then forgets. Sometimes it doesn't send the first line too.
Code:
#define UART0_BUFFER_SIZE 40
#define RX_WAIT 65000
volatile unsigned char UART0_rx_ArrUC85[UART0_BUFFER_SIZE];
volatile unsigned char UART0_rx_ArrLength = 0, UART0_rx_ArrIndex = 0;
void uart0_init( unsigned int baudrate )
{
UBRR0H = (unsigned char) (baudrate>>8);
UBRR0L = (unsigned char) baudrate;
UCSR0B = ( 1 << RXEN0 ) | ( 1 << TXEN0 ) | (1<<RXCIE0);
UCSR0C = ( 1 << USBS0 ) | ( 1 << UCSZ01 ) | ( 1 << UCSZ00 ); // 8N1
}
void USART2Init(UINT16 ubrr_value)
{
UBRR2L = ubrr_value;
UBRR2H = (ubrr_value>>8);
UCSR2C|=(3<<UCSZ20);
UCSR2B = (1<<RXEN2) | (1<<TXEN2);
}
ISR(USART0_RX_vect)
{
unsigned char recChar = UDR0;
if (UART0_BUFFER_SIZE > UART0_rx_ArrLength)
{
UART0_rx_ArrUC85[UART0_rx_ArrIndex++] = recChar;
UART0_rx_ArrLength = UART0_rx_ArrIndex;
}
}
void uart2_putchar(UINT8 data)
{
//Local variables
unsigned int i;
for( i = 0; !( UCSR2A & ( 1 << UDRE2 ) ); i++ ) // Wait for empty transmit buffer
{
if( i > RX_WAIT ) // How long one should wait
{
return ; // Give feedback to function caller
}
}
UDR2 = data; // Start transmitting
//return (int)data; // Cast and return int value
}
void uart2_puts(unsigned char *str)
{
UINT8 dat;
for( ;*str != '\0'; )
{
dat= *str++ ;
uart2_putchar(dat);
}
}
int main()
{
USART2Init(8);
uart0_init(103);
sei();
while(1)
{
if(UART0_rx_ArrLength>0)
{
uart2_puts((unsigned char *) UART0_rx_ArrUC85);
UART0_rx_ArrLength = UART0_rx_ArrIndex = 0;
}
}
}
What could be the issue.
I checked it with same and different baud rates too for UART0 and UART2.
The issue was circuitry power level. The power supply was not sufficient for the Pen-Drive ctrlr and the regulator was not able to source for its communication power level. Hence it was not working sometimes. Further we have tested it and drew a conclusion that after giving sufficient power to the Pen-Drive ctrlr using another power regulator, the above said communication takes nicely place. I hope this can help ppl to draw attention towards the possible circuitry issues.