Dollar Cost Averaging

 

Hi all, 

I'm trying to code a dollar cost averaging expert advisor. I have gotten to the point where it will "average" buy lower and move the average price down. However, what I would like to add is a means to remove the take profit if the lot size equals more than 1 contract and replace with limit orders above the current average price. I'm stumbling on that part and hoping someone could point me in a direction to help me. Thank you. Code attached. 


#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

//
enum typ
  {
   ty1=0, //buy and sell
   ty2=1, //only buy
   ty3=2 //only sell
  };

#include <Trade\Trade.mqh>

CTrade trade;

input string  ci0="=================== Asctrend settings ===================";

input int RISK=4;   //Risk
input int Shift=1; //Shift (0=current candle, 1=previous candle)

input int LotSize = 1;             //Number of Contracts
input int ExtraLotSize = 1;        //2nd Lot Contracts
input int StopLoss = 100;          //Stop Loss
input int TakeProfit = 100;        //Take Profit
input typ TradesType=ty1;          //Type of trading operations
input bool   UseCloseByOppositeSignal=true;       //Use close by opposite signal


int handleASC=0;
int SellStopLimit=5;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{

handleASC=iCustom(_Symbol,PERIOD_CURRENT,"asctrend",RISK);

if(handleASC==INVALID_HANDLE)
 
 {Alert("Error creating indicators: ",GetLastError(),"!!!"); return(INIT_FAILED);}


   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  
   bool buy=false,sell=false;

   double up[],dn[];

   ArraySetAsSeries(up,true);
   ArraySetAsSeries(dn,true);
   
   if(CopyBuffer(handleASC,1,0,Shift+2,up)<=0) return;
   if(CopyBuffer(handleASC,0,0,Shift+2,dn)<=0) return;

   if(up[Shift]>0 && up[Shift]!=EMPTY_VALUE) buy=true;
   //if(dn[Shift]>0 && dn[Shift]!=EMPTY_VALUE) sell=true;
   
   
   
   
   
 
   static datetime timestamp;
   datetime time = iTime(_Symbol,PERIOD_CURRENT,0);
   if(timestamp != time)
   {
      timestamp = time;     
   
 if(buy)
{
   double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      //Initial order entry
   {
      trade.Buy(LotSize,_Symbol,ask,NormalizeDouble(ask - StopLoss *_Point*10,_Digits),NormalizeDouble(ask + TakeProfit *_Point*10,_Digits),NULL);
   }
      //Add a lot if target price is not reached
   {
      if(TakeProfit < POSITION_PRICE_CURRENT) 
      trade.Buy(ExtraLotSize,_Symbol,ask); 
   }   
      //Set limit orders above current price is lot size is greater than 1
   {
      if((OrdersTotal () > 1) && (PositionsTotal() > 1))
      trade.SellLimit(ExtraLotSize,(ask+(10*_Point)),_Symbol,0);
   }
}
   
 
 //if(sell)
   //{
   //double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
   //trade.Sell(LotSize,_Symbol,bid,NormalizeDouble(bid + StopLoss*_Point*10,_Digits),NormalizeDouble(bid - TakeProfit*_Point*10,_Digits),NULL);
   //} 
   
   }
  }    
Reason: