No orders are being closed  [SOLVED]

Questions about MultiCharts and user contributed studies.
User avatar
hb7of9
Posts: 18
Joined: 23 Apr 2014
Has thanked: 9 times
Been thanked: 3 times

No orders are being closed  [SOLVED]

Postby hb7of9 » 15 Sep 2023

Hey Guys :D

I hope someone can shed a bit of light on this.

Issue:
I can see the code entering orders, but no trades are being closed.

Code: Select all

Variables: int TradeCount(0), string TradeString ("1"); TradeCount += 1; TradeString = NumToStr(TradeCount,0); Buy( TradeString ) 1 share next bar at market; Sell("Profit") from entry ( TradeString ) 1 share next bar at close * 1.3 limit ; Sell("Stop") from entry ( TradeString ) 1 share next bar at close *.7 stop ;


The stop loss line/profit line is where orders should be getting executed.
The idea is to enter each bar and exit with a 30% stop or 30% profit.
Entry trade number, for example, Entry #001, should link/match with its closing stop from entry and should be Entry #001.

User avatar
rrams
Posts: 128
Joined: 10 Feb 2011
Location: USA
Has thanked: 7 times
Been thanked: 70 times
Contact:

Re: No orders are being closed

Postby rrams » 16 Sep 2023

hb7of9,

The two reasons no exits are being triggered is because:
1. The TradeString is updated every bar and so the previous entry order name cannot be referenced.
2. The sell limit and stop orders use the current close times .7 or 1.3. This is not 30% profit or loss from the entry price. The close changes every bar.

To reference previous entry names so you can assign a specific sell order in a multi entry open position, you must iterate through the current entries.

Code: Select all

vars: TradeCount(0); TradeCount+=1; if CurrentEntries<4 then Buy(TEXT("LE", TradeCount:0:0)) 1 share next bar market; value1=CurrentEntries-1; while value1>-1 begin // orders cannot be inside a loop. value2=OpenEntryPrice(value1); // print to the editor output every position entry currently open. print("Bought ", PosTradeEntryName(0, value1), " at ", value2:0:2); value1-=1; end; print("");
Please be aware that the SetStopLoss and SetProfitTarget operate on a position basis and not on an entry basis. Also you can't place orders inside a loop. So this makes the PnL handling of individual entries quite difficult.

User avatar
hb7of9
Posts: 18
Joined: 23 Apr 2014
Has thanked: 9 times
Been thanked: 3 times

Re: No orders are being closed

Postby hb7of9 » 17 Sep 2023

Thanks rrams -- much appreciated!
(not a 100% clear, yet, but you've pointed me in the right direction to work from :) )


Return to “MultiCharts”