Financial Instrument Trading Project: High Frequency of a Technical Price Action or Trending Strategy

bool BullishTrend;
bool FalseSignal;
double StopLoss;
double EntryPoint;
double TakeProfit;
int total,ticket;
input double risk = 2;
double Lots;
double TrailingStopLoss;
//First TakeProfit is 1xR
//When hit → Take out 50% and set stoploss to break even and TP to 2xR

int OnInit()
{
return(INIT_SUCCEEDED);
}

void OnTick(){
double StopLevel = MarketInfo(NULL,MODE_STOPLEVEL);
//anchor chart EMAs
double EMA_21_5min =iMA(NULL,PERIOD_M5,21,0,MODE_EMA,PRICE_CLOSE,0);
double EMA_13_5min =iMA(NULL,PERIOD_M5,13,0,MODE_EMA,PRICE_CLOSE,0);
double EMA_8_5min =iMA(NULL,PERIOD_M5,8,0,MODE_EMA,PRICE_CLOSE,0);

//Trend setter
double EMA_21_60min =iMA(NULL,PERIOD_H1,21,0,MODE_EMA,PRICE_CLOSE,0);
double EMA_8_60min =iMA(NULL,PERIOD_H1,8,0,MODE_EMA,PRICE_CLOSE,0);

double HighestCandlePrevious = High[iHighest(NULL,PERIOD_M5,MODE_HIGH,5,1)];
double LowestCandlePrevious = Low[iLowest(NULL,PERIOD_M5,MODE_LOW,5,1)];

double PreviousEMA_21Period_60min[6];
PreviousEMA_21Period_60min[0] = iMA(NULL,PERIOD_H1,21,0,MODE_EMA,PRICE_CLOSE,0);
PreviousEMA_21Period_60min[1] = iMA(NULL,PERIOD_H1,21,0,MODE_EMA,PRICE_CLOSE,1);
PreviousEMA_21Period_60min[2] = iMA(NULL,PERIOD_H1,21,0,MODE_EMA,PRICE_CLOSE,2);
PreviousEMA_21Period_60min[3] = iMA(NULL,PERIOD_H1,21,0,MODE_EMA,PRICE_CLOSE,3);
PreviousEMA_21Period_60min[4] = iMA(NULL,PERIOD_H1,21,0,MODE_EMA,PRICE_CLOSE,4);
PreviousEMA_21Period_60min[5] = iMA(NULL,PERIOD_H1,21,0,MODE_EMA,PRICE_CLOSE,5);

double PreviousEMA_8Period_60min[6];
PreviousEMA_8Period_60min[0] = iMA(NULL,PERIOD_H1,8,0,MODE_EMA,PRICE_CLOSE,0);
PreviousEMA_8Period_60min[1] = iMA(NULL,PERIOD_H1,8,0,MODE_EMA,PRICE_CLOSE,1);
PreviousEMA_8Period_60min[2] = iMA(NULL,PERIOD_H1,8,0,MODE_EMA,PRICE_CLOSE,2);
PreviousEMA_8Period_60min[3] = iMA(NULL,PERIOD_H1,8,0,MODE_EMA,PRICE_CLOSE,3);
PreviousEMA_8Period_60min[4] = iMA(NULL,PERIOD_H1,8,0,MODE_EMA,PRICE_CLOSE,4);
PreviousEMA_8Period_60min[5] = iMA(NULL,PERIOD_H1,8,0,MODE_EMA,PRICE_CLOSE,5);

double PreviousClosingPrices_60min[6];
PreviousClosingPrices_60min[0] = iClose(NULL,PERIOD_H1,0);
PreviousClosingPrices_60min[1] = iClose(NULL,PERIOD_H1,1);
PreviousClosingPrices_60min[2] = iClose(NULL,PERIOD_H1,2);
PreviousClosingPrices_60min[3] = iClose(NULL,PERIOD_H1,3);
PreviousClosingPrices_60min[4] = iClose(NULL,PERIOD_H1,4);
PreviousClosingPrices_60min[5] = iClose(NULL,PERIOD_H1,5);

double PreviousEMA_8Period_5min[6];
PreviousEMA_8Period_5min[0] = iMA(NULL,PERIOD_M5,8,0,MODE_EMA,PRICE_CLOSE,0);
PreviousEMA_8Period_5min[1] = iMA(NULL,PERIOD_M5,8,0,MODE_EMA,PRICE_CLOSE,1);
PreviousEMA_8Period_5min[2] = iMA(NULL,PERIOD_M5,8,0,MODE_EMA,PRICE_CLOSE,2);
PreviousEMA_8Period_5min[3] = iMA(NULL,PERIOD_M5,8,0,MODE_EMA,PRICE_CLOSE,3);
PreviousEMA_8Period_5min[4] = iMA(NULL,PERIOD_M5,8,0,MODE_EMA,PRICE_CLOSE,4);
PreviousEMA_8Period_5min[5] = iMA(NULL,PERIOD_M5,8,0,MODE_EMA,PRICE_CLOSE,5);

double PreviousEMA_13Period_5min[6];
PreviousEMA_13Period_5min[0] = iMA(NULL,PERIOD_M5,13,0,MODE_EMA,PRICE_CLOSE,0);
PreviousEMA_13Period_5min[1] = iMA(NULL,PERIOD_M5,13,0,MODE_EMA,PRICE_CLOSE,1);
PreviousEMA_13Period_5min[2] = iMA(NULL,PERIOD_M5,13,0,MODE_EMA,PRICE_CLOSE,2);
PreviousEMA_13Period_5min[3] = iMA(NULL,PERIOD_M5,13,0,MODE_EMA,PRICE_CLOSE,3);
PreviousEMA_13Period_5min[4] = iMA(NULL,PERIOD_M5,13,0,MODE_EMA,PRICE_CLOSE,4);
PreviousEMA_13Period_5min[5] = iMA(NULL,PERIOD_M5,13,0,MODE_EMA,PRICE_CLOSE,5);

double PreviousEMA_21Period_5min[6];
PreviousEMA_21Period_5min[0] = iMA(NULL,PERIOD_M5,21,0,MODE_EMA,PRICE_CLOSE,0);
PreviousEMA_21Period_5min[1] = iMA(NULL,PERIOD_M5,21,0,MODE_EMA,PRICE_CLOSE,1);
PreviousEMA_21Period_5min[2] = iMA(NULL,PERIOD_M5,21,0,MODE_EMA,PRICE_CLOSE,2);
PreviousEMA_21Period_5min[3] = iMA(NULL,PERIOD_M5,21,0,MODE_EMA,PRICE_CLOSE,3);
PreviousEMA_21Period_5min[4] = iMA(NULL,PERIOD_M5,21,0,MODE_EMA,PRICE_CLOSE,4);
PreviousEMA_21Period_5min[5] = iMA(NULL,PERIOD_M5,21,0,MODE_EMA,PRICE_CLOSE,5);

double PreviousClosingPrices_5min[6];
PreviousClosingPrices_5min[0] = iClose(NULL,PERIOD_M5,0);
PreviousClosingPrices_5min[1] = iClose(NULL,PERIOD_M5,1);
PreviousClosingPrices_5min[2] = iClose(NULL,PERIOD_M5,2);
PreviousClosingPrices_5min[3] = iClose(NULL,PERIOD_M5,3);
PreviousClosingPrices_5min[4] = iClose(NULL,PERIOD_M5,4);
PreviousClosingPrices_5min[5] = iClose(NULL,PERIOD_M5,5);

if (OrdersTotal() > 0){
for(int c=0;c<OrdersTotal();c++){
OrderSelect(c, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol()) total = 1;
else total = 0;
}
} else total = 0;
Print(total);
if (total < 1){
//get trend
for (int x = 0; x < 6; x++){
bool Bear = False, Bull = False;
if (PreviousEMA_8Period_60min > PreviousEMA_21Period_60min) Bull = True;
else if (PreviousEMA_8Period_60min < PreviousEMA_21Period_60min) Bear = True;

     if (Bull && !Bear) BullishTrend = True;
     else if (Bear && !Bull) BullishTrend = False;
     else return;
  }
  //Trade setup?
  for (int x = 0; x < 6; x++){
     if (BullishTrend){
        if (PreviousClosingPrices_60min[x] < PreviousEMA_21Period_60min[x]) return; //No Trade setup
     }
     else {
        if (PreviousClosingPrices_60min[x] > PreviousEMA_21Period_60min[x]) return; //No Trade setup
     }
  }
  
  for (int x = 0; x < 6; x++){
     if (BullishTrend){
        if (PreviousEMA_21Period_5min[x] >= PreviousEMA_13Period_5min[x] || PreviousEMA_13Period_5min[x] >= PreviousEMA_8Period_5min[x]) return; //No Trade setup
     }
     else {
        if (PreviousEMA_21Period_5min[x]<= PreviousEMA_13Period_5min[x] || PreviousEMA_13Period_5min[x] <= PreviousEMA_8Period_5min[x]) return; //No Trade setup
     }
  } 
  
  if (BullishTrend){
     //Looking for long position
     EntryPoint = HighestCandlePrevious + Point*30;
     StopLoss = iLow(NULL,PERIOD_M5,0) - Point*30;
     TakeProfit = EntryPoint + (EntryPoint - StopLoss);
     Lots = 1;
     if (iLow(NULL,PERIOD_M5,0) < PreviousEMA_8Period_5min[0]) {
        //potential trigger bar
        //checking so that bars before this hasn't gone under EMA
        for (int x = 1; x < 6; x++){
           if (PreviousClosingPrices_5min[x] > PreviousEMA_8Period_5min[x]){
              //Buy stop att highest previous candle + 3 pips
              ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,EntryPoint,3,0,0,"Long position",rand()%10000+1,TimeCurrent()+600,Green);
              
              if(ticket>0){
                 if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                    Print("BUY order opened : ",OrderOpenPrice());
                    bool res1 = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(StopLoss,Digits), NormalizeDouble(TakeProfit,Digits),0,Green);
                    if(!res1) 
                       Print("Error in OrderModify. Error code=",GetLastError()); 
                    else 
                       Print("Order modified successfully."); 
              }
              else
                 Print("Error opening BUY order : ",GetLastError());
              return;
           }
        } 
     }
  }
  else {
     //Looking for short position
     //Looking for long position
     EntryPoint = LowestCandlePrevious - Point*30;
     StopLoss = iHigh(NULL,PERIOD_M5,0) + Point*30;
     TakeProfit = EntryPoint - (StopLoss-EntryPoint);
     Lots = 1;
     if (iHigh(NULL,PERIOD_M5,0) > PreviousEMA_8Period_5min[0]) {
        //potential trigger bar
        //checking so that bars before this hasn't gone under EMA
        for (int x = 1; x < 6; x++){
           if (PreviousClosingPrices_5min[x] < PreviousEMA_8Period_5min[x]){
              //Buy stop att highest previous candle + 3 pips
              ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,EntryPoint,3,0,0,"Short position",rand()%10000+1,0,Red);
              if(ticket>0){
                 if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                    Print("SHORT order opened : ",OrderOpenPrice());
                    bool res5 = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(StopLoss,Digits),NormalizeDouble(TakeProfit,Digits),0,Green);
                    if(!res5) 
                       Print("Error in OrderModify. Error code=",GetLastError()); 
                    else 
                       Print("Order modified successfully."); 
              }
              else
                 Print("Error opening Short order : ",GetLastError());
              return;
           }
        } 
     }
  }

}
for(int cnt=0;cnt<total;cnt++){
if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
continue;
if (OrderType() == OP_BUYSTOP){
if (Bid < OrderStopLoss()){
if(!OrderDelete(OrderTicket(),clrAntiqueWhite)) Print(“Error deleting pending order!”);
}
}
else if (OrderType() == OP_SELLSTOP) {
if (Ask > OrderStopLoss()){
if (!OrderDelete(OrderTicket(),clrAntiqueWhite)) Print(“Error deleting pending order!”);
}
}
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if (OrderOpenPrice() == OrderStopLoss()) break;
//— long position is opened
if(OrderType()==OP_BUY)
{
if (OrderOpenPrice() < Bid){
TrailingStopLoss = iLow(NULL,PERIOD_M5,2);
if(Bid>TrailingStopLoss){
if(OrderStopLoss()<TrailingStopLoss)
{
if(!OrderModify(OrderTicket(),OrderOpenPrice(),TrailingStopLoss,OrderTakeProfit(),0,Green))
Print("OrderModify error ",GetLastError());
return;
}
}
}
}
else{
if (OrderOpenPrice() > Ask) {
TrailingStopLoss = iHigh(NULL,PERIOD_M5,2);
if(Ask<TrailingStopLoss){
if(OrderStopLoss()>TrailingStopLoss)
{
if(!OrderModify(OrderTicket(),OrderOpenPrice(),TrailingStopLoss,OrderTakeProfit(),0,Green))
Print("OrderModify error ",GetLastError());
return;
}
}
}
}
}
}
}