{ ============================================================ }
{ PixyTrade EasyLanguage Integration Example                   }
{ ============================================================ }
{
  SETUP:
  1. Build the DLL using build.bat
  2. Copy PixyTrade.dll to: C:\Program Files (x86)\TradeStation\Program
  3. Get your API key from https://pixytrade.com/scanner (Strategy tab)
  4. Enter your API key in the strategy's "APIKey" input field in TradeStation
}

{ ============================================================ }
{ DLL Function Declarations                                    }
{ ============================================================ }

{ Initialize session - returns handle or 0 on error }
{ Parameters: Symbol (string), API Key (string) }
DefineDLLFunc: "PixyTrade.dll", int, "PT_Initialize", lpstr, lpstr;

{ Get armed status: 1=armed, 0=not armed, -1=error }
DefineDLLFunc: "PixyTrade.dll", int, "PT_GetArmed", int;

{ Get RTH only status: 1=RTH only, 0=extended hours, -1=error }
DefineDLLFunc: "PixyTrade.dll", int, "PT_GetRTHOnly", int;

{ Poll for updates: 1=changed, 0=no change, -1=error }
DefineDLLFunc: "PixyTrade.dll", int, "PT_Poll", int;

{ Cleanup session }
DefineDLLFunc: "PixyTrade.dll", void, "PT_Cleanup", int;

{ Get last error message }
DefineDLLFunc: "PixyTrade.dll", lpstr, "PT_GetLastError";


{ ============================================================ }
{ Inputs (User-configurable in TradeStation properties)        }
{ ============================================================ }

Inputs:
    StrategyName(""),                   { Name shown in scanner (optional) }
    APIKey("");                         { Your PixyTrade API key - REQUIRED }


{ ============================================================ }
{ Variables                                                    }
{ ============================================================ }

Vars:
    intrabarpersist sessionHandle(0),
    intrabarpersist isInitialized(false),
    intrabarpersist armed(false),
    intrabarpersist rthOnly(true),
    pollResult(0);


{ ============================================================ }
{ Initialization (runs once)                                   }
{ ============================================================ }

If isInitialized = false Then Begin
    { Initialize with current symbol and API key }
    { BOTH parameters are REQUIRED - DLL will fail without valid API key }
    sessionHandle = PT_Initialize(Symbol, APIKey);

    If sessionHandle > 0 Then Begin
        isInitialized = true;
        Print("PixyTrade initialized for ", Symbol, " (handle: ", sessionHandle, ")");
    End
    Else Begin
        Print("PixyTrade initialization FAILED: ", PT_GetLastError());
    End;
End;


{ ============================================================ }
{ Main Logic (runs every bar/tick)                             }
{ ============================================================ }

If isInitialized Then Begin
    { Check for updates from the scanner }
    pollResult = PT_Poll(sessionHandle);

    If pollResult = 1 Then Begin
        { Values changed, update local state }
        armed = PT_GetArmed(sessionHandle) = 1;
        rthOnly = PT_GetRTHOnly(sessionHandle) = 1;

        Print("PixyTrade update - Armed: ", armed, ", RTH Only: ", rthOnly);
    End;

    { Trading logic based on scanner status }
    If armed Then Begin
        { Market conditions are favorable according to your strategy }

        If rthOnly Then Begin
            { Only trade during Regular Trading Hours }
            If Time >= 930 and Time <= 1600 Then Begin
                { Your entry logic here }
            End;
        End
        Else Begin
            { Extended hours trading allowed }
            { Your entry logic here }
        End;
    End
    Else Begin
        { Market conditions NOT favorable - do not enter new positions }
        { Consider managing existing positions }
    End;
End;


{ ============================================================ }
{ Cleanup (when strategy is removed)                           }
{ ============================================================ }

If LastBarOnChart Then Begin
    If sessionHandle > 0 Then Begin
        PT_Cleanup(sessionHandle);
        Print("PixyTrade session cleaned up");
    End;
End;
