It is probably also a good idea to 'flag' orders sent to the DLL so that no further actions are taken till the last order has been processed.
Here is an indicator that includes these aspects at trades MA crossovers, it works fine,
****BUT DO NOT EXPECT TO MAKE PROFITS WITH IT****
*****IT IS FOR DEMONSTRATION PURPOSES ONLY******
You would need to substitute your own signals.
It has inputs to restrict trading to within set times, probably best to alter the defaults to suite your location.
Another input sets a time when an exit will be forced.
You can turn trading on and off by setting the input 'Trade' to true or false
Here is the code, I hope it helps.
inputs: Price( 0.5*(High + Low )), FastLength( 7 ), SlowLength( 12 ), Trade( False ), AllowEntriesFrom( 1430 ), AllowEntriesTill( 2045 ), ForceExitAt( 2059); variables: IntrabarPersist FastMA( 0 ), IntrabarPersist SlowMA( 0 ), AllowEntries(False), IntrabarPersist MktPosn( 0 ), IntrabarPersist OrderID( " " ), IntrabarPersist Ordered( "No" ) ; If Time >= AllowEntriesFrom and Time <= AllowEntriesTill then AllowEntries = True Else AllowEntries = False; FastMA = AverageFC(Price, FastLength) ; SlowMA = AverageFC(Price, SlowLength) ; Plot1( FastMA, "FastMA" ) ; Plot2( SlowMA, "SlowMA" ) ; If NTConnected(1) and LastBarOnChart and Trade = True and AllowEntries = True then Begin MktPosn = NTMarketPosition( "" ) ; // If the initial long or short is in place, set Ordered = "No" to allow further actions If Ordered = "Long" or Ordered = "Short" then Begin If MktPosn <> 0 then Ordered = "No"; End; // If a long has toggled to a short. If Ordered = "LongToShort" then Begin If MktPosn = -1 then Ordered = "No"; End; // If a short has toggled to a long. If Ordered = "ShortToLong" then Begin If MktPosn = 1 then Ordered = "No"; End; // If first trade is a long. If MktPosn = 0 and Ordered = "No" and FastMA Crosses Above SlowMA then Begin OrderID = NTNewOrderID; If NTBuyMarket(OrderID, 1) = 0 then Ordered = "Long"; // No further actions can take place till Ordered = "No" End; // If first trade is a short. If MktPosn = 0 and Ordered = "No" and FastMA Crosses Below SlowMA then Begin OrderID = NTNewOrderID; If NTSellMarket(OrderID, 1) = 0 then Ordered = "Short"; // No further actions can take place till Ordered = "No" End; // Toggle second and subsequent trades long to short. If MktPosn = 1 and Ordered = "No" and FastMA Crosses Below SlowMA then Begin OrderID = NTNewOrderID; If NTSellMarket(OrderID, 2) = 0 then Ordered = "LongToShort"; // No further actions can take place till Ordered = "No" End; // Toggle second and subsequent trades short to long. If MktPosn = -1 and Ordered = "No" and FastMA Crosses Above SlowMA then Begin OrderID = NTNewOrderID; If NTBuyMarket(OrderID, 2) = 0 then Ordered = "ShortToLong"; // No further actions can take place till Ordered = "No" End; End; If NTConnected(1) and LastBarOnChart and Trade = True and Time >= ForceExitAt then Begin MktPosn = NTMarketPosition( "" ) ; // If the final exit is complete. If Ordered = "ExitLong" or Ordered = "ExitShort" then Begin If MktPosn = 0 then Ordered = "No"; End; // Get flat from a long at end of trading. If MktPosn = 1 and Ordered = "No" then Begin OrderID = NTNewOrderID; If NTSellMarket(OrderID, 1) = 0 then Ordered = "ExitLong"; // No further actions can take place till Ordered = "No" End; // Get flat from a short at end of trading. If MktPosn = -1 and Ordered = "No" then Begin OrderID = NTNewOrderID; If NTBuyMarket(OrderID, 1) = 0 then Ordered = "ExitShort"; // No further actions can take place till Ordered = "No" End; End;
Comment