First Built EA - Problem making Lots increase

 

I just recently made my first EA.I was able to learn enough code to be error free.The EA is quite good and I will be glad to post it in the Future.

Right now I am having a problem getting it to buy more than .10 Lots.I want to step it up to .5.It has a Lot Optimizer in it with a Decrease Factor currently

set to 3. I changed the External to .5 and put the Decrease factor to 0.It hasn't done anything on a Demo acct.

2 More questions: The EA that I made has parts like Orders code etc. from another EA. What I am confused about is the different formats that these have.I have studied a code

manual also.The formats being, this one has No Initialization or DeInitialization blocks.The Start function is at the bottom instead of the Top of the page.

The Opening code below externals:

//+------------------------------------------------------------------+
//| Calculate open positions |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
{
int buys=0,sells=0;
//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;

}

2nd question: Just for fun I want to make an Oscillator EA or one that is in the Market all the time.I know they generally don't work too well but want to make one.How do I make it close a Long Position and go Short or the other way around.


Thanks, Jims Trading Machine

 

Your order selection code is incorrect. It should look like this:

for( int i = OrdersTotal() - 1; i >= 0; i-- )
{
   if( !OrderSelect( i, SELECT_BY_POS ) continue;

   if( OrderSymbol() == Symbol() && OrderMagicNumber() == MAGICMA )
   {
      if( OrderType() == OP_BUY ) buys++;
      if( OrderType() == OP_SELL ) sells++;
   }
}

You should always work backwards through the order queue when selecting by position or else you are inviting bugs. I removed MODE_TRADES because it is the default. I replaced "break" with "continue" so that the rest of the orders are still processed if you get an error from OrderSelect on a single order. Using "break" exits the for loop completely; using "continue" only skips the rest of the current iteration and goes on to the next one.

My personal preference is to not select by position. Any time I open an order I capture the ticket number and store it in an array. I only select by position if I find OrdersTotal() has gone out of sync with the number of orders stored in my tickets array. I hear that orders can be split in two by a broker, and that one has to watch for this. I have yet to see it happen. Also, your EA appears to allow for hedging. You won't be allowed to do this with a US broker.

Your init() and deinit() blocks are not entirely necessary. I use init() to initialize my global variables. Their importance really just depends on the nature of your EA. I have written an EA that loads a neural network library in the init() function, and then deallocates the neural networks in the deinit() function.

As far as your position sizing problem, I would have to see code.

And as for an oscillator based EA, decide what your buy and sell signals will be. Close a buy when you receive a sell signal, then open the sell. Close the sell when you get another buy signal, then open the buy. I find this works best when you operate tick-by-tick and not on bar opens or closes. And when I say works best, I don't mean it is profitable, I mean your orders will just naturally chain together this way. I've done this using Williams %R, and it is quite satisfying when you get a sequence of trades all chained together, all catching the reversals spot on. Unfortunately that doesn't happen often enough. ;)

 

Please, you can help me to correct this code to work and place order.

I attached the code.

// Trailing stop
extern int MA50 = 50;
extern int MA3 = 3;
extern int MA8 = 8;
extern int FastEMA = 12;
extern int SlowEMA = 26;
extern int SignalSMA = 9;
extern double Lots=0.05;
extern int Slippage=3;
extern double  TakeProfit=300; // take profit
extern double  StopLoss=200; // stop loss
//+------------------------------------------------------------------+
int start()
  {
   int arraysize=600;
   double OsMA[];
   double EMAArray[];
   double SMAArray[];
   ArrayResize(OsMA,arraysize);
   ArrayResize(EMAArray,arraysize);
   ArrayResize(SMAArray,arraysize);
   ArraySetAsSeries(OsMA,true);
 
   for(int i3=arraysize-1;i3>=0;i3--)
      OsMA[i3]=iOsMA(NULL,0,12,26,9,PRICE_CLOSE,i3);
   for(i3=arraysize-1;i3>=0;i3--)
      EMAArray[i3]=iMAOnArray(OsMA,0,1,0,OsMA[i3],i3);
   double EMA =EMAArray[i3];
   
   for(i3=arraysize-1;i3>=0;i3--)
      OsMA[i3]=iOsMA(NULL,0,12,26,9,PRICE_CLOSE,i3);
   for(i3=arraysize-1;i3>=0;i3--)
        SMAArray[i3]=iMAOnArray(OsMA,0,8,0,OsMA[i3],i3);
   double SMA =SMAArray[i3];
   double MA50=iMA(NULL,0, 50 ,0,MODE_SMA,PRICE_CLOSE,0);
   double MA3 =iMA(NULL,0, 3 ,0,MODE_LWMA,PRICE_OPEN,0);
   double MA8 =iMA(NULL,0, 8 ,0,MODE_SMA,PRICE_OPEN,0);
//----
   bool Long=false;
   bool Short=false;
   bool Sideways=false;
   if(MA3>MA8 && MA8>MA50 && EMA>SMA) Long=true;
   if(MA3<MA8 && MA8<MA50 && EMA<SMA) Short=true;
   if(Long==true  && OrdersTotal()==0)
     {
      OrderSend(Symbol(),OP_BUY ,Lots,Ask,Slippage,Ask/3,Ask*3,0,0,Blue);
     }
   if(Short==true && OrdersTotal()==0)
     {
      OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,Bid*3,Bid/3,0,0,Red);
     }
   if(Sideways==true  && OrdersTotal()!=0)
     {
      OrderSelect(0, SELECT_BY_POS, MODE_TRADES);
      Comment("Sideways detected");
      if(OrderType()==OP_BUY) OrderClose(OrderTicket(),1,Ask,3,Red);
      if(OrderType()==OP_SELL) OrderClose(OrderTicket(),1,Bid,3,Red);
     }
  }
//+------------------------------------------------------------------+
Reason: