SwingItBaby_Advanced

Author: Michael Zuegg
Price Data Components
Series array that contains the highest prices of each barSeries array that contains the lowest prices of each barSeries array that contains close prices for each bar
Orders Execution
It can change open orders parameters, due to possible stepping strategyIt automatically opens orders when conditions are reachedIt Closes Orders by itself
0 Views
0 Downloads
0 Favorites
SwingItBaby_Advanced
//+------------------------------------------------------------------+
//|                                         SwingItBaby_Advanced.mq4 |
//|                                                    Michael Zuegg |
//|                                       when-money-makes-money.com |
//+------------------------------------------------------------------+
#property copyright "Michael Zuegg"
#property link      "when-money-makes-money.com"


int Orders.Long[3];
int Orders.Long.Status=0;
int Orders.Short[3];
int Orders.Short.Status=0;
double Stop=0;
extern int SL=200;

void Trading.SetToBreakeven(int type,int order){
   int i;
   switch(type){
      case OP_BUY:{
         for(i=Orders.Long.Status-1;i<3;i++){
            OrderSelect(Orders.Long[i],SELECT_BY_TICKET);
            if(OrderStopLoss()!=OrderOpenPrice()){
               if(order==2){
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,CLR_NONE);
               }else{
                  OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble((OrderOpenPrice()+OrderClosePrice())/2,Digits),OrderTakeProfit(),0,CLR_NONE);   
               }
            }
         } 
      break;        
      }
      case OP_SELL:{
         for(i=Orders.Short.Status-1;i<3;i++){
            OrderSelect(Orders.Short[i],SELECT_BY_TICKET);
            if(OrderStopLoss()!=OrderOpenPrice()){
               if(order==2){
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,CLR_NONE);
               }else{
                  OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble((OrderOpenPrice()+OrderClosePrice())/2,Digits),OrderTakeProfit(),0,CLR_NONE);
               }
            }
         }
      break; 
      }
   }
}

int Trading.OpenTrade(int type,int magic,double sl,double tp){
   double op=0;
   switch(type){
      case OP_BUY:{
         op=Ask;
         break;
      }
      case OP_SELL:{
         op=Bid;  
         break;
      }
   }
   int retval=OrderSend(Symbol(),type,0.1,op,0,NormalizeDouble(sl,Digits),NormalizeDouble(tp,Digits),"",magic,0,CLR_NONE);
   return(retval);
}


void Trading.OpenSession(int type){
   int i=0;
   switch(type){
      case OP_BUY:{
         if(Orders.Long.Status==0){
            for(i=0;i<3;i++){
               Orders.Long[i]=Trading.OpenTrade(OP_BUY,101,Stop,0);
            }
            Orders.Long.Status=1;   
         }
      break;
      }
      case OP_SELL:{
         if(Orders.Short.Status==0){
            for(i=0;i<3;i++){
               Orders.Short[i]=Trading.OpenTrade(OP_SELL,101,Stop,0);
            }
            Orders.Short.Status=1;   
         }      
      break;
      }
   }   
}

void Trading.CloseOrder(int type){
   switch(type){
      case OP_BUY:{
         if(Orders.Long.Status!=0){
            OrderSelect(Orders.Long[Orders.Long.Status-1],SELECT_BY_TICKET);
            if(OrderProfit()>0){
               OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,CLR_NONE);
               Orders.Long[Orders.Long.Status-1]=-1;
               Orders.Long.Status++;
               if(Orders.Long.Status==4){
                  Orders.Long.Status=0;
               }else{
                  if(Orders.Long.Status>1){
                     Trading.SetToBreakeven(OP_BUY,Orders.Long.Status);
                  }
               }
            }
         }
      break;
      }
      case OP_SELL:{
         if(Orders.Short.Status!=0){
            OrderSelect(Orders.Short[Orders.Short.Status-1],SELECT_BY_TICKET);
            if(OrderProfit()>0){
               OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,CLR_NONE);
               Orders.Short[Orders.Short.Status-1]=-1;
               Orders.Short.Status++;
               if(Orders.Short.Status==4){
                  Orders.Short.Status=0;
               }else{
                  if(Orders.Short.Status>1){
                     Trading.SetToBreakeven(OP_SELL,Orders.Short.Status);
                  }
               }
            }
         }      
      break;
      }
   }     
}


int getSignal(){

   double trend[2];
   trend[0]=iCustom(Symbol(),Period(),"Gaussian_FilterOfGaussianFilter",0,3,250,1,0,0,0,0,0,0,0,1);
   trend[1]=iCustom(Symbol(),Period(),"Gaussian_FilterOfGaussianFilter",0,3,250,1,0,0,0,0,0,0,3,1);
   double pp=(iClose(Symbol(),PERIOD_D1,1)+iHigh(Symbol(),PERIOD_D1,1)+iLow(Symbol(),PERIOD_D1,1))/3;
   Stop=trend[0];
   if(trend[0]<trend[1]){
      Comment("UP  "+Orders.Long.Status);
   }else{
      Comment("DOWN "+Orders.Short.Status);
   }
      if( iCustom(Symbol(),Period(),"AdvancedCCi-Entrys",1,0)!=0){
         if(trend[0]>trend[1]){
            if(Ask<trend[0]){
               Stop=MathMax(trend[0],Ask+(SL*Point));
            }else{
               Stop=MathMax(High[iHighest(Symbol(),Period(),MODE_HIGH,5,1)],Bid+(SL*Point));
            }
            return(-2);
         }else{
            return(-1);
         }
      }
      if(iCustom(Symbol(),Period(),"AdvancedCCi-Entrys",0,0)!=0){
         if(trend[0]<trend[1]){
            if(Ask>trend[0]){
               Stop=MathMin(trend[0],Bid-(SL*Point));
            }else{
               Stop=MathMin(Low[iLowest(Symbol(),Period(),MODE_LOW,5,1)],Bid-(SL*Point));
            }
            return(2);
         }else{
            return(1);
         }
      }   
}

void CheckOrders(){
      int Long.tmp=0;
      int Short.tmp=0;
      int i=0;
      if(Orders.Long.Status>0){
      for(i=Orders.Long.Status-1;i<3;i++){
         OrderSelect(Orders.Long[i],SELECT_BY_TICKET);
         if(OrderCloseTime()==0){
            Long.tmp++;
         }
      }
      Orders.Long.Status=4-Long.tmp;
      if(Orders.Long.Status==4){
         Orders.Long.Status=0;
      }
      }
      if(Orders.Short.Status>0){
      for(i=Orders.Short.Status-1;i<3;i++){
         OrderSelect(Orders.Short[i],SELECT_BY_TICKET);
         if(OrderCloseTime()==0){
            Short.tmp++;
         }
      }
      Orders.Short.Status=4-Short.tmp;
      if(Orders.Short.Status==4){
         Orders.Short.Status=0;
      }
      }
      
}



int start()
{
   static datetime NewCandle=0;
   if(Time[0]!=NewCandle){
      NewCandle=Time[0];

      CheckOrders();
      int signal=getSignal();
      if(signal<0){
         if(signal==-1){
            Trading.CloseOrder(OP_BUY);
         }
         if(signal==-2){
            Trading.CloseOrder(OP_BUY);
            if(Orders.Short.Status==0){
               Trading.OpenSession(OP_SELL);
               
            }
         }
      }
      if(signal>0){
         if(signal==1){
            Trading.CloseOrder(OP_SELL);
         }
         if(signal==2){
            Trading.CloseOrder(OP_SELL);
            if(Orders.Long.Status==0){
               Trading.OpenSession(OP_BUY);
            }
         }
      }
   }
   return(0);
}

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---