Stock Portfolio Organizer

The ultimate porfolio management solution.

Shares, Margin, CFD's, Futures and Forex
EOD and Realtime
Dividends and Trust Distributions
And Much More ....
For Portfolio Manager Click Here

WiseTrader Toolbox

#1 Selling Amibroker Plugin featuring:

Advanced Adaptive Indicators
Advanced Pattern Exploration
Neural Networks
And Much More ....
Find Out More Here

Visi-Trade for Amibroker (AFL)
kaiji
about 15 years ago
Amibroker (AFL)

Rating:
3 / 5 (Votes 3)
Tags:
amibroker

This Amibroker Script is for educational purposes only. I cannot guarantee
it’s accuracy nor take any responsibility for your use of it. Use at your own risk.
This is not an automated trading interface – it is strictly a interactive interface.
If you want to play with it, do so in your practic account.

NOT DEVELOPED FOR FOREX OR FUTURES – US STOCKS ONLY!!!!!!!!!

This script will allow you to graphically enter your orders for Interactive Brokers.
You can use the chart to select where you want to enter and where you want to place
the stop loss and target exit orders. Once you have selected the price levels, you
can transmit the order to TWS for execution. It includes some advanced features such
as automatically adjusting order sizes based on max shares, max position size, and max amount
you are willing risk on a trade.

Features:
- graphical interface for selecting order parts
- display various order price levels on the chart
- configuable parameters to control position size and risk
- enter with a market, limit, or stop order
- exit with a bracketed OCA orde
- show or hide the interface with a click of button on chart
- send orders for immediate execution in TWS or just park the order set in TWS for you to execute there
- symbol independent statefulness of order and price level values

Limitations:
- only developed and tested US stocks (that’s all I trade)
- futures and forex leverage and contract pecularities are not taken into account and should not be traded with this script.
- foreign stocks have not been tested, so they may not work.
- current positions are not reflected in the interface

By bluueskyy

Similar Indicators / Formulas

Kavach Of Karna v2
Submitted by hbkwarez over 10 years ago
Advanced Elliott Waves
Submitted by MarcosEn over 13 years ago
3_6Day GuaiLiLv
Submitted by motorfly over 13 years ago
Williams Alligator System
Submitted by durgesh1712 over 13 years ago
*Level Breakout system*
Submitted by Tinych over 13 years ago

Indicator / Formula

Copy & Paste Friendly
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
/*------------------------------------------------------------------
 
This Amibroker Script is for educational purposes only. I cannot guarantee
it's accuracy nor take any responsibility for your use of it.  Use at your own
risk.
This is not an automated trading interface - it is strictly a interactive
interface.
If you want to play with it, do so in your practic account.
 
NOT DEVELOPED FOR FOREX OR FUTURES - US STOCKS ONLY!!!!!!!!!
 
This script will allow you to graphically enter your orders for Interactive
Brokers. 
You can use the chart to select where you want to enter and where you want to
place
the stop loss and target exit orders.  Once you have selected the price levels,
you
can transmit the order to TWS for execution.  It includes some advanced
features such
as automatically adjusting order sizes based on max shares, max position size,
and max amount
you are willing risk on a trade.
 
Features:
- graphical interface for selecting order parts
- display various order price levels on the chart
- configuable parameters to control position size and risk
- enter with a market, limit, or stop order
- exit with a bracketed OCA orde
- show or hide the interface with a click of button on chart
- send orders for immediate execution in TWS or just park the
    order set in TWS for you to execute there
- symbol independent statefulness of order and price level values
 
Limitations:
- only developed and tested US stocks (that's all I trade)
- futures and forex leverage and contract pecularities are not taken into
    account and should not be traded with this script.
- foreign stocks have not been tested, so they may not work.
- current positions are not reflected in the interface
 
 
When you initially add the script to a chart, it will show a button "Show
Trading".
Click this to see the trading interface.  Across the top will display the entry
order types.
Select one of these to begin setting up the order.  "BUY" and "SELL" are market
entries
and the ENTRY field will auto-fill with the current price. BSTOP, SSTOP, BLIM,
and SLIM
require you to select the desired entry price.  To select the entry price, you
first click
on the ENTRY button, then you simply click anywhere on the chart where you want
the entry to be at.
After this, you sothe same thing for the STLOSS and TGT fields.  You can choose
to CHECK the order,
SEND the order to TWS, or CLEARthe order.  SEND'ing the order will
automatically check that
the order is valid. For instance, if you create a BLIM order, then the TGT must
be above the
entry price and the STLOSS must be below the entry price.  If there is a
problem with the order, then
the problem will display in the MESSAGE field.  The labels outlined in RED show
the current state
of what you are doing.
 
You can configure a number of parameters to control risk:
 
Max Trade Value         - what is the max position size you are willing to make
Max Shares                  - what is the max number of shares you willing to buy
Enforce Target Order        - always force yourself to enter a target order
Enforce Stop Loss Order - always force yourself to enter a stop loss order
Max Risk Amt $          - what is the max you are willing to risk if you stop loss is
hit
Enforce Max Risk Amt        - force this to always happen
Add Cancel Mkt Order        - parks a non-executed MARKET order in  TWS so that you
can bail out of the
                              position manually if desired (remember to cancel your bracket orders
                              if you do manually exit the position with the market order
Xmit Always On          - do you want the orders executed when sent to TWS or do you
just want
                              to park them in TWS so you can manually execute them.
 
-------------------------------------------------------------------*/
 
