modular trading scrip

 

Hi everyone,


i'm working on a script that count the trades.

Firstly, if there is no trades on the required direction, open a trade.

My problem comes at the second trade. The condition for the next trade is being at profit and calculating the total profit.

The next trade's risk based on the total projected profit from the total trades on this current symbol and comment and for some reason the code doesn't execute the next trade.

Any idea?


Here's the code:

void OnStart()
  {
   //LOOP is required in order to check whether is open trades in long or short and return the number of trade counter
   int trade_counter_long  = 0;
   int trade_counter_short = 0;
   
   int order_total = OrdersTotal();
   for(int i=order_total ; i>=0 ; i--)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {
         if(OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderComment() == trade_comment )
         {
            trade_counter_long += 1;
         }
         if(OrderSymbol() == sym && OrderType() == OP_SELL && OrderComment() == trade_comment )
         {
            trade_counter_short += 1;
         }
      }
   }

//1. if there is no trade, open first trade.
   if(order_type == 0)
     {
      Alert("No direction has been chosen");
     }
   if(trade_counter_long == 0 && order_type == 1)
     {
      double stop_loss = Low[iLowest(NULL,0,MODE_LOW,highest_lowest,0)];
      buy_order(risk_pct,Ask,5,stop_loss,0,trade_comment,123);
      trade_counter_long += 1;
     }
   if(trade_counter_short == 0 && order_type == 2)
     {
      double stop_loss = High[iHighest(NULL,0,MODE_HIGH,highest_lowest,0)];
      sell_order(risk_pct,Bid,5,stop_loss,0,trade_comment,123);
      trade_counter_short += 1;
     }
//2. if there is a trade, check whether it is long or short

//3. if another trade is required in a direction with a trade, check whether the trade is profitable
//+--------------------------------------------------------------------------------------------------------------------------+
//|  Has to create function that calculated the projected profit based on SL,Open price and lot size * currency exchange rate|                                                               
//+--------------------------------------------------------------------------------------------------------------------------+

   double projected_profit_buy   = 0;
   double projected_profit_sell  = 0;
   for(int i=order_total;i>=0;i--)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {
         if(OrderSymbol() == Symbol() && OrderType() == OP_BUY)
         {
         double stop_loss = Low[iLowest(NULL,0,MODE_LOW,highest_lowest,0)];
         double points = (Bid - OrderOpenPrice());
         double curr_exc_rate = OrderProfit() / points / OrderLots();
         projected_profit_buy += (stop_loss - OrderOpenPrice())*OrderLots()*curr_exc_rate; 
            if(projected_profit_buy > 0)
              {
               order_modify(stop_loss,0);
               Alert("order number: " , OrderTicket(), " has been modified");
              }
         }
      }
      if(trade_counter_long > 0 && projected_profit_buy > 0 && order_type == 1) //order type required (and probably being part of the loop in level 3)
       {
         double stop_loss = Low[iLowest(NULL,0,MODE_LOW,highest_lowest,0)];
         double new_risk_pct = (projected_profit_buy*0.3)/accB;
         double lot = lotSize(new_risk_pct,Ask,stop_loss);
         buy_order(lot,Ask,5,stop_loss,0,trade_comment,123);
       }
   }
//4. create a "lock in" for the first trade so whether the next trade is going to fail, the total trades in the same direction and symbol are going to get %gains of previous trades
//5. open next trade and manage the previous stoploss and take profit

   
   



//6. trade management
   
  }
Reason: