# help with my code SMA

Deposit to your account in any convenient way. There are 4 of them here!
175
2013.12.23 12:55

Hello, I have a problem with my code. I'm trying to use two sma (4.9), and when they reach the intersection of two open a position, the first candle goes well but in others no achievement to come out. I'm doing wrong?
Another question is how when I have to make buy or sell, the theory says that when the 4 ma ma intersects the 9 top-down favorite. but I can not do that function.
someone can give me a hand.
thanks

this is the code

```int totalTrades=0;

//SMA
int MATimeFrame = 0;
int MA4Period=4;
int MA9Period=9;
int MMAShift = 0;
int MAShift = 0;

//RSI
int RSITimeFrame = 0;
int RSIShift = 0;
int RSIPeriod=14;
double SignalRSI = 50.0000;

double SMA4, SMA9, RSI;

double SMA4_ant, SMA4_pos;

int init()
{
return(0);
}

void prepareIndicators(){

SMA4     = iMA( NULL, MATimeFrame, MA4Period, MMAShift, MODE_SMA, PRICE_CLOSE, MAShift );
SMA4_ant = iMA( NULL, MATimeFrame, MA4Period, MMAShift, MODE_SMA, PRICE_CLOSE, 1 );
//   SMA4_pos = iMA( NULL, MATimeFrame, MA4Period, MMAShift, MODE_SMA, PRICE_CLOSE, MAShift+2 );

SMA9 = iMA( NULL, MATimeFrame, MA9Period, MMAShift, MODE_SMA, PRICE_CLOSE, MAShift );

}

/*
Abre las posiciones
*/
void openPosition(){

{
//   if((SMA4-SMA9)>=0.0001 && SMA9 == SMA4)
if(SMA9 == SMA4)
{
OrderSend( Symbol(), OP_SELL, 0.01, Bid, 0, 0, 0, "basketNumber", MAGIC ) ;
Alert(Symbol() + " - " + " SMA4 : " + SMA4 + " SMA4 anterior: " + SMA4_ant + " - " + "SMA4 posterio: "+ SMA4_pos);
}

}
}

void closeAll(){
for( int i = 0; i < OrdersTotal(); i++ ) {
if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ) break;
if( OrderSymbol() == Symbol() ){
RefreshRates();
if( OrderProfit() > MinProfit ) {
if( OrderType() == OP_BUY ) OrderClose( OrderTicket(), OrderLots(), Bid, 0 );
if( OrderType() == OP_SELL ) OrderClose( OrderTicket(), OrderLots(), Ask, 0 );
}
}
}
}

void prepare(){
prepareIndicators();
openPosition();
closeAll();
}

int start() {

prepare() ;
return( 0 ) ;
}

```
175
2013.12.24 14:20

done, sorry and thanks

Moderator
17999
2013.12.24 14:24
serdan:

done, sorry and thanks

Thank you
6314
2013.12.24 16:04
` if(SMA9 == SMA4)`

this won't work check if lines cross

175
2013.12.24 18:22

thanks i change te code but the program does the same...