Version( 5.04 ); // requires 5.04 or higher
GfxSetOverlayMode( 0 );
 
MaxTradeSize = Param("Max Trade Value", 1000, 1, 300000, 1000);
MaxShares = Param("Max Shares", 1, 1, 2000, 1);
EnforceTargetOrder = ParamToggle("Enforce Target Order", "No|Yes", 0);
EnforceStopLossOrder = ParamToggle("Enforce Stop Loss Order", "No|Yes", 1);
MaxRiskAmount = Param("Max Risk Amt $", 100, 1, 1500, 10);
EnforceMaxRiskAmt = ParamToggle("Enforce Max Risk Amt", "No|Yes", 1);
AddCancelMktOrder = ParamToggle("Add Cancel Mkt Order", "No|Yes", 1);
TransmitAlwaysOn = ParamToggle("Xmit Always On", "No|Yes", 1);
pAllowAddingToPosition = ParamToggle("Xmit Always On", "No|Yes", 0);
 
 
CellHeight = 20;
CellWidth = 60;
State = "";
 
sTevSymbol = Name() + "_";
sTevVisible = sTevSymbol + "Visible";
sTevState = sTevSymbol + "State";
sTevOrderType = sTevSymbol + "OrderType";
sTevOrderDesc = sTevSymbol + "OrderDesc";
sTevTargetPrice = sTevSymbol + "TargetPrice";
sTevStopLossPrice = sTevSymbol + "StopLossPrice";
sTevEntryPrice = sTevSymbol + "EntryPrice";
sTevShares = sTevSymbol + "Shares";
sTevTransmit = sTevSymbol + "TransmitState";
sTevMessage = sTevSymbol + "Message";
sTevPriceSelectState = sTevSymbol + "PriceSelect";
 
sTevOrderSymbol = sTevSymbol + "OrderSymbol";
sTevOrderShares = sTevSymbol + "OrderShares";
sTevOrderParentId = sTevSymbol + "OrderParentId";
sTevOrderTargetId = sTevSymbol + "OrderTargetId";
sTevOrderStopLossId = sTevSymbol + "OrderStopLossId";
 
 
sPriceSelectType_Entry = "ENTRY";
sPriceSelectType_StopLoss = "SLOSS";
sPriceSelectType_Target = "TGT";
sPriceSelectType_None = "";
 
sOrderType_BuyAtMarket = "BUY";
sOrderType_ShortAtMarket = "SELL";
sOrderType_BuyAtStop = "B STOP";
sOrderType_ShortAtStop = "S STOP";
sOrderType_BuyAtLimit = "B LIM";
sOrderType_ShortAtLimit = "S LIM";
sOrrderType_None = "NA";
 
sStateType_OrderPrep = "Prep";
sStateType_OrderReady = "Ready";
sStateType_OrderNone = "None";
sStateType_OrderSent = "Sent";
 
sTransmitType_No = "No";
sTransmitType_Yes = "Yes";
 
function formatPriceForSecurityType( inputPrice )
{
    OutputPrice = -1;
 
    if ( inputPrice > 0 )
    {
        OutputPrice = inputPrice;
 
        if ( StrFind( Name(), " A0-FX" ) )
        {
            //format price for forex
        }
        else
            if ( StrFind( Name(), " #F" ) )
            {
                //format price for futures
            }
            else
            {
                //format price for stock
                OutputPrice = int( inputPrice * 100 ) / 100;
            }
    }
 
    return OutputPrice;
}
 
function CalculateShares()
{
    Shares = int(MaxTradeSize/Close[BarCount-1]);
 
    if (Shares > MaxShares)
    {
        Shares = int(MaxShares);
    }
 
    return Shares;
}
 
function CalculateSharesByRiskAmount()
{
    Shares = 0;
 
    Entry = StaticVarGet(sTevEntryPrice);
    StopLoss = StaticVarGet(sTevStopLossPrice);
    Target = StaticVarGet(sTevTargetPrice);
     
    ORderType = StaticVarGetText(sTevOrderType);
    if (ORderType == sOrderType_BuyAtMarket || ORderType == sOrderType_ShortAtMarket)
    {
        Entry = Close[BarCount-1];
    }
 
 
    if (IsNull(Entry))
        Entry = -999;
    if (IsNull(StopLoss ))
        StopLoss = -999;
    if (IsNull(Target ))
        Target = -999;
     
    if (Entry != -999 && StopLoss != -999)
    {
        //MaxRiskAmount
        RiskAmtPerShare = abs(Entry - StopLoss);
 
        Shares = int(MaxRiskAmount / RiskAmtPerShare);
 
        TradeSize = Close[BarCount-1] * Shares;
 
        if (TradeSize > MaxTradeSize)
        {
            Shares = int(MaxTradeSize/Close[BarCount-1]);
        }
         
        if (Shares > MaxShares)
        {
            Shares = int(MaxShares);
        }
 
    }
 
    return Shares;
}
 
function GetTransmitBool()
{
    xmit = False;
 
    xmitstate = StaticVarGetText( sTevTransmit );
 
    if ( xmitstate == sTransmitType_Yes )
        xmit = True;
 
    return xmit;
}
 
function ClearState()
{
    StaticVarSetText(sTevState, sStateType_OrderNone);
    StaticVarRemove(sTevOrderType);
    StaticVarRemove(sTevOrderDesc);
    StaticVarRemove(sTevTargetPrice);
    StaticVarRemove(sTevStopLossPrice);
    StaticVarRemove(sTevEntryPrice);
    StaticVarRemove(sTevTransmit);
 
 
    StaticVarSetText(sTevTransmit, sTransmitType_No);
    StaticVarSetText(sTevPriceSelectState, sPriceSelectType_None);
    StaticVarSetText(sTevMessage, "");
 
}
 
function CheckOrder()
{
    CheckedValid = True;
    message = "";
 
    StaticVarSetText(sTevPriceSelectState, sPriceSelectType_None);
    ORderType = StaticVarGetText(sTevOrderType);
    Entry = StaticVarGet(sTevEntryPrice);
    StopLoss = StaticVarGet(sTevStopLossPrice);
    Target = StaticVarGet(sTevTargetPrice);
 
    if (IsNull(Entry))
        Entry = -999;
    if (IsNull(StopLoss ))
        StopLoss = -999;
    if (IsNull(Target ))
        Target = -999;
 
    if (ORderType == "")
    {
        message = "No Order";
        CheckedValid = False;
    }
    else
    {
         
        if (ORderType == sOrderType_BuyAtMarket || ORderType ==
sOrderType_ShortAtMarket)
        {
            Entry = Close[BarCount-1];
        }
        else if (Entry == -999)
        {
            message +=  ": BAD ENTRY";
            CheckedValid = False;
        }
         
        if (StopLoss == -999 && EnforceStopLossOrder == 1)
        {
            message +=  " : BAD ST LOSS";
            CheckedValid = False;
        }
        if (Target == -999 && EnforceTargetOrder == 1)
        {
            message += " : BAD TGT";
            CheckedValid = False;
        }
         
        if (CheckedValid)
        {
            if (ORderType == sOrderType_BuyAtMarket || ORderType == sOrderType_BuyAtStop || ORderType == sOrderType_BuyAtLimit)
            {
                if (Entry > Target && EnforceTargetOrder == 1)
                {
                    message += " : ENTRY > TGT";
                    CheckedValid = False;                  
                }
                if (StopLoss > Entry && EnforceStopLossOrder == 1)
                {
                    message += " : ST LOSS > ENTRY";
                    CheckedValid = False;                  
                }
                if (StopLoss > Target && EnforceStopLossOrder == 1  && EnforceTargetOrder == 1)
                {
                    message += " : ST LOSS > TGT";
                    CheckedValid = False;                  
                }
            }
            else if (ORderType == sOrderType_ShortAtMarket || ORderType == sOrderType_ShortAtStop || ORderType == sOrderType_ShortAtLimit)
            {
                if (Entry < Target && EnforceTargetOrder == 1)
                {
                    message += " : ENTRY < TGT";
                    CheckedValid = False;                  
                }
                if (StopLoss < Entry && EnforceStopLossOrder == 1)
                {
                    message += " : ST LOSS < ENTRY";
                    CheckedValid = False;                  
                }
                if (StopLoss < Target && EnforceStopLossOrder == 1  && EnforceTargetOrder == 1)
                {
                    message += " : ST LOSS < TGT";
                    CheckedValid = False;                  
                }
            }
        }
        //message = ORdertype + " " + NumToStr(int(MaxShares)) + " @ " + NumToStr(Entry)
        //          + ", SL @ " + NumToStr(StopLoss)
        ///         + ", TG @ " + NumToStr(Target);
    }
 
    if (CheckedValid)
        message = "VALID ORDER";
 
    StaticVarSetText(sTevMessage, message);
    return CheckedValid;
 
}
 
