2 issues with New Bot

To add comments, please log in or register
shido641
60
shido641  

Hi All,


This is the first time im posting here as it's my first time doing ANY coding, i seriously didnt know anything about coding since 2 days ago. I must say, it's pretty fun. I have learned quite a bit by trial and error and reading the articles here. Anyways this bot is still a work in progress and my code looks like a mess, nevertheless i have a problem. if i put my TP on the line of code above my SL line of code then when my bot runs it only placed the trade with a TP; if i switch it around then the SL is put in when the order is placed but the TP is missing.

I am obviously doing something completely wrong so i would appreciate if someone can point me in the right direction. So what i did was to get the bid and ask price and then just add on from there so that its a certain amount of pips for TP and a certain amount of pips for SL bearing in mind the brokers minimum SL amount.


Then the other problem is that i just need some code that will stop a crap load of positions opening at the trigger. I just need 1 position opened when the cross happens unless its an opposite trigger


In short

1) SL and TP wont be placed in the order together

2) I need 1 trade opened per cross


Please help, i'm really new to this so im going to need a little explanation of why it doesn't work


// Define our Parameters
input double Lots          = 1;
input int PeriodOne        = 5; // The period for the first SMA
input int PeriodTwo        = 9; // The period for the second SMA


void OnTick()
{
double SlowMA = iMA(NULL,0,PeriodOne,0,MODE_EMA,PRICE_CLOSE,0);
double FastMA = iMA(NULL,0,PeriodTwo,0,MODE_EMA,PRICE_CLOSE,0);
double LastSlowMA = iMA(NULL,0,PeriodOne,0,MODE_EMA,PRICE_CLOSE,1);
double LastFastMA = iMA(NULL,0,PeriodTwo,0,MODE_EMA,PRICE_CLOSE,1);
double TP = MarketInfo(Symbol(),MODE_BID);
double SL = MarketInfo(Symbol(),MODE_ASK);

   
if ((LastFastMA<LastSlowMA)
     &&(FastMA>SlowMA))
     

//--- calculated SL and TP prices
 
 
 double Takeprofit = TP+0.0024;
 double SL1 = SL-0.001;   

//--- place market order to buy 1 lot
   int ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,SL1,Takeprofit,"My order",MAGICNUM,0,clrGreen);
   if(ticket<0)
     {
      Print("OrderSend failed with error #",GetLastError());
     }
   else
      Print("OrderSend placed successfully");

//---
            
 }
shido641
60
shido641  

Ok so i think i solved problem 1. Instead of doing this

 int ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,SL1,Takeprofit,"My order",MAGICNUM,0,clrGreen);

i did this, like right after i posted this

int ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,SL1,TP+0.0024,"My order",MAGICNUM,0,clrGreen);


And that works perfectly. Now for the second problem...i have been doing some reading and i need to put in some loops or something. Or perhaps use my magic number? Can someone assist with the code

Roman Starostin
3913
Roman Starostin  
add extra condition to your code - count open orders:

if ((LastFastMA<LastSlowMA)
     &&(FastMA>SlowMA)) {

        double Takeprofit = TP+0.0024;
        double SL1 = SL-0.001;   
        
        if(CountOrder(OP_BUY) == 0) {
           int ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,SL1,Takeprofit,"My order",MAGICNUM,0,clrGreen);
           if(ticket<0) Print("OrderSend failed with error #",GetLastError());
           else Print("OrderSend placed successfully"); 
        }

}

//+------------------------------------------------------------------+
int CountOrder(int Order_Type) {
   int orders=0;
   for(int i=OrdersTotal()-1;i>=0;i--){
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==false) continue;
      if(OrderSymbol()!=_Symbol || OrderMagicNumber() != MAGICNUM) continue;
      if(Order_Type == OrderType() || Order_Type == -1) orders++;
    }
   return(orders);
}
shido641
60
shido641  
Roman Starostin:
add extra condition to your code - count open orders:

Hi Thanks. Now i am getting an error that says 'CountOrder' - function can be declared only in the global scope. How do i fix that?

Roman Starostin
3913
Roman Starostin  
shido641:

Hi Thanks. Now i am getting an error that says 'CountOrder' - function can be declared only in the global scope. How do i fix that?

Where do you get it?

int CountOrder(int Order_Type) { ... }	
this code you have to set in the bottom of your code, below OnTick()
To add comments, please log in or register