Hi all,
I have been working to simulate a DCA 'bot' (popular under crypto traders). The basic idea is that the bot:
= enters at any moment there is no position (so with the first bar), with small position of e.g. 10 USD
= scales-in every time the price drops x% with e.g. 15 USD, this way you average down
= exits at the time the full position hits a tp%
I decided to use Looping as I want to experiment with different scaling principles. The current version seems to work ok (scale-in correct), except for the first trade. I would expect that his happens at the very first bar in the simulation, but it seems that this signal is skipped. Also the first position seems to miss the tp% and does not show scaling. After this first trade the code seems to work fine.
I stared at the code, tried various variations, using explorer and debugger, but can't seem to debug this. Hoping that you may spot my error?
I run the following code on 1min bars of ADAUSDT pair:
SystemName="DCA002";
bo = Optimize("bo", 10, 10, 25, 5);
so = Optimize("so", 15, 10, 30, 5);
tp = Optimize("tp", 1.2, 0.8,1.8,0.2); //%
so_dist = Optimize("so_dist", 2, 0.5, 2.5, 0.5) ; //%
max_scale_ins = 25;
SetOption("InitialEquity",3000);
SetOption("AccountMargin",100);
SetPositionSize(BO,spsValue);
SetOption("MaxOpenPositions", 2 );
SetOption("MinShares", 0 );
SetOption("CommissionMode",1);
SetOption("CommissionAmount",0.1);
SetTradeDelays(0, 0, 0, 0);
//SetOption("AllowSameBarExit", True);
//SetOption("HoldMinBars", 1);
//SetOption("ActivateStopsImmediately", False);
Buy=BuyPrice=0;
Cover=Short=0;
Sell=SellPrice=0;
_tp = 1+(tp/100);
_so_dist = (so_dist/100);
entry_condition = 1;
in_trade_price_weigth_sum = in_trade_size_sum = 0;
original_entry_price = avg_price = tp_price = so_price = 0;
position_count = 0;
for( i = 1; i < BarCount-1; i++ )
{
if (position_count == 0 AND entry_condition[i] == 1)
{
Buy[i] = 1; BuyPrice[i] = Open[i]; PositionSize[i] = bo;
position_count += 1;
in_trade_size_sum += bo;
in_trade_price_weigth_sum += (bo*BuyPrice[i]);
original_entry_price = avg_price = in_trade_price_weigth_sum / in_trade_size_sum;
tp_price = avg_price * _tp;
so_price = original_entry_price * (1 - (position_count * _so_dist)) ;
}
if (position_count > 0)
if (position_count < max_scale_ins AND Low[i] < so_price)
{
Buy[i] = sigScaleIn; BuyPrice[i] = Min(Open[i],so_price); PositionSize[i] = so;
position_count += 1;
in_trade_size_sum += so;
in_trade_price_weigth_sum += (so*BuyPrice[i]);
avg_price = in_trade_price_weigth_sum / in_trade_size_sum;
tp_price = avg_price * _tp;
so_price = original_entry_price * (1 - (position_count * _so_dist));
}
if (position_count > 0 AND High[i] >= tp_price)
{
Sell[i] = 1; SellPrice[i] = Max(Open[i],tp_price);
position_count = 0;
original_entry_price = avg_price = tp_price = so_price = 0;
in_trade_size_sum = in_trade_price_weigth_sum = 0;
}
}
Filter = True;
AddColumn(entry_condition, "EC", 1.0);
AddColumn(Buy, "Buy", 1.0);
AddColumn(Sell, "Sell", 1.0);
Here is the first 'strange looking' trade that shows a 7.55% profit (with 1.2% tp):
Explorer window does not show any buy signals at the start of bars (whilst entry logic is true):
Whilst in debugger we can see that a buy signal is created:
Examining the entry time of the first trade in explorer I do not see a buy=1 but a ScaleIn signal, this suggests the issue may be in some settings?