function GetOrderDirection(ORderType, entryOrderType)
{
    ORderdirction = "";
 
    if ( entryOrderType == "" )
    {
        if ( ORderType == sOrderType_BuyAtMarket || ORderType == sOrderType_BuyAtStop || ORderType == sOrderType_BuyAtLimit )
        {
            ORderdirction = "BUY";
        }
        else if ( ORderType == sOrderType_ShortAtMarket || ORderType == sOrderType_ShortAtStop || ORderType == sOrderType_ShortAtLimit )
        {
            ORderdirction = "SELL";
        }
    }
    else
   {
        if ( entryOrderType == "BUY" )
       {
        ORderdirction = "SELL";
       }
       else if ( entryOrderType == "SELL" )
       {
        ORderdirction = "BUY";
       }
    }
 
    return ORderdirction;
}
 
 
function GetOrderType(ORderType, entryOrderType)
{
    RtnORderType = "";
     
    if (entryOrderType == "")
    {
        if ( ORderType == sOrderType_BuyAtMarket || ORderType == sOrderType_ShortAtMarket )
            RtnORderType = "MKT";
        else if ( ORderType == sOrderType_BuyAtStop || ORderType == sOrderType_ShortAtStop )
            RtnORderType = "STP";
        else if ( ORderType == sOrderType_BuyAtLimit || ORderType == sOrderType_ShortAtLimit )
            RtnORderType = "LMT";
    }
    else
    {
       if ( ORderType == sOrderType_BuyAtMarket || ORderType == sOrderType_ShortAtMarket )
            RtnORderType = "MKT";
       else if ( ORderType == sOrderType_BuyAtStop || ORderType == sOrderType_ShortAtStop )
           RtnORderType = "STP";
       else if ( ORderType == sOrderType_BuyAtLimit || ORderType == sOrderType_ShortAtLimit )
           RtnORderType = "MIT";
    }
 
    return RtnORderType ;
}
 
function SendOrder()
{
    if (CheckOrder())
    {
        StaticVarSetText(sTevPriceSelectState, sPriceSelectType_None);
        ORderSymbol = Name();
        ORderType = StaticVarGetText(sTevOrderType);
        Entry = formatPriceForSecurityType(StaticVarGet(sTevEntryPrice));
        StopLoss = formatPriceForSecurityType(StaticVarGet(sTevStopLossPrice));
        Target = formatPriceForSecurityType(StaticVarGet(sTevTargetPrice));
        Shares = StaticVarGet(sTevShares);
        //Shares = CalculateShares();
        MasterXMit = GetTransmitBool();
 
        ORderIBType = GetOrderType(ORderType, "");
        ORderIBDirection = GetOrderDirection(ORderType, "");
 
        SLOrderIBType = GetOrderType(sOrderType_BuyAtStop, ORderType);
        SLOrderIBDirection = GetOrderDirection(ORderType, ORderIBDirection);
         
        TGOrderIBType = GetOrderType(sOrderType_BuyAtLimit, ORderType);
        TGOrderIBDirection = GetOrderDirection(ORderType, ORderIBDirection);
 
        parentOrderId = 0;
        targetOrderId = 0;
        stoplossOrderId = 0;
 
        ibc = GetTradingInterface("IB");
 
        // check if we are connected OK
        if( ibc.IsConnected() )
        {  
            // check if we do not have already open position on this stock
            if( ibc.GetPositionSize( Name() ) == 0  ||  pAllowAddingToPosition == 1)
 
            {
                doXmit = False;
 
                if (EnforceStopLossOrder == 0  && EnforceTargetOrder == 0)
                {
                    doXmit = True && MasterXMit;
                }
 
                // transmit order
                if (ORderIBType == "LMT")
                    parentOrderId = ibc.PlaceOrder( ORderSymbol, ORderIBDirection, Shares, ORderIBType, Entry, 0, "GTC", doXmit, 0, "ignoreRth" );
                else if (ORderIBType == "STP")
                    parentOrderId = ibc.PlaceOrder( ORderSymbol, ORderIBDirection, Shares, ORderIBType, 0, Entry, "GTC", doXmit, 0, "ignoreRth" );
                else if (ORderIBType == "MKT")
                    parentOrderId = ibc.PlaceOrder( ORderSymbol, ORderIBDirection, Shares, ORderIBType, 0, 0, "GTC", doXmit, 0, "ignoreRth" );
 
                if (EnforceTargetOrder == 1  || Target > 0)
                {
                    if (EnforceStopLossOrder == 0)
                        doXmit = True && MasterXMit;
 
                targetOrderId = ibc.PlaceOrder( ORderSymbol, TGORderIBDirection, Shares, TGORderIBType, 0, Target,
                                                            "GTC", doXmit, 100, "", parentOrderId, ORderSymbol + "_GRP"  );
 
                }
                if (EnforceStopLossOrder == 1 || StopLoss > 0)
                {
                    doXmit = True && MasterXMit;
                    stoplossOrderId = ibc.PlaceOrder(ORderSymbol, SLORderIBDirection, Shares, SLORderIBType, 0, StopLoss,
                                                            "GTC", doXmit, 100, "", parentOrderId, ORderSymbol + "_GRP" );
 
                }
 
                if (AddCancelMktOrder == 1)
                {
                    MktOrderIBType = GetOrderType(sOrderType_BuyAtMarket, ORderType);
                    MktOrderIBDirection = GetOrderDirection(ORderType, ORderIBDirection);
 
                    ibc.PlaceOrder( ORderSymbol, MktOrderIBDirection, Shares, MktOrderIBType, 0, 0, "GTC", False, 0, "ignoreRth" );
                }
            }
        }
    }
}
 
function SetState(sstate)
{
    StaticVarSetText(sTevState, sstate);
}
 
function PrintInCell( string, row, Col )
{
    GfxDrawText( NumToStr( Close[BarCount-1] ) + string, Col * CellWidth, row * CellHeight, ( Col + 1 ) * CellWidth, ( row + 1 ) * CellHeight, 0 );
}
 
function DrawButton2( col, row, buttonColor, textColor, height, width, text, bold, outlineType, span)
{
    GfxSetBkMode(1);
     
    if (text == "{EMPTY}")
        text = "";
 
    x1 = col * width;
    y1 = row * height;
    x2 = ( col + span ) * width-3;
    y2 = ( row + 1 ) * height-3;
    OutlineWidth = 3;
  
    GfxGradientRect( x1, y1, x2, y2, buttonColor, buttonColor);
 
    GfxSetTextColor(TextColor);
 
    weight = 400;
    if (bold == True)
        weight = 800;
 
    GfxSelectFont( "Tahoma", height/ 2.5, weight);
 
    GfxDrawText( text, x1 + 5, y1 + 2.5, x2, y2, 0 );
 
    if (outlineType == 0)
    {
        GfxSelectPen(colorGrey50, 3 );
        GfxMoveTo( x1, y2);
        GfxLineTo( x2 - 1, y2 );
        GfxLineTo( x2 - 1, y1 + 1);
    }
    else if (OutlineType == 1)
    {
        GfxSelectPen(colorBlack, 2 );
        GfxMoveTo( x1, y2);
        GfxLineTo( x2 - 1, y2 );
        GfxLineTo( x2 - 1, y1 + 1);
        GfxLineTo( x1, y1 + 1);
        GfxLineTo( x1, y2);
    }
    else if (OutlineType == 2)
    {
        GfxSelectPen(colorRed, 2 );
        GfxMoveTo( x1, y2);
        GfxLineTo( x2 - 1, y2 );
        GfxLineTo( x2 - 1, y1 + 1);
        GfxLineTo( x1, y1 + 1);
        GfxLineTo( x1, y2);
    }
}
 
function DrawButton( px, py, Clr1, Clr2, text )
{
    Col = floor( px / CellWidth );
    Row = floor( py / CellHeight );
 
 
    GfxGradientRect( Col * CellWidth, row * CellHeight, ( Col + 1 ) *CellWidth, ( row + 1 ) * CellHeight,
                     Clr1, Clr2 );
 
    PrintInCell( text + " " + row + "," + Col, row, Col );
 
}
 
function OnLMouseButton( x, y, px, py )
{
    priceSelectState = StaticVarGetText(sTevPriceSelectState);
 
    if (priceSelectState == sPriceSelectType_Entry)
    {
        StaticVarSet(sTevEntryPrice, formatPriceForSecurityType(y));
    }
    else    if (priceSelectState == sPriceSelectType_StopLoss)
    {
        StaticVarSet(sTevStopLossPrice, formatPriceForSecurityType(y));
    }
    else    if (priceSelectState == sPriceSelectType_Target)
    {
        StaticVarSet(sTevTargetPrice, formatPriceForSecurityType(y));
    }
}
 
function OnRMouseButton( x, y, px, py )
{
    x = 0;
}
 
function OnMMouseButton( x, y, px, py )
{
    x = 0;
}
 
function OnHoverMouse( x, y, px, py )
{
    x = 0;
}
 
function OnLButtonIsDown( x, y, px, py )
{
    x = 0;
}
 
function EventHandler()
{
    local b, x, y, px, py;
    b = GetCursorMouseButtons();
 
// retrieve co-ordinates in date/value units
    x = GetCursorXPosition( 0 );
    y = GetCursorYPosition( 0 );
 
// retrieve co-ordinates in pixel units
    px = GetCursorXPosition( 1 );
    py = GetCursorYPosition( 1 );
 
    Col = floor( px / CellWidth );
    Row = floor( py / CellHeight );
 
    printf("\nButton: %g, %g, %g", Col, Row, StaticVarGet(sTevVisible)); //StaticVarSet(sTevVisible, 1);
    if ( StaticVarGet(sTevVisible) != 1)
    {
        if ((Col == 0 || Col == 1) && Row == 1 &&  b & 8 &&  b & 1)
        {
            StaticVarSet(sTevVisible, 1);
            //ClearState();
            //SetState(sStateType_OrderPrep);
            //StaticVarSetText(sTevOrderType, sOrderType_BuyAtMarket);
            //StaticVarSet(sTevEntryPrice, Close[BarCount-1]);
        }
 
    }
    ///////////////////////////////////////////////////////////
    else if (Col == 0 && Row == 1 &&  b & 8 &&  b & 1)
    {
        StaticVarSet(sTevVisible, 0);
    }
    else if (Col == 1 && Row == 1 &&  b & 8 &&  b & 1)
    {
        StaticVarSetText("TE_Button", sOrderType_BuyAtMarket);
        ClearState();
        SetState(sStateType_OrderPrep);
        StaticVarSetText(sTevOrderType, sOrderType_BuyAtMarket);
        StaticVarSet(sTevEntryPrice, Close[BarCount-1]);
    }
    else if (Col == 2 && Row == 1 &&  b & 8 &&  b & 1)
    {
        StaticVarSetText("TE_Button", sOrderType_ShortAtMarket);
        ClearState();
        SetState(sStateType_OrderPrep);
        StaticVarSetText(sTevOrderType, sOrderType_ShortAtMarket);
        StaticVarSet(sTevEntryPrice, Close[BarCount-1]);
    }
    else if (Col == 3 && Row == 1 &&  b & 8 &&  b & 1)
    {
        StaticVarSetText("TE_Button", sOrderType_BuyAtStop);
        ClearState();
        SetState(sStateType_OrderPrep);
        StaticVarSetText(sTevOrderType, sOrderType_BuyAtStop);
    }  
    else if (Col == 4 && Row == 1 &&  b & 8 &&  b & 1)
    {
        StaticVarSetText("TE_Button", sOrderType_ShortAtStop);
        ClearState();
        SetState(sStateType_OrderPrep);
        StaticVarSetText(sTevOrderType, sOrderType_ShortAtStop);
    }
    else if (Col == 5 && Row == 1 &&  b & 8 &&  b & 1)
    {
        StaticVarSetText("TE_Button", sOrderType_BuyAtLimit);
        ClearState();
        SetState(sStateType_OrderPrep);
        StaticVarSetText(sTevOrderType, sOrderType_BuyAtLimit);
    }
    else if (Col == 6 && Row == 1 &&  b & 8 &&  b & 1)
    {
        StaticVarSetText("TE_Button", sOrderType_ShortAtLimit);
        ClearState();
        SetState(sStateType_OrderPrep);
        StaticVarSetText(sTevOrderType, sOrderType_ShortAtLimit);
    }
 
    ///////////////////////////////////////////////////
    else if (Col == 2 && Row == 7 &&  b & 8 &&  b & 1)
    {
        StaticVarSetText("TE_Button", "CLEAR");
        ClearState();
    }
    else if (Col == 4 && Row == 4 &&  b & 8 &&  b & 1)
    {
        StaticVarSetText("TE_Button", "PSEL");
        StaticVarSetText(sTevPriceSelectState, sPriceSelectType_None);
    }
    else if (Col == 3 && Row == 7 &&  b & 8 &&  b & 1)
    {
        StaticVarSetText("TE_Button", "SEND");
        SendOrder();
    }
    else if (Col == 0 && Row == 3 &&  b & 8 &&  b & 1)
    {
        StaticVarSetText("TE_Button", "ENTRY");
        StaticVarSetText(sTevPriceSelectState, sPriceSelectType_Entry);
    }
    else if (Col == 0 && Row == 4 &&  b & 8 &&  b & 1)
    {
        StaticVarSetText("TE_Button", "ST LOSS");
        StaticVarSetText(sTevPriceSelectState, sPriceSelectType_StopLoss);
    }
    else if (Col == 0 && Row == 5 &&  b & 8 &&  b & 1)
    {
        StaticVarSetText("TE_Button", "TGT");
        StaticVarSetText(sTevPriceSelectState, sPriceSelectType_Target);
    }
    else if ((Col == 0 || Col == 1) && Row == 7 &&  b & 8 &&  b & 1)
    {
        StaticVarSetText("TE_Button", "CHK ODR");
        //StaticVarSetText(sTevPriceSelectState, sPriceSelectType_Target);
        CheckOrder();
    }
    else if (Col == 2 && Row == 2 &&  b & 8 &&  b & 1)
    {
        StaticVarSetText("TE_Button", "XMIT");
     
        xmitstate = StaticVarGetText(sTevTransmit);
        if (xmitstate == sTransmitType_No || xmitstate == "")
            StaticVarSetText(sTevTransmit, sTransmitType_Yes);
        else
            StaticVarSetText(sTevTransmit, sTransmitType_No);
    }
 
    else if ( b & 8 ) // flag = 8 is set when window just received mouse click
    {
        // not-null means clicked in THIS (current) window
        if ( b & 1 )
            OnLMouseButton( x, y, px, py );
 
        if ( b & 2 )
            OnRMouseButton( x, y, px, py );
 
        if ( b & 4 )
            OnMMouseButton( x, y, px, py );
    }
    else
    {
        //if( b == 0 ) OnHoverMouse( x, y, px, py ); // no button pressed
        if ( b == 1 )
            OnLButtonIsDown( x, y, px, py ); // button pressed
    }
}
 
 
EventHandler();
//RequestTimedRefresh( 1 );
 
if ( StaticVarGet(sTevVisible) == 1)
    DrawButton2( 0, 1, colorBlack, colorWhite, CellHeight, CellWidth, "HIDE",True, 0, 1 );
else
    DrawButton2( 0, 1, colorBlack, colorWhite, CellHeight, CellWidth, "SHOWTRADING", True, 0, 2  );
 
