--+------------------------------------------------------------------+ --| Copyright © 2016, Gehtsoft USA LLC | --| http://fxcodebase.com | --+------------------------------------------------------------------+ --| Support our efforts by donating | --| Paypal: https://goo.gl/9Rj74e | --| BitCoin : 15VCJTLaz12Amr7adHSBtL9v8XomURo9RF | --+------------------------------------------------------------------+ --| Developed by : Mario Jemic | --| mario.jemic@gmail.com | --+------------------------------------------------------------------+ function Init() strategy:name("Delete orders"); strategy:description("Deletes all orders for the instrument if there is no open positions"); strategy.parameters:addGroup("Price"); strategy.parameters:addString("Type", "Price type", "", "Bid"); strategy.parameters:addStringAlternative("Type", "Bid", "", "Bid"); strategy.parameters:addStringAlternative("Type", "Ask", "", "Ask"); strategy.parameters:addString("Period", "Timeframe", "", "m1"); strategy.parameters:setFlag("Period", core.FLAG_PERIODS); strategy.parameters:addGroup("Settings"); strategy.parameters:addString("Account", "Account", "Account to monitor open positions for.", ""); strategy.parameters:setFlag("Account", core.FLAG_ACCOUNT); end local Source = nil; local Offer, Account; function Prepare(onlyName) local name = profile:id() .. "(" .. instance.bid:instrument(); instance:name(name); Account = instance.parameters.Account; Offer = core.host:findTable("offers"):find("Instrument", instance.bid:instrument()).OfferID; if onlyName then return ; end ExtSetupSignal(name .. ":", true); Source = ExtSubscribe(1, nil, "t1", instance.parameters.Type == "Bid", "tick"); end function haveTrades() local enum = core.host:findTable("trades"):enumerator(); local row = enum:next(); while (not found) and (row ~= nil) do if row.AccountID == Account and row.OfferID == Offer then return true; end row = enum:next(); end end function CloseAllOrders() -- new code from victor local enum, row; enum = core.host:findTable("orders"):enumerator(); row = enum:next(); while (row ~= nil) do if row.AccountID == Account and row.OfferID == Offer and (row.Type == "LE" or row.Type == "SE") then -- we want to close the entry orders, not the stop or limit orders etc otherwise we will get errors as they close each other out. local valuemap = core.valuemap(); valuemap.Command = "DeleteOrder"; valuemap.OrderID = row.OrderID; success, msg = terminal:execute(301, valuemap); if not(success) then terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW], "Failed to delete orders for instance: " .. instanceName .. ": " .. msg, instance.bid:date(NOW)); end end row = enum:next(); end end function ExtUpdate(id, source, period) if not(checkReady("trades")) or not(checkReady("orders")) then return ; end if not(haveTrades()) then CloseAllOrders(); end end function ExtAsyncOperationFinished(id, success, message) end function checkReady(table) return core.host:execute("isTableFilled", table); end dofile(core.app_path() .. "\\strategies\\standard\\include\\helper.lua");