Beginner Problems! - page 2

 

Hello,

I think your problem is

#define MAGICNUMBER 111222


I am a complete noob at this code thing, but where is the exit?

void OnTick()
  {
   int i=0;
     {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderMagicNumber()==MAGICNUMBER && OrderSymbol()==Symbol())
         if(OrderType()==OP_BUY)
           {
            if(((Bid-OrderOpenPrice())>(Point*TrailingStop)) && (OrderStopLoss()<(Bid-Point*TrailingStop)))
               OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),NULL,NULL);
           }
      else
         Print("OrderSelect() returned error - ",GetLastError());
      if(OrderType()==OP_SELL)
        {
         if(((OrderOpenPrice()-Ask)>(Point*TrailingStop)) && (OrderStopLoss()>(Ask+Point*TrailingStop)))
            OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),NULL,NULL);
        }
     }



     {
      int total,ord=0,a;

      int mn=MAGICNUMBER;

      string symbol;

      total=OrdersTotal();

      for(a=0;a<total;a++)

        {

         OrderSelect(a,SELECT_BY_POS);

         if(OrderSymbol()==Symbol() && OrderMagicNumber()==mn)ord++;

        }

      if(ord>0)
         return;      
        

   double CurrentSlowMA    = iMA (NULL, 0, SlowMA, 0, MODE_SMA, PRICE_CLOSE, 1);
   double CurrentFastMA    = iMA (NULL, 0, FastMA, 0, MODE_SMA, PRICE_CLOSE, 1);
   double PreviousKline    = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_MAIN, 2);
   double CurrentKline     = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_MAIN, 1);
   double PreviousDline    = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 2);
   double CurrentDline     = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 1);
   double Aa               = StopLoss*.1;
   double Bb               = AccountBalance()*.01;
   double LotSize          = Bb/Aa;
      
   
   for(int pos=OrdersHistoryTotal();pos >=0; pos--)
      if(OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY)
         && OrderMagicNumber() == MAGICNUMBER
         && OrderSymbol() == Symbol()
         && OrderType() == OP_BUY
         && CurrentFastMA>CurrentSlowMA)
         return;
         
   for(int pos=OrdersHistoryTotal();pos >=0; pos--)      
      if(OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY)
         && OrderMagicNumber() == MAGICNUMBER
         && OrderSymbol() == Symbol()
         && OrderType() == OP_SELL
         && CurrentFastMA<CurrentSlowMA)
         return;      
      
      
      if(CurrentFastMA>CurrentSlowMA)
         if(CurrentDline<CurrentKline && CurrentKline>25)
            if(PreviousDline<25 && CurrentDline>25)
               if(OrderSend(Symbol(),OP_BUY,LotSize,Ask,50,Ask-StopLoss*_Point,Ask+TakeProfit*_Point,NULL,111222)<0)
                  Print(__FUNCTION__+" OrderSendError: ",GetLastError());

      if(CurrentFastMA<CurrentSlowMA)
         if(CurrentDline>CurrentKline && CurrentKline<75)
            if(PreviousDline>75 && CurrentDline<75)
               if(OrderSend(Symbol(),OP_SELL,LotSize,Bid,50,Bid+StopLoss*_Point,Bid-TakeProfit*_Point,NULL,111222)<0)
                  Print(__FUNCTION__+" OrderSendError: ",GetLastError());

      //---
     }
  }
//+------------------------------------------------------------------+
 
               OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),NULL,NULL);

 SL,TP,0,clrNONE

 
GrumpyDuckMan:

Hello,

I think your problem is


I am a complete noob at this code thing, but where is the exit?


Hello GrompyDuckMan. Why would the magic number cause a problem? I'm unsure on how to code this EA without that magic number in order to ensure it only works on its own trades.

With regard to your second question, this EA does not have any technical exit rules yet. Exits are handled by the SL, Trailing Stop or TP. Is that what you were asking about? Sorry if there is any confusion, I didn't fully grasp the question. 


Thanks

 
UYPTrade:

Hello GrompyDuckMan. Why would the magic number cause a problem? I'm unsure on how to code this EA without that magic number in order to ensure it only works on its own trades.

With regard to your second question, this EA does not have any technical exit rules yet. Exits are handled by the SL, Trailing Stop or TP. Is that what you were asking about? Sorry if there is any confusion, I didn't fully grasp the question. 


Thanks

   int i=0;

and


   for(int pos=OrdersHistoryTotal();pos >=0; pos--)
      if(OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY)
         && OrderMagicNumber() == MAGICNUMBER
         && OrderSymbol() == Symbol()
         && OrderType() == OP_BUY
         && CurrentFastMA>CurrentSlowMA)
         return;
         
   for(int pos=OrdersHistoryTotal();pos >=0; pos--)      
      if(OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY)
         && OrderMagicNumber() == MAGICNUMBER
         && OrderSymbol() == Symbol()
         && OrderType() == OP_SELL
         && CurrentFastMA<CurrentSlowMA)
         return;  
         OrderSelect(a,SELECT_BY_POS);

Is it i,a or pos.

You never return anything... there is a return value

 
GrumpyDuckMan:

and


Is it i,a or pos.

You never return anything... there is a return value


Hi Grumpy, thank for your reply. As you can see from the thread, I'm not a very experienced coder. This is my first EA and my first real foray into the coding world. In the above code, I have used "i","a" and "ord" for different purposes. If I were to change the "a" or "ord" I would get a warning about hiding the original declaration of i. Not sure if this is a big deal but I tend to try and avoid confusion if I can. I was under the impression that when return is used inside a void function it should not return a value?


Thanks

Reason: