I am trying to code a simple supertrend based system with Martingale positioning sizing technique. My code has 2 scale-ins. For initial entry I am entering with 'x' quantity, when a certainn condition happens after initial condition, I want to scale in with '2x' quantity so the total quantity would become 'x' + '2x' i.e. '3x'. Again after that, if a certain condition happens I want to scale-in one more time with '3x' quantity, so now the total quantity would be '6x'.
I am maintaining a variable "qty_m" as a quantity multiplier, to keep track of the multiple by which the current quantity has changed as compared to initial quantity. And then I am using this multiplier to dynamically change quantity in SetPositionSize() statement such that SetPositionSize( qty * qty_m, iif( Buy == sigScaleIn or Short == sigScaleIn, spsShares, spsNoChange)), where qty is the initial quantity.
I have also tried using SetPositionSize( 200, IIf( Buy == sigScaleIn, spsPercentOfPosition,
spsNoChange ) )
But none of the above mentioned method worked. For every entry and scale-in, it took the same quantity.Position size did not change at all. Please help me. I want the code to be backtest able
for( i = 1; i < BarCount; i++ )
{
if (i > 0)
{
BuyPrice[i] = BuyPrice[i-1];
ShortPrice[i] = ShortPrice[i-1];
target_buy[i] = target_buy[i-1];
Average_price_long[i] = Average_price_long[i-1];
target_short[i] = target_short[i-1];
Average_price_short[i] = Average_price_short[i-1];
Buy_entry_cost[i] = Buy_entry_cost[i-1];
mtm_long[i] = mtm_long[i-1];
Short_entry_cost[i] = Short_entry_cost[i-1];
mtm_short[i] = mtm_short[i-1];
}
if(long_trade[i] AND Buy_on == 0 AND Short_on == 0)
{
Buy[i] = True;
BuyPrice[i] = Max(TrendDown[i-1], O[i]);
//priceatbuy = BuyPrice[i];
Buy_on = BuyPrice[i];
Buy_entry_cost[i] = qty * Buy_on;
Average_price_long[i] = BuyPrice[i];
target_buy[i] = Average_price_long[i]+target;
Second_buy = final_buy = False;
}
if (shrt_trade[i] AND Buy_on == 0 AND Short_on == 0)
{
Short[i] = True;
ShortPrice[i] = Min(TrendUp[i-1], O[i]);
Short_on = ShortPrice[i];
Short_entry_cost[i] = qty * Short_on;
Average_price_short[i] = ShortPrice[i];
target_short[i] = Average_price_short[i] - target;
Second_short = final_short = False;
}
if (Buy_on > 0)
{
mtm_long[i] = L[i] * tq - Buy_entry_cost[i];
//printf("\nmtm_long = %.2f", mtm_long[i]);
if ( L[i] <= (Buy_on - scale_in_1) AND NOT Second_buy AND NOT final_buy AND time[i] < endtime)
{
Buyscale1[i] = true;
first_scale_price = Min(O[i],(Buy_on - scale_in_1));
BuyPrice[i] = first_scale_price;
// initially tq was 1000 now after we add 2 more lots tq would be 3000 i.e. tq*3
qty_m = 2; // quantity multiplier
tq = qty * 3;
Average_price_long[i] = (Buy_on + first_scale_price * 2) * qty / tq;
Buy_entry_cost[i] = tq * Average_price_long[i];
Second_buy = True;
first_scale_in[i] = True;
target_buy[i] = Average_price_long[i] + target;
//target_buy[i] = BuyPrice[i]+20;
}
if (L[i] <= (Buy_on - scale_in_2) AND Second_buy AND NOT final_buy AND time[i] < endtime)
{
qty_m = 3;
tq = tq * 2;
Second_scale_price = Min(O[i],(Buy_on - scale_in_2));
Average_price_long[i] = (Average_price_long[i] + Second_scale_price) * qty * 3 / tq;
Buy_entry_cost[i] = Average_price_long[i] * tq;
target_buy[i] = Average_price_long[i]+target;
final_buy = True;
Buyscale2[i] = true;
BuyPrice[i] = Second_scale_price;
}
if (mtm_long[i] <= sl_mtm AND time[i] < endtime)
{
Sell[i] = True;
SellPrice[i] = C[i];
Buy_on = 0;
Short_on = 0;
tq = qty; //resetting the quantity
mtm_long[i] = Null;
Buy_entry_cost[i] = Null;
Second_buy = final_buy = False;
Second_short = final_short = false;
qty_m = 1;
}
if(H[i] >= target_buy[i] AND time[i] < endtime)
{
Sell[i] = True;
SellPrice[i] = Max(target_buy[i],O[i]);
Buy_on = 0;
Short_on = 0;
tq = qty; //resetting the quantity
mtm_long[i] = Null;
Buy_entry_cost[i] = Null;
Second_buy = final_buy = False;
Second_short = final_short = false;
qty_m = 1;
}
if (time[i] >= endtime)
{
Sell[i] = True;
SellPrice[i] = C[i];
Buy_on = 0;
Short_on = 0;
tq = qty; //resetting the quantity
mtm_long[i] = Null;
Buy_entry_cost[i] = Null;
Second_buy = final_buy = False;
Second_short = final_short = false;
}
if ()
{
// reverse signal handler
}
}
if ( Short_on > 0)
{
mtm_short[i] = Short_entry_cost[i] - H[i] * tq;
if (H[i] >= (Short_on + scale_in_1) AND NOT Second_short AND NOT final_short AND time[i] < endtime)
{
Shortscale1[i] = true;
first_scale_price = Max(O[i], (Short_on + scale_in_1));
ShortPrice[i] = first_scale_price;
qty_m = 2;
tq = qty * 3;
Average_price_short[i] = (Short_on + first_scale_price * 2) * qty / tq;
Short_entry_cost[i] = tq * Average_price_short[i];
Second_short = True;
first_scale_in[i] = True;
target_short[i] = Average_price_short[i] - target;
}
if ( H[i] >= (Short_on + scale_in_2) AND Second_short AND NOT final_short AND time[i] < endtime)
{
qty_m = 3;
tq = tq * 2;
Second_scale_price = Max(O[i], (Short_on + scale_in_2));
Average_price_short[i] = (Average_price_short[i] + Second_scale_price) * qty *3 / tq;
Short_entry_cost[i] = Average_price_short[i] * tq;
target_short[i] = Average_price_short[i] - target;
final_short = True;
Shortscale2[i] = true;
ShortPrice[i] = Second_scale_price;
}
if (mtm_short[i] <= sl_mtm)
{
Cover[i] = True;
CoverPrice[i] = C[i];
Short_on = 0;
Buy_on = 0;
tq = qty;
mtm_short[i] = Null;
Short_entry_cost[i] = Null;
Second_short = final_short = False;
Second_buy = final_buy = False;
qty_m = 1;
printf("entered mtm\n");
}
if(L[i] <= target_short[i] AND time[i] < endtime)
{
Cover[i] = True;
CoverPrice[i] = Min(target_short[i], O[i]);
Short_on = 0;
Buy_on = 0;
Second_short = final_short = false;
Second_buy = final_buy = False;
mtm_short[i] = Null;
Short_entry_cost[i] = Null;
tq = qty;
qty_m = 1;
printf("entered target\n");
}
if (time[i] >= endtime)
{
Cover[i] = True;
CoverPrice[i] = C[i];
Short_on = 0;
Buy_on = 0;
tq = qty;
mtm_short[i] = Null;
Short_entry_cost[i] = Null;
Second_short = final_short = False;
Second_buy = final_buy = False;
qty_m = 1;
}
}
}
//SetPositionSize( qty * qty_m, IIf(Buy == sigScaleIn OR Short == sigScaleOut, spsShares, spsNoChange));
//SetPositionSize(IIf(Buy == sigScaleIn OR Short == sigScaleIn, qty * qty_m, qty), spsShares);
SetPositionSize(200 ,IIf(Buy == sigScaleIn OR Short == sigScaleOut, spsPercentOfPosition, spsNoChange));