[WARNING CLOSED!] Any newbie question, so as not to clutter up the forum. Professionals, don't go by. Can't go anywhere without you. - page 910

 
Vinin:

Victor, take a look at your personal message, please.
 
glasha:
Victor, take a look at your personal message, please.

Seen it
 
dimon74:

OrderSend() is triggered at every tick (as I understand it). To eliminate the problem, place a variable before the start function, for example int H=0.

Further the code:

..........

if ( H==0)

{

OrderSend(blah blah blah);

}

H=1;

And there's no need for algebra. And OrdersTotal() is better not to be used to limit.


I understand, it works for me, I mean I have to open 1 order and it won't open at the next tick

The second order, the second order is only needed when the first one closes

 

If I'm not mistaken, it must be like this?

int H=0;

int F=OrdersTotal();

..........

if(F<1)

{

if ( H==0)

{

OrderSend(blah blah blah);

}

H=1;}

 
gheka:

If I'm not mistaken, it must be like this?

int H=0;

int F=OrdersTotal();

..........

if(F<1)

{

if ( H==0)

{

OrderSend(blah blah blah);

}

H=1;}

total = CountTrades();
if (total < 1) {
ticket = OrderSend
}

int CountTrades() {
   int count = 0;
   for (int trade = OrdersTotal() - 1; trade >= 0; trade--) {
      OrderSelect(trade, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         if (OrderType() == OP_SELL || OrderType() == OP_BUY) count++;
   }
   return (count);
}
 
gheka:


I understand, it works for me, I mean I have to open 1 order and it won't open at the next tick

the second order, the second order is needed only when the first one closes

You should check here.
 

Good afternoon everyone. I am not very good at writing EAs, I tried to learn this stuff, but I just started and it is quite difficult for me. I want to write an EA that would open trades when prices cross a moving average and close them at the opposite crossing, either using stop loss or trailing stop. I found an example of such an EA, but there is no trailing stop or stop loss and trades are opened and closed simply at crossing. I have not got enough knowledge for that. I want to ask someone to help me where to put trailing stop and stoploss and how to make it work:

#property copyright "Jake Sunders"

#property link "feloq@mail.ru"



#define STATE_SQUARE 0

#define STATE_LONG 1

#define STATE_SHORT 2



//---- input parameters

extern int MAPeriod=26;
extern double LotsNumber=1.0;
extern double TrailingStop=50;
extern double StopLoss= 50;


//---- global variables

int CurrentState;

int MyOrderTicket;

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init()

{

//----

if (iMA(NULL, 0, MAPeriod, 0, MODE_SMA, PRICE_CLOSE, 0) > Close[0])

CurrentState = STATE_SHORT;

else CurrentState = STATE_LONG;

MyOrderTicket = 0;
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialisation function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
int err;
double MA;
MA = iMA(NULL, 0, MAPeriod, 0, MODE_EMA, PRICE_CLOSE, 0);
if ( CurrentState == STATE_LONG)
{
if (MA > Close[1]) //the moving average is higher than the close price
{
CurrentState = STATE_SHORT;
//turn to sell
//---close position if opened
if ( MyOrderTicket != 0)
{
if (!OrderClose(MyOrderTicket, LotsNumber, Bid, 3, CLR_NONE))
{
err = GetLastError();
Print("Error at closing the position: ", err);
return(0);
}
MyOrderTicket = 0;
}
RefreshRates();
//--- long position was closed successfully
//--- now let's open a sell position
//--- check for free funds
MyOrderTicket = OrderSend(Symbol(), OP_SELL, LotsNumber, Bid, 3, 0, 0,
NULL, 0, 0, CLR_NONE);
if (MyOrderTicket<0)
{
err = GetLastError();
Print("Error at the opening of the position: ", err);
MyOrderTicket = 0;
}
}
}
else
{
if (MA < Close[1]) // sliding average below closing price
{
CurrentState = STATE_LONG;

//reverse to buy
//---close position if opened
if ( MyOrderTicket != 0)
{
if (!OrderClose(MyOrderTicket, LotsNumber, Ask, 3, CLR_NONE))
{
err = GetLastError();
Print("Error at closing the position: ", err);
return(0);
}
MyOrderTicket = 0;
}
RefreshRates();
//--- short position was closed successfully
//--- now let's open a Buy position
//--- check for free funds
MyOrderTicket = OrderSend(Symbol(), OP_BUY, LotsNumber, Ask, 3, 0, 0,
NULL, 0, 0, CLR_NONE);
if (MyOrderTicket<0)
{
err = GetLastError();
Print("Error at the opening of the position: ", err);
MyOrderTicket = 0;
}
}
}
//----
return(0);
}

 

People, please advise! How do I make a position open and close on the expiry of a candle or candlesticks? That is, it opens at the opening of a new candlestick, and closes

How do I take the indicator values at the close of a candlestick?

 
gheka:

I have buy and sell orders that open at the same price for almost 50-100 positions,

how should i make only one order open, if i'm not mistaken - OrdersTotal()

If so, how should I use it and where should I place it? I feel that without this function I need to input the entire algebra


Wrong conditions for making trades, see example for two MAs.
 
Superjake:

Good afternoon everyone. I am not very good at writing EAs, I tried to learn this stuff, but I just started and it is quite difficult for me. I want to write an EA that would open trades when prices cross a moving average and close them at the opposite crossing, either using stop loss or trailing stop. I found an example of such an EA, but there is no trailing stop or stop loss and trades are opened and closed simply at crossing. I have not got enough knowledge for that. I want to ask someone to help me where to put trailing stop and stoploss and how to make it work:

#property copyright "Jake Sunders"

#property link "feloq@mail.ru"



#define STATE_SQUARE 0

#define STATE_LONG 1

#define STATE_SHORT 2



//---- input parameters

extern int MAPeriod=26;
extern double LotsNumber=1.0;
extern double TrailingStop=50;
extern double StopLoss= 50;


//---- global variables

int CurrentState;

int MyOrderTicket;

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init()

{

//----

if (iMA(NULL, 0, MAPeriod, 0, MODE_SMA, PRICE_CLOSE, 0) > Close[0])

CurrentState = STATE_SHORT;

else CurrentState = STATE_LONG;

MyOrderTicket = 0;
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialisation function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
int err;
double MA;
MA = iMA(NULL, 0, MAPeriod, 0, MODE_EMA, PRICE_CLOSE, 0);
if ( CurrentState == STATE_LONG)
{
if (MA > Close[1]) //the moving average is higher than the close price
{
CurrentState = STATE_SHORT;
//turn to sell
//---close position if opened
if ( MyOrderTicket != 0)
{
if (!OrderClose(MyOrderTicket, LotsNumber, Bid, 3, CLR_NONE))
{
err = GetLastError();
Print("Error at closing the position: ", err);
return(0);
}
MyOrderTicket = 0;
}
RefreshRates();
//--- long position was closed successfully
//--- now let's open a sell position
//--- check for free funds
MyOrderTicket = OrderSend(Symbol(), OP_SELL, LotsNumber, Bid, 3, 0, 0,
NULL, 0, 0, CLR_NONE);
if (MyOrderTicket<0)
{
err = GetLastError();
Print("Error at the opening of the position: ", err);
MyOrderTicket = 0;
}
}
}
else
{
if (MA < Close[1]) // sliding average below closing price
{
CurrentState = STATE_LONG;

//reverse to buy
//---close position if opened
if ( MyOrderTicket != 0)
{
if (!OrderClose(MyOrderTicket, LotsNumber, Ask, 3, CLR_NONE))
{
err = GetLastError();
Print("Error at closing the position: ", err);
return(0);
}
MyOrderTicket = 0;
}
RefreshRates();
//--- short position was closed successfully
//--- now let's open a Buy position
//--- check for free funds
MyOrderTicket = OrderSend(Symbol(), OP_BUY, LotsNumber, Ask, 3, 0, 0,
NULL, 0, 0, CLR_NONE);
if (MyOrderTicket<0)
{
err = GetLastError();
Print("Error at the opening of the position: ", err);
MyOrderTicket = 0;
}
}
}
//----
return(0);
}


Look at this example
Reason: