Full proof Static variables

Im testing a long only bracket order strategy that has a bracket order buy and then also a sell condition. I modified some existing IBC code and it was working smooth until one time I ended up being in a short position which doesn't make sense judging how it is coded these somehow short coditions only occur when the sell condition is met and only happen rarely. If anyone has incite on why it was possible for it to go into a short position or knows a way to make it so its more full proof against every going short that would be great. I was thinking of adding a static variable that tracks position size but I thought that wouldn't really make any difference then how it currently is. Thanks!:

autoTrade = ParamToggle("Auto Trading?","OFF|ON",0);
transmit = ParamToggle( "Transmit", "OFF|ON", 0 );
Reset = ParamTrigger( "Reset All", "RESET" );
BuyTrigger = ParamTrigger( "Place Buy order", "BUY" );
SellTrigger = ParamTrigger( "Place Sell order", "SELL" );

LastTrade = StaticVarGetText( "LastTrade" );
BuyOrderID = StaticVarGetText( "BuyOrderID" );
SellOrderID = StaticVarGetText( "SellOrderID" );
childID = StaticVarGetText("childID");
lmtID = StaticVarGetText("lmtID");

a = ATR(14);
var1 = (LastValue( O, lastmode = True ) + LastValue( a, lastmode = True ));
var2 = (LastValue( O, lastmode = True ) - 5);//LastValue( a, lastmode = True ));
last = LastValue(O, lastmode = True);
RequestTimedRefresh( 5, False );
if( autoTrade )
{

    SetChartBkColor( colorGreen );
    ibc = GetTradingInterface( "IB" );

    if( ibc.IsConnected() )
    {
        BuyPending = ibc.IsOrderPending( BuyOrderID );
        SellPending = ibc.IsOrderPending( SellOrderID );
        ChildPending = ibc.IsOrderPending( childID );
        lmtPending = ibc.IsOrderPending( lmtID );
        IBPosSize = ibc.GetPositionSize( Name() );
        BuyStatus = ibc.GetStatus( BuyORderID, True );
        SellStatus = ibc.GetStatus( SellORderID, True );
        ChildStatus = ibc.GetStatus( childID, True );
        lmtStatus = ibc.GetStatus( lmtID, True );


        if( ( BuyTrigger || LastValue( Buy ) ) )
        {
            if( LastTrade == "Sell" OR LastTrade == "" )
            {
                if( SellStatus == "Filled" OR SellStatus == "" )
                {
                    BuyOrderID = ibc.ModifyOrder( BuyOrderID, Name(), "BUY", 1, "LMT", last, 0, "GTC", False );
                    lmtID = ibc.ModifyOrder( lmtID, Name(), "SELL", 1, "LMT", var1, 0, "GTC", False, 2500, "", BuyOrderID );
                    childID = ibc.ModifyOrder( childID, Name(), "SELL", 1, "STP", var2 , var2 , "GTC", transmit, 2500, "", BuyOrderID );
                    StaticVarSetText( "BuyOrderID", BuyOrderID );
                    StaticVarSetText( "childID", childID );
                    StaticVarSetText( "lmtID", lmtID );
                    StaticVarSetText( "LastTrade", "Buy" );
                    SetChartBkColor( colorBrightGreen ) ;
                }
            }
        }
        else
            if( ( SellTrigger || LastValue( Sell ) ) )
            {
                if( LastTrade == "Buy")// OR LastTrade == "" ) // uncomment for short positions
                {
                    if( BuyStatus == "Filled" ) //OR BuyStatus == "" ) // uncomment for short positions
                    {
                        SellORderID = ibc.ModifyOrder( SellORderID , Name(), "Sell", 1, "MKT", 0, 0, "GTC", transmit );
                        ibc.CancelOrder( childID );
                        ibc.CancelOrder( lmtID );
                        StaticVarSetText( "SellOrderID", SellOrderID );
                        StaticVarSetText( "LastTrade", "Sell" );
                        SetChartBkColor( colorRed ) ;
                    }
                }
            }
            else
                if( ChildStatus == "Filled" || lmtStatus == "Filled")
                {
                    StaticVarSetText( "LastTrade", "Sell" ); // clear last trade
                }
                else
                    if( Reset )
                    {
                        StaticVarSetText( "BuyOrderID", "" );

                        if( BuyPending ) ibc.CancelOrder( BuyOrderID );

                        StaticVarSetText( "SellOrderID", "" );

                        if( SellPending ) ibc.CancelOrder( SellOrderID );

                        StaticVarSetText( "childID", "" ); // clear child id

                        if( childPending ) ibc.CancelOrder( childID );
                        
                        StaticVarSetText( "lmtID", "" ); // clear lmt id

                        if( lmtPending ) ibc.CancelOrder( lmtID );

                        StaticVarSetText( "LastTrade", "" );
                        ibc.CloseAllOpenPositions();

                    }


        LastTWSMsg = ibc.getLastError( 0 );
        Title = "\n" +
                 "Last TWS Error Msg: " + LastTWSMsg + "\n" +
                 " BuyOrderID: " + BuyOrderID + " " + BuyStatus + "\n" +
                 " SellOrderID: " + SellOrderID + " " + SellStatus + "\n" +
                 " childID: " + childID + " " + ChildStatus + "\n" +
                 " lmtID: " + lmtID + " " + lmtStatus + "\n" +
                 " Last Trade: " + LastTrade + "\n" +
                 " TWS Position Size: " + NumToStr( IBPosSize, 1.0, False );
    }
}