View Full Version : Trade Functions

06-05-2014, 06:51 AM

I have this simple code.

It open a buy position when a new bar is formed. Then on next bar it closes the previously opened position and opens a new sell position. This sequence repeats on every new bar.

Now the problem is when the script closes the open position and then open a new position, the close position works fine but it does not open a new position. But when there is no position to close before opening a new position, the code works.

So I think the issue is when code sends close position command and then continue to open new position, some how the NewMarketOrder command fails, may be because the previous CloseMarketOrder command was not finished. How do I handle this situation properly.

dim xxx

Public Sub main()

xxx = 1

End Sub

Public Sub OnCalculate(symbol, symbolPeriod, openVal, highVal, lowVal, closeVal)

if CINT(xxx) =1 then
NewMarketOrder TRADE_ACTION_BUY, CSTR(ChartSymbol(0)),CDBL(0.01)
xxx = 0
if positionTotal() > 0 then
orderId = CSTR(PositionGetTicket(CINT(1)))
PositionGetDouble POSITION_AMOUNT,lots
CloseMarketOrder clng(PositionGetTicket(cint(1))), CDbl(lots)
end if
NewMarketOrder TRADE_ACTION_BUY, CSTR(ChartSymbol(0)),CDBL(0.01)
xxx = 1
msgbox ErrorDescription(GetLastError())
end if
End Sub

06-05-2014, 07:12 AM
Good Morning,

Please wait the event of the first instruction(CloseMarketOrder) before doing another one , so in your case you have to wait .

Public Sub OnPositionTrade (actionType ,ticketID)

06-05-2014, 07:33 AM
Thanks Hassan