```   SMA4 = iMA( NULL, MATimeFrame, MA4Period, MMAShift, MODE_SMA, PRICE_CLOSE, MAShift );
SMA9 = iMA( NULL, MATimeFrame, MA9Period, MMAShift, MODE_SMA, PRICE_CLOSE, MAShift );

SMA4_ant = iMA( NULL, MATimeFrame, MA4Period, MMAShift, MODE_SMA, PRICE_CLOSE, 2 );
SMA9_ant = iMA( NULL, MATimeFrame, MA9Period, MMAShift, MODE_SMA, PRICE_CLOSE, 2);

if(SMA4 > SMA9 && SMA4_ant<=SMA9_ant )
{
Alert(Symbol() + " - " + "B SMA4 : " + SMA4 + " SMA4 anterior: " + SMA4_ant + " - " + "SMA9 : "+ SMA9 + " - " + "SMA9 anterior: "+ SMA9_ant);
}      else{
if(SMA4 < SMA9 && SMA4_ant>=SMA9_ant )
{
OrderSend( Symbol(), OP_SELL, 0.01, Bid, 0, 0, 0, "basketNumber", MAGIC ) ;
Alert(Symbol() + " - " + "S SMA4 : " + SMA4 + " SMA4 anterior: " + SMA4_ant + " - " + "SMA9 : "+ SMA9 + " - " + "SMA9 anterior: "+ SMA9_ant);
175
2013.12.27 01:33

hi again i change mi code but I have a problem, I can only open a single operation. and what I want is to open multiple operations and closing them go when they get to profit.

someone could give me a hand??

thanks

```void openPosition(){
if( AccountFreeMargin() > 0 ){
if((SMA4 >= SMA9 && SMA4_ant<=SMA9_ant)&& (RSI >= SignalRSImiddle && RSI <= SignalRSIhigh))
{
if (OrdersTotal()==0){
{
OrderSend( Symbol(), OP_BUY, lotSize, Ask, slippage, 0, 0, CommentName, MAGIC,0,Green ) ;
Alert(Symbol() + " - " + "B>4 SMA4 : " + SMA4 + " SMA4 anterior: " + SMA4_ant + " - " + "SMA9 : "+ SMA9 + " - " + "SMA9 anterior: "+ SMA9_ant);
Alert(Symbol() + " - " + "B>4 RSI : " + RSI + " RSI Middle: " + SignalRSImiddle + " - " + "RSI HIGH : "+ SignalRSIhigh + " - " + "RSI Low : "+ SignalRSIlow);
Alert(Symbol() + " - " + "B>4 Ask: " + Ask + " Bid: " + Bid);
Alert(Symbol() + " - " + "B>4 pippoint: " + pipPoints);
}
if(Bid<SMA4)
{
OrderSend( Symbol(), OP_SELL, lotSize, Bid, slippage, 0, 0, CommentName, MAGIC,0,Red ) ;
Alert(Symbol() + " - " + "S>4 SMA4 : " + SMA4 + " SMA4 anterior: " + SMA4_ant + " - " + "SMA9 : "+ SMA9 + " - " + "SMA9 anterior: "+ SMA9_ant);
Alert(Symbol() + " - " + "S>4 RSI : " + RSI + " RSI Middle: " + SignalRSImiddle + " - " + "RSI HIGH : "+ SignalRSIhigh + " - " + "RSI Low : "+ SignalRSIlow);
Alert(Symbol() + " - " + "S>4 Bid: " + Bid + " Ask: " + Ask);
Alert(Symbol() + " - " + "S>4 pippoint: " + pipPoints);
}
}
}

if((SMA4 <= SMA9 && SMA4_ant>=SMA9_ant) && (RSI <= SignalRSImiddle && RSI >= SignalRSIlow))
{
if (OrdersTotal()==0){
{
OrderSend( Symbol(), OP_BUY, lotSize, Ask, slippage, 0, 0, CommentName, MAGIC ,0,Yellow) ;
Alert(Symbol() + " - " + "B<4 SMA4 : " + SMA4 + " SMA4 anterior: " + SMA4_ant + " - " + "SMA9 : "+ SMA9 + " - " + "SMA9 anterior: "+ SMA9_ant);
Alert(Symbol() + " - " + "B<4 RSI : " + RSI + " RSI Middle: " + SignalRSImiddle + " - " + "RSI HIGH : "+ SignalRSIhigh + " - " + "RSI Low : "+ SignalRSIlow);
Alert(Symbol() + " - " + "B<4 Ask: " + Ask + " Bid: " + Bid);
Alert(Symbol() + " - " + "B<4 pippoint: " + pipPoints);
}
if(Bid<SMA4)
{
OrderSend( Symbol(), OP_SELL, lotSize, Bid, slippage, 0, 0, CommentName, MAGIC ,0,PaleGreen) ;
Alert(Symbol() + " - " + "S<4 SMA4 : " + SMA4 + " SMA4 anterior: " + SMA4_ant + " - " + "SMA9 : "+ SMA9 + " - " + "SMA9 anterior: "+ SMA9_ant);
Alert(Symbol() + " - " + "S<4 RSI : " + RSI + " RSI Middle: " + SignalRSImiddle + " - " + "RSI HIGH : "+ SignalRSIhigh + " - " + "RSI Low : "+ SignalRSIlow);
Alert(Symbol() + " - " + "S<4 Bid: " + Bid + " Ask: " + Ask);
Alert(Symbol() + " - " + "S<4 pippoint: " + pipPoints);
}
}
//Alert("Error Number: "+GetLastError() + "Description: " + ErrorDescription());
}

}
}

void closeAll(){
for( int i = 0; i < OrdersTotal(); i++ ) {
if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ) break;
if( OrderSymbol() == Symbol() ){
RefreshRates();
//            if( OrderProfit() > MinProfit) | ( OrderProfit() > MinProfit/2) | ( OrderProfit() > MinProfit/3){
if( OrderProfit() > MinProfit){
if( OrderType() == OP_BUY ) OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 0,White );
if( OrderType() == OP_SELL ) OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 0,Blue );
}
}
}
}```
Moderator
17999
2013.12.27 09:51
serdan:

hi again i change mi code but I have a problem, I can only open a single operation. and what I want is to open multiple operations and closing them go when they get to profit.

someone could give me a hand??

thanks

Do your OrderSend() calls fail ? don't you want to know ? and if they do fail don't you want to know why ? Please read and implement this: What are Function return values ? How do I use them ?
6314
2013.12.27 12:28
serdan:

hi again i change mi code but I have a problem, I can only open a single operation. and what I want is to open multiple operations and closing them go when they get to profit.

someone could give me a hand??

thanks

do you see it ??? why can you only open a single operation. After one trade opend OrdersTotal becomes ...........
``` if (OrdersTotal()==0){
{
.....
}```
175
2013.12.27 12:52

hi, yes i see it, and i put it because but opens many operations in the same candle....

175
2013.12.27 13:03
RaptorUK:
Do your OrderSend() calls fail ? don't you want to know ? and if they do fail don't you want to know why ? Please read and implement this: What are Function return values ? How do I use them ?

thanks for the suggestions, I have a pending error handling and optimizing the code.
I'm just starting to get the robot to work as I want ..
Moderator
17999
2013.12.27 13:06

serdan:

thanks for the suggestions, I have a pending error handling and optimizing the code.

I'm just starting to get the robot to work as I want ..

Error checking and reporting should go in from the start, how else will you find the cause of some errors in your code ?

Your CloseAll() function will not close all, your loop MUST count down not up: Loops and Closing or Deleting Orders

12