I understand the use cases for the different exit functions. However i believe neither one performs the way i want my algo to work.
Method1 : SetStopContract, SetProfitTarget(10)
I know this would set different profit target levels for different trades. E.g. Trade A (entry 90) -> Exit@100, Trade B(entry 95) -> Exit@105. This is good since it isolates each trade individually. However, I am unable to set multiple profit target levels because this function would close out the entire position. So when price reaches 100, the entirety of Trade A will be closed while Trade B is still running. I am trying to set multiple profit target levels. E.g. Trade A(entry 90) -> Exit1(50% of position)@ 100, Exit2(50% of position)@110. So setstopcontract doesnt work.
Method2 : Sell Stop and Sell Limit
I know to set different profit target levels i would have to use Sell Stop. Here is a code snippet.
Code: Select all
if condition1 ... condition2 ....
...
...
pt1 = close + 2 * Multiplier * atr;
pt2 = close + 5 * Multiplier * atr;
pt3 = close + 7 * Multiplier * atr;
Buy ("Buy") contract_size contracts next bar on market;
if marketposition <> 0 and close >= pt1 then begin
Sell("PT1") 0.5 * currentcontracts contracts next bar on market;//close;
end;
if marketposition <> 0 and close >= pt2 then begin
Sell("PT2") 0.5 * currentcontracts contracts next bar on market;//close;
end;
if marketposition <> 0 and close >= pt3 then begin
Sell("PT3") next bar on market;//close;
end;
However, this also becomes a problem since pt1, pt2, pt3 are updated whenever there is a new buy order. So the first open trade is following the latest open trade's profit target levels. Which is not right. So i thought i could solve this by storing the SL level of each trade in an array. Then i would loop through each open order to get its entrycontracts and entryprice and calculate each trades' PT levels then close them. However another issue occurs because it says i am unable to send orders in a loop.
Code: Select all
if condition1 ... condition2 ....
...
...
arr[OpenEntriesCount] = sl;
Buy ("Buy") contract_size contracts next bar on market;
if marketposition <> 0 then begin
for i = 0 to OpenEntriesCount - 1 begin
entry_price = OpenEntryPrice(i);
entry_contracts = OpenEntryContracts(i);
sl_level = array_getfloatvalue(arr, i);
pt1 = entry_price + 1 * (entry_price - sl_level);
pt2 = entry_price + 5 * (entry_price - sl_level);
pt3 = entry_price + 10 * (entry_price - sl_level);
if close >= pt1 then
Sell("PT1") 0.5 * entry_contracts contracts next bar on market;
if close >= pt2 then
Sell("PT2") 0.25 * entry_contracts contracts next bar on market;
if close >= pt3 then
Sell("PT3") 0.125 * entry_contracts contracts next bar on market;
end;
end;
Please could someone help me out, ive been trying to implement this for quite some time. Is there no functionality to set multiple profit target levels for each individual trade? Thank you in advance to any help.