Advertisement
Guest User

qlib

a guest
Jan 30th, 2013
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.14 KB | None | 0 0
  1. function SendLimitOrder(ClassCode,SecCode,Operation,Price,Volume,Account,ClientCode)
  2.  
  3. if (ClassCode==nil or SecCode==nil or Operation==nil or Price==nil or Volume==nil or Account==nil) then
  4. return false,0,"lib.SendLimitOrder(): Can`t send order. Nil parameters."
  5. end
  6. local trans_id=math.random(2000000000)
  7. local transaction={
  8. ["TRANS_ID"]=tostring(trans_id),
  9. ["ACTION"]="NEW_ORDER",
  10. ["CLASSCODE"]=ClassCode,
  11. ["SECCODE"]=SecCode,
  12. ["OPERATION"]=Operation,
  13. ["QUANTITY"]=Volume,
  14. ["PRICE"]=tostring(Price),
  15. ["ACCOUNT"]=Account
  16. }
  17.  
  18. if ClientCode==nil then
  19. transaction.client_code=Account
  20. else
  21. transaction.client_code=ClientCode
  22. end
  23.  
  24. local res=sendTransaction(transaction)
  25.  
  26. if res~="" then
  27. return false,trans_id, "lib.SendLimitOrder():"..res
  28. else
  29. 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
  30. end
  31.  
  32. end
  33.  
  34. function SendTPSLOrder(ClassCode,SecCode,Operation,Price,TPOffset,SLOffset,MaxOffset,DefSpread,Volume,Account,ClientCode)
  35.  
  36. 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
  37. return false,0,"lib.SendTPSLOrder(): Can`t send order. Nil parameters."
  38. end
  39.  
  40. local StopLoss,TakeProfit=0,0
  41.  
  42. local trans_id=math.random(2000000000)
  43. if (Operation=="B") then
  44. StopLoss = Price + SLOffset
  45. TakeProfit = Price - TPOffset
  46. end
  47.  
  48. if (Operation=="S") then
  49. StopLoss = Price - SLOffset
  50. TakeProfit = Price + TPOffset
  51. end
  52.  
  53. StopLoss,TakeProfit = RoundToStep(StopLoss,TakeProfit)
  54.  
  55. local transaction={
  56. ["TRANS_ID"]=tostring(trans_id),
  57. ["ACTION"]="NEW_STOP_ORDER",
  58. ["STOP_ORDER_KIND"]="TAKE_PROFIT_AND_STOP_LIMIT_ORDER",
  59. ["STOPPRICE"]=tostring(TakeProfit),
  60. ["OFFSET"]=tostring(MaxOffset),
  61. ["OFFSET_UNITS"]="PRICE_UNITS",
  62. ["SPREAD"]=tostring(DefSpread),
  63. ["SPREAD_UNITS"]="PRICE_UNITS",
  64. ["STOPPRICE2"]=tostring(StopLoss),
  65. ["CLASSCODE"]=ClassCode,
  66. ["SECCODE"]=SecCode,
  67. ["OPERATION"]=Operation,
  68. ["QUANTITY"]=Volume,
  69. ["PRICE"]=tostring(Price),
  70. ["ACCOUNT"]=Account,
  71. ["EXPIRY_DATE"]="GTC",
  72. }
  73.  
  74. if ClientCode==nil then
  75. transaction.client_code=Account
  76. else
  77. transaction.client_code=ClientCode
  78. end
  79.  
  80. local res=sendTransaction(transaction)
  81. if res~="" then
  82. return false,trans_id, "lib.SendTPSLOrder():"..res
  83. else
  84. 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
  85. end
  86.  
  87. end
  88.  
  89. function RoundToStep(ClassCode,SecCode,StopLoss,TakeProfit)
  90.  
  91. if (ClassCode == nil or SecCode == nil or StopLoss == nil or TakeProfit == nil) then
  92. message("lib.RoundToStep("..ClassCode..","..SecCode..","..StopLoss..","..TakeProfit.."): error occured, one of parameters is nil",3)
  93. return 0
  94. end
  95.  
  96. local step = getParamEx(ClassCode,SecCode,"SEC_PRICE_STEP")
  97.  
  98. if step~=0 then
  99. return math.floor(StopLoss / step)*step, math.floor(TakeProfit / step)*step
  100. end
  101.  
  102. end
  103.  
  104. function GetDate()
  105. return getTradeDate() --возвращает таблицу date,year,month,day
  106. end
  107.  
  108. function GetTime(frame, amount, timeframe)
  109.  
  110. local t = ""
  111. local a = tostring(getInfoParam("SERVERTIME"))
  112. --message(a,2)
  113. for s in a:gmatch('%d+') do
  114. t=t..s
  115. end
  116.  
  117. local hour,min,sec = tonumber(string.sub(t, 1, 2)),tonumber(string.sub(t, 3, 4)),tonumber(string.sub(t, 5, 6))
  118.  
  119. if (timeframe == nil and amount == nil and frame==nil) then
  120. return t,tonumber(t)
  121. end
  122. if (frame == "prev") then
  123. if (timeframe == nil or amount == nil) then
  124. message("lib.GetTime("..frame..","..amount..","..timeframe.."): error occured, one of parameters is nil",3)
  125. return 0
  126. end
  127. if (min - timeframe * amount < timeframe) then
  128. min = 60+(MINUTE-(timeframe*amount))
  129. hour = hour-1
  130. else
  131. min = min-(amount*timeframe)
  132. end
  133. end
  134. local result = hour*10000 + min*100 + sec
  135.  
  136. return tostring(result),result
  137. end
  138.  
  139. function GetRoundedTime(frame, amount, timeframe)
  140. -- функция получает время, округленное до таймфрейма
  141. local t = ""
  142. local a = tostring(getInfoParam("SERVERTIME"))
  143. --message(a,2)
  144. for s in a:gmatch('%d+') do
  145. t=t..s
  146. end
  147. if (timeframe == nil and amount == nil and frame==nil) then
  148. message("lib.GetRoundedTime("..frame..","..amount..","..timeframe.."): error occured, all of parameters are nil",3)
  149. return 0
  150. end
  151. local hour,min,sec = tonumber(string.sub(t, 1, 2)),tonumber(string.sub(t, 3, 4)),tonumber(string.sub(t, 5, 6))
  152. if (frame == "cur") then
  153. if (timeframe == nil) then
  154. message("lib.GetRoundedTime("..frame..","..amount..","..timeframe.."): error occured, timeframe is nil",3)
  155. return 0
  156. end
  157. min = timeframe * math.floor(min/timeframe)
  158. end
  159.  
  160. if (frame == "prev") then
  161. if (timeframe == nil or amount == nil) then
  162. message("lib.GetRoundedTime("..frame..","..amount..","..timeframe.."): error occured, one of parameters is nil",3)
  163. return 0
  164. end
  165. if (min - timeframe * amount < timeframe) then
  166. min = timeframe * math.floor((60+(MINUTE-(timeframe*amount)))/timeframe)
  167. hour = hour - 1
  168. else
  169. min = timeframe * math.floor((min-(amount*timeframe))/timeframe)
  170. end
  171. end
  172.  
  173. local result = hour*10000 + min*100 + sec
  174.  
  175. return tostring(result),result
  176. end
  177.  
  178. function GetBoughtCount(ClientCode,SecCode,Account)
  179. return getDepo(ClientCode,SecCode,Account).depo_current_balance --не факт что работает
  180. end
  181.  
  182. function CheckTime()
  183. local time=GetTime("cur").num
  184.  
  185. if (time <= 100000) or (time >= 235000) or (time > 135900 and time < 140000) or (time > 184400 and time < 184500) then
  186. return "S" --закрытие позиций перед клирингами
  187. else
  188. if time >= 100500 and time <= 235000 then
  189. return "T"
  190. else
  191. return "N"
  192. end
  193. end
  194. end
  195.  
  196. function GetGraphValueByCandle(tag, date, candle_num, line) --candle_num означает номер свечи СПРАВА, 0 - текущая, 1 - на предыдущем таймфрейме
  197. local CandleCount = getNumCandles(tag) -- количество свечек на графике
  198. local LinesCount = getLinesCount(tag) -- количество линий у графика
  199.  
  200. if ( candle_num == nil ) then
  201. candle_num = CandleCount --если номер свечи не передался в функцию берем последнюю
  202. end
  203. if ( line == nil ) then
  204. line = 0
  205. end
  206. if (CandleCount == nil or LinesCount == nil) then
  207. message("lib.GetGraphValueByCandle("..tag..","..date..","..candle_num..","..line.."): error occured, cannot aqquire candle or line data",3)
  208. return 0
  209. end
  210. if (tag == nil or date == nil) then
  211. message("lib.GetGraphValueByCandle("..tag..","..date..","..candle_num..","..line.."): error occured, one of parameters is nil",3)
  212. return 0
  213. end
  214.  
  215. --line=0 (например для графика цены)
  216. t, num, legend = getCandlesByIndex(tag, line, CandleCount - candle_num, candle_num)
  217. if ( num == 0 ) then
  218. message("lib.GetGraphValueByCandle("..tag..","..date..","..candle_num..","..line.."): error occured, no candles aqquired",3)
  219. return 0
  220. end
  221. return t[candle_num]
  222. end
  223. --[[
  224. function GetQuotes()
  225.  
  226. end ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement