# Calculate stoploss

Hello everyone,
I am having a hard time coding an API trading strategy with a stock company. I have coded buy and sell orders and additional buying and selling points according to the trend.
For example: When the trend reverses, there will be a buy/sell order with 5 contracts. After that, if the conditions are met, I will add buy/sell orders with 1 contract/2 contracts. However, for hedging, I want to create a stoploss order for the extra buy/sell part (not included). includes the first buy/sell orders of the trend)
I determined to be able to calculate:
1. the number of orders/buys/sells according to the up/down trend (calculated from the first buy/sell signal)
2.Average buying/selling price = (Purchase/selling price + buying/selling fee) x number of contracts/Number of contracts
3. Stoploss price = if closing price </> average price

Thanks everyone

//----------------------Dat them 2 HD------------------//

DK_Sell_2HD=BarsBuy>BarsSell AND ((Tem5<=-0.6 AND V<Vp1) OR (Tem5>-0.6 AND Tem5<=-0.3 AND DK_Vol)) AND NOT Notrade AND Ref(Tem5,-1)>-0.6 AND NOT Sell;
SellPrice_2HD = ValueWhen(DK_Sell_2HD,C,1);

//----------------------Dat them 3 HD/5HD------------------//
SellPrice_5HD = ValueWhen(DK_Sell_5HD,C,1);

PlotShapes(IIf(DK_Sell_2HD, shapeDownArrow, shapeNone),colorYellow, 0,High,-15);
PlotShapes(IIf(DK_Sell_5HD, shapeDownArrow, shapeNone),colorTurquoise, 0,High,-15);

AlertIF( (DK_Sell_2HD OR DK_Sell_5HD), "SOUND C:\\Windows\\Media\\Ring08.wav", "Audio alert", 2, 15, 1 );

{
ih =
InternetClose(ih);
}
if (Sell[BarCount-2]==True)
{
ih =
InternetClose(ih);
}

{
ih =
InternetClose(ih);
}
if (DK_Sell_2HD[BarCount-2]==True)
{
ih =
InternetClose(ih);
}

{
ih =
InternetClose(ih);
}
if (DK_Sell_5HD[BarCount-2]==True)
{
ih =