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;
inputstring ci0="=================== Asctrend settings ===================";
inputint RISK=4; //Riskinputint Shift=1; //Shift (0=current candle, 1=previous candle)inputint LotSize = 1; //Number of Contractsinputint ExtraLotSize = 1; //2nd Lot Contractsinputint StopLoss = 100; //Stop Lossinputint TakeProfit = 100; //Take Profitinput typ TradesType=ty1; //Type of trading operationsinputbool UseCloseByOppositeSignal=true; //Use close by opposite signalint handleASC=0;
int SellStopLimit=5;
//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+intOnInit()
{
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 |//+------------------------------------------------------------------+voidOnTick()
{
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;staticdatetime 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);//}
}
}
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
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.