Help with code

To add comments, please log in or register
bdelphia
17
bdelphia  
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>  
#include <Trade\AccountInfo.mqh>
void OnTick()
  {
  //--Defining Variables for Information for Comment
   double myAccountBalance = AccountInfoDouble(ACCOUNT_BALANCE);
   double myAccountProfit = AccountInfoDouble(ACCOUNT_PROFIT);
   double myAccountEquity = AccountInfoDouble(ACCOUNT_EQUITY);
   
      //--Set and Define Buy Price Variable
   double AskPrice=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      //--Set and Define Sell Price Variable
   double BidPrice=SymbolInfoDouble(_Symbol,SYMBOL_BID);
   
   int myOrderT=PositionsTotal();
     double myMovingAverageDefinition;
     double myMA[];
   //--Set & Define Variable for Moving Average, period variable could be (20 or 50 for first number)
   myMovingAverageDefinition  =  iMA (_Symbol,_Period,20,2,MODE_EMA,PRICE_MEDIAN);
   
   CopyBuffer(myMovingAverageDefinition,0,0,1,myMA);
     
   
 
 //Comment on and Retrieve Variables or Information
   Comment("Account Balance: ",myAccountBalance,"\n","Account Profit: ", myAccountProfit,"\n","Account Equity: ",myAccountEquity,"\n","Moving Average Number",myMA[0],"\n","Ask Price for Buy: ",AskPrice,"\n","Bid Price for Sell: ",BidPrice,"\n");
   
 
      //Buy Trade Request
     
      //Defining of the Trade request
      
       
  
      //--If price below moving average then buy
      
      if(AskPrice<myMA[0])
      {
      MqlTradeRequest requestB;
      MqlTradeResult resultB;
      ZeroMemory(requestB);
       //--Buy
      requestB.action    =TRADE_ACTION_DEAL;
      requestB.symbol    =_Symbol;
      requestB.volume     =1;
      requestB.type_filling     =ORDER_FILLING_FOK;
      requestB.type      =ORDER_TYPE_BUY;
      requestB.price      =SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      requestB.tp     =SymbolInfoDouble(_Symbol,SYMBOL_ASK)+100*_Point;
      requestB.sl     =SymbolInfoDouble(_Symbol,SYMBOL_ASK)-100*_Point;
      requestB.deviation    =50;
         if(myOrderT<4)
         {
         OrderSend(requestB,resultB);
         return;
         }
      }
            if(AskPrice>myMA[0])
      {
        MqlTradeRequest requestS;
        MqlTradeResult resultS;
        ZeroMemory(requestS);
        //--Sell
        requestS.action    =TRADE_ACTION_DEAL;
        requestS.symbol    =_Symbol;
        requestS.volume     =1;
        requestS.type_filling     =ORDER_FILLING_FOK;
        requestS.type      =ORDER_TYPE_SELL;
        requestS.price      =SymbolInfoDouble(_Symbol,SYMBOL_BID);
        requestS.tp     =SymbolInfoDouble(_Symbol,SYMBOL_BID)+100*_Point;
        requestS.sl     =SymbolInfoDouble(_Symbol,SYMBOL_BID)-100*_Point;
        requestS.deviation    =50;
               if(myOrderT<4)
         {
         OrderSend(requestS,resultS);
         return;
         }
            
      }   
 
  
 
 if ((myAccountEquity - myAccountBalance) > 2)
   {
      CloseAllOrders();
   }
 } 
void CloseAllOrders()
  {
  CTrade trade;
  int i=PositionsTotal()-1;
  while (i>=0)
   {
      if (trade.PositionClose(PositionGetSymbol(i)))  i--;
   }
   
 }

Im having problems with this code switching to engaging in different trade orders, so specifically these lines 

     
      if(AskPrice<myMA[0])
      {
      MqlTradeRequest requestB;
      MqlTradeResult resultB;
      ZeroMemory(requestB);
       //--Buy
      requestB.action    =TRADE_ACTION_DEAL;
      requestB.symbol    =_Symbol;
      requestB.volume     =1;
      requestB.type_filling     =ORDER_FILLING_FOK;
      requestB.type      =ORDER_TYPE_BUY;
      requestB.price      =SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      requestB.tp     =SymbolInfoDouble(_Symbol,SYMBOL_ASK)+100*_Point;
      requestB.sl     =SymbolInfoDouble(_Symbol,SYMBOL_ASK)-100*_Point;
      requestB.deviation    =50;
         if(myOrderT<4)
         {
         OrderSend(requestB,resultB);
         return;
         }
      }
            if(AskPrice>myMA[0])
      {
        MqlTradeRequest requestS;
        MqlTradeResult resultS;
        ZeroMemory(requestS);
        //--Sell
        requestS.action    =TRADE_ACTION_DEAL;
        requestS.symbol    =_Symbol;
        requestS.volume     =1;
        requestS.type_filling     =ORDER_FILLING_FOK;
        requestS.type      =ORDER_TYPE_SELL;
        requestS.price      =SymbolInfoDouble(_Symbol,SYMBOL_BID);
        requestS.tp     =SymbolInfoDouble(_Symbol,SYMBOL_BID)+100*_Point;
        requestS.sl     =SymbolInfoDouble(_Symbol,SYMBOL_BID)-100*_Point;
        requestS.deviation    =50;
               if(myOrderT<4)
         {
         OrderSend(requestS,resultS);
         return;
         }
            
      }   
 
  

I thought that if written like this when the first if statement is true then it would go through with the localized order which it does but the issue is with the second order which is when th if statement is true but the order doesn't go so and i hae fiddled wit hit a lot and the issue to me seems to be MqlTrade and i was wondering how to write this so it works,

Thank you

lippmaje
747
lippmaje  
        requestS.type      =ORDER_TYPE_SELL;
        requestS.price      =SymbolInfoDouble(_Symbol,SYMBOL_BID);
        requestS.tp     =SymbolInfoDouble(_Symbol,SYMBOL_BID)-100*_Point;
        requestS.sl     =SymbolInfoDouble(_Symbol,SYMBOL_BID)+100*_Point;

TP must be below price for this order type (and SL above.)

And it is recommended to use the current tick to get bid and ask:

    MqlTick tick;
    if(!SymbolInfoTick(_Symbol,tick)) { Print("SymbolInfoTick failed, error = ",GetLastError()); ExpertRemove(); return; }
    double AskPrice=tick.ask;
    double BidPrice=tick.bid;
To add comments, please log in or register