Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ######## Description: Exit buttons for LONG/SHORT positions ##########
- // Delivered by ::: tazotodua@gmail.com; //
- #region Namespaces
- using elsystem;
- using tsdata.common;
- using tsdata.trading;
- using elsystem.windows.forms;
- using elsystem.drawing;
- #endregion
- [IntraBarOrderGeneration = true]
- inputs: SafetyNet(0.1);
- //inputs: WindowArea(1 {1:top; 2:right; 3:bottom; 4:left});
- vars: WindowArea(1);
- variables:
- Panel pnlConfig( NULL ),
- ComboBox cbAccountNum( NULL ),
- intrabarpersist string SymbolToTrade( "" ),
- intrabarpersist string MainAccountId(tt_getaccountid),
- intrabarpersist string SelectedAccountId(""),
- intrabarpersist int holdedQuantity(0),
- OrdersProvider OrdersInfo( NULL ),
- AccountsProvider AccountsInfo( NULL ),
- Button btnCancelOpenOrds( NULL ),
- OrderTicket ExitTicket( NULL ),
- Order theOrder( NULL );
- // Init method is called when indicator is initialized; code creates all order tickets at initialization
- method void Init( Object InitSender, InitializedEventArgs InitArgs )
- begin
- SymbolToTrade = OrderTicket.GetTradingSymbolFor( Symbol, Category );
- CreateDataProviders();
- CreateBracketForm();
- end;
- // this is the Updated event handler for the OrdersProvider
- method void OrdersInfo_Updated( Object OrdersInfoSender, OrderUpdatedEventArgs OrdersInfoArgs )
- begin
- end;
- vars:string PosResetIdentifier("reset contracts amount");
- //method to create the WinForm and populate it with controls
- Vars: tsdata.trading.Position the_Position(null), tsdata.trading.Account the_Account(null), tsdata.trading.Order the_Order(null);
- Vars: bool IsVerticalForm (WindowArea=2 or WindowArea=3);
- vars: Form frmMain(null), TextBox SafetyNetField( NULL );
- method void CreateBracketForm()
- vars: int buttonWidth, int buttonHeight, int fullHeight, int fullWidth;
- begin
- fullWidth = iff(IsVerticalForm, 110, 800) astype int;
- fullHeight = iff(IsVerticalForm, 180, 45) astype int;
- buttonWidth =80;
- buttonHeight =20;
- //
- frmMain = Form.Create( !( "Exit orders"), fullWidth, fullHeight );
- frmMain.TopMost = true;
- If (WindowArea=1) then
- frmMain.Dock = DockStyle.Top
- else If (WindowArea=2) then
- frmMain.Dock = DockStyle.Right
- else If (WindowArea=3) then
- frmMain.Dock = DockStyle.Bottom
- else If (WindowArea=4) then
- frmMain.Dock = DockStyle.Left
- ;
- //we will use the TradeStation platform theme for this indicator
- //frmMain.UseTheme = UseThemeMode.Full;
- //create the Panels that will go into the Form
- pnlConfig = Panel.Create( fullWidth, fullHeight );
- pnlConfig.BorderStyle = BorderStyle.fixed3d;
- pnlConfig.Location( 0, 0 );
- pnlConfig.ForeColor = Color.LightGray;
- pnlConfig.BackColor = Color.Red;
- //
- pnlConfig.AddControl( createButton("ComboBoxAccount", "", buttonWidth+20, buttonHeight) astype ComboBox );
- pnlConfig.AddControl( createButton("LabelPosition", "", 55, buttonHeight) astype Label );
- pnlConfig.AddControl( createButton("Button", "Close 1/2", buttonWidth, buttonHeight) astype button );
- pnlConfig.AddControl( createButton("Button", "Close 1/3", buttonWidth, buttonHeight) astype button );
- pnlConfig.AddControl( createButton("Button", "Close 1/4", buttonWidth, buttonHeight) astype button );
- pnlConfig.AddControl( createButton("TextBox_SafetyNet", SafetyNet.ToString(), 40, buttonHeight) astype TextBox );
- //pnlConfig.AddControl( createButton(PosResetIdentifier, 110, 35) );
- frmMain.AddControl( pnlConfig );
- frmMain.Show();
- value1= tt_OrderPlacementObjectsCheck;
- //OrdersInfo.Updated += OrdersInfo_Updated;
- end;
- var: last_btn_loc_Y(0),last_btn_loc_X(0), Label PositionLabel(null), object res(null);
- Method object createButton(string type_, string name, int width, int height)
- vars: Button btn, TextBox tb, Label lb, ComboBox cb;
- begin
- If (type_= "ComboBoxAccount") then begin
- //
- cb = ComboBox.Create( "cmbbx", width, height );
- cb.Location( last_btn_loc_X, last_btn_loc_Y );
- cb.Sorted = true;
- cb.FlatStyle = FlatStyle.Flat;
- cb.DropDownStyle = ComboBoxStyle.DropDownList;
- for value44 = 0 to AccountsInfo.Count - 1 begin
- cb.AddItem( AccountsInfo[value44].AccountID );
- end;
- cb.SelectedIndex = cb.FindStringExact(mainAccountId);
- cbAccountNum = cb;
- res= cb;
- end;
- If (type_= "Button") then begin
- btn = Button.Create( !( name ), width, height );
- btn.Location( last_btn_loc_X, last_btn_loc_Y );
- btn.Click += btn_click;
- // btn.ForeColor = Color.White;
- btn.BackColor = Color.White;
- btn.FlatStyle = FlatStyle.Flat;
- res= btn;
- end;
- If (type_= "LabelPosition") then begin
- lb = Label.Create( !( name ), width, height );
- lb.Location( last_btn_loc_X, last_btn_loc_Y );
- PositionLabel = lb;
- res= lb;
- end;
- If (type_.Contains( "TextBox")) then begin
- tb = TextBox.Create( !( name ), width, height );
- tb.Width = width;
- tb.Height = height;
- tb.Location( last_btn_loc_X, last_btn_loc_Y );
- if(type_="TextBox_SafetyNet") then begin
- SafetyNetField = tb;
- end;
- res= tb;
- end;
- // add offset
- If IsVerticalForm then begin
- last_btn_loc_X = 0;
- last_btn_loc_Y = last_btn_loc_Y + height + 10;
- end
- Else begin
- last_btn_loc_X = last_btn_loc_X + width + 10;
- last_btn_loc_Y = 0;
- end;
- Return res;
- end;
- Method void RefreshPositionsP() begin
- SelectedAccountId = cbAccountNum.SelectedItem.ToString();
- If the_Position=null then begin
- value1= tt_GetPosition (symbol, mainAccountId, the_Position);
- If (the_Position<>null) then
- the_Position.Updated += posUpdate;
- end;
- If (the_Position=null) then begin
- pnlConfig.BackColor = Color.Red;
- End
- Else begin
- pnlConfig.BackColor = Color.Azure;
- end;
- posUpdate(null,null);
- end;
- Method void posUpdate(Object Sender, PositionUpdatedEventArgs Args) begin
- If (the_Position = null) then return;
- holdedQuantity = the_Position.Quantity;
- PositionLabel.Text = holdedQuantity.ToString();
- if( the_Position.Type = PositionType.LongPosition) then PositionLabel.BackColor = Color.LightGreen;
- if( the_Position.Type = PositionType.ShortPosition) then PositionLabel.BackColor = Color.LightPink;
- if( the_Position.Type = PositionType.FlatPosition) then PositionLabel.BackColor = Color.White;
- end;
- //this method is called when the Cancel Open Orders is clicked
- method void btn_click( object btnSender, EventArgs btnArgs ) variables: int ReturnValue, Button btn, int tradeSize; begin
- btn = btnSender astype button;
- // if OrdersInfo <> NULL then begin
- // if OrdersInfo.Count > 0 then
- // value1= 0 ;
- // end;
- RefreshPositionsP();
- If (the_Position = null) then begin
- value1=tt_popupmessage("Position not detected", false) ;
- end;
- //If (btn.Text=PosResetIdentifier) then begin RefreshPositionsP(); return; end;
- {
- 3) if long, issue sell limit order for specified amount of shares at the current bid price - "Safety Net"
- 4) if short, issue buy limit order for specified amount of shares at the current ask price + "Safety Net"
- }
- If (btn.Text.Contains("1/2")) then
- tradeSize = (holdedQuantity/2) astype int
- else If (btn.Text.Contains("1/3")) then
- tradeSize = round(holdedQuantity/3,0) astype int
- else If (btn.Text.Contains("1/4")) then
- tradeSize = round(holdedQuantity/4,0) astype int
- ;
- exitOrder(tradeSize);
- //StrToNum( tbTradeSize.Text ) astype int;
- end;
- vars: bool isLong(false);
- method void exitOrder(int tradesize) variables: string AccountNum; begin
- If (the_Position.Type = PositionType.FlatPosition or tradeSize=0) then begin
- Value1 = tt_popupmessage("you are flat, or yet not obtained positon", false );
- return;
- end;
- AccountNum = cbAccountNum.Text;
- ExitTicket = new OrderTicket;
- ExitTicket.Symbol = SymbolToTrade;
- ExitTicket.SymbolType = Category;
- ExitTicket.Account = AccountNum;
- ExitTicket.Quantity = tradesize;
- isLong = the_Position.Type = PositionType.LongPosition;
- if(isLong) then
- ExitTicket.Action = OrderAction.Sell
- Else
- ExitTicket.Action = OrderAction.BuyToCover;
- value1= StrToNum(SafetyNetField.Text);
- ExitTicket.Type = OrderType.Limit;
- ExitTicket.LimitPrice = iff( isLong, InsideBid - value1, InsideAsk + value1) ;
- print(InsideBid, InsideAsk);
- ExitTicket.LimitPriceStyle = PriceStyle.None;
- //ExitTicket.LimitPriceOffset = value1;
- // http://tinyurl.com/yck5j36a
- theOrder = ExitTicket.Send();
- theOrder.Updated += orderUpdated;
- end;
- method void orderUpdated( elsystem.Object sender, OrderUpdatedEventArgs args ) begin
- //Print("OrdUpd:" + args.Message.ToString());
- end;
- method void CreateDataProviders()
- begin
- // create AccountsProvider
- AccountsInfo = new AccountsProvider;
- AccountsInfo.Realtime = false;
- AccountsInfo.Load = true;
- //create OrdersProvider
- OrdersInfo = new OrdersProvider;
- OrdersInfo.Symbols += SymbolToTrade;
- OrdersInfo.Realtime = true;
- OrdersInfo.States += "cancelpending, partiallyfilled, received, sending," + "sent, queued";
- OrdersInfo.Load = true;
- end;
- If tt_isRealTime or LastBarOnChart then begin
- if the_Position=null then RefreshPositionsP();
- end;
Add Comment
Please, Sign In to add comment