retesere20

----recycler-----

Sep 6th, 2018
70
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ######## Description: Exit buttons for LONG/SHORT positions ##########
  2. // Delivered by ::: tazotodua@gmail.com; //
  3.  
  4. #region Namespaces
  5. using elsystem;
  6. using tsdata.common;
  7. using tsdata.trading;
  8. using elsystem.windows.forms;
  9. using elsystem.drawing;
  10. #endregion
  11.  
  12. [IntraBarOrderGeneration = true]
  13. inputs: SafetyNet(0.1);
  14.  
  15. //inputs: WindowArea(1 {1:top; 2:right; 3:bottom; 4:left});
  16. vars: WindowArea(1);
  17. variables:
  18. Panel pnlConfig( NULL ),
  19. ComboBox cbAccountNum( NULL ),
  20.  
  21.  
  22. intrabarpersist string SymbolToTrade( "" ),
  23. intrabarpersist string MainAccountId(tt_getaccountid),
  24. intrabarpersist string SelectedAccountId(""),
  25.  
  26. intrabarpersist int holdedQuantity(0),
  27. OrdersProvider OrdersInfo( NULL ),
  28. AccountsProvider AccountsInfo( NULL ),
  29. Button btnCancelOpenOrds( NULL ),
  30. OrderTicket ExitTicket( NULL ),
  31. Order theOrder( NULL );
  32.  
  33. // Init method is called when indicator is initialized; code creates all order tickets at initialization
  34. method void Init( Object InitSender, InitializedEventArgs InitArgs )
  35. begin
  36. SymbolToTrade = OrderTicket.GetTradingSymbolFor( Symbol, Category );
  37. CreateDataProviders();
  38. CreateBracketForm();
  39. end;
  40.  
  41. // this is the Updated event handler for the OrdersProvider
  42. method void OrdersInfo_Updated( Object OrdersInfoSender, OrderUpdatedEventArgs OrdersInfoArgs )
  43. begin
  44.  
  45. end;
  46.  
  47. vars:string PosResetIdentifier("reset contracts amount");
  48. //method to create the WinForm and populate it with controls
  49.  
  50. Vars: tsdata.trading.Position the_Position(null), tsdata.trading.Account the_Account(null), tsdata.trading.Order the_Order(null);
  51.  
  52.  
  53.  
  54. Vars: bool IsVerticalForm (WindowArea=2 or WindowArea=3);
  55. vars: Form frmMain(null), TextBox SafetyNetField( NULL );
  56. method void CreateBracketForm()
  57. vars: int buttonWidth, int buttonHeight, int fullHeight, int fullWidth;
  58. begin
  59.  
  60. fullWidth = iff(IsVerticalForm, 110, 800) astype int;
  61. fullHeight = iff(IsVerticalForm, 180, 45) astype int;
  62. buttonWidth =80;
  63. buttonHeight =20;
  64. //
  65. frmMain = Form.Create( !( "Exit orders"), fullWidth, fullHeight );
  66. frmMain.TopMost = true;
  67.  
  68. If (WindowArea=1) then
  69. frmMain.Dock = DockStyle.Top
  70. else If (WindowArea=2) then
  71. frmMain.Dock = DockStyle.Right
  72. else If (WindowArea=3) then
  73. frmMain.Dock = DockStyle.Bottom
  74. else If (WindowArea=4) then
  75. frmMain.Dock = DockStyle.Left
  76. ;
  77. //we will use the TradeStation platform theme for this indicator
  78. //frmMain.UseTheme = UseThemeMode.Full;
  79.  
  80. //create the Panels that will go into the Form
  81. pnlConfig = Panel.Create( fullWidth, fullHeight );
  82. pnlConfig.BorderStyle = BorderStyle.fixed3d;
  83. pnlConfig.Location( 0, 0 );
  84. pnlConfig.ForeColor = Color.LightGray;
  85. pnlConfig.BackColor = Color.Red;
  86.  
  87. //
  88. pnlConfig.AddControl( createButton("ComboBoxAccount", "", buttonWidth+20, buttonHeight) astype ComboBox );
  89. pnlConfig.AddControl( createButton("LabelPosition", "", 55, buttonHeight) astype Label );
  90. pnlConfig.AddControl( createButton("Button", "Close 1/2", buttonWidth, buttonHeight) astype button );
  91. pnlConfig.AddControl( createButton("Button", "Close 1/3", buttonWidth, buttonHeight) astype button );
  92. pnlConfig.AddControl( createButton("Button", "Close 1/4", buttonWidth, buttonHeight) astype button );
  93. pnlConfig.AddControl( createButton("TextBox_SafetyNet", SafetyNet.ToString(), 40, buttonHeight) astype TextBox );
  94.  
  95. //pnlConfig.AddControl( createButton(PosResetIdentifier, 110, 35) );
  96.  
  97. frmMain.AddControl( pnlConfig );
  98. frmMain.Show();
  99.  
  100. value1= tt_OrderPlacementObjectsCheck;
  101. //OrdersInfo.Updated += OrdersInfo_Updated;
  102. end;
  103.  
  104.  
  105. var: last_btn_loc_Y(0),last_btn_loc_X(0), Label PositionLabel(null), object res(null);
  106. Method object createButton(string type_, string name, int width, int height)
  107. vars: Button btn, TextBox tb, Label lb, ComboBox cb;
  108. begin
  109.  
  110. If (type_= "ComboBoxAccount") then begin
  111. //
  112. cb = ComboBox.Create( "cmbbx", width, height );
  113. cb.Location( last_btn_loc_X, last_btn_loc_Y );
  114. cb.Sorted = true;
  115. cb.FlatStyle = FlatStyle.Flat;
  116. cb.DropDownStyle = ComboBoxStyle.DropDownList;
  117. for value44 = 0 to AccountsInfo.Count - 1 begin
  118. cb.AddItem( AccountsInfo[value44].AccountID );
  119. end;
  120. cb.SelectedIndex = cb.FindStringExact(mainAccountId);
  121. cbAccountNum = cb;
  122. res= cb;
  123. end;
  124.  
  125.  
  126. If (type_= "Button") then begin
  127. btn = Button.Create( !( name ), width, height );
  128. btn.Location( last_btn_loc_X, last_btn_loc_Y );
  129. btn.Click += btn_click;
  130. // btn.ForeColor = Color.White;
  131. btn.BackColor = Color.White;
  132. btn.FlatStyle = FlatStyle.Flat;
  133. res= btn;
  134. end;
  135.  
  136. If (type_= "LabelPosition") then begin
  137. lb = Label.Create( !( name ), width, height );
  138. lb.Location( last_btn_loc_X, last_btn_loc_Y );
  139. PositionLabel = lb;
  140. res= lb;
  141. end;
  142.  
  143. If (type_.Contains( "TextBox")) then begin
  144. tb = TextBox.Create( !( name ), width, height );
  145. tb.Width = width;
  146. tb.Height = height;
  147. tb.Location( last_btn_loc_X, last_btn_loc_Y );
  148. if(type_="TextBox_SafetyNet") then begin
  149. SafetyNetField = tb;
  150. end;
  151. res= tb;
  152. end;
  153.  
  154.  
  155. // add offset
  156. If IsVerticalForm then begin
  157. last_btn_loc_X = 0;
  158. last_btn_loc_Y = last_btn_loc_Y + height + 10;
  159. end
  160. Else begin
  161. last_btn_loc_X = last_btn_loc_X + width + 10;
  162. last_btn_loc_Y = 0;
  163. end;
  164.  
  165. Return res;
  166. end;
  167.  
  168.  
  169. Method void RefreshPositionsP() begin
  170. SelectedAccountId = cbAccountNum.SelectedItem.ToString();
  171. If the_Position=null then begin
  172. value1= tt_GetPosition (symbol, mainAccountId, the_Position);
  173. If (the_Position<>null) then
  174. the_Position.Updated += posUpdate;
  175. end;
  176. If (the_Position=null) then begin
  177. pnlConfig.BackColor = Color.Red;
  178. End
  179. Else begin
  180. pnlConfig.BackColor = Color.Azure;
  181. end;
  182. posUpdate(null,null);
  183. end;
  184.  
  185.  
  186. Method void posUpdate(Object Sender, PositionUpdatedEventArgs Args) begin
  187. If (the_Position = null) then return;
  188.  
  189. holdedQuantity = the_Position.Quantity;
  190. PositionLabel.Text = holdedQuantity.ToString();
  191. if( the_Position.Type = PositionType.LongPosition) then PositionLabel.BackColor = Color.LightGreen;
  192. if( the_Position.Type = PositionType.ShortPosition) then PositionLabel.BackColor = Color.LightPink;
  193. if( the_Position.Type = PositionType.FlatPosition) then PositionLabel.BackColor = Color.White;
  194. end;
  195.  
  196. //this method is called when the Cancel Open Orders is clicked
  197. method void btn_click( object btnSender, EventArgs btnArgs ) variables: int ReturnValue, Button btn, int tradeSize; begin
  198. btn = btnSender astype button;
  199. // if OrdersInfo <> NULL then begin
  200. // if OrdersInfo.Count > 0 then
  201. // value1= 0 ;
  202. // end;
  203.  
  204. RefreshPositionsP();
  205. If (the_Position = null) then begin
  206. value1=tt_popupmessage("Position not detected", false) ;
  207. end;
  208. //If (btn.Text=PosResetIdentifier) then begin RefreshPositionsP(); return; end;
  209.  
  210. {
  211. 3) if long, issue sell limit order for specified amount of shares at the current bid price - "Safety Net"
  212. 4) if short, issue buy limit order for specified amount of shares at the current ask price + "Safety Net"
  213.  
  214. }
  215. If (btn.Text.Contains("1/2")) then
  216. tradeSize = (holdedQuantity/2) astype int
  217. else If (btn.Text.Contains("1/3")) then
  218. tradeSize = round(holdedQuantity/3,0) astype int
  219. else If (btn.Text.Contains("1/4")) then
  220. tradeSize = round(holdedQuantity/4,0) astype int
  221. ;
  222.  
  223. exitOrder(tradeSize);
  224. //StrToNum( tbTradeSize.Text ) astype int;
  225. end;
  226.  
  227. vars: bool isLong(false);
  228. method void exitOrder(int tradesize) variables: string AccountNum; begin
  229.  
  230. If (the_Position.Type = PositionType.FlatPosition or tradeSize=0) then begin
  231. Value1 = tt_popupmessage("you are flat, or yet not obtained positon", false );
  232. return;
  233. end;
  234.  
  235. AccountNum = cbAccountNum.Text;
  236. ExitTicket = new OrderTicket;
  237. ExitTicket.Symbol = SymbolToTrade;
  238. ExitTicket.SymbolType = Category;
  239. ExitTicket.Account = AccountNum;
  240. ExitTicket.Quantity = tradesize;
  241. isLong = the_Position.Type = PositionType.LongPosition;
  242. if(isLong) then
  243. ExitTicket.Action = OrderAction.Sell
  244. Else
  245. ExitTicket.Action = OrderAction.BuyToCover;
  246.  
  247. value1= StrToNum(SafetyNetField.Text);
  248. ExitTicket.Type = OrderType.Limit;
  249. ExitTicket.LimitPrice = iff( isLong, InsideBid - value1, InsideAsk + value1) ;
  250.  
  251. print(InsideBid, InsideAsk);
  252. ExitTicket.LimitPriceStyle = PriceStyle.None;
  253. //ExitTicket.LimitPriceOffset = value1;
  254. // http://tinyurl.com/yck5j36a
  255. theOrder = ExitTicket.Send();
  256. theOrder.Updated += orderUpdated;
  257. end;
  258.  
  259. method void orderUpdated( elsystem.Object sender, OrderUpdatedEventArgs args ) begin
  260. //Print("OrdUpd:" + args.Message.ToString());
  261. end;
  262.  
  263. method void CreateDataProviders()
  264. begin
  265. // create AccountsProvider
  266. AccountsInfo = new AccountsProvider;
  267. AccountsInfo.Realtime = false;
  268. AccountsInfo.Load = true;
  269.  
  270. //create OrdersProvider
  271. OrdersInfo = new OrdersProvider;
  272. OrdersInfo.Symbols += SymbolToTrade;
  273. OrdersInfo.Realtime = true;
  274. OrdersInfo.States += "cancelpending, partiallyfilled, received, sending," + "sent, queued";
  275. OrdersInfo.Load = true;
  276. end;
  277.  
  278.  
  279. If tt_isRealTime or LastBarOnChart then begin
  280. if the_Position=null then RefreshPositionsP();
  281. end;
RAW Paste Data