# 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 version   "1.00"

//
enum typ
{
ty3=2 //only sell
};

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 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)

return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{

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(dn[Shift]>0 && dn[Shift]!=EMPTY_VALUE) sell=true;

static datetime timestamp;
datetime time = iTime(_Symbol,PERIOD_CURRENT,0);
if(timestamp != time)
{
timestamp = time;

{
//Initial order entry
{
}
//Add a lot if target price is not reached
{
if(TakeProfit < POSITION_PRICE_CURRENT)
}
//Set limit orders above current price is lot size is greater than 1
{
if((OrdersTotal () > 1) && (PositionsTotal() > 1))