if ( StaticVarGet(sTevVisible) == 1)
{
 
    if (TransmitAlwaysOn == 1)
        StaticVarSetText(sTevTransmit, sTransmitType_Yes);
 
    DrawButton2( 1, 1, colorBlack, colorWhite, CellHeight, CellWidth, "BUY",True, 0, 1 );
    DrawButton2( 2, 1, colorBlack, colorWhite, CellHeight, CellWidth, "SELL",True, 0, 1 );
    DrawButton2( 3, 1, colorBlack, colorWhite, CellHeight, CellWidth, "B STOP",True, 0, 1 );
    DrawButton2( 4, 1, colorBlack, colorWhite, CellHeight, CellWidth, "S STOP",True, 0, 1 );
    DrawButton2( 5, 1, colorBlack, colorWhite, CellHeight, CellWidth, "B LIM",True, 0, 1 );
    DrawButton2( 6, 1, colorBlack, colorWhite, CellHeight, CellWidth, "S LIM",True, 0, 1 );
 
    DrawButton2( 4, 2, colorBlack, colorWhite, CellHeight, CellWidth, "STATE",True, 2, 1 );
    DrawButton2( 5, 2, colorGrey50, colorWhite, CellHeight, CellWidth,StaticVarGetText( sTevState ), True, 1, 1 );
 
    DrawButton2( 4, 3, colorBlack, colorWhite, CellHeight, CellWidth, "BTN",True, 2, 1 );
    DrawButton2( 5, 3, colorGrey50, colorWhite, CellHeight, CellWidth,StaticVarGetText( "TE_Button" ), True, 1, 1 );
 
    DrawButton2( 4, 4, colorBlack, colorWhite, CellHeight, CellWidth, "PSEL",True, 2, 1 );
    DrawButton2( 5, 4, colorGrey50, colorWhite, CellHeight, CellWidth,StaticVarGetText( sTevPriceSelectState ), True, 1, 1 );
 
    DrawButton2( 0, 2, colorBlack, colorWhite, CellHeight, CellWidth, "O TYPE",True, 0, 1 );
    DrawButton2( 1, 2, colorGrey50, colorWhite, CellHeight, CellWidth,StaticVarGetText( sTevOrderType ), True, 1, 1 );
    DrawButton2( 2, 2, colorBlack, colorWhite, CellHeight, CellWidth, "XMIT",True, 0, 1 );
    DrawButton2( 3, 2, colorGrey50, colorWhite, CellHeight, CellWidth,StaticVarGetText( sTevTransmit ), True, 1, 1 );
 
    Shares = 0;
 
    if ( EnforceMaxRiskAmt == 1 )
    {
        Shares = CalculateSharesByRiskAmount();
        StaticVarSet( sTevShares, Shares );
    }
    else
    {
        Shares = CalculateShares();
        StaticVarSet( sTevShares, Shares );
    }
 
    DrawButton2( 2, 3, colorBlack, colorWhite, CellHeight, CellWidth, "SHARES",True, 0, 1 );
 
    DrawButton2( 3, 3, colorGrey50, colorWhite, CellHeight, CellWidth,NumToStr( Shares, 5 ), True, 1, 1 );
 
    DrawButton2( 0, 3, colorBlack, colorWhite, CellHeight, CellWidth, "ENTRY",True, 0, 1 );
    DrawButton2( 1, 3, colorGrey50, colorWhite, CellHeight, CellWidth,NumToStr( StaticVarGet( sTevEntryPrice ), 0.2 ), True, 1, 1 );
 
    DrawButton2( 0, 4, colorBlack, colorWhite, CellHeight, CellWidth, "STLOSS", True, 0, 1 );
    DrawButton2( 1, 4, colorGrey50, colorWhite, CellHeight, CellWidth,NumToStr( StaticVarGet( sTevStopLossPrice ), 0.2 ), True, 1, 1 );
 
    DrawButton2( 0, 5, colorBlack, colorWhite, CellHeight, CellWidth, "TGT",True, 0, 1 );
    DrawButton2( 1, 5, colorGrey50, colorWhite, CellHeight, CellWidth,NumToStr( StaticVarGet( sTevTargetPrice ), 0.2 ), True, 1, 1 );
 
    DrawButton2( 0, 6, colorBlack, colorWhite, CellHeight, CellWidth,"Message", True, 0, 1 );
    DrawButton2( 1, 6, colorGrey50, colorWhite, CellHeight, CellWidth,StaticVarGetText( sTevMessage ), False, 1, 5 );
 
    DrawButton2( 0, 7, colorBlack, colorWhite, CellHeight, CellWidth, "CHECKORDER", True, 0, 2 );
    DrawButton2( 2, 7, colorBlack, colorWhite, CellHeight, CellWidth, "CLEAR",True, 0, 1 );
    DrawButton2( 3, 7, colorBlack, colorWhite, CellHeight, CellWidth, "SEND",True, 0, 1 );
 
    Plot( StaticVarGet( sTevEntryPrice ), "ENTRY"colorBlue, styleLine |styleDashed | styleNoTitle );
    Plot( StaticVarGet( sTevStopLossPrice ), "SL"colorRed, styleLine |styleDashed | styleNoTitle );
    Plot( StaticVarGet( sTevTargetPrice ), "TGT"colorGreen, styleLine |styleDashed | styleNoTitle );
}
_SECTION_END();

0 comments

Leave Comment

Please login here to leave a comment.

Back