confused about order management

Posted By: pchen90

confused about order management - 06/23/20 06:43


Say I'm trying to write an algo with four different scenarios; each with its own entry, profit target and stop loss order settings.

pseudocode:

if condition1:
- go long immediately at market, set a stop loss order SLM points below entry price, set a profit take order PTM points above entry price

if condition 2:
- as above, but go short instead of long

if condition 3:

- submit a resting limit buy order at the current Ask price, set a stop loss order SLL points below entry price, set a profit take order PTL points above entry price

if condition 4:
- as above, but enter with a resting limit sell at the current Bid price


All the above are mutually exclusive. My reading of the relevant manual page (https://manual.zorro-project.com/buylong.htm) made me think I need to pass the static parameters directly to enterShort() or enterLong() when they're called but I'm having some issues with how I've done it below.

1) No trades seem to enter at market (have already determined condtion1 and condition2 are triggering correctly).
2) Logs seem to suggest stops are being trailed when I don't want them to be (log snippet below script)

Can anyone point me in the right direction? Should I be using a trade management function for this, or is the below roughly correct (with wrong syntax)?

Code

	// => Enter at market
	if (condition1) // Long Entry
	{
		enterLong(_lots,0,SLM,PTM);		
	}	
									
	else if (condition2) // Short Entry
	{

		enterShort(_lots,0,SLM,PTM);		
	}											
									
        // => Passive entry						
	else if (condition3)
	{

		enterLong(_lots,-_ticks,SLL,PTL);		
	}			
			
	else if (condition4) 
	{
		enterShort(_lots,-_ticks,SLL,PTL);	
	}
	
	



Trade logs - stops being trailed

[3269: Fri 14-01-03 15:30] -880 0 33/34 (163.07)
Enter 500 Long SPY Entry -0.025000 Stop 0.75000 TP 0.050000 at 15:30:00
(SPY::L) Long 500@163.04 Entry limit
(SPY::L) Entry limit 163.04 hit by 163.03 at 15:30:00
[SPY::L27033] Long 500@163.08 Risk 396 p at 15:30:00

[3270: Fri 14-01-03 15:31] -880 -18.63 33/35 (163.05)
Enter 500 Long SPY Entry -0.025000 Stop 0.75000 TP 0.050000 at 15:31:00
[SPY::L] Skipped (Max = 1)

[3271: Fri 14-01-03 15:32] -880 +1.36 34/34 (163.09)
Enter 500 Long SPY Entry -0.025000 Stop 0.75000 TP 0.050000 at 15:32:00
[SPY::L] Skipped (Max = 1)
[SPY::L27033] Trail 500@163.08 Stop 162.34 at 15:32:00

[3272: Fri 14-01-03 15:33] -880 +11.37 34/34 (163.11)
Enter 500 Long SPY Entry -0.025000 Stop 0.75000 TP 0.050000 at 15:33:00
[SPY::L] Skipped (Max = 1)
[SPY::L27033] Trail 500@163.08 Stop 162.36 at 15:33:00


Code




Posted By: Grat

Re: confused about order management - 06/23/20 07:46

Try this:

MaxLong = MaxShort = 5; // don't open several trades
Posted By: Petra

Re: confused about order management - 06/23/20 12:04

When stops are trailed, you have set the Trail variable somewhere in your code. And if you want to enter at market, don't set the Entry variable.
Posted By: pchen90

Re: confused about order management - 06/27/20 02:11

Thanks for the responses all! On a related question now I'm trying to create a more detailed trade log function to diagnose my algo's performance.

Say that I am working with 5-min bars. I want to know each time a resting limit order is filled in simulation (as either an entry or stop loss/profit target), what the high and low of that bar in which the simulated order was filled. Output should be to a csv file with columns identifying the time, bar number, trade type (entry/stop loss/profit target) and the OHLC prices of the bar.

However, it's not clear to me from the manual how order fill events are handled in simulated trade...

Should I be using a TMF outside the main run() function like this? I ran the below and couldn't even get it to print to the default logfile. Also - how do I get the actual fill price of the simulated trade from Entry/Stop/TakeProfit and not the price offset variable I provided as an argument?

Code

int ExtremeHitLog()
{
	if(TradeIsEntry) {
			string line = strf(
			  "Entry, %02i/%02i/%02i %02i:%02i, %.5f, %.5f, %.5f, %.5f\n",
			  day(),month(),year()%100,hour(),minute(),
			  priceOpen(),priceHigh(),priceLow(),priceClose());
		
			file_append("Data\\extremehits.csv",line);
		}
	

	//if(TradeIsStop)

	//if(TradeIsProfit)

   return 16; // trigger tmf at next event
}
Posted By: Petra

Re: confused about order management - 06/27/20 06:40

The idea is generally right, for finding out why it does not work, check the whole code and debug it with the watch function. Also read the troubleshooting page in the manual.
© 2024 lite-C Forums