Example pairs trading code 0

Posted on 12, March 2014

in Category SID


Description for pairsMovArb_res SID 772

Some sample pairs trading code. Do NOT TRADE, for example only.

Example pairs trading code/sid Notes for this Trading Account:

Example pairs trading code/sid Rules for this Trading Account:

Example pairs trading code/sid Notes for this Trading System:

Example pairs trading code/sid Rules for this Trading System:

Example pairs trading code/sid Begin event logic:

$pairPositions = {};

Example pairs trading code/sid Bar event logic:

my $s0 = Symbol();
my $s0_mov = log(Ref(Close, 0) / Ref(Open, -$p)); 

my $trade = 0;

my @symbols = PortfolioSymbolsX();
foreach my $s1 (@symbols) {

	if(
		(($s1->Symbol eq 'USDJPY') && (($s0 eq 'AUDUSD') || ($s0 eq 'NZDUSD'))) ||
		(($s0 eq 'USDCAD') && ($s1->Symbol eq 'AUDUSD')) ||
		(($s0 eq 'AUDUSD') && ($s1->Symbol eq 'NZDUSD'))
	) {

		my $s1_mov = log($s1->Ref($s1->Close, 0) / $s1->Ref($s1->Open, -$p));

		my $diff = ColumnDiff($s0_mov, $s1_mov);
		my $sd = StaDev($diff, $minObservations);
		my $mean = SMA($diff, $minObservations);

		my $latestMov = Now($diff);
		my $latestSD = Now($sd);
		my $latestMean = Now($mean);

		if(!PairsPosition($s0, $s1->Symbol)) { 
			if(($latestMov >= $latestMean + 2*$latestSD) || ($latestMov <= $latestMean - 2*$latestSD)) {

				if(Now(Close) - Ago(Close, $p) > 0) {
					$trade = -1;
				} else {
					$trade = 1;
				}

			}

			my $units = RoundToUnitSize(ForexUnitsAvailable($posSize));

			my $s0_exitLevel = Now(SMA(High-Low, $minObservations));
			my $s1_exitLevel = Now($s1->SMA($s1->High-$s1->Low, $minObservations));
			my $key = GenerateKey($s0, $s1->Symbol);

			if($trade > 0) {
				my $OCA = "L_${s0}_S_${s1}";
				PlaceBuyMarket($OCA,$units,OnFill => {key=>$key, dir=>$trade, OCA=>$OCA, tp=>Now(Close)+($s0_exitLevel), sl=>Now(Close)-($s0_exitLevel), units=>$units});

				my $OCA = "contra_L_${s0}_S_${s1}";
				$s1->PlaceSellMarket($OCA,$units,OnFill => {key=>$key, dir=>$trade, OCA=>$OCA, tp=>Now($s1->Close)-($s1_exitLevel), sl=>Now($s1->Close)+($s1_exitLevel), units=>$units});

				registerPairsPosition($s0, $s1->Symbol);
			} elsif($trade < 0) {
				my $OCA = "S_${s0}_L_${s1}";
				PlaceSellMarket($OCA,$units,OnFill => {key=>$key, dir=>$trade, OCA=>$OCA, tp=>Now(Close)-($s0_exitLevel), sl=>Now(Close)+($s0_exitLevel), units=>$units});

				my $OCA = "contra_L_${s0}_S_${s1}";
				$s1->PlaceBuyMarket($OCA,$units,OnFill => {key=>$key, dir=>$trade, OCA=>$OCA, tp=>Now($s1->Close)+($s1_exitLevel), sl=>Now($s1->Close)-($s1_exitLevel), units=>$units});

				RegisterPairsPosition($s0, $s1->Symbol);
			}


		}

	}

}

sub GenerateKey
{
	my ($s0, $s1) = @_;
	return "${s0}_${s1}";
}


sub PairsPosition
{
	my ($s0, $s1) = @_;
	my $key = GenerateKey($s0, $s1);
	my $v = $pairPositions->{$key};
	if($v > 0) {
		#Output("*** Qty Positions $key: $v");
		return 1;
	}
	#Output("=== Qty Positions $key: $v");
	return 0;
}


sub RegisterPairsPosition
{
	my ($s0, $s1) = @_;
	my $key = GenerateKey($s0, $s1);
	
    $pairPositions->{$key}=$pairPositions->{$key}+1;
}


Example pairs trading code/sid OrderFill event logic:

my $r = shift;


if($r->{exitkey}) {
	Output("exitkey exists");
	DeregisterPairsPosition_Key($r->{exitkey});

} else {
	Output("exitkey NOT exists");

	if($r->{dir} > 0) {

		# stop loss for long position
		PlaceOrder( StopPrice => $r->{sl},Quantity => $r->{units},OrderType => OrderTypeSellStop,Label => $r->{OCA}, OCA => $r->{OCA}, OnFill=>{exitkey=>$r->{key}});
		Output("PlaceOrder( StopPrice => $r->{sl},Quantity => $r->{units},OrderType => OrderTypeSellStop,Label => $r->{OCA}, OCA => $r->{OCA}, OnFill=>{key=>$r->{key}}))");
		# take profit for long position
		PlaceOrder( LimitPrice => $r->{tp}, Quantity => $r->{units}, OrderType => OrderTypeSellLimit, Label => $r->{OCA}, OCA => $r->{OCA}, OnFill=>{exitkey=>$r->{key}});
		Output("PlaceOrder( LimitPrice => $r->{tp}, Quantity => $r->{units}, OrderType => OrderTypeSellLimit, Label => $r->{OCA}, OCA => $r->{OCA}, OnFill=>{key=>$r->{key}});");

	}

	if($r->{dir} < 0) {

		# stop loss for short position
		PlaceOrder( StopPrice => $r->{sl}, Quantity => $r->{units}, OrderType => OrderTypeBuyStop, Label => $r->{OCA}, OCA => $r->{OCA}, OnFill=>{exitkey=>$r->{key}});
		#Output("PlaceOrder( StopPrice => $r->{sl}, Quantity => $r->{units}, OrderType => OrderTypeBuyStop, Label => $r->{OCA}, OCA => $r->{OCA});");
		# take profit for short position
		PlaceOrder( LimitPrice => $r->{tp}, Quantity => $r->{units}, OrderType => OrderTypeBuyLimit, Label => $r->{OCA}, OCA => $r->{OCA}, OnFill=>{exitkey=>$r->{key}});
		#Output("PlaceOrder( LimitPrice => $r->{tp}, Quantity => $r->{units}, OrderType => OrderTypeBuyLimit, Label => $r->{OCA}, OCA => $r->{OCA});");

	}
}


sub DeregisterPairsPosition
{
	my ($s0, $s1) = @_;
	my $key = GenerateKey($s0, $s1);
	
    $pairPositions->{$key}=$pairPositions->{$key}-1;
}


sub DeregisterPairsPosition_Key
{
	my ($key) = @_;
	
    $pairPositions->{$key}=$pairPositions->{$key}-1;
}




0 Comments

Leave a Message