Results 1 to 3 of 3

Thread: Trade Functions

  1. #1
    Senior Member
    Join Date
    May 2014
    Posts
    145

    Default Trade Functions

    Hello,

    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.

    Code:
    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
    else
        if positionTotal() > 0 then
            orderId = CSTR(PositionGetTicket(CINT(1)))
             PositionSelect(Clng(orderId))
            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

  2. #2
    Administrator Hassan-HS's Avatar
    Join Date
    Jun 2013
    Posts
    795

    Default

    Good Morning,


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

    Code:
    Public Sub OnPositionTrade (actionType ,ticketID)

  3. #3
    Senior Member
    Join Date
    May 2014
    Posts
    145

    Default

    Thanks Hassan

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. MQL to VTL conversion -- Similar Functions needed
    By mayankj990@gmail.com in forum VertexFX API/VTL Forum
    Replies: 8
    Last Post: 06-18-2014, 04:31 PM
  2. VTL Functions
    By AmitSharma in forum VertexFX API/VTL Forum
    Replies: 6
    Last Post: 03-11-2014, 02:48 PM
  3. Similar VTL functions needed
    By soori1234 in forum VertexFX API/VTL Forum
    Replies: 4
    Last Post: 01-28-2014, 06:19 PM
  4. Please help to return value from functions in client script
    By nhasan in forum VertexFX API/VTL Forum
    Replies: 8
    Last Post: 01-09-2014, 09:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •