Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function SendLimitOrder(ClassCode,SecCode,Operation,Price,Volume,Account,ClientCode)
- if (ClassCode==nil or SecCode==nil or Operation==nil or Price==nil or Volume==nil or Account==nil) then
- return false,0,"lib.SendLimitOrder(): Can`t send order. Nil parameters."
- end
- local trans_id=math.random(2000000000)
- local transaction={
- ["TRANS_ID"]=tostring(trans_id),
- ["ACTION"]="NEW_ORDER",
- ["CLASSCODE"]=ClassCode,
- ["SECCODE"]=SecCode,
- ["OPERATION"]=Operation,
- ["QUANTITY"]=Volume,
- ["PRICE"]=tostring(Price),
- ["ACCOUNT"]=Account
- }
- if ClientCode==nil then
- transaction.client_code=Account
- else
- transaction.client_code=ClientCode
- end
- local res=sendTransaction(transaction)
- if res~="" then
- return false,trans_id, "lib.SendLimitOrder():"..res
- else
- return true,trans_id, "lib.SendLimitOrder(): Limit order sended sucesfully. Class="..ClassCode.." Sec="..SecCode.." Dir="..Operation.." Price="..Price.." Vol="..Volume.." Acc="..Account.." Trans_id="..trans_id
- end
- end
- function SendTPSLOrder(ClassCode,SecCode,Operation,Price,TPOffset,SLOffset,MaxOffset,DefSpread,Volume,Account,ClientCode)
- if (ClassCode==nil or SecCode==nil or Operation==nil or Price==nil or Volume==nil or Account==nil or TPOffset==nil or SLOffset==nil or MaxOffset==nil or DefSpread==nil) then
- return false,0,"lib.SendTPSLOrder(): Can`t send order. Nil parameters."
- end
- local StopLoss,TakeProfit=0,0
- local trans_id=math.random(2000000000)
- if (Operation=="B") then
- StopLoss = Price + SLOffset
- TakeProfit = Price - TPOffset
- end
- if (Operation=="S") then
- StopLoss = Price - SLOffset
- TakeProfit = Price + TPOffset
- end
- StopLoss,TakeProfit = RoundToStep(StopLoss,TakeProfit)
- local transaction={
- ["TRANS_ID"]=tostring(trans_id),
- ["ACTION"]="NEW_STOP_ORDER",
- ["STOP_ORDER_KIND"]="TAKE_PROFIT_AND_STOP_LIMIT_ORDER",
- ["STOPPRICE"]=tostring(TakeProfit),
- ["OFFSET"]=tostring(MaxOffset),
- ["OFFSET_UNITS"]="PRICE_UNITS",
- ["SPREAD"]=tostring(DefSpread),
- ["SPREAD_UNITS"]="PRICE_UNITS",
- ["STOPPRICE2"]=tostring(StopLoss),
- ["CLASSCODE"]=ClassCode,
- ["SECCODE"]=SecCode,
- ["OPERATION"]=Operation,
- ["QUANTITY"]=Volume,
- ["PRICE"]=tostring(Price),
- ["ACCOUNT"]=Account,
- ["EXPIRY_DATE"]="GTC",
- }
- if ClientCode==nil then
- transaction.client_code=Account
- else
- transaction.client_code=ClientCode
- end
- local res=sendTransaction(transaction)
- if res~="" then
- return false,trans_id, "lib.SendTPSLOrder():"..res
- else
- return true,trans_id, "lib.SendTPSLOrder(): TPSL order sended sucesfully. Class="..ClassCode.." Sec="..SecCode.." Dir="..Operation.." Price="..Price.." TakeProfit="..TakeProfit.." StopLoss="..StopLoss.." MaxOffset="..MaxOffset.." Trans_id="..trans_id
- end
- end
- function RoundToStep(ClassCode,SecCode,StopLoss,TakeProfit)
- if (ClassCode == nil or SecCode == nil or StopLoss == nil or TakeProfit == nil) then
- message("lib.RoundToStep("..ClassCode..","..SecCode..","..StopLoss..","..TakeProfit.."): error occured, one of parameters is nil",3)
- return 0
- end
- local step = getParamEx(ClassCode,SecCode,"SEC_PRICE_STEP")
- if step~=0 then
- return math.floor(StopLoss / step)*step, math.floor(TakeProfit / step)*step
- end
- end
- function GetDate()
- return getTradeDate() --возвращает таблицу date,year,month,day
- end
- function GetTime(frame, amount, timeframe)
- local t = ""
- local a = tostring(getInfoParam("SERVERTIME"))
- --message(a,2)
- for s in a:gmatch('%d+') do
- t=t..s
- end
- local hour,min,sec = tonumber(string.sub(t, 1, 2)),tonumber(string.sub(t, 3, 4)),tonumber(string.sub(t, 5, 6))
- if (timeframe == nil and amount == nil and frame==nil) then
- return t,tonumber(t)
- end
- if (frame == "prev") then
- if (timeframe == nil or amount == nil) then
- message("lib.GetTime("..frame..","..amount..","..timeframe.."): error occured, one of parameters is nil",3)
- return 0
- end
- if (min - timeframe * amount < timeframe) then
- min = 60+(MINUTE-(timeframe*amount))
- hour = hour-1
- else
- min = min-(amount*timeframe)
- end
- end
- local result = hour*10000 + min*100 + sec
- return tostring(result),result
- end
- function GetRoundedTime(frame, amount, timeframe)
- -- функция получает время, округленное до таймфрейма
- local t = ""
- local a = tostring(getInfoParam("SERVERTIME"))
- --message(a,2)
- for s in a:gmatch('%d+') do
- t=t..s
- end
- if (timeframe == nil and amount == nil and frame==nil) then
- message("lib.GetRoundedTime("..frame..","..amount..","..timeframe.."): error occured, all of parameters are nil",3)
- return 0
- end
- local hour,min,sec = tonumber(string.sub(t, 1, 2)),tonumber(string.sub(t, 3, 4)),tonumber(string.sub(t, 5, 6))
- if (frame == "cur") then
- if (timeframe == nil) then
- message("lib.GetRoundedTime("..frame..","..amount..","..timeframe.."): error occured, timeframe is nil",3)
- return 0
- end
- min = timeframe * math.floor(min/timeframe)
- end
- if (frame == "prev") then
- if (timeframe == nil or amount == nil) then
- message("lib.GetRoundedTime("..frame..","..amount..","..timeframe.."): error occured, one of parameters is nil",3)
- return 0
- end
- if (min - timeframe * amount < timeframe) then
- min = timeframe * math.floor((60+(MINUTE-(timeframe*amount)))/timeframe)
- hour = hour - 1
- else
- min = timeframe * math.floor((min-(amount*timeframe))/timeframe)
- end
- end
- local result = hour*10000 + min*100 + sec
- return tostring(result),result
- end
- function GetBoughtCount(ClientCode,SecCode,Account)
- return getDepo(ClientCode,SecCode,Account).depo_current_balance --не факт что работает
- end
- function CheckTime()
- local time=GetTime("cur").num
- if (time <= 100000) or (time >= 235000) or (time > 135900 and time < 140000) or (time > 184400 and time < 184500) then
- return "S" --закрытие позиций перед клирингами
- else
- if time >= 100500 and time <= 235000 then
- return "T"
- else
- return "N"
- end
- end
- end
- function GetGraphValueByCandle(tag, date, candle_num, line) --candle_num означает номер свечи СПРАВА, 0 - текущая, 1 - на предыдущем таймфрейме
- local CandleCount = getNumCandles(tag) -- количество свечек на графике
- local LinesCount = getLinesCount(tag) -- количество линий у графика
- if ( candle_num == nil ) then
- candle_num = CandleCount --если номер свечи не передался в функцию берем последнюю
- end
- if ( line == nil ) then
- line = 0
- end
- if (CandleCount == nil or LinesCount == nil) then
- message("lib.GetGraphValueByCandle("..tag..","..date..","..candle_num..","..line.."): error occured, cannot aqquire candle or line data",3)
- return 0
- end
- if (tag == nil or date == nil) then
- message("lib.GetGraphValueByCandle("..tag..","..date..","..candle_num..","..line.."): error occured, one of parameters is nil",3)
- return 0
- end
- --line=0 (например для графика цены)
- t, num, legend = getCandlesByIndex(tag, line, CandleCount - candle_num, candle_num)
- if ( num == 0 ) then
- message("lib.GetGraphValueByCandle("..tag..","..date..","..candle_num..","..line.."): error occured, no candles aqquired",3)
- return 0
- end
- return t[candle_num]
- end
- --[[
- function GetQuotes()
- end ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement